Method and device for managing off-heap memory
By adopting memory pooling management methods in the off-heap memory of virtual machine (jvm), the problems of low efficiency and fragmentation of memory allocation use are solved, and more efficient memory management and use are achieved.
Patent Information
- Application Number
- CN202110185188.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-02-10
- Publication Date
- 2025-04-15
- Estimated Expiration
- 2041-02-10
AI Technical Summary
The use of off-heap memory of virtual machines (jvm) lacks an effective memory management unit, resulting in low allocation and memory fragmentation problems.
Provide a method and device for managing off-heap memory. Through memory pooling management, it obtains the memory size requested to be allocated, and judges the available memory pages or blocks based on the memory size, and allocates and recycles memory segments to avoid memory fragmentation.
It effectively avoids memory fragmentation, reduces memory application and release operations, improves memory allocation and recycling efficiency, and greatly optimizes memory usage efficiency in network transmission scenarios.
Smart Images

Figure CN113742058B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a method and device for managing off-heap memory. Background Art
[0002] When the virtual machine (JVM) platform creates an object or allocates memory, the default operation is performed on the memory segment managed by the virtual machine (JVM). The memory does not need to be released manually by the user. The virtual machine (JVM) will automatically determine when the memory should be reclaimed based on the references to the memory. When the virtual machine platform performs garbage collection, it will copy the memory still in use to the new memory space managed by the virtual machine (JVM). Since garbage collection is a very frequent operation, active memory will be continuously copied back and forth during the garbage collection (GC) process, resulting in performance loss. At the same time, the performance loss caused by continuous memory application and recovery cannot be ignored. Therefore, the virtual machine (JVM) platform also provides an interface to allocate memory intervals that are not managed by the virtual machine (JVM), and users can manage memory application and recovery by themselves.
[0003] In the process of implementing the present invention, the inventors found that there are at least the following problems in the prior art:
[0004] Although the use of virtual machine (JVM) off-heap memory can effectively avoid the performance loss caused by garbage collection, the lack of a convenient and effective memory management unit to manage and track memory usage can easily lead to problems such as low allocation efficiency and memory fragmentation. Summary of the invention
[0005] In view of this, an embodiment of the present invention provides a method and device for managing off-heap memory, which can manage memory segments requested by users that are not managed by a virtual machine (JVM), and manage memory in a pooled manner, effectively avoiding memory fragmentation, while reducing memory application and release operations. Since there is no garbage collection (GC) process and no back-and-forth copying, especially in network transmission scenarios, the memory usage efficiency can be greatly optimized and the transmission efficiency can be improved.
[0006] To achieve the above objective, according to one aspect of an embodiment of the present invention, a method for managing off-heap memory is provided.
[0007] A method for managing off-heap memory, wherein the off-heap memory includes at least one memory block, each memory block includes at least one memory page, each memory page includes at least one memory segment, and the method includes: obtaining the memory size requested for allocation from a memory allocation request; judging whether there are available memory pages in the allocated memory pages according to the memory size; if so, obtaining an available memory page; otherwise, obtaining an available memory block and obtaining an available memory page from the memory block; and allocating a memory segment for the memory allocation request from the memory page.
[0008] Optionally, after obtaining the memory size requested for allocation from the memory allocation request, the method further includes: regularizing the memory size into a predefined memory specification;
[0009] And, judging whether there are available memory pages in the allocated memory pages according to the memory size includes: obtaining memory pages corresponding to the memory specification from the allocated memory pages according to the memory specification corresponding to the memory size; judging whether there are unallocated memory pages in the memory pages corresponding to the memory specification according to the memory segment list corresponding to the memory page, wherein the memory segment list records the allocation status of the memory segments included in the memory page.
[0010] Optionally, the division of memory blocks is recorded through a full binary tree structure, the memory block corresponds to the root node of the full binary tree, each memory page corresponds to a leaf node of the full binary tree, the full binary tree has a height value, each node corresponds to a level, and the level value increases step by step from the root node to the leaf node; obtaining an available memory block and obtaining an available memory page from the memory block includes: judging whether the memory block is available according to the level value corresponding to the root node, and selecting an available memory block from the available memory blocks; for the full binary tree corresponding to the memory block, recursively traversing downward step by step starting from the root node, judging the level values of the left child node and the right child node of the current node in turn from left to right until an available leaf node is obtained, and generating an available memory page according to the obtained leaf node.
[0011] Optionally, after obtaining an available memory page from the memory block, it also includes: increasing the level value of the leaf node corresponding to the memory page by 1, and updating the level value of the parent node of the current node step by step starting from the leaf node, wherein the level value of the parent node is updated to the minimum value of the level values of the left child node and the right child node of the parent node.
[0012] Optionally, the method also includes: if all memory segments in the memory page are released, the level value of the leaf node corresponding to the memory page is reduced by 1, and the level value of the parent node of the current node is updated step by step starting from the leaf node, wherein the level value of the parent node is updated to the minimum value of the level values of the left child node and the right child node of the parent node.
[0013] Optionally, after generating an available memory page according to the obtained leaf node, it also includes: setting the memory segment length of the memory page according to the memory specification corresponding to the memory size, and creating a memory segment list for recording the allocation of memory segments in the memory page.
[0014] Optionally, obtaining an available memory block includes: determining whether there is unused block memory in the allocated memory blocks; if so, obtaining an available memory block; otherwise, creating a memory block and generating a full binary tree to record the division of the memory block.
[0015] According to another aspect of an embodiment of the present invention, a device for managing off-heap memory is provided.
[0016] A device for managing off-heap memory, wherein the off-heap memory includes at least one memory block, each memory block includes at least one memory page, each memory page includes at least one memory segment, and the device includes: a memory request acquisition module, used to acquire the memory size requested for allocation from a memory allocation request; a memory page judgment module, used to judge whether there are available memory pages in the allocated memory pages according to the memory size; a memory page acquisition module, used to acquire an available memory page if so; otherwise, acquire an available memory block and acquire an available memory page from the memory block; a memory request allocation module, used to allocate a memory segment for the memory allocation request from the memory page.
[0017] Optionally, the device also includes a memory regularization module, which is used to: regularize the memory size into a predefined memory specification; the memory page judgment module is also used to: obtain the memory page corresponding to the memory specification from the allocated memory pages according to the memory specification corresponding to the memory size; and judge whether there are any unallocated memory pages in the memory page corresponding to the memory specification according to the memory segment list corresponding to the memory page, and the memory segment list records the allocation status of the memory segments included in the memory page.
[0018] Optionally, the division of memory blocks is recorded through a full binary tree structure, the memory block corresponds to the root node of the full binary tree, each memory page corresponds to a leaf node of the full binary tree, the full binary tree has a height value, each node corresponds to a level, and the level value increases step by step from the root node to the leaf node; the memory page acquisition module is also used to: judge whether the memory block is available according to the level value corresponding to the root node, and select an available memory block from the available memory blocks; for the full binary tree corresponding to the memory block, recursively traverse downward step by step starting from the root node, and judge the level values of the left child node and the right child node of the current node in turn from left to right until an available leaf node is obtained, and generate an available memory page according to the obtained leaf node.
[0019] Optionally, the device also includes a node update module, which is used to increase the level value of the leaf node corresponding to the memory page by 1, and update the level value of the parent node of the current node step by step starting from the leaf node, wherein the level value of the parent node is updated to the minimum value of the level values of the left child node and the right child node of the parent node.
[0020] Optionally, the device also includes a memory release module, which is used to reduce the level value of the leaf node corresponding to the memory page by 1 if all memory segments in the memory page are released, and update the level value of the parent node of the current node step by step starting from the leaf node, wherein the level value of the parent node is updated to the minimum value of the level values of the left child node and the right child node of the parent node.
[0021] Optionally, the device also includes a memory segment setting module, which is used to: set the memory segment length of the memory page according to the memory specification corresponding to the memory size, and create a memory segment list for recording the allocation of memory segments in the memory page.
[0022] Optionally, the memory page acquisition module is also used to: determine whether there is unused block memory in the allocated memory block; if so, obtain an available memory block; otherwise, create a memory block and generate a full binary tree to record the division of the memory block.
[0023] According to yet another aspect of an embodiment of the present invention, an electronic device for managing off-heap memory is provided.
[0024] An electronic device for managing off-heap memory includes: one or more processors; a storage device for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors implement the method for managing off-heap memory provided by an embodiment of the present invention.
[0025] According to still another aspect of the embodiments of the present invention, a computer readable medium is provided.
[0026] A computer-readable medium stores a computer program, which, when executed by a processor, implements a method for managing off-heap memory provided by an embodiment of the present invention.
[0027] An embodiment of the above invention has the following advantages or beneficial effects: obtaining the memory size requested for allocation from a memory allocation request; judging whether there are available memory pages in the allocated memory pages according to the memory size; if so, obtaining an available memory page; otherwise, obtaining an available memory block and obtaining an available memory page from the memory block; allocating a memory segment for the memory allocation request from the memory page, using the memory management structure of the present invention to manage and control the memory segments requested by users that are not managed by the virtual machine (JVM), managing the memory in a pooled manner, effectively avoiding memory fragmentation, and reducing memory application and release operations, greatly improving memory allocation and recovery efficiency, and achieving a significant optimization of memory usage efficiency in network transmission scenarios and improving transmission efficiency.
[0028] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with the specific implementation manner. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.
[0030] Figure 1 This is a schematic diagram of the off-heap memory allocation relationship in an embodiment of the present invention;
[0031] Figure 2 is a schematic diagram of main steps of a method for managing off-heap memory according to an embodiment of the present invention;
[0032] Figure 3 It is a data structure diagram of a memory segment list according to an embodiment of the present invention;
[0033] Figure 4 is a schematic diagram of the structure of a full binary tree according to an embodiment of the present invention;
[0034] Figure 5 It is a schematic diagram of the updating process of node level value after memory allocation according to an embodiment of the present invention;
[0035] Figure 6 is a schematic diagram of a process of updating a node level value after memory allocation according to another embodiment of the present invention;
[0036] Figure 7 It is a schematic diagram of the updating process of the node level value after memory allocation according to another embodiment of the present invention;
[0037] Figure 8is a schematic diagram of a memory application process according to an embodiment of the present invention;
[0038] Fig. 9 is a schematic diagram of a flow chart of allocating memory pages from a memory block according to an embodiment of the present invention;
[0039] Fig.10 is a schematic diagram of main modules of a device for managing off-heap memory according to an embodiment of the present invention;
[0040] Fig.11 is an exemplary system architecture diagram to which embodiments of the present invention may be applied;
[0041] Fig.12 It is a schematic diagram of the structure of a computer system of a terminal device or a server suitable for implementing an embodiment of the present invention. DETAILED DESCRIPTION
[0042] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.
[0043] Figure 1 Schematic diagram of off-heap memory allocation relationship in an embodiment of the present invention. Figure 1 As shown, in one embodiment of the present invention, the memory relationship diagram mainly includes the relationship and meaning between the memory manager MemAllocator, the memory pool MemArena, the memory block MemChunk, and the memory page MemPage. First, the structure MemAllocator is defined to allocate and cache memory, the structure MemPage is defined to represent a page of allocated memory, and the default size is 8KB. The structure MemChunk is defined to represent 16MB of memory, which is divided according to the memory size of 8KB per page, with a total of 2048 nodes. The structure MemArena is defined as a memory area, which defaults to 64MB of memory and is used to cache the allocated memory. The memory page list (MemPageList) represents a list of allocated memory pages (MemPage), and the list length defaults to 2048. When the memory segment represented by MemPage is used, it will be added to the memory page list MemPageList. When the length of MemPageList exceeds 2048, the excess memory will be released and no longer cached. When there is a new memory allocation request, it is first determined from MemPageList whether there is allocated but unused memory. If so, it is directly allocated from it.
[0044] The memory block list MemChunkList represents the list of allocated memory blocks MemChunk, which is used to cache MemChunk. If the default length of MemPageList is 2048, the size of MemChunkList is 1. If the default length of MemPageList is 4096, the size of MemChunkList is 2. When all memory pages MemPage in the memory block MemChunk are no longer in use, the memory block MemChunk will be released.
[0045] In the embodiment of the present invention, in order to facilitate calculation and memory alignment operations, according to the above definition, the size of a memory page (MemPage) is 8KB, and the following 9 sizes of memory specification types are predefined:
[0046] The minimum value of the memory specifications of 16B, 32B, 64B, 128B, 256B, 512B, 1024B, 2048B, and 4096B is 16B, and the maximum value is 4096B, i.e. 4KB, which are the sizes of 2 to the power of n respectively.
[0047] Figure 2 FIG. 4 is a schematic diagram of the main steps of a method for managing off-heap memory according to an embodiment of the present invention. Figure 2 As shown, the method for managing off-heap memory according to an embodiment of the present invention mainly includes the following steps S201 to S204.
[0048] Step S201, obtaining the memory size requested for allocation from the memory allocation request;
[0049] Step S202, judging whether there are available memory pages in the allocated memory pages according to the memory size;
[0050] Step S203, if yes, obtain an available memory page; otherwise, obtain an available memory block and obtain an available memory page from the memory block;
[0051] Step S204: allocate a memory segment from the memory page for the memory allocation request.
[0052] According to one embodiment of the present invention, after obtaining the requested memory size from the memory allocation request, it also includes: regularizing the memory size into a predefined memory specification. In a remote call network transmission scenario, the size of the memory application may be an irregular memory block such as 9B, 700B, 1.5KB, 5KB, etc. Assuming that 10B of memory is to be allocated during network transmission, a page of 8KB of memory is requested from the operating system. According to the memory type described above, 10B will fall within the memory interval of (0B, 16B], and will be regularized into a memory specification type of 16B.
[0053] According to another embodiment of the present invention, judging whether there are available memory pages in the allocated memory pages according to the memory size includes: obtaining the memory pages corresponding to the memory specifications from the allocated memory pages according to the memory specifications corresponding to the memory size; judging whether there are unallocated memory pages in the memory pages corresponding to the memory specifications according to the memory segment list corresponding to the memory pages, the memory segment list recording the allocation status of the memory segments included in the memory pages.
[0054] In an embodiment of the present invention, the division of memory blocks is recorded by a full binary tree structure, the memory block corresponds to the root node of the full binary tree, each memory page corresponds to a leaf node of the full binary tree, the full binary tree has a height value, each node corresponds to a level, and the level value from the root node to the leaf node increases step by step. According to an embodiment of the present invention, for a new memory block, a new full binary tree will be generated when it is divided into memory pages. For example, 16MB is used as a large block of memory MemChunk, and the size of each page of memory is 8KB, so there are 2048 units in total, which are described in the form of a full binary tree to form a full binary tree with a height of 12 and 2048 leaf nodes. The types of full binary tree nodes include leaf nodes and non-leaf nodes, and each leaf node represents a page of memory (8KB). The nodes of the binary tree have a level value, and the level value is 1 from the root node to the leaf node step by step. In an embodiment of the present invention, the level value of the root node of the newly generated full binary tree is 0, so the level value of the leaf node is 11 (the height value of the tree -1).
[0055] According to an embodiment of the present invention, obtaining an available memory block and obtaining an available memory page from the memory block may include, for example: judging whether the memory block is available according to the level value corresponding to the root node, and selecting an available memory block from the available memory blocks; recursively traversing the full binary tree corresponding to the memory block from the root node downward step by step, judging the level values of the left child node and the right child node of the current node in order from left to right until an available leaf node is obtained, and generating an available memory page according to the obtained leaf node.
[0056] According to an embodiment of the present invention, after obtaining an available memory page from a memory block, it also includes: increasing the level value of the leaf node corresponding to the memory page by 1, and updating the level value of the parent node of the current node step by step from the leaf node upward, wherein the level value of the parent node is updated to the minimum value of the level values of the left child node and the right child node of the parent node. In combination with the above embodiment, the height of the full binary tree is 12, and when applying for memory, it is determined whether there are available leaf points, and the application is made from left to right. If the current leaf node is available, the level information of the current leaf node is set to 12, and then the level information of the parent node is recursively updated step by step upward. The level information of the parent node is the smaller value of the level values of the left child node and the right child node. If all leaf nodes under the parent node have been allocated, the level information in the parent node is the same as the level information of all child nodes. It can be seen that when all memory pages in a memory block are used, the level values of all leaf nodes are 12. Correspondingly, the level values of other nodes including the root node will also be updated to 12. Then, by judging whether the level value of the root node is 12, we can know whether the leaf nodes have been used up. If the level value of the root node is not 12, it means that the leaf nodes have not been used up. Then we can recursively traverse downward step by step. According to the method of first left node and then right node, by judging the level value of each node, we can know whether there are available leaf nodes under the subtree corresponding to the node. By judging the level values of the left and right subtrees, we decide to traverse the left and right subtrees until an available leaf node is found. In the process of searching for leaf nodes, since the division of the memory block is a complete binary tree structure, the recursive judgment has the search efficiency of binary search, which is simple and efficient.
[0057] According to another embodiment of the present invention, it also includes: if all memory segments in the memory page are released, the level value of the leaf node corresponding to the memory page is reduced by 1, and the level value of the parent node of the current node is updated step by step from the leaf node, wherein the level value of the parent node is updated to the minimum value of the level values of the left child node and the right child node of the parent node. In combination with the above embodiment, the height of the full binary tree is 12. Assuming that the memory of a certain memory segment (for example, the size is 64B) is released, the corresponding counter in the corresponding MemPage will be reduced by one. When the counter is reduced to 0, it means that the leaf node is released. If the leaf node is released, the level value of the leaf node is updated to 11, and then recursively traverse upward step by step, update the level value of the parent node to the minimum value of the level value of the left child node and the right child node, and traverse all MemChunks in the MemPageList that have the same number as the current MemPage. If the number of MemPages is 2048 and none of them are in use, it means that all leaf nodes under the entire MemChunk tree are no longer in use, then the 16MB of memory represented by the MemChunk can be released.
[0058] In an embodiment of the present invention, after generating an available memory page according to the obtained leaf node, it also includes: setting the memory segment length of the memory page according to the memory specification corresponding to the memory size, and creating a memory segment list for recording the allocation of memory segments in the memory page, Figure 3 is a data structure diagram of a memory segment list according to an embodiment of the present invention, such as Figure 3 As shown, this data structure is used to record the usage of a page of memory. When the page memory size is 8KB, 10B of memory is allocated and rounded to 16B. The result of 8KB / 16B is 512, so the 8KB page memory will be divided into 512 parts. Now only one of them is used, so a data structure is needed to record the usage of the page memory. Because the smallest memory segment is 16B and the page memory size is 8KB, there are at most 512 parts. A bitmap is used to mark the allocation and usage of memory. In the virtual machine (jvm), the long integer (Long) is 64 bits, so an array of type Long and length 8 (512 / 64) is needed to describe the usage of a page of memory. Each bit represents 16B of memory. When the memory represented by this position is used, the position is set to 1.
[0059] In an embodiment of the present invention, obtaining an available memory block includes: determining whether there is unused block memory in the allocated memory block; if so, obtaining an available memory block; otherwise, creating a memory block and generating a full binary tree to record the division of the memory block. When the size of MemChunk is 16MB, it is divided into 2048 units according to the size of MemPage of 8KB to form a binary tree, and there are 2048 leaf nodes.
[0060] To facilitate the description of the allocation and recycling process, the original 16MB memory block structure is reduced to 128KB in size, and the memory page is 8KB in size as an example for specific introduction.
[0061] Figure 4 This is a schematic diagram of the structure of a full binary tree of an embodiment of the present invention. Assuming that there is a 128KB memory block and a memory page of 8KB, a full binary tree structure is used to organize and manage each page of memory. Then there are 16 leaf nodes (128KB / 8KB), and a full binary tree with a height of 5 needs to be constructed, wherein the level value of the root node is 0 and the level value of the leaf node is 4. When a page of memory is to be allocated, it is determined whether the level value of the root node is less than 5. If so, it indicates that there are unallocated leaf nodes, and then the level values of the left and right subtrees are determined to perform recursive traversal to find available leaf nodes; otherwise, it indicates that all leaf nodes of the current binary tree have been allocated.
[0062] Figure 51 is a schematic diagram of the updating process of the node level value after memory allocation according to an embodiment of the present invention. According to the full binary tree structure shown above, when the 16th node (node id=16) with a level value of 4 is allocated, the process of updating the level value of each node includes: updating the current level value of the 16th node to 5, updating the level value of its parent node (i.e., the parent node of the 16th node) 8 to the smaller level value 4 of the level value 5 of the 16th node and the level value 4 of the 17th node, gradually updating the level value of node 4 to the smaller level value 3 of the level value 4 of the 8th node and the level value 3 of the 9th node, updating the level value of node 2 to the smaller level value 3 of the level value 4 of the 4th node and the level value 2 of the 5th node, and updating the level value of node 1 to the smaller level value 1 of the level value 2 of the 2nd node and the level value 1 of the 3rd node, such as Figure 5 As shown, after the requested memory is allocated to the 16th node with a level value of 4, the level value of the full binary tree structure is updated.
[0063] Figure 6 FIG. 1 is a schematic diagram of a process of updating a node level value after memory allocation according to another embodiment of the present invention. Figure 6 As shown, when the requested memory continues to be allocated to the 18th node with a level value of 4, since the present invention searches for leaf nodes from left to right, the 17th node must have been allocated at this time. Then, the updating process of the level values of each node includes: updating the level value of node 18 to 5, updating the level value of its parent node 9 to the smaller level value 4 between the level value 5 of the 18th node and the level value 4 of the 19th node, and updating the level value of node 4 upward to the smaller level value 4 between the level value 5 of the 8th node and the level value 4 of the 9th node, and no update of the level values of other nodes.
[0064] Figure 7 FIG. 1 is a schematic diagram of a process of updating a node level value after memory allocation according to another embodiment of the present invention. After the left subtree of the root node is allocated, when the 24th node is allocated, as shown in FIG. Figure 7 As shown, all the level values of the left subtree of the root node are 5. When searching for allocation, the right subtree is directly searched. By judging the level value of the current node, it can be determined whether there is an unallocated leaf node under the current node. At this time, the level value update process of each node includes: updating the level value of node 24 to 4, updating the level value of its parent node 12 to 4, updating the level value of node 6 to 3, updating the level value of node 3 to 3, and updating the level value of root node 1 to 2.
[0065] Figure 8 Schematic diagram of the memory application process of an embodiment of the present invention. Figure 8 As shown, the memory application process of the embodiment of the present invention mainly includes:
[0066] (1) Obtain the requested memory size from the memory allocation request, and adjust the memory size to the predefined memory specification. For example, if a 60-byte memory request is received, adjust the requested memory size to 64 bytes, create MemArena, and initialize it.
[0067] (2) Determine whether there are available memory pages in the allocated page memory, wherein the available memory page refers to: a memory page whose memory segment size is consistent with the memory specification corresponding to the applied memory size and has not been fully allocated. According to the memory specification corresponding to the memory size, obtain the memory page corresponding to the memory specification from the allocated memory page; according to the memory segment list corresponding to the memory page, determine whether there are unallocated memory pages in the memory page corresponding to the memory specification. The memory segment list records the allocation of memory segments included in the memory page. In the embodiment of the present invention, MemPageList is used to determine whether there is a page memory with Pagesize of 64B, and a Long type array that records the usage of a page of memory is used to determine whether the page memory has been allocated;
[0068] (3) If available, obtain an available memory page, and allocate a memory segment for the memory allocation request according to the allocation of memory segments in the memory page recorded in the memory segment list, and use a bitmap array to record the usage of the memory segment;
[0069] (4) Otherwise, determine whether there are any unused memory blocks in the allocated memory blocks; if so, obtain an available memory block, determine whether the memory block is available according to the level value corresponding to the root node, and select an available memory block from the available memory blocks; otherwise, create a memory block, generate a full binary tree to record the division of the memory block, and add the created memory block to the memory block list;
[0070] (5) Get an available memory page from the memory block, and allocate a segment of memory for the memory allocation request according to the allocation of memory segments in the memory page recorded in the memory segment list, and use a bitmap array to record the usage of the memory segments.
[0071] Fig. 9 is a schematic diagram of a flow chart of allocating memory pages from a memory block in an embodiment of the present invention, such as Fig. 9 As shown, the process of allocating memory pages from a memory block mainly includes:
[0072] (1) According to the memory page application, it is determined whether there are available leaf nodes in the memory block, and whether there are available leaf nodes according to the level value corresponding to the root node of the full binary tree structure. In this embodiment, assuming that the height value of the tree is 12, by determining whether the level value of the root node is 12, it can be known whether the leaf nodes of the full binary tree corresponding to the memory block have been used up;
[0073] (2) According to the full binary tree structure, recursively traverse downward from the root node level by level, and judge the level values of the left child node and the right child node of the current node in order from left to right until an available leaf node is obtained. Specifically, the following steps may be included:
[0074] Step 1, get the left child node of the current node;
[0075] Step 2, determine whether the left child node is available; if so, execute step 3, otherwise execute step 5;
[0076] Step 3, determine whether the left child node is a leaf node; if so, execute step 7, otherwise, execute step 4;
[0077] Step 4, update the left child node to the current node and execute step 1;
[0078] Step 5, get the right child node of the current node, and determine whether the right child node is a leaf node; if so, execute step 7, otherwise execute step 6;
[0079] Step 6, update the right child node to the current node, and execute step 1;
[0080] Step 7, obtain available leaf nodes;
[0081] (3) Increase the level value of the leaf node corresponding to the memory page by 1, and update the level value of the parent node of the current node step by step from the leaf node upward, wherein the level value of the parent node is updated to the minimum value of the level values of the left child node and the right child node of the parent node;
[0082] (4) Generate an available memory page based on the obtained leaf node and return it, set the memory segment length of the memory page, and create a memory segment list to record the allocation of memory segments in the memory page. Specifically, a bitmap array can be used to record the usage of memory segments;
[0083] (5) Add the returned memory page to the memory page list.
[0084] Fig.10 is a schematic diagram of main modules of a device for managing off-heap memory according to an embodiment of the present invention, such as Fig.10 As shown, the device 1000 for managing off-heap memory according to an embodiment of the present invention mainly includes a memory request acquisition module 1001 , a memory page judgment module 1002 , a memory page acquisition module 1003 , and a memory request allocation module 1004 .
[0085] A memory request acquisition module 1001 is used to acquire the memory size requested for allocation from a memory allocation request;
[0086] The memory page determination module 1002 is used to determine whether there are available memory pages in the allocated memory pages according to the memory size;
[0087] The memory page acquisition module 1003 is used to acquire an available memory page if available; otherwise, acquire an available memory block and acquire an available memory page from the memory block;
[0088] The memory request allocation module 1004 is used to allocate a memory segment for the memory allocation request from the memory page.
[0089] In an embodiment of the present invention, the device 1000 also includes a memory regularization module (not shown in the figure), which is used to: regularize the memory size into a predefined memory specification; the memory page judgment module 1002 is also used to: obtain the memory page corresponding to the memory specification from the allocated memory pages according to the memory specification corresponding to the memory size; according to the memory segment list corresponding to the memory page, determine whether there are unallocated memory pages in the memory page corresponding to the memory specification, and the memory segment list records the allocation status of the memory segments included in the memory page.
[0090] In an embodiment of the present invention, the division of memory blocks is recorded by a full binary tree structure, the memory block corresponds to the root node of the full binary tree, each memory page corresponds to a leaf node of the full binary tree, the full binary tree has a height value, each node corresponds to a level, and the level value increases step by step from the root node to the leaf node; the memory page acquisition module 1003 is also used to: determine whether the memory block is available according to the level value corresponding to the root node, and select an available memory block from the available memory blocks; for the full binary tree corresponding to the memory block, recursively traverse downward step by step starting from the root node, and judge the level values of the left child node and the right child node of the current node in order from left to right until an available leaf node is obtained, and generate an available memory page according to the obtained leaf node.
[0091] According to one embodiment of the present invention, the device 1000 also includes a node update module (not shown in the figure), which is used to: increase the level value of the leaf node corresponding to the memory page by 1, and update the level value of the parent node of the current node step by step from the leaf node upward, wherein the level value of the parent node is updated to the minimum value of the level values of the left child node and the right child node of the parent node.
[0092] According to another embodiment of the present invention, the device 1000 also includes a memory release module (not shown in the figure), which is used to: if all memory segments in the memory page are released, reduce the level value of the leaf node corresponding to the memory page by 1, and update the level value of the parent node of the current node step by step starting from the leaf node, wherein the level value of the parent node is updated to the minimum value of the level values of the left child node and the right child node of the parent node.
[0093] According to another embodiment of the present invention, the device 1000 also includes a memory segment setting module (not shown in the figure), which is used to: set the memory segment length of the memory page according to the memory specification corresponding to the memory size, and create a memory segment list for recording the allocation of memory segments in the memory page.
[0094] In an embodiment of the present invention, the memory page acquisition 1003 module is also used to: determine whether there is unused block memory in the allocated memory block; if so, obtain an available memory block; otherwise, create a memory block and generate a full binary tree to record the division of the memory block.
[0095] Fig.11 An exemplary system architecture 1100 is shown to which the method for managing off-heap memory or the apparatus for managing off-heap memory according to the embodiments of the present invention can be applied.
[0096] like Fig.11 As shown, system architecture 1100 may include terminal devices 1101, 1102, 1103, a network 1104, and a server 1105. Network 1104 is used to provide a medium for communication links between terminal devices 1101, 1102, 1103 and server 1105. Network 1104 may include various connection types, such as wired, wireless communication links, or optical fiber cables, etc.
[0097] Users can use terminal devices 1101, 1102, 1103 to interact with server 1105 through network 1104 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 1101, 1102, 1103, such as virtual machine applications, memory management tools, off-heap memory management, etc. (only as examples).
[0098] The terminal devices 1101 , 1102 , and 1103 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, and desktop computers, etc.
[0099] The server 1105 may be a server that provides various services, such as a background management server (only an example) that provides support for shopping websites browsed by users using the terminal devices 1101, 1102, and 1103. The background management server may analyze and process the received data such as the memory application request, and feed back the processing result (such as the memory allocation address - only an example) to the terminal device.
[0100] It should be noted that the method for managing off-heap memory provided in the embodiment of the present invention is generally executed by the server 1105 , and accordingly, the device for managing off-heap memory is generally set in the server 1105 .
[0101] It should be understood that Fig.11 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.
[0102] Reference below Fig.12 , which shows a schematic diagram of the structure of a computer system 1200 of a terminal device or a server suitable for implementing an embodiment of the present invention. Fig.12 The terminal device or server shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.
[0103] like Fig.12 As shown, the computer system 1200 includes a central processing unit (CPU) 1201, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 1202 or a program loaded from a storage part 1208 into a random access memory (RAM) 1203. In the RAM 1203, various programs and data required for the operation of the system 1200 are also stored. The CPU 1201, the ROM 1202, and the RAM 1203 are connected to each other via a bus 1204. An input / output (I / O) interface 1205 is also connected to the bus 1204.
[0104] The following components are connected to the I / O interface 1205: an input section 1206 including a keyboard, a mouse, etc.; an output section 1207 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 1208 including a hard disk, etc.; and a communication section 1209 including a network interface card such as a LAN card, a modem, etc. The communication section 1209 performs communication processing via a network such as the Internet. A drive 1210 is also connected to the I / O interface 1205 as needed. A removable medium 1211, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 1210 as needed, so that a computer program read therefrom is installed into the storage section 1208 as needed.
[0105] In particular, according to the embodiments disclosed in the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 1209, and / or installed from the removable medium 1211. When the computer program is executed by the central processing unit (CPU) 1201, the above-mentioned functions defined in the system of the present invention are executed.
[0106] It should be noted that the computer-readable medium shown in the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present invention, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.
[0107] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present invention. In this regard, each box in the flow chart or block diagram can represent a module, a program segment, or a part of a code, and the above-mentioned module, program segment, or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0108] The units or modules involved in the embodiments of the present invention may be implemented by software or hardware. The units or modules described may also be arranged in a processor, for example, they may be described as: a processor including a memory request acquisition module, a memory page judgment module, a memory page acquisition module, and a memory request allocation module. The names of these units or modules do not, in some cases, constitute limitations on the units or modules themselves, for example, the memory request acquisition module may also be described as "a module for obtaining the size of memory requested to be allocated from a memory allocation request".
[0109] As another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiment; or it may exist independently without being assembled into the device. The above computer-readable medium carries one or more programs, and when the above one or more programs are executed by a device, the device includes: obtaining the requested memory size from the memory allocation request; judging whether there are available memory pages in the allocated memory pages according to the memory size; if so, obtaining an available memory page; otherwise, obtaining an available memory block and obtaining an available memory page from the memory block; and allocating a memory segment from the memory page for the memory allocation request.
[0110] According to the technical solution of the embodiment of the present invention, the size of the memory requested for allocation is obtained from the memory allocation request; according to the memory size, whether there is an available memory page in the allocated memory page is determined; if so, an available memory page is obtained; otherwise, an available memory block is obtained, and an available memory page is obtained from the memory block; a technical means of allocating a memory segment for the memory allocation request from the memory page is used, and the memory management structure of the present invention is used to manage and control the memory segments requested by the user that are not managed by the virtual machine (JVM), and the memory is pooled and managed, which effectively avoids memory fragmentation, and at the same time reduces the memory application and release operations, and the memory allocation and recovery efficiency is greatly improved, so as to achieve the effect of greatly optimizing the memory usage efficiency and improving the transmission efficiency in the scenario of network transmission.
[0111] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A method for managing off-heap memory, characterized in that: The off-heap memory includes at least one memory block, each memory block includes at least one memory page, each memory page includes at least one memory segment, and the method includes: Get the requested memory size from the memory allocation request; According to the memory size, determining whether there are available memory pages in the allocated memory pages; If yes, obtain an available memory page; otherwise, obtain an available memory block and obtain an available memory page from the memory block; Allocating a memory segment from the memory page for the memory allocation request; The division of memory blocks is recorded through a full binary tree structure, the memory block corresponds to the root node of the full binary tree, each memory page corresponds to a leaf node of the full binary tree, the full binary tree has a height value, each node corresponds to a level, and the level value increases step by step from the root node to the leaf node; obtaining an available memory block and obtaining an available memory page from the memory block includes: judging whether the memory block is available according to the level value corresponding to the root node, and selecting an available memory block from the available memory blocks; for the full binary tree corresponding to the memory block, recursively traversing downward step by step from the root node, judging the level values of the left child node and the right child node of the current node in turn from left to right until an available leaf node is obtained, and generating an available memory page according to the obtained leaf node; After obtaining an available memory page from the memory block, it also includes: increasing the level value of the leaf node corresponding to the memory page by 1, and updating the level value of the parent node of the current node step by step starting from the leaf node, wherein the level value of the parent node is updated to the minimum value of the level values of the left child node and the right child node of the parent node.
2. The method according to claim 1, characterized in that After obtaining the memory size requested for allocation from the memory allocation request, the method further includes: regularizing the memory size into a predefined memory specification; And, judging whether there are available memory pages in the allocated memory pages according to the memory size includes: According to the memory specification corresponding to the memory size, obtaining a memory page corresponding to the memory specification from the allocated memory pages; According to the memory segment list corresponding to the memory page, it is determined whether there are any unallocated memory pages in the memory page corresponding to the memory specification, wherein the memory segment list records the allocation status of the memory segments included in the memory page.
3. The method according to claim 1, characterized in that: The method further comprises: If all memory segments in a memory page are released, the level value of the leaf node corresponding to the memory page is reduced by 1, and the level value of the parent node of the current node is updated step by step starting from the leaf node, wherein the level value of the parent node is updated to the minimum value of the level values of the left child node and the right child node of the parent node.
4. The method according to claim 1, characterized in that: After generating an available memory page based on the obtained leaf node, it also includes: According to the memory specification corresponding to the memory size, the memory segment length of the memory page is set, and a memory segment list is created to record the allocation of memory segments in the memory page.
5. The method according to claim 1, characterized in that: Getting a free memory block involves: Determine whether there is any unused block memory in the allocated memory block; If so, get an available memory block; Otherwise, create a memory block and generate a full binary tree to record the division of the memory block.
6. A device for managing off-heap memory, characterized in that: The off-heap memory includes at least one memory block, each memory block includes at least one memory page, each memory page includes at least one memory segment, and the device includes: A memory request acquisition module is used to obtain the requested memory size from the memory allocation request; A memory page determination module, used for determining whether there are available memory pages in the allocated memory pages according to the memory size; A memory page acquisition module, used to acquire an available memory page if available; otherwise, acquire an available memory block and acquire an available memory page from the memory block; A memory request allocation module, configured to allocate a memory segment from the memory page for the memory allocation request; The division of memory blocks is recorded through a full binary tree structure, wherein the memory block corresponds to the root node of the full binary tree, each memory page corresponds to a leaf node of the full binary tree, the full binary tree has a height value, each node corresponds to a level, and the level value increases step by step from the root node to the leaf node; the memory page acquisition module is also used to: determine whether the memory block is available according to the level value corresponding to the root node, and select an available memory block from the available memory blocks; for the full binary tree corresponding to the memory block, recursively traverse downward step by step from the root node, and judge the level values of the left child node and the right child node of the current node in turn from left to right until an available leaf node is obtained, and generate an available memory page according to the obtained leaf node; The device also includes a node update module, which is used to increase the level value of the leaf node corresponding to the memory page by 1, and update the level value of the parent node of the current node step by step starting from the leaf node, wherein the level value of the parent node is updated to the minimum value of the level values of the left child node and the right child node of the parent node.
7. An electronic device for managing off-heap memory, characterized in that: include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 5.
8. A computer readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 5 is implemented.
Citation Information
Patent Citations
Memory allocation method
CN102012870A
Memory management method and device
CN110928803A