WO2023029982A1 - 一种内存的分配方法及系统 - Google Patents

一种内存的分配方法及系统 Download PDF

Info

Publication number
WO2023029982A1
WO2023029982A1 PCT/CN2022/112800 CN2022112800W WO2023029982A1 WO 2023029982 A1 WO2023029982 A1 WO 2023029982A1 CN 2022112800 W CN2022112800 W CN 2022112800W WO 2023029982 A1 WO2023029982 A1 WO 2023029982A1
Authority
WO
WIPO (PCT)
Prior art keywords
memory
virtual address
group
allocation
groups
Prior art date
Application number
PCT/CN2022/112800
Other languages
English (en)
French (fr)
Inventor
于杰
Original Assignee
济南外部指针科技有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 济南外部指针科技有限公司 filed Critical 济南外部指针科技有限公司
Publication of WO2023029982A1 publication Critical patent/WO2023029982A1/zh

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5011Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
    • G06F9/5016Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals the resource being the memory

Definitions

  • the invention belongs to the field of memory allocation, in particular to a memory allocation method and system.
  • the CPU when it needs to process a certain variable, it may load part of the memory page where the variable is located into the CPU's L1 cache, or before that, load the memory page where the variable is located from the swap partition of the hard disk into the main memory , or decompressed from the main memory, but in the cache or main memory, some locations loaded with this variable are rarely accessed, which results in some bytes being transferred and stored to the cache and main memory, is not accessed.
  • each associated variable group contains more than two variables, and if a variable in a certain associated variable group is accessed, the Other variables, likely to be accessed for a short period of time as well. It can be deduced that if the variables in a certain associated variable group are concentrated in some memory pages, it may be beneficial to improve the memory access efficiency of the CPU. However, if the variables in a certain associated variable group are scattered to too many In the memory page, it may not be conducive to improving the efficiency of CPU memory access.
  • a small object is a relative concept.
  • a small object refers to a memory object whose length is much smaller than a memory page. It is generally believed that the length of a small object is less than 64 bytes.
  • the present invention proposes a memory allocation method and system, which divides the virtual addresses of the heap memory in the user space into several groups for management, and each group is called a virtual address group; and assigns each virtual address group Allocate a group of locks to manage the allocation of virtual addresses in this virtual address group, each group of locks contains at least one lock, compared with using a group of locks to manage the allocation of all virtual addresses, the resource competition caused is more moderate; it can Try to give full play to the advantages of different algorithms; and it can more conveniently improve the "rationality of small object allocation" of the system.
  • the present invention adopts the following technical solutions:
  • a memory allocation method comprising:
  • the memory allocator uses the memory allocation algorithm to allocate memory for each object group in the corresponding virtual address group according to the length of the requested memory.
  • the memory allocator searches for free memory of a suitable size under the premise of ensuring memory address alignment; if the memory allocator finds free memory of a suitable size, it returns the location for the object group; otherwise, it is determined that the memory allocation fails .
  • the memory allocation failure is directly notified.
  • the required memory is allocated in another virtual address group, and if the failure still occurs, the allocation failure is notified.
  • the memory allocator is also used to release the virtual address in the virtual address group by using a memory release algorithm when the memory needs to be released.
  • the memory allocation algorithm and the memory release algorithm adopted by different virtual address groups may be different.
  • each virtual address group is assigned a group of locks for managing the allocation and release of virtual addresses in the virtual address group, and each group of locks contains at least one lock.
  • the specific method of grouping is: when a thread starts, select a virtual address group from all virtual address groups to bind to the thread, and allocate all objects that the thread needs to allocate in the heap memory of the user space It is divided into an object group, and the virtual address group corresponding to the object group is the virtual address group bound to the thread.
  • a memory allocation system comprising:
  • a virtual address group division module configured to: divide the virtual address of the heap memory in the user space into multiple virtual address groups
  • the object group division module is configured to: group all objects that need to be allocated in the user space, and obtain multiple object groups and virtual address groups corresponding to each object group;
  • the memory allocation module is configured as follows: the memory allocator allocates memory for each object group by using a memory allocation algorithm in the corresponding virtual address group according to the length of memory requested.
  • a memory release module which is configured to: when the memory allocator needs to release memory, use a memory release algorithm to release the virtual addresses in the virtual address group.
  • the present invention divides the virtual address of the heap memory in the user space into a plurality of virtual address groups, and assigns a group of locks to each virtual address group for management.
  • this virtual address group the allocation of virtual addresses.
  • the resource competition caused is more moderate.
  • the present invention divides the virtual address of the heap memory in the user space into a plurality of virtual address groups, and each virtual address group can adopt different algorithms as required to allocate and release the memory, which contributes to the maximum Take advantage of different memory allocation algorithms.
  • the present invention simplifies the process of aggregating small objects that are "suitable for combination” and isolating small objects that are “suitable for separation”: specifying the range of virtual addresses of small objects, and providing convenient.
  • Fig. 1 is a flow chart of a memory allocation method of the present invention
  • Fig. 2 is a flow chart of the memory allocation algorithm of the first embodiment.
  • Memory object used to store data, can be used by threads, and object in the present invention generally refers to the memory in the heap in the user space.
  • the present invention provides a memory allocation method.
  • a memory allocation method comprising:
  • S1 Preliminarily divide the virtual address into multiple spaces, and determine the virtual address used as the heap memory in the user space; the space includes a kernel space and a user space, and the user space includes a heap memory.
  • sys11 is some kind of operating system, and, in sys11, virtual addresses between virtual address n11 and virtual address n12 can be used by memory in the heap in user space.
  • n11 0x00FFFFFFFF
  • n12 0x000100000000.
  • the virtual address that is higher than n11 and can be accessed belongs to the kernel space; the virtual address that is not higher than n11 and can be accessed belongs to the user space; the virtual address lower than n12 cannot be used as the address of memory in the heap; n12 to The virtual address between n11 can be used as the address of memory in the heap.
  • S2 Divide the virtual address of the heap memory in the user space into multiple virtual address groups according to the division rules.
  • the virtual address of the heap memory in the user space is divided into multiple primary virtual address groups and a secondary virtual address group; the size of the address space managed by each virtual address group can be different, and the addresses it contains do not have to be continuous
  • Each virtual address group is assigned a group of locks to manage the allocation of virtual addresses in the virtual address group, and each group of locks contains at least one lock.
  • the virtual addresses between virtual address n11 and virtual address n12 can be used for the memory in the heap.
  • the virtual addresses between n11 and n12 can be divided into several "virtual address groups", generally In this case, the virtual addresses contained in each virtual address group are continuous, and the virtual addresses contained in each memory page will not be allocated to two virtual address groups.
  • a set of locks is assigned to each virtual address group to manage the allocation of virtual addresses in this virtual address group, compared to using a set of locks to manage the allocation of virtual addresses of heap memory in all user spaces , resulting in less competition for resources.
  • 51 virtual address groups are divided from the virtual address space, including 50 primary virtual address groups and 1 secondary virtual address group; for example, taking sys11 as an example, for virtual addresses between n12 and n11, It can be agreed that the division result is:
  • Addresses 0x000100000000 to 0x00017FFFFFFF are the 01st primary virtual address group
  • Addresses 0x000180000000 to 0x0001FFFFFFFF are the 02nd primary virtual address group
  • Addresses 0x001980000000 to 0x0019FFFFFF are the 50th primary virtual address group
  • serial numbers 0 to 49 to represent the 1st to 50th primary virtual address groups, and other serial numbers to represent undivided. It can be seen that no memory page belongs to two virtual address groups (including the primary virtual address group and the secondary virtual address group undivided), and each virtual address group (including the primary virtual address group and the secondary virtual address group undivided) contains virtual addresses is continuous. That is, after the virtual address of the heap memory is divided into multiple virtual address groups, each memory page is not divided into two virtual address groups, and the virtual addresses contained in each virtual address group are continuous.
  • Addresses 0x000100001020 to 0x000100001100 are the 01st primary virtual address group
  • Addresses 0x000100001101 to 0x000100001200 are the 02nd primary virtual address group
  • Addresses 0x000100001201 to 0x000180000002 are the 03rd primary virtual address group
  • Addresses 0x000180000003 to 0x000200000002 are the 04th primary virtual address group
  • Addresses 0x001800020000 to 0x0019FFFFFFFF are the 50th primary virtual address group
  • sequence numbers 0 to 49 to represent the 1st to 50th primary virtual address groups, and other sequence numbers represent the secondary virtual address groups undivided. It can be seen that the 01st, 02nd, and 03rd primary virtual address groups share the same page, and the virtual addresses contained in the secondary virtual address group undivided are not continuous.
  • 3 31, 256, 4000 virtual address groups, etc. can be fixedly divided from the virtual addresses as required.
  • the user specifies how many virtual address groups are divided from the virtual address and the virtual address contained in each virtual address group before the memory in the heap is applied for when the program starts.
  • S3 Group all the objects that need to be allocated in the user space to obtain multiple object groups and the virtual address groups corresponding to each object group; after the virtual address groups are divided, the objects of different groups will be in the corresponding virtual address Assignment within the group.
  • the specific methods of grouping include:
  • the first grouping method when a thread starts, select a virtual address group among all virtual address groups to bind to the thread, and divide all objects that the thread needs to allocate in the heap memory of the user space into an object group , the virtual address group corresponding to the object group is the virtual address group bound to the thread.
  • a main virtual address group is selected from all main virtual address groups to be bound to the thread, and all objects that the thread needs to allocate are divided into an object group, and the virtual address corresponding to the object group is The address group is the main virtual address group bound to the thread.
  • the thread needs to apply for memory, it allocates memory for the object group from the main virtual address group bound to the thread.
  • multiple threads may bind a certain virtual address group together, but no matter whether this situation occurs or not, when the thread selects the virtual address group to be bound, it is least used by other threads. Select from several primary virtual address groups bound.
  • each thread is uniquely bound to one main virtual address group.
  • the resource competition caused by applying for memory between threads is greatly reduced. Therefore, the memory allocation method of the present application can effectively reduce resource competition in many cases.
  • the second grouping method divide all objects into 3 groups, most of the objects in the first group are likely to be accessed by the CPU, that is, the probability of the contained objects being accessed by the CPU is greater than the first threshold; the second Most positions of all objects in the group have a low possibility of being accessed by the CPU, that is, the probability of the contained objects being accessed by the CPU is less than the second threshold; other objects are classified into the third group.
  • all objects in the first group apply for memory space in the 01st virtual address group
  • all objects in the second group apply for memory space in the 02nd virtual address group
  • Memory space all objects in the third group apply for memory space in undivided.
  • the memory page in the 01st virtual address group may have a larger value because it is frequently accessed. Possibly, it resides in the memory. Therefore, the memory allocation method of the present application has higher CPU memory access efficiency.
  • the memory allocation method of this application may have higher memory utilization efficiency.
  • the third grouping method divide all objects into about 50 object groups, and each object group corresponds to a virtual address group.
  • 50 groups are divided from all small objects, and the small objects in each group will be frequently accessed, and are suitable to be placed in the same cache line (cache line).
  • Programmers apply for these 50 groups of small objects in the 1st to 50th virtual address groups respectively. undivided, used for other object applications.
  • the third grouping method may be an optimization of the second grouping method in some cases.
  • Trd1 and Trd2 are two threads; Trd1 needs to allocate an object obj11 with a length of 9 bytes at time t1, and obj11 is often accessed; Trd1 needs to allocate at time t2 Object obj12 with a length of 4 bytes, and obj12 is not frequently accessed; Trd2 needs to allocate an object obj21 with a length of 19 bytes at time t1, and obj21 is frequently accessed; Trd2 needs to allocate a length of 44 words at time t2 The object obj22 of the section, and obj22 is accessed infrequently.
  • obj11 and obj12 belong to one object group and can be allocated in the 01st virtual address group; obj21 and obj22 belong to one object group and can be allocated in Assigned in the 02nd virtual address group.
  • obj11 and obj21 belong to one object group and can be allocated in the 01st virtual address group
  • obj12 and obj22 belong to one object group and can be allocated in the 01st virtual address group Assigned in the 02nd virtual address group.
  • S4 Determine the length of the memory that needs to be applied for; the memory allocator uses the memory allocation algorithm in the corresponding virtual address group to allocate memory for each An object group looks for free memory and allocates memory.
  • the memory allocation algorithms adopted by different virtual address groups may be different.
  • the memory allocation scheme can be: the memory allocator locks the lock of the virtual address group corresponding to a certain object group; within the virtual address range of the locked virtual address group, a customized or fixed allocation algorithm is adopted to ensure memory address alignment Under the premise, look for free memory; if the memory allocator finds free memory, then open the lock of the virtual address group, and return the position used by the object group; otherwise, determine that the memory allocation has failed, and open the lock of the virtual address group.
  • each block of memory allocated as an object in the heap corresponds to a data structure called a header.
  • the member variables of the structure header include used and next: used is used to record the length of the memory that is actually marked as occupied during a certain memory allocation; next is used to record the address of the next header structure.
  • the memory allocator can know the address of the first header structure of a certain virtual address group, and then can traverse all the header structures of the virtual address group starting from the first header structure.
  • the memory allocator can also calculate the length of the free area between the header structure and the next header structure through the address of a header structure, the value of the header structure used, and the value of the header structure next.
  • each virtual address group selects a segment from the starting position of the virtual address group for private anonymous mapping, and initialize the first header structure and the last header structure within this range .
  • the next of the first header structure points to the last header structure, and used is equal to 0; the last header structure is located at the end of the virtual address that has completed private anonymous mapping, and its next points to empty, and used is equal to 0.
  • Block free memory can provide memory with a length greater than or equal to sizeof(header)+len(length of header structure+length of memory to be requested), and the starting position of the provided memory is aligned. During the traversal process, before traversing the next header structure of a header structure, store the address of the header structure in last.
  • the memory allocator finds a free memory area, it unlocks the lock of the virtual address group and returns the location used by the object group. Specifically: the memory allocator mallocx stores the start address of the found free area into nStart; from nStart to nStart The memory between +sizeof(header)-1 is used to store the header structure; the address pointed to by nStart+sizeof(header) should be guaranteed to be memory-aligned; set the header structure corresponding to the header structure and last; in these two After the header structure is set, mallocx opens the lock lock01 of the virtual address group, stores the value of nStart+sizeof(header) (the location used by the object group) into nMem, and returns to nMem.
  • the second measure is to allocate the required memory in another virtual address group, preferably in the sub-virtual address group undivided, and if it still fails, it is notified that the allocation failed.
  • the memory allocator is also used to release the virtual addresses in the virtual address group by using the memory release algorithm when the memory needs to be released.
  • the memory release algorithms adopted by different virtual address groups may be different.
  • the memory release scheme can be: determine the virtual address group where the memory to be released is located, and then lock the lock of the virtual address group; detect whether the header structure corresponding to the memory to be released is legal, if not, open the virtual address group lock, and send information to the main program; otherwise, set the header structure, and related other header structures, mark the occupied memory as free, realize memory release, and open the lock of the virtual address group.
  • the memory allocator starts from the first header structure and traverses all header structures; during the traversal process, before traversing the next header structure of a certain header structure, the address of the header structure is stored in last; if the memory to be released is not found corresponding to the header structure, unlock the lock of the virtual address group, and send information to the main program; otherwise, set the header structure corresponding to the memory to be released and the header structure corresponding to last, and mark the occupied memory as free , realize memory release, and open the lock of the virtual address group.
  • nMem For example, set nMem to 0x000100000400, when the user needs to release nMem, according to the value of nMem, determine that nMem is in the 01st virtual address group, then lock lock01, check whether nMem-sizeof(header) points to a legal header structure, If not, open lock01 and execute the abort function, otherwise, by setting the header structure corresponding to the memory to be released and the header structure corresponding to last, the memory that was originally marked as occupied is marked as free, so as to realize memory release, and finally , open lock01.
  • the same virtual address group can also use different algorithms according to the characteristics of the allocated memory, such as length, to perform memory allocation and memory release; and different virtual address groups do not need to use the same algorithm. Perform memory allocation and memory release.
  • a certain algorithm may be particularly suitable for some object groups, and another algorithm may be particularly suitable for other object groups.
  • Different virtual address groups use different algorithms to make use of the strengths of each algorithm.
  • the memory allocation method in this application provides convenience for improving the "rationality of small object allocation" in the heap and improving system efficiency. Since multiple groups of locks can be used to manage virtual addresses, resource competition between multiple threads when applying for virtual addresses is alleviated to a certain extent.
  • the present invention also provides a memory allocation system
  • a memory allocation system comprising:
  • a virtual address group division module which is configured to: divide the virtual address of the heap memory in the user space into a plurality of virtual address groups;
  • the object group division module is configured to: group all objects that need to be allocated in the user space, and obtain multiple object groups and virtual address groups corresponding to each object group;
  • the memory allocation module is configured as follows: the memory allocator allocates memory for each object group by using a memory allocation algorithm in the corresponding virtual address group according to the length of memory requested.
  • the memory release module is configured to: when the memory allocator needs to release the memory, use the memory release algorithm to release the virtual addresses in the virtual address group.

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Memory System Of A Hierarchy Structure (AREA)

Abstract

本发明公开了一种内存的分配方法及系统,包括:将用户空间中的堆内存的虚拟地址划分到多个虚拟地址组中;对所有需要在用户空间中分配的对象进行分组,得到多个对象组以及每个对象组所对应的虚拟地址组;内存分配器根据需要申请内存的长度,在对应的虚拟地址组内采用内存分配算法为每个对象组分配内存。将虚拟地址分为若干组管理,降低了资源竞争,还可以充分发挥不同内存管理算法的特长。

Description

一种内存的分配方法及系统
本发明要求于2021年9月3日提交中国专利局、申请号为202111031787.4、发明名称为“一种内存的分配方法及系统”的中国专利申请的优先权,其全部内容通过引用结合在本发明中。
技术领域
本发明属于内存分配领域,尤其涉及一种内存的分配方法及系统。
背景技术
本部分的陈述仅仅是提供了与本发明相关的背景技术信息,不必然构成在先技术。
首先,当CPU需要处理某个变量时,可能会将这个变量所在内存页的一部分载入CPU一级缓存,还可能在此之前,将这个变量所在内存页,从硬盘的交换分区载入主内存,或者从主内存中解压,但是,缓存或者主内存中,有些与这个变量一起载入的位置,实际很少被访问,这造成了,某些字节被传输、存储至缓存与主内存,却不被访问的情况。
其次,计算机系统中可能存在若干“关联变量组”,每个关联变量组都含有两个以上的变量,且,某个关联变量组中的一个变量,如果被访问,则这个关联变量组中的其他变量,很可能在很短的时间内也被访问。可以推论,如果某一个关联变量组内的变量,集中在某些内存页中,则,可能有利于,提高CPU的访存效率,但,如果某一个关联变量组中的变量被分散到过多的内存页中,则,可能不利于,提高CPU访存的效率。
再次,当多个处理器内核的缓存行共享相同位置的某些内存,但是实际使用这些位置的不同字节时,可能造成大量不必要的,为确保缓存一致性而进行的操作。
小对象是一个相对的概念,小对象是指长度远小于一个内存页的内存对象,一般认为小对象的长度小于64个字节。
如果某几个小对象被分配到同一个内存页中,可能总体上恶化以上问题,则这几个小对象,被认定为是“宜分离”的;如果某几个小对象被分配到同一个内存页中,可能总体上优化以上问题,则这几个小对象,被认定为是“宜结合”的。如果某个系统中,“宜分离”的小对象很少在同一个页面中,而“宜结合”的小对象,集中在某些页面中,则称,这个系统具有较高的“小对象分配的合理性”。
另外,如果系统在分配虚拟地址时,只使用一组锁,对全部虚拟地址进行管理,则,可能存在较高的资源竞争。
最后,用以实现内存的分配与释放的算法,对分配速度、资源消耗、减少碎片等问题无法全部进行最佳处理。过往,一个进程内,一般只能固定地采取某几个算法,且分配同样大小的内存时一般只能使用相同的算法。
发明内容
本发明为了解决上述问题,提出了一种内存的分配方法及系统,将用户空间中的堆内存的虚拟地址分为若干组管理,每个组称为虚拟地址组;并给每个虚拟地址组分配一组锁,用以管理本虚拟地址组内虚拟地址的分配,每组锁含至少一个锁,相较于使用一组锁管理所有的虚拟地址的分配,造成的资源竞争更为缓和;可以尽量发挥不同算法的优点;且可以更便捷的提高系统“小对象分配的合理性”。
根据一些实施例,本发明采用如下技术方案:
一种内存的分配方法,包括:
将用户空间中的堆内存的虚拟地址划分到多个虚拟地址组中;
对所有需要在用户空间中分配的对象进行分组,得到多个对象组以及每个对象组所对应的虚拟地址组;
内存分配器根据需要申请内存的长度,在对应的虚拟地址组内采用内存分配算法为每个对象组分配内存。
进一步的,所述内存分配器在保证内存地址对齐的前提下,寻找合适大小的空闲内存;如果内存分配器找到合适大小的空闲内存,则返回供对象组使用的位置;否则,判定内存分配失败。
进一步的,判定内存分配失败后,直接告知分配失败。
进一步的,判定内存分配失败后,在另一个虚拟地址组内分配所需内存,如果仍然失败,才告知分配失败。
进一步的,所述内存分配器还用于,在需要释放内存时,采用内存释放算法对虚拟地址组内虚拟地址进行释放。
进一步的,不同虚拟地址组采用的内存分配算法和内存释放算法可以不同。
进一步的,每个虚拟地址组分配一组锁,用以管理虚拟地址组内虚拟地址的分配与释放,每组锁含至少一个锁。
进一步的,所述分组的具体方法为:某个线程启动时,在所有虚拟地址组中选择一个虚拟地址组与该线程绑定,并将该线程需要在用户空间的堆内存中分配的所有对象划分到一个对象组,所述对象组对应的虚拟地址组即为与该线程绑定的虚拟地址组。
一种内存的分配系统,包括:
虚拟地址组划分模块,其被配置为:将用户空间中的堆内存的虚拟地址划分到多个虚拟地址组中;
对象组划分模块,其被配置为:对所有需要在用户空间中分配的对象进行分组,得到多个对象组以及每个对象组所对应的虚拟地址组;
内存分配模块,其被配置为:内存分配器根据需要申请内存的长度,在对应的虚拟地址组内采用内存分配算法为每个对象组分配内存。
进一步的,还包括内存释放模块,其被配置为:内存分配器在需要释放内存时,采用内存释放算法对本虚拟地址组内虚拟地址进行释放。
本发明的有益效果是:
1、本发明将用户空间中的堆内存的虚拟地址划分到多个虚拟地址组中,并给每个虚拟地址组分配一组锁,用以管理,本虚拟地址组内,虚拟地址的分配,相较于,使用一组锁,管理所有用户空间中的堆内存的虚拟地址的分配,造成的资源竞争更为缓和。
2、本发明将用户空间中的堆内存的虚拟地址划分到多个虚拟地址组中,每个虚拟地址组可以根据需要采用不同的算法,用以对内存进行分配和释放,这有助于最大发挥不同内存分配算法的优势。
3、本发明简化实现聚合“宜结合”的小对象,与隔离“宜分离”的小对象的过程为——指定小对象的虚拟地址的范围,为提高堆中小对象分配的合理性,提供了便利。
附图说明
构成本发明的一部分的说明书附图用来提供对本发明的进一步理解,本发 明的示意性实施例及其说明用于解释本发明,并不构成对本发明的不当限定。
图1是本发明一种内存的分配方法的流程图;
图2是第一个实施例的内存分配算法的流程图。
具体实施方式
下面结合附图与实施例对本发明作进一步说明。
应该指出,以下详细说明都是例示性的,旨在对本发明提供进一步的说明。除非另有指明,本文使用的所有技术和科学术语具有与本发明所属技术领域的普通技术人员通常理解的相同含义。
需要注意的是,这里所使用的术语仅是为了描述具体实施方式,而非意图限制根据本发明的示例性实施方式。如在这里所使用的,除非上下文另外明确指出,否则单数形式也意图包括复数形式,此外,还应当理解的是,当在本说明书中使用术语“包含”和/或“包括”时,其指明存在特征、步骤、操作、器件、组件和/或它们的组合。
术语解释:
内存对象(简称对象):用来存放数据,可以供线程使用,本发明中对象一般指用户空间中的堆中的内存。
第一方面,本发明提供了一种内存的分配方法。
一种内存的分配方法,包括:
S1:将虚拟地址初步划分为多个空间,确定用作用户空间中的堆内存的虚拟地址;所述空间包括内核空间、用户空间,用户空间又含有堆内存。
假设,sys11是某种操作系统,且,sys11中,虚拟地址n11到虚拟地址n12之间的虚拟地址,可以供用户空间中的堆中内存使用。
以sys11是一种64位计算机系统为例,设n11等于0x00FFFFFFFFFF,n12 等于0x000100000000。比n11高,且可以被访问的虚拟地址,属于内核空间;不比n11高,且可以被访问的虚拟地址,属于用户空间;比n12低的虚拟地址,不能用作堆中内存的地址;n12到n11之间的虚拟地址,可以用作堆中内存的地址。
但,多数操作系统已经设定,哪些虚拟地址供内核使用,哪些虚拟地址供用户程序使用,哪些虚拟地址可以供用户空间中的堆中内存使用。
S2:按照划分规则,将用户空间中的堆内存的虚拟地址划分到多个虚拟地址组中。
优选的,将用户空间中的堆内存的虚拟地址划分到多个主虚拟地址组和一个副虚拟地址组中;每个虚拟地址组管理的地址空间大小可以不同,其所含地址也不必是连续的;每个虚拟地址组分配一组锁,用以管理本虚拟地址组内虚拟地址的分配,每组锁含至少一个锁。
对于sys11计算机系统,虚拟地址n11到虚拟地址n12之间的虚拟地址,可以供堆中内存使用,应用本发明时,会将n11到n12之间的虚拟地址划分为若干“虚拟地址组”,一般情况下,每个虚拟地址组所含虚拟地址是连续的,且每个内存页所含虚拟地址不会被分配到两个虚拟地址组内。
当虚拟地址组内的虚拟地址是连续的,且每个内存页都未被划分到两个虚拟地址组时,以下结论是基本正确的:如果两个变量在同一个虚拟地址组内,则他们有可能在同一个页面内,且有可能存放位置相邻很近;如果两个变量不在同一个虚拟地址组内,则他们不可能在同一个页面内。
可以推论:将“宜结合”的小对象分配到同一个虚拟地址组内,且,将“宜分离”的小对象分配到不同的虚拟地址组内,是提高“小对象分配合理性”的有效办法。
而且,给每个虚拟地址组分配一组锁,用以管理,本虚拟地址组内,虚拟地址的分配,相较于,使用一组锁,管理所有用户空间中的堆内存的虚拟地址的分配,造成的资源竞争更为缓和。
综上所述,将虚拟地址分为若干虚拟地址组,有利于提高系统效率。
在具体实施中,从虚拟地址空间中划分出51个虚拟地址组,包括50个主虚拟地址组和1个副虚拟地址组;例如,以sys11为例,对于n12到n11之间的虚拟地址,可以约定划分结果为:
地址0x000100000000至0x00017FFFFFFF是第01个主虚拟地址组;
地址0x000180000000至0x0001FFFFFFFF是第02个主虚拟地址组;
...
地址0x001980000000至0x0019FFFFFFFF是第50个主虚拟地址组;
其他虚拟地址,划入副虚拟地址组undivided。
用序号0至49代表第1到第50个主虚拟地址组,其他序号代表undivided。可以看出,没有内存页属于两个虚拟地址组(包括主虚拟地址组和副虚拟地址组undivided),且每个虚拟地址组(包括主虚拟地址组和副虚拟地址组undivided)所含虚拟地址是连续的。即将堆内存的虚拟地址划分到多个虚拟地址组后,每个内存页均未被划分到两个虚拟地址组,且每个虚拟地址组所含虚拟地址是连续的。
当然,也可以规定划分结果为:
地址0x000100001020至0x000100001100是第01个主虚拟地址组;
地址0x000100001101至0x000100001200是第02个主虚拟地址组;
地址0x000100001201至0x000180000002是第03个主虚拟地址组;
地址0x000180000003至0x000200000002是第04个主虚拟地址组;
...
地址0x001800020000至0x0019FFFFFFFF是第50个主虚拟地址组;
其他虚拟地址,划入副虚拟地址组undivided。
用序号0至49代表第1到第50个主虚拟地址组,其他序号代表副虚拟地址组undivided。可以看出,第01个、第02个、第03个主虚拟地址组分享了同一个页面,而且,副虚拟地址组undivided所含虚拟地址不是连续的。
另外,还可以根据需要,固定地从虚拟地址中分出3个、31个、256个、4000个虚拟地址组等。或者用户在程序启动时,未申请堆中内存前,指定从虚拟地址中划分出多少个虚拟地址组,以及每个虚拟地址组所含虚拟地址。
但无论如何划分,同一个虚拟地址,不能划分到不同的虚拟地址组中。
S3:对所有需要在用户空间中分配的对象进行分组,得到多个对象组以及每个对象组所对应的虚拟地址组;虚拟地址组划分完毕后,不同组的对象,将在相应的虚拟地址组内分配。
分组的具体方法包括:
第一种分组方法:某个线程启动时,在所有虚拟地址组中选择一个虚拟地址组与该线程绑定,并将该线程需要在用户空间的堆内存中分配的所有对象划分到一个对象组,所述对象组对应的虚拟地址组即为与该线程绑定的虚拟地址组。
优选的,某个线程启动时,在所有主虚拟地址组中选择一个主虚拟地址组与该线程绑定,并将该线程需要分配的所有对象划分到一个对象组,所述对象组对应的虚拟地址组即为与该线程绑定的主虚拟地址组,当该线程需要申请内存时,从该线程所绑定的主虚拟地址组中为对象组分配内存,当该线程无法从与其绑定的主虚拟地址组中获得其所需虚拟地址时,根据约定或者参数,尝试从副虚拟地址组中进行分配或者直接返回失败。
当某个线程退出时,认为这个线程不再绑定其曾经绑定的虚拟地址组。
当程序中的线程过于多时,可能出现多个线程共同绑定某个虚拟地址组的情况,但是无论这种情况是否出现,线程选定需要绑定的虚拟地址组时,都从最少被其他线程绑定的若干主虚拟地址组中选择。
如果程序中的线程少于50,则每个线程唯一绑定一个主虚拟地址组,在很多情况下,线程之间因申请内存而造成的资源竞争极大降低。因此,本申请的内存分配方法,在很多情况下,可以有效的降低资源竞争。
还可以采用以下分组方法:
第二种分组方法:将所有对象分为3组,第1组的所有对象的多数位置都具有较高的可能被CPU访问,即所含对象被CPU访问的概率都大于第一阈值;第2组的所有对象的多数位置都具有较低的可能被CPU访问,即所含对象被CPU访问的概率都小于第二阈值;其他对象归入第3组。
指定每个对象组所对应的虚拟地址组,优选的,第1组的所有对象都在第01个虚拟地址组内申请内存空间,第2组的所有对象都在第02个虚拟地址组内申请内存空间,第3组的所有对象都在undivided内申请内存空间。
在采用LRU页面置换算法的系统中,由于第01个虚拟地址组内集中了经常被访问的内存,所以,第01个虚拟地址组中的内存页,可能因经常被访问,而具有更大的可能,驻留在内存中,因此,本申请的内存分配方法,具有较高的CPU访存效率。
在采用LRU页面置换算法的系统中,由于第02个虚拟地址组内集中了不经常被访问的内存,所以,第02个虚拟地址组中的内存页,可能因长期不被访问,而被写到交换分区或者被压缩,因此,本申请的内存分配方法,可能具有较高的内存利用效率。
第三种分组方法:将所有对象划分到大约50个对象组中,每个对象组对应一个虚拟地址组。优选的,从所有小对象中分出50个组,每个组中的小对象都会被高频访问,且适宜放在同一个缓存行(cache line)中。程序员在第1个至第50个虚拟地址组中,分别申请这50组小对象。undivided,用于其他对象的申请。
在所述第二种分组方法中,同样是会被高频访问的两个小对象,可能不会被同时访问,或者会被同时访问,但是不宜放在同一个cache line中。第三种分组方法可以是对第二种分组方法,在某些情况下的优化。
举例介绍第一种和第二种分组方法的具体使用。
例如,设t1、t2是计算机中的两个时刻,Trd1、Trd2是两个线程;Trd1在 t1时刻需要分配长度为9个字节的对象obj11,且obj11经常被访问;Trd1在t2时刻需要分配长度为4个字节的对象obj12,且obj12不经常被访问;Trd2在t1时刻需要分配长度为19个字节的对象obj21,且obj21经常被访问;Trd2在t2时刻需要分配长度为44个字节的对象obj22,且obj22不经常被访问。
如果仅用第一种分组方法(即“绑定线程”分配方法),则:obj11、obj12归于一个对象组,可以在第01个虚拟地址组中分配;obj21、obj22归于一个对象组,可以在第02个虚拟地址组中分配。
如果仅用第二种分组方法(即“按访问频率”分配方法),则:obj11、obj21归于一个对象组,可以在第01个虚拟地址组中分配;obj12、obj22归于一个对象组,可以在第02个虚拟地址组中分配。
S4:确定需要申请内存的长度;内存分配器根据需要申请内存的长度,在保证内存地址对齐的前提下,例如在保证自然对齐的前提下,在对应的虚拟地址组内采用内存分配算法为每个对象组寻找空闲内存,并分配内存。不同虚拟地址组采用的内存分配算法可以不同。
内存分配方案可以为:内存分配器锁住某个对象组对应的虚拟地址组的锁;在锁定的虚拟地址组的虚拟地址范围内,采用定制化的或者固定的分配算法,在保证内存地址对齐的前提下,寻找空闲内存;如果内存分配器找到空闲内存,则打开所述虚拟地址组的锁,返回供对象组使用的位置;否则,判定内存分配失败,打开所述虚拟地址组的锁。
结合图2说明,如何在某个虚拟地址组内分配内存。设,所使用的内存分配器为mallocx,内存分配器mallocx的函数原型为void*mallocx(size_t len,int id),其中len为程序所申请内存的长度,即某个对象组的某个对象需要申请内存的长度,id为虚拟地址组的序号,即这个对象组所对应的虚拟地址组的序号。
sys11中,每一块被作为堆中对象而被分配的内存对应一个数据结构,称为header。结构header的成员变量包括used与next:used用于记录,某次内存分 配时,实际被标记为已占用的内存的长度;next用于记录,下一个header结构的地址。
内存分配器,可知某个虚拟地址组的第一个header结构的地址,进而可以从第一个header结构开始,遍历这个虚拟地址组的全部header结构。内存分配器还可以通过某个header结构的地址、这个header结构used的数值、这个header结构next的数值,计算出这个header结构与其下一个header结构之间的空闲区域的长度。
每个虚拟地址组所含虚拟地址被使用前,从所述虚拟地址组的起始位置,选择一段,进行私有匿名映射,并在这段范围内,初始化第一个header结构与最后一个header结构。第一个header结构的next指向最后一个header结构,used等于0;最后一个header结构位于已经完成私有匿名映射的虚拟地址的末尾,其next指向空,used等于0。
当需要分配内存时,内存分配器获取,id的数值,以及所需分配的内存的长度len。内存分配器根据id的数值,判定用户所指是所有(51个)虚拟地址组中的哪一个。假设id=0,则内存分配器mallocx,锁住管理第01个主虚拟地址组的锁lock01,然后在虚拟地址范围0x000100000000至0x00017FFFFFFF内,从第一个header结构开始遍历,寻找一块空闲内存,这块空闲内存可以提供长度大于等于sizeof(header)+len(header结构的长度+需要申请内存的长度)的内存,且所提供的内存的起始位置是对齐的。遍历过程中,遍历某个header结构的下一个header结构前,将这个header结构的地址存入last。
如果遍历到最后一个header也未发现合适大小的内存,则在最后一个header之后的虚拟地址中,选择一段虚拟地址added,added的长度合适,且在第01个主虚拟地址组范围内。然后,对added进行私有匿名映射,并在added的末尾初始化一个新的header,将last所指header的next指向这个header。这个header成为新的最后一个header,原来的最后一个header已经变为空闲内存。在last所指header与所述新的最后一个header之间查找所需内存。
如果所述虚拟地址组内未进行私有匿名映射的虚拟地址中,无法提供所述added,则判定分配失败。
如果内存分配器找到空闲内存区域,则打开该虚拟地址组的锁,返回供对象组使用的位置,具体的:内存分配器mallocx将所找到的空闲区域的起始地址存入nStart;nStart到nStart+sizeof(header)-1之间的内存,用于存储header结构;应保证nStart+sizeof(header)所指地址,是内存对齐的;设置该header结构与last所对应的header结构;在这两个header结构设置结束后,mallocx打开该虚拟地址组的锁lock01,将nStart+sizeof(header)的值(供对象组使用的位置)存入nMem,返回nMem。
如果内存分配器mallocx未找到合适内存,则判定本次内存分配失败。
内存分配失败时的处理,当在指定的虚拟地址范围内,分配内存失败时,可以根据约定或者参数采取以下措施:
措施一,直接告知分配失败;
措施二,可以在另一虚拟地址组内分配所需内存,优选的可以在副虚拟地址组undivided内分配所需内存,如果仍然失败,才告知分配失败。
S5:释放内存,内存分配器还用于,在需要释放内存时,采用内存释放算法对本虚拟地址组内虚拟地址进行释放。不同虚拟地址组采用的内存释放算法可以不同。
内存释放方案可以为:判定出待释放内存所在虚拟地址组,然后锁住该虚拟地址组的锁;检测待释放内存所对应的header结构是否是合法的,如果不是,则打开该虚拟地址组的锁,并向主程序发送信息;否则,设置所述header结构,以及相关其他header结构,将标记为已经占用的内存标记为空闲,实现内存释放,并打开该虚拟地址组的锁。
内存分配器,从第一个header结构开始,遍历所有header结构;遍历过程中,遍历某个header结构的下一个header结构前,将这个header结构的地址存入last;如果未找到待释放内存所对应的header结构,则打开该虚拟地址组的锁, 并向主程序发送信息;否则,设置待释放内存所对应的header结构以及last所对应的header结构,将标记为已经占用的内存标记为空闲,实现内存释放,并打开该虚拟地址组的锁。
例如,设nMem为0x000100000400,当用户需要释放nMem时,根据nMem的值,判定出nMem在第01个虚拟地址组内,然后锁住lock01,检测nMem-sizeof(header)是否指向合法的header结构,如果不是,则打开lock01,并执行abort函数,否则,通过设置待释放内存所对应的header结构以及last所对应的header结构,将原来标记为已经占用的内存标记为空闲,从而实现内存释放,最后,打开lock01。
程序处理如SIGINT这样的信号时,所进行的过程,可称为信号的处理过程。使用S4、S5所述算法时,为了避免死锁,禁止在信号的处理过程中申请、删除堆中内存。
S4、S5所述内存分配与内存释放算法便于说明,但存在若干问题,如:效率不高,没有删除不必要的映射等。
除了S4、S5所述算法,同一个虚拟地址组还可以根据所分配内存的特点,如长度,而采用不同的算法,进行内存分配与内存释放;而不同的虚拟地址组不必使用相同的算法,进行内存分配与内存释放。某种算法可能特别适合某些对象组,另一种算法可能特别适合另一些对象组,不同的虚拟地址组采用不同的算法有利于发挥每种算法的特长。
综上所述,本申请中的内存分配方法,为提高堆中“小对象分配的合理性”、提高系统效率,提供了便利。由于可以使用多组锁,管理虚拟地址,所以,多线程之间申请虚拟地址时的资源竞争得到一定程度的缓解。
第二方面,本发明还提供了一种内存的分配系统;
一种内存的分配系统,包括:
虚拟地址组划分模块,其被配置为:将用户空间中的堆内存的虚拟地址划 分到多个虚拟地址组中;
对象组划分模块,其被配置为:对所有需要在用户空间中分配的对象进行分组,得到多个对象组以及每个对象组所对应的虚拟地址组;
内存分配模块,其被配置为:内存分配器根据需要申请内存的长度,在对应的虚拟地址组内采用内存分配算法为每个对象组分配内存。
内存释放模块,其被配置为:内存分配器在需要释放内存时,采用内存释放算法对本虚拟地址组内虚拟地址进行释放。
以上所述仅为本发明的优选实施例而已,并不用于限制本发明,对于本领域的技术人员来说,本发明可以有各种更改和变化。凡在本发明的精神和原则之内,所作的任何修改、等同替换、改进等,均应包含在本发明的保护范围之内。

Claims (10)

  1. 一种内存的分配方法,其特征在于,包括:
    将用户空间中的堆内存的虚拟地址划分到多个虚拟地址组中;
    对所有需要在用户空间中分配的对象进行分组,得到多个对象组以及每个对象组所对应的虚拟地址组;
    内存分配器根据需要申请内存的长度,在对应的虚拟地址组内采用内存分配算法为每个对象组分配内存。
  2. 如权利要求1所述的一种内存的分配方法,其特征在于,所述内存分配器在保证内存地址对齐的前提下,寻找合适大小的空闲内存;如果内存分配器找到合适大小的空闲内存,则返回供对象组使用的位置;否则,判定内存分配失败。
  3. 如权利要求2所述的一种内存的分配方法,其特征在于,判定内存分配失败后,直接告知分配失败。
  4. 如权利要求2所述的一种内存的分配方法,其特征在于,判定内存分配失败后,在另一个虚拟地址组内分配所需内存,如果仍然失败,才告知分配失败。
  5. 如权利要求1所述的一种内存的分配方法,其特征在于,所述内存分配器还用于,在需要释放内存时,采用内存释放算法对虚拟地址组内虚拟地址进行释放。
  6. 如权利要求1所述的一种内存的分配方法,其特征在于,不同虚拟地址组采用的内存分配算法和释放内存算法可以不同。
  7. 如权利要求1所述的一种内存的分配方法,其特征在于,每个虚拟地址组分配一组锁,用以管理虚拟地址组内虚拟地址的分配与释放,每组锁含至少 一个锁。
  8. 如权利要求1所述的一种内存的分配方法,其特征在于,所述分组的具体方法为:某个线程启动时,在所有虚拟地址组中选择一个虚拟地址组与该线程绑定,并将该线程需要在用户空间的堆内存中分配的所有对象划分到一个对象组,所述对象组对应的虚拟地址组即为与该线程绑定的虚拟地址组。
  9. 一种内存的分配系统,其特征在于,包括:
    虚拟地址组划分模块,其被配置为:将用户空间中的堆内存的虚拟地址划分到多个虚拟地址组中;
    对象组划分模块,其被配置为:对所有需要在用户空间中分配的对象进行分组,得到多个对象组以及每个对象组所对应的虚拟地址组;
    内存分配模块,其被配置为:内存分配器根据需要申请内存的长度,在对应的虚拟地址组内采用内存分配算法为每个对象组分配内存。
  10. 如权利要求9所述的一种内存的分配系统,其特征在于,还包括内存释放模块,其被配置为:内存分配器在需要释放内存时,采用内存释放算法对本虚拟地址组内虚拟地址进行释放。
PCT/CN2022/112800 2021-09-03 2022-08-16 一种内存的分配方法及系统 WO2023029982A1 (zh)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN202111031787.4 2021-09-03
CN202111031787.4A CN114116194A (zh) 2021-09-03 2021-09-03 一种内存的分配方法及系统

Publications (1)

Publication Number Publication Date
WO2023029982A1 true WO2023029982A1 (zh) 2023-03-09

Family

ID=80441193

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2022/112800 WO2023029982A1 (zh) 2021-09-03 2022-08-16 一种内存的分配方法及系统

Country Status (2)

Country Link
CN (1) CN114116194A (zh)
WO (1) WO2023029982A1 (zh)

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114116194A (zh) * 2021-09-03 2022-03-01 济南外部指针科技有限公司 一种内存的分配方法及系统

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20050193169A1 (en) * 2004-03-01 2005-09-01 Ahluwalia Manish K. Memory management
CN105468542A (zh) * 2014-09-03 2016-04-06 杭州华为数字技术有限公司 地址分配方法及装置
CN110928803A (zh) * 2018-09-19 2020-03-27 阿里巴巴集团控股有限公司 一种内存管理方法及装置
CN112817756A (zh) * 2021-01-25 2021-05-18 上海壁仞智能科技有限公司 计算机可读取存储介质、内存管理单元的虚拟化方法和装置
CN114116194A (zh) * 2021-09-03 2022-03-01 济南外部指针科技有限公司 一种内存的分配方法及系统

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20050193169A1 (en) * 2004-03-01 2005-09-01 Ahluwalia Manish K. Memory management
CN105468542A (zh) * 2014-09-03 2016-04-06 杭州华为数字技术有限公司 地址分配方法及装置
CN110928803A (zh) * 2018-09-19 2020-03-27 阿里巴巴集团控股有限公司 一种内存管理方法及装置
CN112817756A (zh) * 2021-01-25 2021-05-18 上海壁仞智能科技有限公司 计算机可读取存储介质、内存管理单元的虚拟化方法和装置
CN114116194A (zh) * 2021-09-03 2022-03-01 济南外部指针科技有限公司 一种内存的分配方法及系统

Also Published As

Publication number Publication date
CN114116194A (zh) 2022-03-01

Similar Documents

Publication Publication Date Title
US11681473B2 (en) Memory system and control method
US10552337B2 (en) Memory management and device
US11797436B2 (en) Memory system and method for controlling nonvolatile memory
CN107844267B (zh) 缓冲区分配和存储器管理
JP2012523610A (ja) マルチスレッドアプリケーション用のハッシュテーブルのコンカレントリハッシュの実行
CN108959113B (zh) 用于闪存感知堆存储器管理的方法和系统
US9081503B2 (en) Methods and systems for maintaining a storage volume with holes and filling holes
WO2024099448A1 (zh) 内存释放、内存恢复方法、装置、计算机设备及存储介质
US9552295B2 (en) Performance and energy efficiency while using large pages
WO2023029982A1 (zh) 一种内存的分配方法及系统
US7562204B1 (en) Identifying and relocating relocatable kernel memory allocations in kernel non-relocatable memory
CN115617542A (zh) 内存交换方法、装置、计算机设备及存储介质
US9176792B2 (en) Class-based mutex
CN116225693A (zh) 元数据管理方法、装置、计算机设备及存储介质
WO2021238594A1 (zh) 存储介质管理方法、装置、设备以及计算机可读存储介质
US10303375B2 (en) Buffer allocation and memory management
US10168911B1 (en) Defragmentation of persistent main memory
US9747226B2 (en) Packet processing system, method and device to optimize packet buffer space
CN117435352B (zh) 一种定长变长数据混合管理的轻量化内存优化分配方法
US12008248B2 (en) Systems, methods, and devices for utilization aware memory allocation
EP4120087A1 (en) Systems, methods, and devices for utilization aware memory allocation
US20210286725A1 (en) Information processing apparatus, computer-readable recording medium having stored therein memory control program, and computer-readable recording medium having stored therein information processing program
CN117311638A (zh) 缓存管理方法、缓存管理装置、处理器
JP2022111330A (ja) メモリシステムおよび制御方法
CN116382579A (zh) 内存规整方法、装置、计算机设备及存储介质

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 22863122

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE