A memory management method and operating system for reducing fragmentation
By creating memory pools in memory management, dividing multi-level memory pages, and establishing a mapping relationship between logical space and memory pool, the problem of memory fragmentation is solved, and the rapid allocation and efficient utilization of memory is achieved.
Patent Information
- Application Number
- CN202510149534.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-11
- Publication Date
- 2025-05-13
- Estimated Expiration
- 2045-02-11
AI Technical Summary
Existing memory management methods are difficult to effectively alleviate memory fragmentation, resulting in the inability to effectively utilize memory space.
By creating a memory pool and dividing it into multi-level memory pages according to the preset size, a mapping relationship between logical space and memory pool is established, a page table is used to record the mapping relationship of memory pages, and automatic recycling is carried out through the memory recycling table.
It realizes rapid allocation of memory and reduces fragmentation, improving memory utilization efficiency.
Smart Images

Figure CN119621609B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of computer storage, and in particular relates to a memory management method and an operating system for alleviating fragmentation. Background Art
[0002] The two key points of memory management are to reduce memory fragmentation and improve efficiency. Memory fragmentation refers to the process of memory allocation and release, in which the memory space is gradually divided into many discontinuous small blocks, resulting in the ineffective use of free memory. There are two main forms of memory fragmentation:
[0003] (1) External fragmentation: refers to the free memory blocks distributed in different locations of the memory space. Although the total amount of free memory is sufficient, the allocation request of a large memory block may not be met due to discontinuity.
[0004] (2) Internal fragmentation: This refers to the waste of unused parts of the allocated block due to the allocated memory block being slightly larger than the actual demand. Even if the memory block is allocated successfully, some memory will be left idle.
[0005] The current mainstream memory management method aims to optimize these two points. In terms of memory allocation strategy, there are generally two solutions:
[0006] (1) Fixed partition allocation: The memory is divided into memory partitions according to the specified size, such as 2KB, 4KB, 8KB, 16KB, 32KB, etc. When allocating memory, a larger memory partition is allocated according to the size of the requested memory. This method is simple and easy to manage, but it may cause internal fragmentation;
[0007] (2) Variable partitioning: Based on the requested memory size, the first free memory partition that can meet the requested size is searched from the beginning of the memory, and memory is allocated from this memory partition. This method may cause external fragmentation.
[0008] In order to improve the effective use of memory, some strategies are introduced, such as memory partition merging and memory partition defragmentation.
[0009] Merge free memory partitions: When releasing a memory partition, if the adjacent memory partitions are also free, you can merge them into a larger memory partition to reduce external fragmentation. For example, after releasing a memory partition, if the adjacent memory partitions are free, you can merge the three memory partitions into a larger memory partition. However, in this case, if the adjacent memory partitions are occupied, the merge cannot be performed.
[0010] Memory partition defragmentation (also known as memory compression): gather the scattered free partitions in the memory together to form a larger continuous memory partition. This usually requires pausing the running program or stopping data access, moving each allocated memory to one end, and thus releasing a continuous free memory partition. Memory defragmentation is more expensive and is suitable for execution when the system is idle or before starting important tasks. Summary of the invention
[0011] The present invention aims to solve the technical problems existing in the prior art and provides a memory management method and operating system for reducing fragmentation. Memory management is performed through a memory pool, a logical space is created and mapped with the memory pool, a page table is used to record the mapping relationship between the memory pages of the memory pool and the memory pages of the logical space, and a memory recovery table records the memory information supporting automatic recovery and the total memory capacity in the memory recovery table in the order of application. Memory allocation, memory access, memory release and memory recovery are all performed through the logical space. The present invention realizes the rapid allocation of memory and reduces the fragmentation of memory.
[0012] The present invention provides a memory management method for alleviating fragmentation, comprising:
[0013] initialization:
[0014] Divide a block of memory and establish a memory pool. Divide the memory pool into multiple levels of memory pages according to the preset size. Divide the memory pages of each level from the top level to the bottom level, and mark the information of each memory page. The information of each memory page includes the information of all memory pages of the next level.
[0015] Create a logical space and divide the logical space into minimum memory pages for mapping with the memory in the memory pool;
[0016] Create a page table to record the mapping relationship between memory pages in the logical space and memory pages in the memory pool, and mark the usage of each memory page in the logical space;
[0017] Create a memory recycling table to record the memory information that supports automatic recycling and the total memory capacity in the memory recycling table in the order of memory application;
[0018] Create an address translation module, which is responsible for converting the memory address of the logical space into the memory address of the memory pool through the page table;
[0019] Memory allocation strategy: When allocating, search for free memory pages from the top-level memory page. If the top-level free memory pages are not enough to complete the memory allocation, memory recycling is performed. After the memory recycling is completed, search for free memory pages from the top-level memory page again; after the memory allocation of the memory pool is successful, update the information of the current level memory page and the previous level memory page in the memory pool, map the memory pages in the memory pool to the memory pages in the logical space, and update the page table; when applying for memory, set whether the memory allows automatic recycling through the flag bit; if the successfully allocated memory allows automatic recycling, the memory information is recorded at the end of the memory recycling table;
[0020] Memory access strategy: The address translation module extracts the memory address in the logical space, finds the memory address in the corresponding memory pool through the page table, and performs data access;
[0021] Memory release strategy: If the address of the memory to be released is the first address of the memory allocated to the logical space, the memory page in the corresponding memory pool is released according to the memory length allocated to the logical space, and the information of the relevant memory page is updated, and the page table is updated;
[0022] Memory recovery strategy: If the amount of missing memory is greater than the total memory capacity recorded in the memory recovery table, memory recovery is terminated and the memory allocation fails. If the amount of missing memory is less than or equal to the total memory capacity recorded in the memory recovery table, memory is released starting from the earliest allocated memory in the memory recovery table until the needs are met, and the memory recovery table is updated synchronously.
[0023] Preferably, the multi-level memory page includes a parent memory page and a child memory page, and the child memory page is smaller than the parent memory page; a multi-branch tree is constructed based on the multi-level memory pages, the top-level parent memory page is the root node, and the remaining memory pages are child nodes, and each parent memory page is divided into its subordinate child memory pages level by level, and the information of the corresponding current memory page is recorded at each node.
[0024] Preferably, the space allocation of each memory page in the memory pool is consistent with the physical design of the memory device.
[0025] Preferably, the space allocated to each memory page is an integer multiple of 2, in KB, MB or GB.
[0026] Preferably, the space allocation ratio of the parent memory page to the child memory page is 1:16-1:64, where the denominator is an integer multiple of 2.
[0027] Preferably, the minimum memory page size is 2-8 KB, which is an integer multiple of 2.
[0028] Preferably, the space allocation ratio of the parent memory page to the child memory page is 1:32, and the minimum memory page size is 4 KB.
[0029] Preferably, the space allocated to each memory page in the memory pool is a multiple of 2.
[0030] Preferably, the information of the memory page includes the number of the current memory page, the memory size, the free memory size, and the maximum continuous free memory size, and the memory information supporting automatic recycling in the memory recycling table includes the first address of the logical space allocation and the memory size.
[0031] Preferably, the size of the logical space is at least twice that of the memory pool; when the memory allocation is successful, a free space of equal size is sequentially found from the beginning of the logical space, and the memory pages in the memory pool are mapped to the logical space.
[0032] Preferably, the memory allocation strategy also includes:
[0033] When allocating, traverse the multi-branch tree nodes starting from the root node, skip the branches with no free memory, fill the free memory of the current node and then traverse the next node;
[0034] When the requested memory is not an integer multiple of the minimum memory page, the integer multiple part and the remainder part are allocated separately. The integer multiple part is allocated using the complete memory page in the memory pool, and the remainder part is allocated using the largest continuous free memory in the allocated memory in the memory pool, or using the new minimum memory page in the memory pool, and allocated from the first node found.
[0035] Preferably, when allocating memory, search for free memory pages starting from the root node; if the free memory of the root node is insufficient to complete the memory allocation, enter the memory recovery step, and search for free memory pages from the root node again after the memory recovery is completed; after the memory pool memory is successfully allocated, update the current node information and recursively update the parent node information, find the free space that meets the size of the allocated memory page in the logical space in order, map the memory pages in the memory pool to the memory pages in the logical space, and update the page table; if the allocated memory allows automatic recovery, the memory information is recorded at the end of the memory recovery table.
[0036] Preferably, the memory address of the logical space includes the logical space memory page number and the offset address within the memory page, and the maximum value of the offset address within the memory page is equal to the size of the minimum memory page minus 1.
[0037] Preferably, the memory access strategy specifically includes: the address conversion module extracts the memory page number of the logical space in the memory address in the logical space, checks whether the memory page of the logical space is allocated through the page table, and if the memory page is allocated, uses the page table to find the memory page number of the corresponding memory pool, and finds the corresponding memory address for data access through the memory page number of the memory pool and the offset address within the memory page; if the memory page is not allocated, returns an illegal access;
[0038] When mapping memory pages, if the previous and next memory pages of the logical space correspond to the memory pages of the memory pool and are also continuous, then the two memory pages in the logical space are marked as continuous; the memory access strategy also includes continuous memory access. When accessing the next logical space memory page, if the memory page of the logical space has a continuous mark, the address conversion module no longer looks up the page table for memory address conversion, but directly uses the address of the current logical space memory page for address offset operations.
[0039] Preferably, when releasing memory, check whether the address of the memory to be released is marked as the first address of the memory allocated by the logical space. If not, return an illegal operation; if so, release all memory pages and mark them as unallocated according to the memory allocated by the logical space and the memory allocation length; update the information of the relevant child nodes on the multi-branch tree, and recursively update the information on the parent node; update the page table information, and remove the corresponding memory allocation information;
[0040] When the memory is released, if the memory supports automatic recycling, the corresponding information in the memory recycling table is removed; after the memory is released, if the surrounding space is also free, the maximum continuous free memory size is updated at the same time.
[0041] The present invention provides an operating system for memory management that reduces fragmentation, including a memory management module. The memory management module uses the above-mentioned memory management method that reduces fragmentation to perform memory management.
[0042] Compared with the prior art, the present invention has the following beneficial effects:
[0043] (1) The space allocation of memory pages in the present invention is consistent with the physical design of the memory device; the number of memory page levels and the allocation of memory page space can be adjusted according to the actual scenario, and set based on the high-efficiency memory space requirement. Too much memory page space allocation can reduce memory utilization and cause memory waste; too little memory page space allocation can result in too many memory pages or too many memory page levels, increasing the maintenance cost of the memory pages.
[0044] (2) When searching for free memory pages, the present invention starts from the root node. If the root node has no free memory, memory is recycled. When allocating, traversal starts from the root node. During the traversal of the nodes, branches without free memory are skipped to speed up the search for free memory. The free memory of the current node is filled before traversing the next node. The largest continuous free memory is used first to reduce the number of memory allocations, thereby improving the memory allocation efficiency.
[0045] (3) The memory address of the logical space of the present invention includes the logical space memory page number and the offset address within the memory page; the maximum value of the offset address within the memory page is equal to the size of the minimum memory page minus one, thereby avoiding the need for additional calculations for memory addressing.
[0046] (4) The present invention can achieve continuous memory access by mapping memory pages in the memory pool to continuous memory pages in the logical space. When accessing the next memory page of the logical space, if the next memory page of the logical space has a continuous mark, the address conversion module no longer searches the page table for memory address conversion, but directly uses the address of the current memory page to perform address offset operations to access data, thereby eliminating the step of repeated address conversion and speeding up memory access. BRIEF DESCRIPTION OF THE DRAWINGS
[0047] Figure 1 A schematic diagram of multi-level memory pages divided according to a memory management method for alleviating fragmentation according to an embodiment of the present invention.
[0048] Figure 2 A technical roadmap for a memory management method for alleviating fragmentation according to an embodiment of the present invention. DETAILED DESCRIPTION
[0049] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. The described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0050] The present invention provides a memory management method for alleviating fragmentation, comprising:
[0051] initialization:
[0052] Divide a block of memory and establish a memory pool. Divide the memory pool into multiple levels of memory pages according to the preset size. Divide the memory pages of each level from the top level to the bottom level, and mark the information of each memory page. The information of each memory page includes the information of all memory pages of the next level.
[0053] Create a logical space and divide the logical space into minimum memory pages for mapping with the memory in the memory pool;
[0054] Create a page table to record the mapping relationship between memory pages in the logical space and memory pages in the memory pool, and mark the usage of each memory page in the logical space;
[0055] Create a memory recycling table to record the memory information that supports automatic recycling and the total memory capacity in the memory recycling table in the order of memory application;
[0056] Create an address translation module, which is responsible for converting the memory address of the logical space into the memory address of the memory pool through the page table;
[0057] Memory allocation strategy: When allocating, search for free memory pages from the top-level memory page. If the top-level free memory pages are not enough to complete the memory allocation, memory recycling is performed. After the memory recycling is completed, search for free memory pages from the top-level memory page again; after the memory allocation of the memory pool is successful, update the information of the current level memory page and the previous level memory page in the memory pool, map the memory pages in the memory pool to the memory pages in the logical space, and update the page table; when applying for memory, set whether the memory allows automatic recycling through the flag bit; if the successfully allocated memory allows automatic recycling, the memory information is recorded at the end of the memory recycling table;
[0058] Memory access strategy: The address translation module extracts the memory address in the logical space, finds the memory address in the corresponding memory pool through the page table, and performs data access;
[0059] Memory release strategy: If the address of the memory to be released is the first address of the memory allocated to the logical space, the memory page in the corresponding memory pool is released according to the memory length allocated to the logical space, and the information of the relevant memory page is updated, and the page table is updated;
[0060] Memory recovery strategy: If the amount of missing memory is greater than the total memory capacity recorded in the memory recovery table, memory recovery is terminated and the memory allocation fails. If the amount of missing memory is less than or equal to the total memory capacity recorded in the memory recovery table, memory is released starting from the earliest allocated memory in the memory recovery table until the needs are met, and the memory recovery table is updated synchronously.
[0061] According to a specific implementation scheme of the present invention, a multi-level memory page includes a parent memory page and a child memory page, and the child memory page is smaller than the parent memory page; a multi-branch tree is constructed based on the multi-level memory pages, the top-level parent memory page is the root node, and the remaining memory pages are child nodes, and each parent memory page is divided into its subordinate child memory pages level by level, and the information of the corresponding current memory page is recorded at each node.
[0062] According to a specific embodiment of the present invention, the space allocation of each memory page in the memory pool is consistent with the physical design of the memory device.
[0063] According to a specific implementation of the present invention, the space allocated to each memory page is an integer multiple of 2, in units of KB, MB or GB.
[0064] According to a specific implementation of the present invention, the space allocation ratio between the parent memory page and the child memory page is 1:16-1:64, where the denominator is an integer multiple of 2.
[0065] According to a specific implementation of the present invention, the minimum memory page is 2-8 KB, which is an integer multiple of 2.
[0066] According to a specific implementation of the present invention, the space allocation ratio between the parent memory page and the child memory page is 1:32, and the minimum memory page size is 4 KB.
[0067] According to a specific embodiment of the present invention, the space allocation of each memory page in the memory pool is a multiple of 2.
[0068] According to a specific implementation scheme of the present invention, the memory page information includes the current memory page number, memory size, free memory size, and maximum continuous free memory size. The memory information supporting automatic recovery in the memory recovery table includes the first address and memory size of the logical space allocation.
[0069] According to a specific implementation scheme of the present invention, the size of the logical space is at least twice that of the memory pool; when the memory allocation is successful, a free space of equal size is sequentially found from the beginning of the logical space, and the memory pages in the memory pool are mapped to the logical space.
[0070] According to a specific embodiment of the present invention, the memory allocation strategy further includes:
[0071] When allocating, traverse the multi-branch tree nodes starting from the root node, skip the branches with no free memory, fill the free memory of the current node and then traverse the next node;
[0072] When the requested memory is not an integer multiple of the minimum memory page, the integer multiple part and the remainder part are allocated separately. The integer multiple part is allocated using the complete memory page in the memory pool, and the remainder part is allocated using the largest continuous free memory in the allocated memory in the memory pool, or using the new minimum memory page in the memory pool, and allocated from the first node found.
[0073] According to a specific implementation scheme of the present invention, when allocating memory, a search for free memory pages is started from the root node; if the free memory of the root node is insufficient to complete the memory allocation, the memory recovery step is entered, and after the memory recovery is completed, the free memory pages are searched from the root node again; after the memory pool memory is successfully allocated, the current node information is updated and the parent node information is recursively updated, and the free space that meets the size of the allocated memory page is found in sequence in the logical space to map the memory pages in the memory pool to the memory pages in the logical space, and the page table is updated; if the allocated memory allows automatic recovery, the memory information is recorded at the end of the memory recovery table.
[0074] According to a specific implementation of the present invention, the memory address of the logical space includes the logical space memory page number and the offset address within the memory page, and the maximum value of the offset address within the memory page is equal to the size of the minimum memory page minus 1.
[0075] According to a specific implementation scheme of the present invention, the memory access strategy specifically includes: an address conversion module extracts a memory page number of a logical space in a memory address in a logical space, checks whether the memory page of the logical space is allocated through a page table, and if the memory page is allocated, uses the page table to find the memory page number of the corresponding memory pool, and finds the corresponding memory address for data access through the memory page number of the memory pool and the offset address within the memory page; if the memory page is not allocated, returns an illegal access;
[0076] When mapping memory pages, if the previous and next memory pages of the logical space correspond to the memory pages of the memory pool and are also continuous, then the two memory pages in the logical space are marked as continuous; the memory access strategy also includes continuous memory access. When accessing the next logical space memory page, if the memory page of the logical space has a continuous mark, the address conversion module no longer looks up the page table for memory address conversion, but directly uses the address of the current logical space memory page for address offset operations.
[0077] According to a specific implementation scheme of the present invention, when releasing memory, check whether the address of the memory to be released is marked as the first address of the memory allocated by the logical space. If not, return an illegal operation; if so, release all memory pages and mark them as unallocated according to the memory allocated by the logical space and the memory allocation length; update the information of the relevant child nodes on the multi-branch tree, and recursively update the information on the parent node; update the page table information, and remove the corresponding memory allocation information;
[0078] When the memory is released, if the memory supports automatic recycling, the corresponding information in the memory recycling table is removed; after the memory is released, if the surrounding space is also free, the maximum continuous free memory size is updated at the same time.
[0079] The present invention provides an operating system for memory management that reduces fragmentation, including a memory management module. The memory management module uses the above-mentioned memory management method that reduces fragmentation to perform memory management.
[0080] Example 1
[0081] According to a specific implementation scheme of the present invention, the memory management method for alleviating fragmentation of the present invention is described in detail below.
[0082] like Figure 2 As shown, the present invention provides a memory management method for alleviating fragmentation, comprising:
[0083] initialization:
[0084] Divide a block of memory and establish a memory pool. Divide the memory pool into multiple levels of memory pages according to the preset size. Divide the memory pages of each level from the top level to the bottom level, and mark the information of each memory page. The information of each memory page includes the information of all memory pages of the next level.
[0085] Create a logical space and divide the logical space into minimum memory pages for mapping with the memory in the memory pool;
[0086] Create a page table to record the mapping relationship between memory pages in the logical space and memory pages in the memory pool, and mark the usage of each memory page in the logical space;
[0087] Create a memory recycling table to record the memory information that supports automatic recycling and the total memory capacity in the memory recycling table in the order of memory application;
[0088] Create an address translation module, which is responsible for converting the memory address of the logical space into the memory address of the memory pool through the page table;
[0089] Memory allocation strategy: When allocating, search for free memory pages from the top-level memory page. If the top-level free memory pages are not enough to complete the memory allocation, memory recycling is performed. After the memory recycling is completed, search for free memory pages from the top-level memory page again; after the memory allocation of the memory pool is successful, update the information of the current level memory page and the previous level memory page in the memory pool, map the memory pages in the memory pool to the memory pages in the logical space, and update the page table; when applying for memory, set whether the memory allows automatic recycling through the flag bit; if the successfully allocated memory allows automatic recycling, the memory information is recorded at the end of the memory recycling table;
[0090] Memory access strategy: The address translation module extracts the memory address in the logical space, finds the memory address in the corresponding memory pool through the page table, and performs data access;
[0091] Memory release strategy: If the address of the memory to be released is the first address of the memory allocated to the logical space, the memory page in the corresponding memory pool is released according to the memory length allocated to the logical space, and the information of the relevant memory page is updated, and the page table is updated;
[0092] Memory recovery strategy: If the amount of missing memory is greater than the total memory capacity recorded in the memory recovery table, memory recovery is terminated and the memory allocation fails. If the amount of missing memory is less than or equal to the total memory capacity recorded in the memory recovery table, memory is released starting from the earliest allocated memory in the memory recovery table until the needs are met, and the memory recovery table is updated synchronously.
[0093] Example 2
[0094] According to a specific implementation scheme of the present invention, the memory management method for alleviating fragmentation of the present invention is described in detail below.
[0095] The present invention provides a memory management method for alleviating fragmentation, which includes five parts:
[0096] The first part, initialization:
[0097] A block of memory is allocated to build a memory pool. Subsequent memory management is performed in the memory pool, and only the memory in the memory pool is managed;
[0098] The memory pool is divided into memory pages according to the preset size, and the memory pages are further divided into sub-memory pages to build a multi-branch tree of memory pages at all levels. The node is the location where the memory page is distributed in a tree structure, so the memory page is equivalent to the node, and the memory page information is equivalent to the node information.
[0099] The size of the child memory page is smaller than the parent memory page, and the information of each memory page is marked. The information of each memory page includes the information of all memory pages at the next level. The top-level parent memory page is the root node, and the remaining memory pages are child nodes. The subordinate child memory pages are divided from each parent memory page step by step, and the information of the corresponding current memory page is recorded at each node.
[0100] There is only one root node in the tree structure, which is all the memory in the memory pool, which is the first-level memory page; (for example, the size of the first-level memory page can be 4GB); the second-level memory page is divided from the first-level memory page, the first-level memory page is the parent node of the second-level memory page, and the second-level memory page is the child node of the first-level memory page; (if the ratio of 1:32 is used, the size of the second-level memory page is 128MB, and the number of second-level memory pages is 32); the third-level memory page is divided from the second-level memory page, the second-level memory page is the parent node of the third-level memory page, and the third-level memory page is the child node of the second-level memory page; (if the ratio of 1:32 is used, the size of the third-level memory page is 4MB, each second-level memory page has 32 child memory pages, and the total number of third-level memory pages is 1024). Level 4, level 5, level 6... The following memory page levels are analogous, and the child memory is a subdivision of the parent memory page.
[0101] The space allocation of memory pages is consistent with the physical design of the memory device, and in this embodiment, it is a multiple of 2;
[0102] The number of memory page levels and memory page space allocation can be adjusted according to actual scenarios and set based on the high-efficiency memory space requirements. Too much memory page space allocation can reduce memory usage and cause memory waste; too little memory page space allocation can result in too many memory pages or too many memory page levels, increasing the maintenance cost of memory pages.
[0103] In this embodiment, the space allocation ratio of the parent memory page to the child memory page is 1:32, and the minimum memory page is 4KB. This allocation scheme can meet most usage scenarios, reduce the number of memory page levels, and the number of memory pages at each level is not large, which reduces the maintenance cost of memory pages.
[0104] Take the top-level parent memory page as the root node and the remaining memory pages as child nodes to build a multi-branch tree, and record the memory page number, memory size, free memory size, and maximum continuous free memory size of each node;
[0105] Create a logical space for mapping with the memory in the memory pool. The logical space is twice the size of the memory pool. The logical space is divided based on the smallest memory page. The logical space with twice the size can use the first adaptation strategy (that is, find the free space that meets the allocated memory page size in the logical space in sequence) to use up the space of the memory pool. Use the mark bit to record the usage of each memory page in the logical space.
[0106] Create a page table to record the mapping relationship between memory pages in the logical space and memory pool memory pages;
[0107] Create a memory recovery table to record the memory information that supports automatic recovery in the order of memory application (the memory information that supports automatic recovery in the memory recovery table includes the first address of logical space allocation and memory size) and the total memory capacity in the memory recovery table;
[0108] Create an address translation module, which is responsible for converting the memory address of the logical space into the memory address of the memory pool through the page table.
[0109] The second part, memory allocation:
[0110] When applying for memory, set the flag to allow automatic recycling of the memory.
[0111] Free memory page search allocation strategy:
[0112] Starting from the top root node of the multi-branch tree, if the root node does not have enough free memory, memory is directly reclaimed. If the root node has enough memory, the nodes are traversed from top to bottom and from left to right until allocatable memory is found, and the traversal stops. In this process, memory is allocated according to the memory page information on the node, and branches with no free memory are skipped (in this embodiment, such as Figure 1 As shown in the figure, there are 5 levels of memory pages, starting from the top-level root node. Because the information of the root node includes the information of all its child nodes, if the root node has no available memory, then its child nodes must have no available memory. The memory recorded by each parent node includes the sum of the memory of all its child nodes. Correspondingly, if a parent node has no available memory, then its child nodes must have no available memory). Speed up the search of free memory, fill up the free memory of the current node before traversing the next node, give priority to allocating the maximum continuous free memory, reduce the number of memory allocations, and thus improve memory allocation efficiency;
[0113] Free memory pages are searched from the root node through a tree structure. If the root node does not have enough free memory, the current node and all its child nodes are skipped to reclaim memory. When the current node has only a portion of free memory, this portion of free memory is allocated to this node and its child nodes, and the remaining memory requests are searched and allocated in other nodes. The search order is from left to right and from top to bottom on the multi-branch tree.
[0114] This search strategy is to skip branches with no free memory and quickly find free memory.
[0115] When the requested memory is not an integer multiple of the minimum memory page, the integer multiple part and the remainder part are allocated separately. The integer multiple part is allocated using a complete memory page in the memory pool. The remainder part is allocated using the largest continuous free memory in the allocated memory in the memory pool (for example, after the Nth allocation, if there is a memory page with 2 / 3 free, then the next allocation starts from the free 2 / 3 memory in this memory page), or the new minimum memory page in the memory pool is used for allocation, and the allocation is performed from the first node found;
[0116] If the free memory of the root node is insufficient to complete the memory allocation, the memory recycling process is started. After the memory recycling process is completed, memory allocation is performed again. If the free memory is still insufficient to complete the memory allocation after the memory recycling process is completed, the memory allocation fails.
[0117] Memory allocation example:
[0118] (1) Apply for 1MB of memory and first traverse from the root node. If the root node does not have 1MB of free memory, enter the memory recycling phase.
[0119] (2) The root node has enough free memory to traverse the secondary nodes from left to right;
[0120] (3) If the first secondary node has no free memory, the child nodes of the first secondary node will no longer be traversed.
[0121] (4) If the second secondary node has 512KB of free memory, allocate 512KB of memory to the second secondary node, and the remaining 512KB of memory is allocated by other secondary nodes. Traverse the child nodes of the second secondary node from left to right, skip the child nodes with no free memory, and traverse recursively until 512KB of free memory is allocated;
[0122] (5) Continue to traverse and query the secondary nodes, allocate the remaining 512KB, and the search steps are the same as step 4;
[0123] After the memory pool is successfully allocated, the memory page information of the current node is updated and the memory page information of the parent node is recursively updated. A free space of equal size is found in sequence from the beginning of the logical space (first adaptation strategy), and the memory pages in the memory pool are mapped to the logical space. The purpose is to map discontinuous memory pages in the memory pool into continuous memory pages in the logical space, so as to achieve continuous memory access through the logical space. When mapping memory, if the memory pages before and after the logical space correspond to the memory pages of the memory pool, the two memory pages are marked as continuous.
[0124] After the memory pool is successfully allocated, the mark of the memory corresponding to the logical space is updated as allocated, the mapping relationship between the logical space memory page and the memory pool memory page in the page table is updated, the first address and memory allocation length of the logical space allocated memory are marked, and the first address of the logical space allocated memory is returned;
[0125] If the allocated memory allows automatic recycling, the memory information that supports automatic recycling will be recorded at the end of the memory recycling table;
[0126] The memory address of the logical space includes the logical space memory page number and the offset address within the memory page. The maximum value of the offset address within the memory page is equal to the size of the minimum memory page minus 1, which avoids the need for additional calculations for memory addressing.
[0127] Part 3, memory access:
[0128] The program accesses data through the memory address of the logical space. The address conversion module extracts the logical space memory page number from the memory address of the logical space, and uses the page table to check whether the memory page is allocated: if it is not allocated, an illegal access is returned; if the memory is allocated, the page table is used to find the memory page number of the memory pool, and the corresponding memory address is found through the memory page number of the memory pool and the offset address within the memory page to access the data.
[0129] Continuous memory access: When accessing the next logical space memory page, if the next memory page of the logical space has a continuous mark, the address translation module no longer looks up the page table for memory address translation, but directly uses the address of the current logical space memory page for address offset operation to access data, eliminating the step of repeated address translation and speeding up memory access.
[0130] Part 4, memory release:
[0131] When releasing memory, check whether the address of the released memory is marked as the first address of the memory allocated by the logical space. If not, return an illegal operation; if so, according to the memory allocation length allocated by the logical space, release all memory pages corresponding to the memory allocation length and mark them as unallocated;
[0132] After the memory is released, the memory page information of the corresponding node on the multi-branch tree is updated, and the memory page information on the parent node is recursively updated;
[0133] After the memory is released, the page table information is updated, the corresponding memory allocation information is removed, and the mapping relationship between the memory page of the memory pool and the logical space is deleted;
[0134] After the memory is released, if the memory supports automatic recycling, the corresponding information in the memory recycling table is removed;
[0135] After the memory is released, if the surrounding space is also free, the size of the maximum continuous free memory is updated at the same time;
[0136] Part 5, memory recovery:
[0137] Step 1, when the free memory is insufficient to complete the memory allocation, the amount of missing memory is compared with the total memory capacity recorded in the memory recovery table; if the amount of missing memory is greater than the total memory capacity recorded in the memory recovery table, the memory recovery process is terminated; if the amount of missing memory is less than or equal to the total memory capacity recorded in the memory recovery table, the next step is entered;
[0138] Step 2: Release memory starting from the earliest allocated memory in the memory recovery table, and update the memory recovery table synchronously;
[0139] Step 3, check whether the released memory is greater than or equal to the amount of missing memory. If the released memory is less than the amount of missing memory, repeat steps 2 and 3 until the total amount of released memory is greater than or equal to the amount of missing memory, and then proceed to the next step.
[0140] End the memory recycling process.
[0141] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention are included in the protection scope of the present invention.
Claims
1. A memory management method for reducing fragmentation, characterized in that: include: initialization: Divide a block of memory and establish a memory pool. Divide the memory pool into multiple levels of memory pages according to the preset size. Divide the memory pages of each level from the top level to the bottom level, and mark the information of each memory page. The information of each memory page includes the information of all memory pages of the next level. Create a logical space and divide the logical space into minimum memory pages for mapping with the memory in the memory pool; Create a page table to record the mapping relationship between memory pages in the logical space and memory pages in the memory pool, and mark the usage of each memory page in the logical space; Create a memory recycling table to record the memory information that supports automatic recycling and the total memory capacity in the memory recycling table in the order of memory application; Create an address translation module, which is responsible for converting the memory address of the logical space into the memory address of the memory pool through the page table; Memory allocation strategy: When allocating, search for free memory pages starting from the top-level memory page. If the top-level free memory pages are not enough to complete the memory allocation, memory recycling is performed. After memory recycling is completed, search for free memory pages starting from the top-level memory page again. After the memory allocation of the memory pool is successful, the information of the memory page of the current level and the memory page of the previous level in the memory pool is updated, the memory page in the memory pool is mapped to the memory page in the logical space, and the page table is updated; when applying for memory, the flag bit is used to set whether the memory allows automatic recycling; if the successfully allocated memory allows automatic recycling, the memory information is recorded at the end of the memory recycling table; Memory access strategy: The address translation module extracts the memory address in the logical space, finds the memory address in the corresponding memory pool through the page table, and performs data access; Memory release strategy: If the address of the memory to be released is the first address of the memory allocated to the logical space, the memory page in the corresponding memory pool is released according to the memory length allocated to the logical space, and the information of the relevant memory page is updated, and the page table is updated; Memory recovery strategy: If the amount of missing memory is greater than the total memory capacity recorded in the memory recovery table, memory recovery is terminated and the memory allocation fails. If the amount of missing memory is less than or equal to the total memory capacity recorded in the memory recovery table, memory is released starting from the earliest allocated memory in the memory recovery table until the demand is met, and the memory recovery table is updated synchronously.
2. The memory management method for reducing fragmentation according to claim 1, characterized in that: The multi-level memory page includes a parent memory page and a child memory page, wherein the child memory page is smaller than the parent memory page; a multi-branch tree is constructed based on the multi-level memory page, wherein the top-level parent memory page is the root node and the other memory pages are child nodes, and each parent memory page is divided into its subordinate child memory pages step by step, and the information of the corresponding current memory page is recorded at each node. The space allocation of each memory page in the memory pool is consistent with the physical design of the memory device, and the space allocated to each memory page is an integer multiple of 2, in KB, MB or GB. The space allocation ratio of the parent memory page to the child memory page is 1:16-1:64, wherein the denominator is an integer multiple of 2, and the smallest memory page is 2-8KB, which is an integer multiple of 2.
3. The memory management method for reducing fragmentation according to claim 2, characterized in that: The information of the memory page includes the number of the current memory page, the memory size, the free memory size, and the maximum continuous free memory size. The memory information that supports automatic recycling in the memory recycling table includes the first address of the logical space allocation and the memory size.
4. The memory management method for reducing fragmentation according to claim 3, characterized in that: The size of the logical space must be at least twice that of the memory pool. When memory allocation is successful, a free space of equal size is found in sequence from the beginning of the logical space, and the memory pages in the memory pool are mapped to the logical space.
5. The memory management method for reducing fragmentation according to any one of claims 2 to 4, characterized in that: Memory allocation strategies also include: When allocating, traverse the multi-branch tree nodes starting from the root node, skip the branches with no free memory, fill the free memory of the current node and then traverse the next node; When the requested memory is not an integer multiple of the minimum memory page, the integer multiple part and the remainder part are allocated separately. The integer multiple part is allocated using the complete memory page in the memory pool, and the remainder part is allocated using the largest continuous free memory in the allocated memory in the memory pool, or using the new minimum memory page in the memory pool, and allocated from the first node found.
6. The memory management method for reducing fragmentation according to claim 5, characterized in that: When allocating memory, search for free memory pages from the root node. If the free memory of the root node is insufficient to complete the memory allocation, the memory recycling step is entered. After the memory recycling is completed, search for free memory pages from the root node again. After the memory pool is successfully allocated, the current node information is updated and the parent node information is recursively updated. The free space that meets the allocated memory page size is found in the logical space in sequence to map the memory pages in the memory pool to the memory pages in the logical space, and the page table is updated. If the allocated memory allows automatic recovery, the memory information is recorded at the end of the memory recovery table.
7. The memory management method for reducing fragmentation according to any one of claims 2 to 4, characterized in that: The memory address of the logical space includes the logical space memory page number and the offset address within the memory page. The maximum value of the offset address within the memory page is equal to the size of the minimum memory page minus 1.
8. The memory management method for reducing fragmentation according to claim 7, characterized in that: The memory access strategy specifically includes: the address conversion module extracts the memory page number of the logical space in the memory address in the logical space, checks whether the memory page of the logical space is allocated through the page table, and if the memory page is allocated, uses the page table to find the memory page number of the corresponding memory pool, and finds the corresponding memory address for data access through the memory page number of the memory pool and the offset address within the memory page; if the memory page is not allocated, returns an illegal access; When mapping memory pages, if the previous and next memory pages of the logical space correspond to the memory pages of the memory pool and are also continuous, then the two memory pages in the logical space are marked as continuous; the memory access strategy also includes continuous memory access. When accessing the next logical space memory page, if the memory page of the logical space has a continuous mark, the address conversion module no longer looks up the page table for memory address conversion, but directly uses the address of the current logical space memory page for address offset operations.
9. The memory management method for reducing fragmentation according to any one of claims 3 to 4, characterized in that: When releasing memory, check whether the address of the memory to be released is marked as the first address of the memory allocated in the logical space. If not, return an illegal operation; If yes, allocate memory according to the logical space and memory allocation length, release all memory pages and mark them as unallocated; Update the information of related child nodes on the multi-branch tree, and recursively update the information on the parent node; Update the page table information and remove the corresponding memory allocation information; When the memory is released, if the memory supports automatic recycling, the corresponding information in the memory recycling table is removed; After the memory is released, if the surrounding space is also free, the maximum continuous free memory size is updated at the same time.
10. An operating system for memory management that reduces fragmentation, characterized in that: It includes a memory management module, and the memory management module uses the memory management method for reducing fragmentation according to any one of claims 1 to 9 to perform memory management.
Citation Information
Patent Citations
Method for managing dynamic internal memory base on discontinuous page
CN101231619A
Memory management method and device, heterogeneous system, storage medium and program product
CN118820126A