A memory allocation method and system
By dividing the virtual addresses of user space memory into multiple groups and allocating locks, the problems of unreasonable memory allocation and resource contention are solved, the efficiency and rationality of memory allocation are improved, and the allocation process for small objects is simplified.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-09-03
- Publication Date
- 2026-04-03
AI Technical Summary
In existing technologies, memory allocation suffers from high resource contention, unreasonable memory allocation, and inadequate handling of fragmentation issues. Furthermore, unreasonable allocation of small objects leads to low memory access efficiency.
The virtual addresses of heap memory in user space are divided into multiple virtual address groups, each group is assigned a set of locks, and different memory allocation and release algorithms are used. Group management is carried out according to the characteristics of objects to improve the rationality of small object allocation.
By using multiple locks for management and group allocation, resource contention is mitigated, the rationality of memory allocation and system efficiency are improved, and the process of rationally allocating small objects is simplified.
Smart Images

Figure CN114116194B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of memory allocation, and particularly relates to a memory allocation method and system. Background Technology
[0002] The statements in this section are merely background information related to the present invention and do not necessarily constitute prior art.
[0003] First, when the CPU needs to process a variable, it may load a portion of the memory page containing that variable into the CPU's L1 cache. Before that, it may load the memory page containing that variable from the hard drive's swap partition into main memory, or decompress it from main memory. However, some locations in the cache or main memory that are loaded along with this variable are rarely accessed. This results in some bytes being transferred and stored in the cache and main memory, but not being accessed.
[0004] Secondly, a computer system may contain several "associated variable groups," each containing more than two variables. If one variable in an associated variable group is accessed, the other variables in that group are likely to be accessed very quickly. It can be inferred that if the variables within an associated variable group are concentrated in certain memory pages, it may improve CPU memory access efficiency. However, if the variables in an associated variable group are scattered across too many memory pages, it may negatively impact CPU memory access efficiency.
[0005] Furthermore, when cache lines of multiple processor cores share certain memory locations but actually use different bytes from those locations, it can cause a large number of unnecessary operations to ensure cache consistency.
[0006] Small objects are a relative concept. A small object is a memory object whose length is much smaller than a memory page. Generally, a small object is considered to be less than 64 bytes in length.
[0007] If allocating several small objects to the same memory page might worsen the above problems overall, then these small objects are considered "suitable for separation." Conversely, if allocating several small objects to the same memory page might improve the above problems overall, then these small objects are considered "suitable for combination." If in a system, "suitable for separation" small objects are rarely in the same page, while "suitable for combination" small objects are concentrated in certain pages, then the system is said to have a high degree of "reasonableness in small object allocation."
[0008] Additionally, if the system uses only one set of locks to manage all virtual addresses when allocating them, there may be a high level of resource contention.
[0009] Finally, the algorithms used for memory allocation and deallocation cannot optimally handle all issues related to allocation speed, resource consumption, and fragmentation reduction. In the past, a single process typically used only a few algorithms, and the same algorithm was generally used only when allocating the same amount of memory. Summary of the Invention
[0010] To address the aforementioned problems, this invention proposes a memory allocation method and system. The method divides the virtual addresses of heap memory in user space into several groups for management, each group being called a virtual address group. Each virtual address group is assigned a lock to manage the allocation of virtual addresses within that group. Each lock group contains at least one lock. Compared to using a single lock to manage the allocation of all virtual addresses, this method mitigates resource contention, maximizes the advantages of different algorithms, and more conveniently improves the "rationality of small object allocation" in the system.
[0011] According to some embodiments, the present invention adopts the following technical solution:
[0012] A memory allocation method, comprising:
[0013] The virtual addresses of the heap memory in user space are divided into multiple virtual address groups;
[0014] All objects that need to be allocated in user space are grouped to obtain multiple object groups and the corresponding virtual address groups for each object group;
[0015] The memory allocator allocates memory for each object group within the corresponding virtual address group using a memory allocation algorithm, based on the required memory length.
[0016] Furthermore, the memory allocator searches for a suitable size of free memory while ensuring memory address alignment; if the memory allocator finds a suitable size of free memory, it returns the location for the object group to use; otherwise, the memory allocation is deemed to have failed.
[0017] Furthermore, if memory allocation fails, the system will directly notify the user of the allocation failure.
[0018] Furthermore, if memory allocation fails, the system attempts to allocate the required memory in another virtual address group. Only if this also fails will the system be notified of allocation failure.
[0019] Furthermore, the memory allocator is also used to release virtual addresses within the virtual address group using a memory release algorithm when memory needs to be released.
[0020] Furthermore, different virtual address groups may use different memory allocation and release algorithms.
[0021] Furthermore, each virtual address group is assigned a set of locks to manage the allocation and release of virtual addresses within the virtual address group, and each set of locks contains at least one lock.
[0022] Furthermore, the specific method of grouping is as follows: when a thread starts, a virtual address group is selected from all virtual address groups and bound to the thread, and all objects that the thread needs to allocate in the heap memory in user space are divided into an object group. The virtual address group corresponding to the object group is the virtual address group bound to the thread.
[0023] A memory allocation system, comprising:
[0024] The virtual address group partitioning module is configured to divide the virtual addresses of the heap memory in user space into multiple virtual address groups;
[0025] The object group partitioning module is configured to group all objects that need to be allocated in user space, resulting in multiple object groups and the virtual address group corresponding to each object group;
[0026] The memory allocation module is configured such that the memory allocator allocates memory for each object group within the corresponding virtual address group using a memory allocation algorithm, based on the required length of memory requested.
[0027] Furthermore, it also includes 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 within the current virtual address group.
[0028] The beneficial effects of this invention are:
[0029] 1. This invention divides the virtual addresses of heap memory in user space into multiple virtual address groups and assigns a lock to each virtual address group to manage the allocation of virtual addresses within the same virtual address group. Compared to using a single lock to manage the allocation of virtual addresses of heap memory in all user spaces, this method results in a more moderate resource contention.
[0030] 2. This invention divides the virtual address of heap memory in user space into multiple virtual address groups. Each virtual address group can use different algorithms as needed to allocate and release memory, which helps to maximize the advantages of different memory allocation algorithms.
[0031] 3. The present invention simplifies the process of aggregating small objects that are "suitable for combination" and isolating small objects that are "suitable for separation" by specifying the range of virtual addresses of small objects, which provides convenience for improving the rationality of small object allocation in the heap. Attached Figure Description
[0032] The accompanying drawings, which form part of this invention, are used to provide a further understanding of the invention. The illustrative embodiments of the invention and their descriptions are used to explain the invention and do not constitute an improper limitation of the invention.
[0033] Figure 1 This is a flowchart of a memory allocation method according to the present invention;
[0034] Figure 2 This is a flowchart of the memory allocation algorithm in the first embodiment. Detailed Implementation
[0035] The present invention will be further described below with reference to the accompanying drawings and embodiments.
[0036] It should be noted that the following detailed description is illustrative and intended to provide further explanation of the invention. Unless otherwise specified, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this invention pertains.
[0037] It should be noted that the terminology used herein is for the purpose of describing particular embodiments only and is not intended to limit the scope of exemplary embodiments according to the invention. As used herein, the singular form is intended to include the plural form as well, unless the context clearly indicates otherwise. Furthermore, it should be understood that when the terms "comprising" and / or "including" are used in this specification, they indicate the presence of features, steps, operations, devices, components, and / or combinations thereof.
[0038] Terminology Explanation:
[0039] Memory object (or simply object): Used to store data and can be used by threads. In this invention, an object generally refers to the memory in the heap in user space.
[0040] In a first aspect, the present invention provides a method for allocating memory.
[0041] A memory allocation method, comprising:
[0042] S1: The virtual address space is initially divided into multiple spaces, and the virtual address space used as the heap memory in the user space is determined; the space includes the kernel space and the user space, and the user space contains the heap memory.
[0043] Suppose that sys11 is some kind of operating system, and that the virtual addresses between virtual address n11 and virtual address n12 in sys11 can be used by the heap memory in user space.
[0044] Taking sys11, a 64-bit computer system, as an example, let n11 equal 0x00FFFFFFFFFF and n12 equal 0x000100000000. Virtual addresses higher than n11 that can be accessed belong to the kernel space; virtual addresses not higher than n11 that can be accessed belong to the user space; virtual addresses lower than n12 cannot be used as addresses in heap memory; virtual addresses between n12 and n11 can be used as addresses in heap memory.
[0045] However, most operating systems have already defined which virtual addresses are used by the kernel, which virtual addresses are used by user programs, and which virtual addresses can be used by heap memory in user space.
[0046] S2: According to the partitioning rules, the virtual addresses of the heap memory in user space are divided into multiple virtual address groups.
[0047] Preferably, the virtual addresses of the heap memory in user space are divided into multiple primary virtual address groups and one secondary virtual address group; the size of the address space managed by each virtual address group can be different, and the addresses contained therein do not have to be contiguous; each virtual address group is allocated a set of locks to manage the allocation of virtual addresses within the virtual address group, and each set of locks contains at least one lock.
[0048] For the sys11 computer system, the virtual address between virtual address n11 and virtual address n12 can be used by the heap memory. When applying this invention, the virtual address between n11 and n12 will be divided into several "virtual address groups". Generally, the virtual addresses contained in each virtual address group are contiguous, and the virtual addresses contained in each memory page will not be allocated to two virtual address groups.
[0049] When virtual addresses within a virtual address group are contiguous, and each memory page is not divided into two virtual address groups, the following conclusions are generally correct: if two variables are in the same virtual address group, they may be in the same page and may be stored in very close proximity; if two variables are not in the same virtual address group, they cannot be in the same page.
[0050] It can be inferred that allocating "suitable for combination" small objects to the same virtual address group and "suitable for separation" small objects to different virtual address groups is an effective way to improve the "rationality of small object allocation".
[0051] Furthermore, allocating a set of locks to each virtual address group to manage the allocation of virtual addresses within that virtual address group results in less resource contention compared to using a single set of locks to manage the allocation of virtual addresses in the heap memory of all user spaces.
[0052] In conclusion, dividing virtual addresses into several virtual address groups is beneficial to improving system efficiency.
[0053] In practical implementation, 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, the virtual address space between n12 and n11 can be divided as follows:
[0054] Addresses 0x000100000000 to 0x00017FFFFFFF are the 01st primary virtual address group;
[0055] Addresses 0x000180000000 to 0x0001FFFFFFFF are the 02nd primary virtual address group; ...
[0056] Addresses 0x001980000000 to 0x0019FFFFFFFF are the 50th major virtual address group;
[0057] Other virtual addresses are assigned to the undivided sub-virtual address group.
[0058] Using numbers 0 to 49 to represent the 1st to 50th major virtual address groups, and other numbers to represent undivided virtual addresses, it can be seen that no memory page belongs to two virtual address groups (including the major virtual address group and the undivided virtual address group), and the virtual addresses contained in each virtual address group (including the major virtual address group and the undivided virtual address group) are contiguous. In other words, after dividing the virtual addresses of the heap memory 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 contiguous.
[0059] Of course, the division result can also be defined as:
[0060] Addresses 0x000100001020 to 0x000100001100 are the 01st primary virtual address group;
[0061] Addresses 0x000100001101 to 0x000100001200 are the 02nd primary virtual address group;
[0062] Addresses 0x000100001201 to 0x000180000002 are the 3rd primary virtual address group;
[0063] Addresses 0x000180000003 to 0x000200000002 are the 4th primary virtual address group; ...
[0064] Addresses 0x001800020000 to 0x0019FFFFFFFF are the 50th major virtual address group;
[0065] Other virtual addresses are assigned to the undivided sub-virtual address group.
[0066] Numbers 0 to 49 represent the 1st to 50th primary virtual address groups, and the other numbers represent the undivided secondary virtual address groups. It can be seen that the 1st, 2nd, and 3rd primary virtual address groups share the same page, and the virtual addresses contained in the undivided secondary virtual address group are not consecutive.
[0067] In addition, as needed, 3, 31, 256, or 4000 virtual address groups can be fixed from the virtual address space. Alternatively, when the program starts and before allocating memory in the heap, the user can specify how many virtual address groups to divide from the virtual address space, and the virtual addresses contained in each virtual address group.
[0068] However, regardless of the division method, the same virtual address cannot be assigned to different virtual address groups.
[0069] S3: Group all objects that need to be allocated in user space to obtain multiple object groups and the corresponding virtual address groups for each object group; after the virtual address groups are divided, objects from different groups will be allocated within the corresponding virtual address groups.
[0070] Specific methods for grouping include:
[0071] The first grouping method: When a thread starts, a virtual address group is selected from all virtual address groups and bound to the thread. All objects that the thread needs to allocate in the heap memory in user space are divided into an object group. The virtual address group corresponding to the object group is the virtual address group bound to the thread.
[0072] Preferably, when a thread starts, it selects one of the main virtual address groups from all the main virtual address groups and binds it to the thread. All objects that the thread needs to allocate are divided into an object group. The virtual address group corresponding to the object group is the main virtual address group bound to the thread. When the thread needs to request memory, memory is allocated to the object group from the main virtual address group to which the thread is bound. When the thread cannot obtain the required virtual address from the main virtual address group to which it is bound, it attempts to allocate from the secondary virtual address group or returns failure directly, according to the agreement or parameters.
[0073] When a thread exits, it is assumed that the thread is no longer bound to the virtual address group it was previously bound to.
[0074] When there are too many threads in a program, multiple threads may be bound to the same virtual address group. However, regardless of whether this happens, when a thread selects the virtual address group to bind to, it will choose from the few main virtual address groups that are bound to the least by other threads.
[0075] If the program has fewer than 50 threads, each thread is uniquely bound to a main virtual address group. In many cases, resource contention caused by threads requesting memory is greatly reduced. Therefore, the memory allocation method proposed in this application can effectively reduce resource contention in many situations.
[0076] The following grouping methods can also be used:
[0077] The second grouping method is to divide all objects into three groups. In the first group, most positions of all objects have a high probability of being accessed by the CPU, meaning that the probability of all objects being accessed by the CPU is greater than the first threshold. In the second group, most positions of all objects have a low probability of being accessed by the CPU, meaning that the probability of all objects being accessed by the CPU is less than the second threshold. All other objects are assigned to the third group.
[0078] Specify the virtual address group corresponding to each object group. Preferably, all objects in the first group request memory space in the first virtual address group, all objects in the second group request memory space in the second virtual address group, and all objects in the third group request memory space in the undivided address group.
[0079] In systems employing the LRU page replacement algorithm, since the 01st virtual address group contains frequently accessed memory, memory pages in the 01st virtual address group are more likely to remain in memory due to frequent access. Therefore, the memory allocation method of this application has high CPU memory access efficiency.
[0080] In systems employing the LRU page replacement algorithm, since the second virtual address group contains memory that is not frequently accessed, memory pages in the second virtual address group may be written to the swap partition or compressed due to long-term inactivity. Therefore, the memory allocation method proposed in this application may have high memory utilization efficiency.
[0081] The third grouping method involves dividing all objects into approximately 50 object groups, each corresponding to a virtual address group. Preferably, 50 groups are created from all the smaller objects, ensuring that the smaller objects in each group are frequently accessed and suitable for placement within the same cache line. The programmer then allocates these 50 groups of smaller objects in virtual address groups 1 through 50. `undivided` is used for allocating other objects.
[0082] In the second grouping method, two frequently accessed small objects may not be accessed simultaneously, or they may be accessed simultaneously but should not be placed in the same cache line. The third grouping method can be an optimization of the second grouping method in certain situations.
[0083] Examples will be given to illustrate the specific use of the first and second grouping methods.
[0084] For example, let t1 and t2 be two moments in the computer, and Trd1 and Trd2 be two threads; at moment t1, Trd1 needs to allocate an object obj11 with a length of 9 bytes, and obj11 is frequently accessed; at moment t2, Trd1 needs to allocate an object obj12 with a length of 4 bytes, and obj12 is not frequently accessed; at moment t1, Trd2 needs to allocate an object obj21 with a length of 19 bytes, and obj21 is frequently accessed; at moment t2, Trd2 needs to allocate an object obj22 with a length of 44 bytes, and obj22 is not frequently accessed.
[0085] If only the first grouping method (i.e., the "thread-bound" allocation method) is used, then: 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 the 02nd virtual address group.
[0086] If only the second grouping method (i.e., the "by access frequency" allocation method) is used, then: 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 02nd virtual address group.
[0087] S4: Determine the length of memory to be requested; the memory allocator, based on the requested memory length, and while ensuring memory address alignment (e.g., natural alignment), uses a memory allocation algorithm within the corresponding virtual address group to find free memory for each object group and allocates the memory. Different virtual address groups may use different memory allocation algorithms.
[0088] The memory allocation scheme can be as follows: The memory allocator locks 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 used to find free memory while ensuring memory address alignment; if the memory allocator finds free memory, it unlocks the virtual address group and returns the location for the object group to use; otherwise, the memory allocation is determined to have failed, and the lock on the virtual address group is unlocked.
[0089] Combination Figure 2Explain how to allocate memory within a specific virtual address group. Assume the memory allocator used is mallocx, and the function prototype of mallocx is void. The `mallocx(size_t len, int id)` function is used to calculate the length of memory requested by the program, which is the length of memory that a specific object in a certain object group needs to request, and `id` is the virtual address group number, which is the virtual address group number corresponding to this object group.
[0090] In sys11, each block of memory allocated as a heap object corresponds to a data structure called header. The header structure has two member variables: used and next. used records the length of memory actually marked as occupied during a memory allocation; next records the address of the next header structure.
[0091] The memory allocator knows the address of the first header structure in a virtual address group, and can then traverse all header structures in that virtual address group, starting from the first header structure. The memory allocator can also calculate the length of the free region between a given header structure and its next header structure using the address of that header structure, the value of its `used` parameter, and the value of its `next` parameter.
[0092] Before each virtual address group is used, a segment is selected from the beginning of the virtual address group for private anonymous mapping. Within this segment, the first and last header structures are initialized. The next pointer of the first header structure points to the last header structure, and its used value is 0. The last header structure is located at the end of the virtual addresses that have completed private anonymous mapping, its next pointer points to null, and its used value is 0.
[0093] When memory allocation is needed, the memory allocator obtains the value of `id` and the length `len` of the memory to be allocated. Based on the value of `id`, the memory allocator determines which of the 51 virtual address groups the user is referring to. Assuming `id=0`, the memory allocator `mallocx` locks `lock01`, which manages the 01st major virtual address group. Then, within the virtual address range 0x000100000000 to 0x00017FFFFFFF, it traverses from the first header structure, searching for a free memory block that can provide memory of length greater than or equal to `sizeof(header) + len` (the length of the header structure + the length of the memory to be allocated), and the starting position of the provided memory is aligned. During the traversal, before traversing the next header structure of a given header structure, the address of the current header structure is stored in `last`.
[0094] If no suitable memory is found even after traversing to the last header, a virtual address range called `added` is selected from the virtual addresses following the last header. `added` must be of suitable length and within the first major virtual address group. Then, a private anonymous mapping is performed on `added`, and a new header is initialized at the end of `added`, with the `next` pointer of the header pointed to by `last` pointing to this new header. This new header becomes the new last header, and the original last header becomes free memory. The required memory is then searched between the header pointed to by `last` and the new last header.
[0095] If the added address cannot be provided in the virtual address group that has not undergone private anonymous mapping, the allocation is deemed to have failed.
[0096] If the memory allocator finds a free memory region, it unlocks the virtual address group and returns the location available for the object group. Specifically: the memory allocator mallocx stores the starting address of the found free region into nStart; the memory between nStart and nStart+sizeof(header)-1 is used to store the header structure; the address pointed to by nStart+sizeof(header) should be memory aligned; the header structure is set to match the header structure corresponding to last; after these two header structures are set, mallocx unlocks the virtual address group lock01, stores the value of nStart+sizeof(header) (the location available for the object group) into nMem, and returns nMem.
[0097] If the memory allocator mallocx cannot find suitable memory, the memory allocation is considered to have failed.
[0098] Handling memory allocation failures: When memory allocation fails within a specified virtual address range, the following measures can be taken based on conventions or parameters:
[0099] Measure 1: Directly inform the recipient that the allocation has failed;
[0100] The second measure is to allocate the required memory in another virtual address group, preferably in the undivided sub-virtual address group. Only if this still fails should the allocation failure be reported.
[0101] S5: Release memory. The memory allocator is also used to release virtual addresses within the current virtual address group using a memory release algorithm when memory needs to be released. Different virtual address groups may use different memory release algorithms.
[0102] The memory release scheme can be as follows: determine the virtual address group where the memory to be released is located, and then lock the virtual address group; check whether the header structure corresponding to the memory to be released is valid. If not, unlock the virtual address group and send information to the main program; otherwise, set the header structure and other related header structures, mark the memory marked as occupied as free, realize memory release, and unlock the virtual address group.
[0103] The memory allocator starts with the first header structure and traverses all header structures. During the traversal, before traversing the next header structure of a given header structure, the address of that header structure is stored in `last`. If no header structure corresponding to the memory to be released is found, the lock of that virtual address group is unlocked and a message is sent to the main program. Otherwise, the header structure corresponding to the memory to be released and the header structure corresponding to `last` are set, the memory marked as occupied is marked as free, the memory is released, and the lock of that virtual address group is unlocked.
[0104] For example, if nMem is 0x000100000400, when a user needs to release nMem, based on the value of nMem, it is determined that nMem is in the 01st virtual address group. Then, lock01 is locked, and it is checked whether nMem - sizeof(header) points to a valid header structure. If not, lock01 is opened and the abort function is executed. 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, thereby realizing memory release. Finally, lock01 is opened.
[0105] The process a program performs when handling signals such as SIGINT is called the signal handling process. When using the algorithms described in S4 and S5, to avoid deadlock, it is prohibited to allocate or delete memory in the heap during the signal handling process.
[0106] The memory allocation and release algorithms described in S4 and S5 are easy to explain, but they have several problems, such as low efficiency and failure to remove unnecessary mappings.
[0107] In addition to the algorithms described in S4 and S5, the same virtual address group can also use different algorithms for memory allocation and deallocation based on the characteristics of the allocated memory, such as its length; while different virtual address groups do not need to use the same algorithm for memory allocation and deallocation. A certain algorithm may be particularly suitable for some object groups, while another algorithm may be particularly suitable for other object groups. Using different algorithms for different virtual address groups helps to leverage the strengths of each algorithm.
[0108] In summary, the memory allocation method in this application facilitates the improvement of the "reasonable allocation of small objects in the heap" and enhances system efficiency. Because multiple sets of locks can be used to manage virtual addresses, resource contention when multiple threads request virtual addresses is alleviated to some extent.
[0109] Secondly, the present invention also provides a memory allocation system;
[0110] A memory allocation system, comprising:
[0111] The virtual address group partitioning module is configured to divide the virtual addresses of the heap memory in user space into multiple virtual address groups;
[0112] The object group partitioning module is configured to group all objects that need to be allocated in user space, resulting in multiple object groups and the virtual address group corresponding to each object group;
[0113] The memory allocation module is configured such that the memory allocator allocates memory for each object group within the corresponding virtual address group using a memory allocation algorithm, based on the required length of memory requested.
[0114] The memory release module is configured to use a memory release algorithm to release virtual addresses within the current virtual address group when the memory allocator needs to release memory.
[0115] The above description is merely a preferred embodiment of the present invention and is not intended to limit the invention. Various modifications and variations can be made to the present invention by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.
Claims
1. A memory allocation method, characterized in that, include: The virtual addresses of the heap memory in user space are divided into multiple virtual address groups. The same virtual address cannot be divided into different virtual address groups. All objects that need to be allocated in user space are grouped to obtain multiple object groups and the corresponding virtual address groups for each object group; The object is used to store data for use by threads, and refers to the memory in the heap in user space. The specific grouping method includes: when a thread starts, select a virtual address group from all virtual address groups and bind it to the thread, and divide all objects that the thread needs to allocate in the heap memory in user space into an object group. The virtual address group corresponding to the object group is the virtual address group bound to the thread. Alternatively, all objects can be divided into three groups: objects in the first group have a probability of being accessed by the CPU that is greater than the first threshold; objects in the second group have a probability of being accessed by the CPU that is less than the second threshold; and other objects are assigned to the third group. Alternatively, all objects can be divided into object groups, with each object group corresponding to a virtual address group. Small objects in each group will be accessed frequently and placed in the same cache line. Each virtual address group is assigned a set of locks to manage the allocation and release of virtual addresses within the virtual address group. Each set of locks contains at least one lock. The memory allocator allocates memory for each object group within the corresponding virtual address group according to the required memory length, using a memory allocation algorithm. When memory needs to be released, a memory release algorithm is used to release the virtual addresses within the virtual address group. Different virtual address groups use different memory allocation and release algorithms.
2. The memory allocation method as described in claim 1, characterized in that, The memory allocator searches for a suitable amount of free memory while ensuring memory address alignment. If the memory allocator finds a suitable amount of free memory, it returns the location for the object group to use; otherwise, the memory allocation is considered to have failed.
3. The memory allocation method as described in claim 2, characterized in that, If memory allocation fails, notify the user directly of the failure.
4. The memory allocation method as described in claim 2, characterized in that, If memory allocation fails, the system attempts to allocate the required memory in another virtual address group. Only if this also fails will the system be notified of allocation failure.
5. A memory allocation system, characterized in that, include: The virtual address group partitioning module is configured to divide the virtual addresses of the heap memory in user space into multiple virtual address groups. The same virtual address cannot be partitioned into different virtual address groups. The object group partitioning module is configured to group all objects that need to be allocated in user space, resulting in multiple object groups and the virtual address group corresponding to each object group; The object is used to store data for use by threads, and refers to the memory in the heap in user space. The specific grouping method includes: when a thread starts, select a virtual address group from all virtual address groups and bind it to the thread, and divide all objects that the thread needs to allocate in the heap memory in user space into an object group. The virtual address group corresponding to the object group is the virtual address group bound to the thread. Alternatively, all objects can be divided into three groups: objects in the first group have a probability of being accessed by the CPU that is greater than the first threshold; objects in the second group have a probability of being accessed by the CPU that is less than the second threshold; and other objects are assigned to the third group. Alternatively, all objects can be divided into object groups, with each object group corresponding to a virtual address group. Small objects in each group will be accessed frequently and placed in the same cache line. Each virtual address group is assigned a set of locks to manage the allocation and release of virtual addresses within the virtual address group. Each set of locks contains at least one lock. When a thread starts, it selects a virtual address group from all virtual address groups and binds it to the thread. It also divides all objects that the thread needs to allocate in the heap memory of user space into an object group. The virtual address group corresponding to the object group is the virtual address group bound to the thread. The memory allocation module is configured such that: the memory allocator allocates memory for each object group within the corresponding virtual address group according to the required length of memory requested; and when memory needs to be released, it releases the virtual addresses within the virtual address group using a memory release algorithm. Different virtual address groups use different memory allocation and release algorithms.
6. A memory allocation system as described in claim 5, characterized in that, It also includes 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 within the current virtual address group.
Citation Information
Patent Citations
Address distribution method and device
CN105468542A
Memory allocation method, memory release method, and related device
CN109144718A
Method for programmer-controlled cache line eviction policy
CN1804816A
Memory management
US20050193169A1