Memory management method for reducing memory fragmentation
By dividing the heap area into cache areas in the memory management of the real-time operating system and declaring a fixed-length memory pool linked list in the cache area, the serious problem of memory fragmentation is solved, memory allocation efficiency is improved, and the operation requirements of the real-time operating system are adapted.
Patent Information
- Application Number
- CN202411726550.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-28
- Publication Date
- 2025-03-21
- Estimated Expiration
- 2044-11-28
AI Technical Summary
The memory management method of existing real-time operating systems frequently divides the memory blocks in the heap area, resulting in serious memory fragmentation and low allocation efficiency, which cannot meet the operation requirements of the real-time operating system.
A memory management method that reduces memory fragmentation is adopted. By dividing the cache area in the heap area, creating a linked list array, and declaring a fixed-length memory pool linked list in the cache area, then directly querying the fixed-length memory pool linked list from the cache area when the user requests memory, and only allocating the memory block from the heap area when the cache area cannot be met.
It reduces the frequency of memory block cutting in the heap area, reduces memory fragmentation, improves the efficiency of memory allocation, and can better adapt to the operation requirements of the real-time operating system.
Smart Images

Figure CN119668848A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of memory management of real-time operating systems, and in particular to a memory management method for reducing memory fragmentation. Background Art
[0002] Memory management in a real-time operating system is a key component to ensure the real-time performance and stability of the real-time operating system. Memory management ensures the real-time performance and stability of the system through dynamic memory allocation, management of memory heap and memory pool, and effective memory allocation algorithms, while reducing memory fragmentation problems and improving memory utilization.
[0003] At present, if the memory block obtained is larger than the requested memory block, the dynamic memory allocation method of the real-time operating system will divide the memory block into two parts, one part is used to meet the memory request, and the other part is reserved as a new free memory block in the memory heap area. For example, the memory management method of the embedded system disclosed in the patent document with application number 201711347959.2 pre-sets the memory block size of the heap area at the stage of creating the memory pool, and initializes it to a fixed-length memory pool structure. When the user has a memory request, according to the user's memory size requirements, the memory block needs to be frequently divided. After multiple memory allocations and releases, a large number of small and discontinuous free areas will be left, and there is a serious problem of memory fragmentation; for the real-time operating system, even if the total free memory is sufficient, because the memory addresses of the small blocks are discontinuous, they cannot be allocated as a whole large memory block, and there is a problem of low allocation efficiency and unsuitability for the operation requirements of the real-time operating system. Summary of the invention
[0004] The purpose of the present invention is to provide a memory management method that reduces memory fragmentation in view of the problem that the memory management method in the prior art frequently divides the memory blocks of the heap area, causing serious memory fragmentation, resulting in low allocation efficiency and not adapting to the running requirements of the real-time operating system.
[0005] In order to achieve the above object, the technical solution adopted by the present invention is:
[0006] A memory management method for reducing memory fragmentation comprises the following steps:
[0007] S1, heap area initialization: initialize the memory of the heap area to a variable-length memory pool; create a free linked list of the variable-length memory pool; the free linked list is an empty node linked list;
[0008] S2. Buffer area initialization:
[0009] S21, allocating a plurality of first memory blocks from the heap area to form a cache area, and creating a linked list array corresponding to the first memory blocks;
[0010] S22. Create a fixed-length memory pool linked list corresponding to each type of the first memory block according to the capacity type of the first memory block; the fixed-length memory pool linked list is an empty node linked list;
[0011] S23, setting the fixed-length memory pool linked list in the linked list array in order from small to large according to the capacity of the corresponding first memory block, so that each array element of the linked list array represents a first memory block, and then declaring a fixed-length memory pool in the buffer area, and constructing a data structure of the buffer area;
[0012] S3, memory allocation:
[0013] S31, according to the size of the requested memory, querying in the cache whether there is a fixed-length memory pool linked list matching the size of the requested memory;
[0014] S311. If a matching fixed-length memory pool linked list is found, the first node of the fixed-length memory pool linked list is obtained from the cache area, and the obtained fixed-length memory pool is returned; the fixed-length memory pool includes a pointer to a bidirectional linked list of free memory blocks;
[0015] S312, if no matching fixed-length memory pool linked list is found, allocate a second memory block from the heap area to the cache area; initialize the second memory block as a fixed-length memory pool; initialize the free linked list, construct the fixed-length memory pool linked list; add the fixed-length memory pool to the fixed-length memory pool linked list;
[0016] S32, checking whether there is a free memory block in the bidirectional linked list;
[0017] S33, if the free memory block does not exist, return to execute step S312;
[0018] S34: If the free memory block exists, a fixed-length memory pool is allocated to respond to the user's request for memory.
[0019] The memory management method for reducing memory fragmentation described in the present invention is, first, by dividing the first memory into a cache area in the heap area, creating a linked list array, declaring the memory block of the cache area to correspond to the memory pool linked list, and then constructing the cache area into a data structure that declares the fixed-length memory pool linked list; when a user requests memory, the fixed-length memory pool can be directly queried from the cache area to obtain the fixed-length memory pool, and the first memory block of the requested memory is allocated in the fixed-length memory pool, and only when the fixed-length memory pool in the cache area cannot find the first memory block that meets the requested memory size requirement, the second memory block is allocated from the heap area, and there is no need to directly divide the memory of the heap area immediately, thereby reducing the frequency of memory division in the heap area, and reducing memory fragmentation at one time; second, if the fixed-length memory pool linked list cannot be queried in the cache area, the second memory block is allocated from the heap area, and the second memory block is initially allocated in coordination with the fixed-length memory pool. The above method creates a corresponding free linked list by converting the memory blocks in the heap area. The free linked list in the heap area and the fixed-length memory pool linked list in the cache area are both empty node linked lists, that is, they are only declared but not initialized, so it is easier to obtain matching memory blocks. Then, in the allocation stage, the second memory block is initialized as a fixed-length memory pool, and the free linked list is initialized as a fixed-length memory pool linked list, so that the memory in the heap area can be quickly allocated as fixed-size memory blocks. Not only is the allocation speed faster, but the memory blocks in the heap area and the memory blocks in the cache area can be reused repeatedly, which further reduces the frequency of memory block segmentation in the heap area. In short, this scheme combines the variable-length memory pool in the heap area with the fixed-length memory pool in the cache area to reduce the frequency of memory block cutting in the heap area, thereby reducing memory fragmentation, improving the efficiency of memory allocation, and being able to better match the running requirements of the real-time operating system.
[0020] Preferably, the memory management method for reducing memory fragmentation described in the present invention, the S312 specifically includes the following steps: constructing the fixed-length memory pool into a node object of the fixed-length memory pool linked list, and declaring a next pointer and a prev pointer; pointing the next pointer to the head node of the fixed-length memory pool linked list; and pointing the prev pointer to the node of the next fixed-length memory pool.
[0021] As a preferred solution of the present invention, after the second memory block is initialized as a fixed-length memory pool, the node object of the fixed-length memory pool linked list is constructed as the fixed-length memory pool, and the free linked list is initialized, so that the creation of the fixed-length memory pool linked list can be further quickly completed, and the initialization of the fixed-length memory pool can further improve the efficiency of memory allocation.
[0022] Preferably, the memory management method for reducing memory fragmentation described in the present invention, the S31 specifically includes the following steps: traversing the linked list array, searching for the memory block corresponding to the subscript of the linked list array, and obtaining the matching fixed-length memory pool linked list from the linked list array through the subscript.
[0023] As a preferred solution of the present invention, by traversing the linked list array and searching for the corresponding fixed-length memory pool linked list by looking up the subscript, based on the data structure of the fixed-length memory pool, it is possible to quickly judge and obtain the fixed-length memory pool linked list, and return the obtained fixed-length memory pool, or execute heap area allocation memory block, thereby further improving the efficiency of memory acquisition and further optimizing the memory management method.
[0024] The memory management method for reducing memory fragmentation described in the present invention, the S33 specifically also includes the following steps: when there are no free memory blocks in the bidirectional linked list, the next pointer and the prev pointer of the fixed-length memory pool are both set to null pointers, thereby making the bidirectional linked list without free memory blocks detached from the fixed-length memory pool linked list.
[0025] As a preferred solution of the present invention, by checking whether there are free memory blocks in the bidirectional linked list, for the processing method of the non-existence of free memory blocks, when allocating the second memory block from the heap area, the bidirectional linked list without free memory blocks can be modified, thereby making the bidirectional linked list that does not meet the free memory blocks be separated from the fixed-length memory pool linked list, avoiding node identification errors of the found fixed-length memory pool linked list, ensuring that the nodes on the fixed-length memory pool linked list have free memory blocks, improving the accuracy of obtaining free memory blocks, realizing the maintenance of the fixed-length memory pool linked list, avoiding multiple checks, and further improving the memory allocation efficiency.
[0026] Preferably, the memory management method for reducing memory fragmentation of the present invention further includes the following steps:
[0027] S4. Release the memory block:
[0028] S41, checking whether the address and capacity of the memory block in the cache area belong to the fixed-length memory pool; clearing the business data of the third memory block that does not belong to the fixed-length memory pool;
[0029] S42, adding the third memory block to the bidirectional linked list, so that the bidirectional linked list can manage the third memory block;
[0030] S43, checking the free linked list of the buffer area and the memory space of the fixed-length memory pool;
[0031] S44: If the third memory block of the cache area and the memory space of the cache area are not in use, add the third memory block of the cache area to a free linked list of the heap area.
[0032] As a preferred solution of the present invention, the fixed-length memory pool is checked by release so that there is no free memory pool in the current fixed-length memory pool, the space of the entire large memory block of the memory pool is released and added to the corresponding free linked list, and the free linked list management of the heap area is used. Each release check is carried out to make the release time timely and ensure the memory utilization rate; through the free linked list management, the released fixed memory blocks are specially managed, the frequency of heap area allocation is reduced, and they are merged into large memory blocks in the heap area, further improving the allocation efficiency of the heap area.
[0033] Preferably, the memory management method for reducing memory fragmentation of the present invention allocates the second memory block from the heap area to the cache area, specifically comprising the following steps:
[0034] Check whether the head node of the free linked list is empty. If the head node is not empty, directly return the free memory block to the cache area; if the head node is empty, allocate a second memory block from the variable-length memory pool and return the address of the second memory block.
[0035] As a preferred solution of the present invention, by checking whether there are free memory blocks in the heap area and judging by searching the free linked list, it is possible to avoid splitting a larger second memory block when there are free memory blocks in the heap area, and then by allocating matching requested memory in the heap area and the cache area, the number of heap area splits is further reduced, and the allocation of free memory blocks can be completed more quickly, thereby further improving the allocation efficiency.
[0036] In summary, due to the adoption of the above technical solution, the beneficial effects of the present invention are:
[0037] 1. By building a fixed-length memory pool cache area corresponding to the memory block type, avoid directly allocating memory from the heap area. Correspondingly, the heap area allocation adopts variable-length memory pool management, combining the variable-length memory pool of the heap area and the fixed-length memory pool of the cache area to reduce the frequency of heap memory block cutting and reduce memory fragmentation.
[0038] 2. In the process of querying and obtaining the requested memory, a fixed-length memory pool linked list is declared in the cache area to match and obtain the memory block. Compared with searching and obtaining the requested memory in a variable-length memory pool, the query and acquisition efficiency is higher, the memory block acquisition speed is faster, and the memory allocation efficiency is higher. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] Figure 1 It is a schematic diagram of the process of initializing the heap area and the cache area of the present invention;
[0040] Figure 2 It is a data structure schematic diagram of the fixed-length memory pool buffer area of the present invention;
[0041] Figure 3It is a schematic diagram of the memory allocation process of the present invention;
[0042] Figure 4 It is a schematic diagram of the flow of memory release of the present invention. DETAILED DESCRIPTION
[0043] The present invention is described in detail below.
[0044] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail in the following embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.
[0045] Embodiment 1:
[0046] refer to Figure 1-Figure 3 As shown, this embodiment provides a memory management method for reducing memory fragmentation, comprising the following steps:
[0047] S1, heap area initialization: initialize the memory of the heap area to a variable-length memory pool; create a free linked list of the variable-length memory pool of the cache area; the free linked list of the cache area is an empty node linked list;
[0048] S2, cache area initialization: S21, divide a plurality of first memory blocks from the heap area to form a cache area, and create a linked list array corresponding to the first memory block in the cache area; S22, according to the capacity type of the first memory block in the cache area, create a fixed-length memory pool linked list corresponding to each first memory block in the cache area; the fixed-length memory pool linked list in the cache area is an empty node linked list; S23, set the fixed-length memory pool linked list in the cache area linked list array in order from small to large according to the capacity of the corresponding first memory block in the cache area, so that each array element of the cache area linked list array represents a first memory block in the cache area, and then declare a fixed-length memory pool in the cache area, and construct the data structure of the cache area; specifically, after the cache area is initialized, the data structure of the fixed-length memory pool can be created such as Figure 2 As shown, the buffer area linked list array can be created as an array with a length of 72, and the array elements are fixed-length memory pool linked lists; the buffer area initialization can be specifically the following process: initialization declaration to build a linked list array → fixed-length memory pool linked list → declaration of fixed-length memory pool → data structure of memory block, to achieve a one-to-one correspondence between the linked list array and the memory block of the fixed-length memory pool buffer area; for example, linked list array subscript 0 element → 2KB fixed-length memory pool linked list → 2KB fixed-length memory pool → 2KB memory block, that is, subscript 0 → 2KB memory block.
[0049] S3, memory allocation:
[0050] S31, according to the size of the requested memory, querying in the cache whether there is a fixed-length memory pool linked list that matches the size of the requested memory in the cache;
[0051] S311. If a matching fixed-length memory pool linked list is found, the first node of the fixed-length memory pool linked list of the cache area is obtained from the cache area, and the obtained fixed-length memory pool is returned; the fixed-length memory pool of the cache area includes a pointer to a bidirectional linked list of free memory blocks;
[0052] S312, if no matching fixed-length memory pool linked list is found, allocate a second memory block from the cache heap area to the cache area; initialize the second memory block of the cache area as a fixed-length memory pool; initialize the cache area free linked list, construct a cache area fixed-length memory pool linked list; add the cache area fixed-length memory pool to the cache area fixed-length memory pool linked list;
[0053] It should be noted that step S312 in this embodiment includes allocation of the second memory block in the heap area, creation of nodes of the fixed-length memory pool linked list, and creation of the fixed-length memory pool linked list.
[0054] S32. Check whether there are free memory blocks in the bidirectional linked list of the cache area; S33. If there are no free memory blocks in the cache area, return to execute the S312 step; specifically, the S312 of the present invention specifically includes the following steps: construct the fixed-length memory pool of the cache area into a node object of the fixed-length memory pool linked list of the cache area, and declare the next pointer and the prev pointer; point the cache area next pointer to the head node of the fixed-length memory pool linked list; point the cache area prev pointer to the node of the next fixed-length memory pool.
[0055] S34. If there are free memory blocks in the cache area, a fixed-length memory pool is allocated to respond to the user's request for memory.
[0056] It should be noted that the cache initialization can be understood as the definition, declaration, and initialization setting of variables, objects, or data structures. It should also be noted that the heap area is all memory space for memory management, including divided cache areas, non-fixed allocated memory blocks, and unallocated memory space; the cache area is understood as a fixed memory block used by a fixed-length memory pool, and only the memory space used by a specified long memory pool is specified. The cache area is part of the memory heap.
[0057] It should be noted that the fixed-length or indefinite-length memory pool includes a memory block management header node and a memory that can be read and written by users. The memory management header node includes the starting address of the memory pool, the total size of the memory pool, a pointer to a doubly linked list of free memory blocks, a pointer to a doubly linked list of memory blocks, and a free linked list; the free linked list is only used to manage fixed-size released unallocated memory blocks, that is, memory blocks released by the buffer; the structure of the fixed-length memory pool includes a management header and a memory that can be read and written by users, and the area available to users is understood to be divided into memory blocks of the same size according to the memory block size of the fixed-length memory pool linked list; the management header includes the starting address of the memory pool, the total size of the memory pool, a pointer to a doubly linked list of free memory blocks, and prev pointers pointing to the previous next and next nodes of the fixed-length memory linked list respectively; wherein the next pointer is understood to be the next node in the linked list used to point to the current node in the linked list, and the prev pointer is relative to the next pointer, and is used to point to the previous node of the current node in the linked list. For example, if there is a node p, then the next pointer of node P is a pointer to the node immediately following p.
[0058] Specifically, S31 described in the present invention specifically includes the following steps: traversing the cache linked list array, searching for the memory block corresponding to the subscript of the cache linked list array, and obtaining a matching cache fixed-length memory pool linked list from the cache linked list array through the cache subscript.
[0059] Specifically, S33 described in the present invention also includes the following steps: when there are no free memory blocks in the cache area bidirectional linked list, the next pointer and prev pointer of the cache area fixed-length memory pool are set to null pointers, thereby making the cache area bidirectional linked list without free memory blocks detached from the cache area fixed-length memory pool linked list.
[0060] Specifically, allocating a second memory block from the cache heap area to the cache area includes the following steps: checking whether the head node of the cache free list is empty; if the cache head node is not empty, directly returning the cache free memory block to the cache; if the cache head node is empty, allocating a second memory block from the cache indefinite length memory pool, and returning the address of the second memory block in the cache.
[0061] It should be noted that the "first" and "second" in the first memory block and the second memory block are only for the convenience of clear description and no specific numerical interpretation is given; in the present invention, the first memory block is understood to be a memory block of a fixed size configured by the system, and the capacity is understood to be the size of the first memory block, for example, the system configured memory block size is 2KB, 4KB, 6KB, 8KB... etc., a memory block of a fixed size and the same number as the length of the linked list array; the second memory block is understood to be a larger memory divided from the heap area, for example, a large memory block with a capacity of 4096KB.
[0062] In the present invention, the linked list is understood as a non-continuous and non-sequential storage structure on a physical storage unit. The logical order of data elements is realized by the pointer link order in the linked list. The linked list nodes can be allocated at any position in the memory and connected through pointers. The array is understood as a continuous and sequential storage structure on a physical storage unit; a number of elements of the same type are organized in an ordered form, and the elements in the array are stored together in memory in this order, and the order of the elements is determined by the position or subscript of the element in the array; the subscript is understood as an integer index of a specific element in the array, which is used to identify the position of each element in the array; the linked list array is understood as the element type; the memory is understood as a component in the computer used to temporarily store the calculation data in the CPU and the data exchanged with the external memory such as the hard disk; the heap area is understood as the area used for dynamic memory allocation in the memory model; the fixed-length memory pool is understood as a memory allocation technology, which allocates and releases fixed-size memory blocks, and each time a fixed-size memory is allocated, no memory fragmentation will be generated; the variable-length memory pool is understood as a memory management technology, which allows dynamic allocation of memory blocks of different sizes to meet the needs of various data structures and objects; the memory block is understood as a continuous memory space for storing data or instructions; the free memory block is understood as a memory block that does not store data or instructions.
[0063] Specifically, in order to further allocate more memory blocks for allocation, the memory management method for reducing memory fragmentation according to the present invention further includes the following steps:
[0064] S4. Release memory blocks: S41. Check whether the address and memory block capacity of the cache memory block of the cache belongs to the fixed-length memory pool; clean up the business data of the third memory block that does not belong to the fixed-length memory pool; S42. Add the third memory block to the cache bidirectional linked list, so that the cache bidirectional linked list can manage the third memory block of the cache; S43. Check the memory space of the cache free list and the fixed-length memory pool; S44. If the third memory block of the cache and the cache memory space are not in use, add the third memory block of the cache to the free linked list of the heap.
[0065] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the protection scope of the present invention.
Claims
1. A memory management method for reducing memory fragmentation, characterized in that: The following steps are involved: S1, heap area initialization: initialize the memory of the heap area to a variable-length memory pool; create a free linked list of the variable-length memory pool; the free linked list is an empty node linked list; S2. Buffer area initialization: S21, allocating a plurality of first memory blocks from the heap area to form a cache area, and creating a linked list array corresponding to the first memory blocks; S22. Create a fixed-length memory pool linked list corresponding to each type of the first memory block according to the capacity type of the first memory block; the fixed-length memory pool linked list is an empty node linked list; S23, setting the fixed-length memory pool linked list in the linked list array in order from small to large according to the capacity of the corresponding first memory block, so that each array element of the linked list array represents a first memory block, and then declaring a fixed-length memory pool in the buffer area, and constructing a data structure of the buffer area; S3, memory allocation: S31, according to the size of the requested memory, querying in the cache whether there is a fixed-length memory pool linked list matching the size of the requested memory; S311. If a matching fixed-length memory pool linked list is found, the first node of the fixed-length memory pool linked list is obtained from the cache area, and the obtained fixed-length memory pool is returned; the fixed-length memory pool includes a pointer to a bidirectional linked list of free memory blocks; S312, if no matching fixed-length memory pool linked list is found, allocate a second memory block from the heap area to the cache area; initialize the second memory block as a fixed-length memory pool; initialize the free linked list, construct the fixed-length memory pool linked list; add the fixed-length memory pool to the fixed-length memory pool linked list; S32, checking whether there is a free memory block in the bidirectional linked list; S33, if the free memory block does not exist, return to execute step S312; S34: If the free memory block exists, a fixed-length memory pool is allocated to respond to the user's request for memory.
2. The memory management method for reducing memory fragmentation according to claim 1, characterized in that: The S312 specifically includes the following steps: constructing the fixed-length memory pool into a node object of the fixed-length memory pool linked list, and declaring a next pointer and a prev pointer; pointing the next pointer to the head node of the fixed-length memory pool linked list; and pointing the prev pointer to the next node of the fixed-length memory pool.
3. The memory management method for reducing memory fragmentation according to claim 1, characterized in that: The S31 specifically includes the following steps: traversing the linked list array, searching for the memory block corresponding to the subscript of the linked list array, and obtaining the matching fixed-length memory pool linked list from the linked list array through the subscript.
4. The memory management method for reducing memory fragmentation according to claim 1, characterized in that: The S33 specifically also includes the following steps: when there is no free memory block in the bidirectional linked list, the next pointer and prev pointer of the fixed-length memory pool are both set to null pointers, thereby making the bidirectional linked list without free memory blocks detached from the fixed-length memory pool linked list.
5. The memory management method for reducing memory fragmentation according to any one of claims 1 to 4, characterized in that: The following steps are also included: S4. Release the memory block: S41, checking whether the address and capacity of the memory block in the cache area belong to the fixed-length memory pool; clearing the business data of the third memory block that does not belong to the fixed-length memory pool; S42, adding the third memory block to the bidirectional linked list, so that the bidirectional linked list can manage the third memory block; S43, checking the memory space of the free linked list and the fixed-length memory pool; S44: If the third memory block and the memory space are not in use, add the third memory block to a free linked list in the heap area.
6. The memory management method for reducing memory fragmentation according to any one of claims 1 to 4, characterized in that: Allocating a second memory block from the heap area to the cache area specifically includes the following steps: Check whether the head node of the free linked list is empty. If the head node is not empty, directly return the free memory block to the cache area; if the head node is empty, allocate a second memory block from the variable-length memory pool and return the address of the second memory block.
Citation Information
Patent Citations
An Embedded Software Memory Management Method
CN108038002B
Memory control method for embedded systems
CN102915276A
Method of managing memory allocation of heap space
CN106855788A
Embedded software memory management method
CN108038002A
Dynamic object cache pool distribution method
CN110109677A