Memory management method, device and equipment, storage medium and computer program product

Through the memory pool management tree and balanced reconstruction mechanism, the problem of inflexible static allocation and management in memory management is solved, and efficient and flexible management of memory resources is achieved to adapt to the memory needs of different application scenarios.

CN120407204AActive Publication Date: 2025-08-01LANGCHAO ELECTRONIC INFORMATION IND CO LTD
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
CN202510898141.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-07-01
Publication Date
2025-08-01
Estimated Expiration
2045-07-01

AI Technical Summary

Technical Problem

In the existing memory management system, the size of the predefined memory pool cannot be adjusted dynamically, resulting in insufficient or insufficient utilization of memory resources, and insufficient flexibility when managing custom memory pools, making it difficult to adapt to the memory needs of different application scenarios.

Method used

The dynamic management mechanism of the memory pool management tree is adopted to manage memory pools of different sizes through a binary tree structure, and accurately add nodes when receiving custom memory pool creation instructions. The balance reconstruction mechanism ensures the balance of the tree and supports users to flexibly create and manage memory pools.

Benefits of technology

It improves the flexibility and efficiency of memory management, ensures the stability of the memory management structure, can adapt to diverse memory needs, reduce memory fragmentation, and improve memory resource utilization.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120407204A_ABST
    Figure CN120407204A_ABST
Patent Text Reader

Abstract

The invention discloses a memory management method, device and equipment, a storage medium and a computer program product, and relates to the technical field of computers, the method comprises the following steps: receiving a creation instruction of a user-defined memory pool, and adding a node corresponding to the user-defined memory pool in a memory pool management tree based on the size of the user-defined memory pool; the memory pool management tree is a binary tree, and nodes in the memory pool management tree are used for managing memory pools of corresponding sizes; when the memory pool management tree does not meet the balance condition, performing balance reconstruction on the to-be-adjusted sub-tree according to a construction mode of the memory pool management tree so as to adjust the memory pool management tree, and enabling the adjusted memory pool management tree to meet the balance condition again; the balance condition is that the sub-tree height difference of the memory pool management tree is smaller than or equal to a preset value, the to-be-adjusted sub-tree comprises a to-be-adjusted node and a father node of the to-be-adjusted node, and the to-be-adjusted node is a node enabling the memory pool management tree not to meet the balance condition. The flexibility of memory management is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and more particularly, to a memory management method, apparatus, device, storage medium, and computer program product. Background Art

[0002] In a computer system, memory management is one of the key factors affecting system performance. In the related art, memory management systems usually rely on predefined memory pools, the sizes of which are determined during system initialization and cannot be dynamically adjusted. Such a static memory management approach has many limitations. First, the sizes of the predefined memory pools are fixed and cannot be dynamically adjusted according to the actual workload, resulting in the situation that memory resources cannot be fully utilized in some cases and memory shortages may occur in other cases. Second, it is difficult for the memory management system to manage custom memory pools, and users cannot flexibly create and manage memory pools of different sizes.

[0003] Therefore, how to improve the flexibility of memory management is a technical problem that needs to be solved by those skilled in the art. Summary of the Invention

[0004] The object of the present invention is to provide a memory management method, apparatus, device, storage medium, and computer program product, which improve the flexibility of memory management.

[0005] To achieve the above object, the present invention provides a memory allocation method, including: receiving a creation instruction for a custom memory pool, and adding a node corresponding to the custom memory pool to a memory pool management tree based on the size of the custom memory pool; wherein, the memory pool management tree is a binary tree, and the nodes in the memory pool management tree are used to manage memory pools of corresponding sizes. The sizes of the memory pools managed by the nodes in the left subtree of the target node in the memory pool management tree are all smaller than the size of the memory pool managed by the target node, and the sizes of the memory pools managed by the nodes in the right subtree of the target node are all larger than the size of the memory pool managed by the target node; when the memory pool management tree does not meet the balance condition, perform a balance reconstruction on the subtree to be adjusted according to the construction method of the memory pool management tree to adjust the memory pool management tree so that the adjusted memory pool management tree meets the balance condition again; wherein, the balance condition is that the height difference between the subtrees of the memory pool management tree is less than or equal to a preset value, and the subtree to be adjusted includes the node to be adjusted and the parent node of the node to be adjusted, and the node to be adjusted is the node that causes the memory pool management tree not to meet the balance condition.

[0006] To achieve the above object, the present invention provides a memory allocation device, comprising: an adding module, configured to receive a creation instruction of a custom memory pool, and add a node corresponding to the custom memory pool in a memory pool management tree based on the size of the custom memory pool; wherein, the memory pool management tree is a binary tree, and the nodes in the memory pool management tree are used to manage memory pools of corresponding sizes. The sizes of the memory pools managed by the nodes in the left subtree of a target node in the memory pool management tree are all smaller than the size of the memory pool managed by the target node, and the sizes of the memory pools managed by the nodes in the right subtree of the target node are all larger than the size of the memory pool managed by the target node; a reconstructing module, configured to, when the memory pool management tree does not meet the balance condition, perform a balance reconstruction on the subtree to be adjusted according to the construction method of the memory pool management tree, so as to adjust the memory pool management tree, such that the adjusted memory pool management tree meets the balance condition again; wherein, the balance condition is that the height difference between subtrees of the memory pool management tree is less than or equal to a preset value. The subtree to be adjusted includes a node to be adjusted and the parent node of the node to be adjusted, and the node to be adjusted is the node that causes the memory pool management tree not to meet the balance condition.

[0007] To achieve the above object, the present invention provides an electronic device, comprising: a memory, configured to store a computer program; a processor, configured to implement the steps of the above memory allocation method when executing the computer program.

[0008] To achieve the above object, the present invention provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the above memory allocation method are implemented.

[0009] To achieve the above object, the present invention provides a computer program product, comprising a computer program. When the computer program is executed by a processor, the steps of the above memory allocation method are implemented.

[0010] The beneficial effects of the present invention are as follows: The memory management method provided by the present invention efficiently manages predefined memory pools and custom memory pools by creating a memory pool management tree, and the memory pool management tree is a binary tree that meets the balance condition. When a creation instruction for a custom memory pool is received, a corresponding node can be accurately added to the memory pool management tree, thereby supporting users to flexibly create memory pools of different sizes. In addition, when the height difference between the subtrees of the memory pool management tree exceeds a preset value, the node causing the imbalance can be automatically identified and balanced and reconstructed, and the unbalanced tree can be readjusted to a binary tree that meets the balance condition. This dynamic balance mechanism not only improves the flexibility and efficiency of memory pool management, but also ensures the stability and performance of the memory management structure. In this way, the present invention effectively solves the problems of static allocation, inflexible management, and easy imbalance of the structure existing in traditional memory management technologies, and improves the flexibility of memory management. The present invention also discloses a memory management device, an electronic device, a computer-readable storage medium, and a computer program product, which can also achieve the above technical effects.

[0011] It should be understood that the above general description and the following detailed description are only exemplary and do not limit the present invention. BRIEF DESCRIPTION OF THE DRAWINGS

[0012] In order to more clearly illustrate the embodiments of the present invention, the following will briefly introduce the drawings required for the embodiments. Obviously, the drawings in the following description are only some embodiments of the present invention, and those of ordinary skill in the art can obtain other drawings without creative efforts based on these drawings.

[0013] Figure 1 It is a flowchart of a memory management method shown according to an exemplary embodiment.

[0014] Figure 2 It is a schematic diagram of a memory pool management tree shown according to an exemplary embodiment.

[0015] Figure 3 It is a schematic diagram of adding a node corresponding to a custom memory pool to a memory pool management tree shown according to an exemplary embodiment.

[0016] Figure 4 It is a schematic diagram of balance reconstruction of a memory pool management tree shown according to an exemplary embodiment.

[0017] Figure 5 It is a structural diagram of a memory management system shown according to an exemplary embodiment.

[0018] Figure 6 It is a schematic diagram of the connection between empty object stacks and full object stacks shown according to an exemplary embodiment.

[0019] Figure 7 Flowchart of a method for adjusting the capacity of an object stack shown according to an exemplary embodiment.

[0020] Figure 8 Flowchart of a method for adjusting the capacity of a memory pool cache layer shown according to an exemplary embodiment.

[0021] Figure 9 Flowchart of the memory management method in the application embodiment provided by the present invention.

[0022] Figure 10 Flowchart of the dynamic shrinkage and expansion mechanism of the memory pool in the application embodiment provided by the present invention.

[0023] Figure 11 Flowchart of the internal recycling strategy of the memory pool in the application embodiment provided by the present invention.

[0024] Figure 12 Flowchart of the passive recycling mechanism of the memory pool in the application embodiment provided by the present invention.

[0025] Figure 13 Structural diagram of a memory management device shown according to an exemplary embodiment.

[0026] Figure 14 Structural diagram of an electronic device shown according to an exemplary embodiment. Detailed implementation manners

[0027] Next, the technical solutions in the embodiments of the present invention will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts belong to the protection scope of the present invention.

[0028] It should be noted that in the description of the present invention, the terms "including", "comprising" or any other variant thereof are intended to cover a non-exclusive inclusion, so that a process, method, article or device including a series of elements not only includes those elements, but also includes other elements not expressly listed, or further includes elements inherent to such process, method, article or device. The terms "first", "second", etc. in the present invention are used to distinguish similar objects, rather than to describe a specific order or sequence.

[0029] In order to enable those skilled in the art of the present technology to better understand the solution of the present invention, the present invention will be further described in detail below in conjunction with the accompanying drawings and specific implementation manners.

[0030] In modern computer systems, the operating system, as the core system software, is responsible for managing and scheduling various hardware resources of the computer to support the efficient operation of application programs. Among them, memory management is one of the key tasks of the operating system. With the continuous development of computer technology, the memory requirements of application programs have become increasingly diverse, not only requiring the operating system to efficiently allocate and recycle memory, but also needing to be flexible enough to adapt to memory usage patterns in different application scenarios.

[0031] In related technologies, memory management in the operating system usually adopts a static allocation method. The size of the memory pool is determined during system initialization and cannot be dynamically adjusted according to the actual workload. This static memory management method has many limitations. First, the size of the predefined memory pool is fixed and cannot be dynamically adjusted according to the actual workload, resulting in insufficient utilization of memory resources in some cases and memory shortages in other cases. Second, the operating system has difficulties in managing custom memory pools, and users cannot flexibly create and manage memory pools of different sizes. In addition, with the increase in multi-tasking and high-concurrency application scenarios, the operating system needs to manage memory resources more efficiently to meet the dynamic memory requirements of different processes and application programs.

[0032] Against this background, the present invention proposes a memory management method, aiming to solve the problems existing in the operating system's memory management in related technologies through a dynamic, flexible, and efficient memory management mechanism, and improve the performance and resource utilization efficiency of the operating system. This method supports users to dynamically create and manage memory pools of different sizes according to different application scenarios and requirements by introducing a dynamic management mechanism of custom memory pools and a memory pool management tree. At the same time, the balance reconstruction mechanism ensures the stability and efficiency of the memory management structure. In addition, this method further optimizes the allocation and recycling process of memory resources, reduces memory fragmentation, and improves memory utilization through the dynamic capacity adjustment of the object stack and the dynamic capacity adjustment strategy of the memory pool cache layer. This memory management method can better meet the high requirements of modern operating systems for memory management and provide strong support for the efficient operation of the operating system.

[0033] Embodiments of the present invention provide a memory management method, which is described in detail in combination with the execution flow of the memory management method. Refer to Figure 1 , a flowchart of a memory management method shown according to an exemplary embodiment.

[0034] S101: Receive the creation instruction of the custom memory pool, and add a node corresponding to the custom memory pool to the memory pool management tree based on the size of the custom memory pool; wherein, the memory pool management tree is a binary tree, and the nodes in the memory pool management tree are used to manage the memory pools of corresponding sizes. For any target node in the memory pool management tree, the sizes of the memory pools managed by the nodes in the left subtree of the target node are all smaller than the size of the memory pool managed by the target node, and the sizes of the memory pools managed by the nodes in the right subtree of the target node are all larger than the size of the memory pool managed by the target node.

[0035] As a feasible implementation manner, this embodiment further includes: creating a memory pool management tree based on the sizes of the predefined memory pools; wherein, the memory pool management tree is a binary tree that meets the balance condition. The predefined memory pools are memory pools predefined during system initialization, and their sizes are fixed, which are used to meet common memory allocation requirements. The size of a memory pool refers to the size of the memory objects therein. The memory pool management tree is a data structure for managing memory pools. In this embodiment, a binary tree that meets the balance condition is adopted, which is a special binary tree. The height difference between the left and right subtrees of each node does not exceed a preset value, which ensures the balance of the tree, thereby improving the efficiency of search, insertion, and deletion operations. The target node is any node in the memory pool management tree and is used to manage a memory pool of a specific size.

[0036] In specific implementation, first collect the size information of all predefined memory pools, and then construct a memory pool management tree based on these sizes. During the construction process, select a suitable node as the root node, usually a memory pool with a middle size, to ensure the balance of the tree. For each node, its left subtree contains all memory pools with sizes smaller than the size of this node, while its right subtree contains all memory pools with sizes larger than the size of this node. By means of recursion, select a suitable root node for each subtree, and finally construct the entire memory pool management tree.

[0037] As a feasible implementation manner, a memory pool management tree is created based on the sizes of the predefined memory pools, including: creating nodes corresponding to the predefined memory pools, sorting the nodes corresponding to the predefined memory pools in ascending order according to the sizes of the corresponding predefined memory pools, selecting the middle node in the sorting result or the node with the largest size of the corresponding predefined memory pool among the two middle nodes as the root node of the memory pool management tree, and determining the root node as the current node; determining the left subtree node set and the right subtree node set of the current node; wherein, the left subtree node set of the current node includes left subtree nodes, and the nodes corresponding to the predefined memory pools with sizes smaller than the size of the predefined memory pool corresponding to the current node, and the right subtree node set of the current node includes right subtree nodes, and the nodes corresponding to the predefined memory pools with sizes larger than the size of the predefined memory pool corresponding to the current node; sorting the left subtree nodes in the left subtree node set in ascending order according to the sizes of the corresponding predefined memory pools, selecting the middle node in the sorting result or the node with the largest size of the corresponding predefined memory pool among the two middle nodes as the root node of the left subtree, sorting the left subtree nodes in the right subtree node set in ascending order according to the sizes of the corresponding predefined memory pools, selecting the middle node in the sorting result or the node with the largest size of the corresponding predefined memory pool among the two middle nodes as the root node of the right subtree; re-determining the root nodes of the left subtree and the right subtree as the current node, and re-entering the step of determining the left subtree node set and the right subtree node set of the current node until the memory pool management tree contains all the nodes corresponding to the predefined memory pools.

[0038] In a specific implementation, first, a corresponding node is created for each predefined memory pool, and these nodes will serve as the basic building blocks of the memory pool management tree. Then, these nodes are sorted in ascending order according to the sizes of their corresponding predefined memory pools. The purpose of this is to conveniently select a suitable node as the root node of the tree in subsequent steps, thereby constructing a binary tree that meets the balance condition. In the sorted node list, the node at the middle position or the larger one of the two middle nodes is selected as the root node of the memory pool management tree. This step is to ensure that the initial memory pool management tree is as balanced as possible, thereby improving the efficiency of subsequent operations. After selecting the root node, it is marked as the current node for the next step of operation. Then, it is necessary to determine the left subtree node set and the right subtree node set of the current node. Specifically, all nodes are traversed, and those nodes with memory pool sizes smaller than the memory pool size of the current node are classified into the left subtree node set, while those nodes with memory pool sizes larger than the memory pool size of the current node are classified into the right subtree node set. Next, the same operations are performed on the left subtree node set and the right subtree node set respectively. For the nodes in the left subtree node set, they are sorted in ascending order according to the sizes of their corresponding predefined memory pools, and then the middle node or the larger one of the two middle nodes in the sorting result is selected as the root node of the left subtree. Similarly, for the nodes in the right subtree node set, they are also sorted in ascending order according to the sizes of their corresponding predefined memory pools, and the middle node or the larger one of the two middle nodes in the sorting result is selected as the root node of the right subtree. Finally, the root nodes of the left subtree and the right subtree are respectively marked as the new current nodes, and then the above steps are repeated for each new current node, that is, determining its left subtree node set and right subtree node set, and continuing to construct the subtrees until all nodes corresponding to the predefined memory pools are correctly inserted into the memory pool management tree.

[0039] For example, the sizes of the predefined memory pools from small to large are: 8 bytes, 16 bytes, 32 bytes, 64 bytes, 128 bytes, 256 bytes, 512 bytes, 1 KB, 4 KB, 8 KB, 16 KB, 32 KB. Taking 512 bytes as the root node, the left subtree is: 8 bytes, 16 bytes, 32 bytes, 64 bytes, 128 bytes, 256 bytes, and the root node of the left subtree is selected as 64 bytes; similarly, the root node of the right subtree is 8 KB. And so on, a complete memory pool management tree is constructed as Figure 2 shown.

[0040] It can be seen that in this step, by constructing a binary tree that meets the balance condition as the memory pool management tree, different-sized memory pools can be quickly located and managed. The structure of the balanced binary tree ensures the efficiency of operations. Whether it is searching, inserting, or deleting operations, their time complexity is relatively low, improving the efficiency of memory management. Especially when the number of memory pools is large, it can significantly reduce the operation time and enhance the system performance.

[0041] In step S101, when the system receives an instruction from the user to create a custom memory pool, it first obtains the size information of the custom memory pool. A custom memory pool is a memory pool dynamically created by the user according to specific requirements. Its size is not predefined but specified by the user. Then, based on the size information of the custom memory pool, a new node is inserted at an appropriate position in the memory pool management tree. The insertion process follows the rules of a binary search tree, that is, if the size of the new node is smaller than the size of the current node, it is inserted into the left subtree of the current node; if the size of the new node is larger than the size of the current node, it is inserted into the right subtree of the current node. In this way, it can be ensured that the new node is correctly placed in the memory pool management tree while maintaining the order of the tree.

[0042] For example, the user needs to create a custom memory pool with an object size of 18KB and inserts the custom-sized node using the standard binary search tree (BST) logic. Traversal of the tree structure: starting from the root node (256 bytes): 18KB > 256 bytes Enter the right subtree (root node is 8KB); 18KB > 8KB Enter the right subtree (root node is 32KB); 18KB < 32KB Enter the left subtree (root node is 16KB); 18KB > 16KB It needs to be inserted into the right subtree of the 16KB node, as Figure 3 shown.

[0043] It can be seen that this step supports the creation of custom memory pools by users, greatly improving the flexibility of memory management. Users can dynamically create memory pools of different sizes according to different application scenarios and requirements without being restricted by predefined memory pool sizes. This flexibility enables memory management to better adapt to various complex workloads, improve the utilization rate of memory resources, and meet the needs of different users.

[0044] For the constructed memory management tree, each node is used to manage memory blocks of a specific size. The node contains the following key information: the memory pool size, which is used to clarify the size of the memory blocks managed by this node; the subtree pointers include pointers to the left subtree and the right subtree. The left subtree corresponds to a memory pool of a smaller size, and the right subtree corresponds to a memory pool of a larger size, forming an ordered tree structure that facilitates the quick search and positioning of memory pools of different sizes. At the same time, the node is associated with a linked list head pointer of the memory pool of the current size to manage the memory pool of this size; the node-level spin lock is used for concurrent control to ensure the atomicity and consistency of operations on the memory pool (such as allocation, recycling, insertion, deletion, etc.) in a multi-threaded environment, avoid data competition and conflicts, and guarantee the correctness and stability of memory management.

[0045] As a feasible implementation, this embodiment further includes: when receiving a deletion instruction for a custom memory pool to be deleted, determining whether the node corresponding to the custom memory pool to be deleted in the memory pool management tree has child nodes; if so, performing a balance reconstruction on the child nodes to obtain a target subtree, deleting the node corresponding to the custom memory pool to be deleted in the memory pool management tree, and replacing the position corresponding to the node corresponding to the custom memory pool to be deleted in the memory pool management tree with the target subtree; if not, directly deleting the node corresponding to the custom memory pool to be deleted in the memory pool management tree.

[0046] In a specific implementation, when receiving an instruction to delete a custom memory pool, the system first checks whether the node corresponding to the custom memory pool to be deleted in the memory pool management tree has child nodes. If this node has child nodes, then the system will start a balance reconstruction process. The purpose of this process is to ensure that after deleting the node, the memory pool management tree can maintain its balance and avoid the tree structure from being unbalanced due to the removal of the node, thereby affecting the efficiency of memory management. When performing balance reconstruction, the system will process the child nodes of the node to be deleted and reorganize these child nodes through a specific algorithm to construct a new balanced subtree, that is, the target subtree. This target subtree is constructed according to the principles of a balanced binary tree, ensuring that the height difference between its left and right subtrees does not exceed a preset threshold. Once the target subtree is constructed, the system will execute the deletion operation to remove the node corresponding to the custom memory pool to be deleted in the memory pool management tree. Subsequently, the system will replace the target subtree at the original position of the node to be deleted, thus completing the entire deletion and reconstruction process. In this way, the memory pool management tree can still maintain balance after deleting the node, ensuring the efficiency and stability of memory management operations. If the node to be deleted does not have child nodes, then the node is directly deleted without performing complex balance reconstruction operations. This is because deleting a node without child nodes will not affect the balance of the memory pool management tree. This direct deletion method simplifies the operation process, improves the processing efficiency, and also reduces unnecessary computational overhead.

[0047] For example, if it is necessary to delete a 1KB memory pool, starting from the root node (512 bytes), 1KB > 512 bytes Enter the right subtree. At the root node of the right subtree (8KB), 1KB < 8KB Enter the left subtree (4KB). At the 4KB node, 1KB < 4KB Enter the left subtree (1KB) and delete this node: If the target node has no children, delete it directly. If the target node has children, move the children up to fill the position.

[0048] It can be seen that through this flexible node deletion method in this embodiment, while ensuring the balance of the memory pool management tree, the overall performance and response speed of the memory pool management system are also improved.

[0049] S102: When the memory pool management tree does not meet the balance condition, perform balance reconstruction on the subtree to be adjusted according to the construction method of the memory pool management tree, so as to adjust the memory pool management tree to make the adjusted memory pool management tree meet the balance condition again; wherein, the balance condition is that the height difference between the left and right subtrees of the memory pool management tree is less than or equal to a preset value, the subtree to be adjusted includes the node to be adjusted and the parent node of the node to be adjusted, and the node to be adjusted is the node that causes the memory pool management tree not to meet the balance condition.

[0050] Among them, the height difference between subtrees is the difference in height between the left and right subtrees of a node, which is used to measure the balance degree of the tree. The preset value is a threshold set in advance, such as 2. When the height difference between subtrees exceeds this value, it is considered that the tree has lost its balance. The node to be adjusted is the node that causes the memory pool management tree to lose its balance and needs to be adjusted to restore the balance of the tree.

[0051] In this step, after inserting or deleting a node into the memory pool management tree, it is necessary to check the balance of the tree. Specifically, it is to check whether the height difference between the left and right subtrees of each node exceeds the preset value, which indicates that the subtree has lost its balance and needs to be adjusted. In the related art, rotation operations in balanced binary trees, such as left rotation, right rotation, left - right rotation, and right - left rotation, are usually used to adjust the tree structure. However, these rotation operations may be triggered relatively frequently, especially in the case of frequent insertion or deletion of nodes, which will increase the complexity and overhead of the adjustment. Therefore, this embodiment adopts a reconstruction method based on the binary tree construction principle. Specifically, the subtree to be adjusted includes the node that causes the imbalance, that is, the node to be adjusted and its parent node. For example, as Figure 4 shown, the nodes to be adjusted that cause the memory pool management tree not to meet the balance condition are 18KB and 20KB, and their parent node is 16KB. Therefore, the subtree to be adjusted is the subtree containing these nodes 16KB, 18KB, and 20KB.

[0052] When performing reconstruction, all nodes in the subtree to be adjusted are first collected, and these nodes will be re-sorted according to their corresponding memory pool sizes. After sorting, in the manner of constructing a binary tree, a suitable node is selected as the new root node. Usually, the node in the middle position after sorting is selected, which can maximize the guarantee that the height difference between the left and right subtrees is minimized, thus achieving better balance. Then, the same operation is recursively performed on the left and right subtrees until the entire subtree to be adjusted is reorganized into a binary tree structure that meets the balance condition.

[0053] As a feasible implementation method, the balance reconstruction of the subtree to be adjusted is carried out in the manner of constructing a memory pool management tree, including: sorting the nodes in the subtree to be adjusted in ascending order according to the sizes of their corresponding memory pools, selecting the middle node in the sorting result or the node with the largest corresponding memory pool size among the two middle nodes as the root node of the reorganized subtree, and determining the root node as the current node; determining the set of left subtree nodes and the set of right subtree nodes of the current node; among them, the set of left subtree nodes of the current node includes left subtree nodes and nodes whose corresponding memory pool sizes are smaller than the corresponding memory pool size of the current node, and the set of right subtree nodes of the current node includes right subtree nodes and nodes whose corresponding memory pool sizes are larger than the corresponding memory pool size of the current node; sorting the left subtree nodes in the set of left subtree nodes in ascending order according to the sizes of their corresponding memory pools, selecting the middle node in the sorting result or the node with the largest corresponding memory pool size among the two middle nodes as the root node of the left subtree, sorting the left subtree nodes in the set of right subtree nodes in ascending order according to the sizes of their corresponding memory pools, selecting the middle node in the sorting result or the node with the largest corresponding memory pool size among the two middle nodes as the root node of the right subtree; re-determining the root nodes of the left subtree and the right subtree as the current node, and re-entering the step of determining the set of left subtree nodes and the set of right subtree nodes of the current node until all nodes in the subtree to be adjusted are included in the reorganized subtree.

[0054] In a specific implementation, first, the nodes in the subtree to be adjusted are sorted in ascending order according to the size of their corresponding memory pools. In the sorted node list, the node at the middle position or the node with the larger size among the two middle nodes is selected as the root node of the memory pool management tree. After selecting the root node, it is marked as the current node for the next step of operation. Then, it is necessary to determine the left subtree node set and the right subtree node set of the current node. Specifically, all nodes are traversed, and those nodes with memory pool sizes smaller than the memory pool size of the current node are classified into the left subtree node set, while those nodes with memory pool sizes larger than the memory pool size of the current node are classified into the right subtree node set. Next, the same operations are performed on the left subtree node set and the right subtree node set respectively. For the nodes in the left subtree node set, they are sorted in ascending order according to the size of their corresponding memory pools, and then the middle node in the sorting result or the node with the larger size among the two middle nodes is selected as the root node of the left subtree. Similarly, for the nodes in the right subtree node set, they are also sorted in ascending order according to the size of their corresponding memory pools, and the middle node in the sorting result or the node with the larger size among the two middle nodes is selected as the root node of the right subtree. Finally, the root nodes of the left subtree and the right subtree are respectively marked as the new current nodes, and then the above steps are repeated for each new current node, that is, determining its left subtree node set and right subtree node set, and continuing to construct the subtree until all nodes in the subtree to be adjusted are correctly inserted into the reconstructed subtree.

[0055] After the reconstruction is completed, the reconstructed subtree will replace the original subtree to be adjusted, so that the entire memory pool management tree is restored to balance again. This reconstruction method effectively avoids the frequent adjustment problems that may be brought by traditional rotation operations, reduces the number and complexity of adjustments. At the same time, it can ensure that the height of the tree remains within a reasonable range, thus maintaining the efficiency of the memory pool management tree. In this way, this embodiment can maintain the balance of the memory pool management tree when dynamically inserting or deleting custom memory pool nodes, improve the stability and performance of memory management, and ensure the efficient execution of memory allocation and recycling operations.

[0056] The memory management method provided by the embodiments of the present invention efficiently manages predefined memory pools and custom memory pools by creating a memory pool management tree, and the memory pool management tree is a binary tree that meets the balance condition. When a creation instruction for a custom memory pool is received, corresponding nodes can be accurately added to the memory pool management tree, thereby supporting users to flexibly create memory pools of different sizes. In addition, when the height difference between the subtrees of the memory pool management tree exceeds a preset value, the node causing the imbalance can be automatically identified and balanced and reconstructed, and the unbalanced tree can be readjusted to a binary tree that meets the balance condition. This dynamic balance mechanism not only improves the flexibility and efficiency of memory pool management, but also ensures the stability and performance of the memory management structure. In this way, the present invention effectively solves the problems existing in traditional memory management technologies, such as static allocation, inflexible management, and easy imbalance of the structure, and improves the flexibility of memory management.

[0057] The memory management method provided in the above embodiments is applied to a memory management system, and the structure diagram of the memory management system is as Figure 5As shown in the figure, it includes an interface layer, a memory pool layer, and a heap memory. The memory pool layer includes multiple memory pools. A memory pool is a memory management technique that pre-allocates a large memory space and divides it into multiple memory objects of a fixed size for the system or application to reuse during runtime, avoiding frequent calls to the operating system's memory allocation and release functions, reducing memory fragmentation, and improving memory allocation and release efficiency. That is, the size of the memory pool is the size of the memory objects therein. A memory object is the basic allocation unit in the memory pool and is the specific entity that the application operates on when applying for or releasing memory in the memory pool. The memory pool includes a Per-CPU layer (processor layer), a memory pool cache layer, and a free list layer. In the Per-CPU layer, each CPU core cache maintains two object stacks, the currently loaded object stack and the spare object stack. An object stack is a collection of multiple memory objects of the same size organized in a stack structure in memory pool management. The objects within each object stack are of the same size, and the top of the stack is the allocable memory object. When allocating, the memory object is taken from the top of the stack, and when releasing, the memory object is put back on the top of the stack, featuring the last-in, first-out characteristic, which facilitates efficient management of reusable memory objects. The CPU core can quickly access the currently loaded object stack to allocate and release memory objects without cross-core competition, improving cache and memory allocation speeds and reducing access latency. The memory pool cache layer is located between the Per-CPU layer and the free list layer and is the middle layer of memory pool management. It holds full object stacks and empty object stacks, and each interaction with the Per-CPU layer is in the form of an entire object stack, improving efficiency. The free list layer obtains memory blocks composed of multiple memory pages from the Heap layer and slices the memory blocks into individual memory objects of a fixed size. The free list layer is responsible for interacting with the operating system's Heap layer. The Heap layer is the dynamic memory allocation area of the operating system. When the program runs, it applies for and releases memory, but frequent operations are prone to problems such as fragmentation and large management overhead. Therefore, the memory pool often pre-allocates large chunks of memory from the heap and then divides them into memory objects for the application to use, reducing direct operations on the heap and improving memory management efficiency and system performance.

[0058] As a feasible implementation, in each memory pool of different sizes, the memory pool cache layer includes an empty object stack queue and a full object stack queue. The empty object stack queue includes multiple empty object stacks, and the full object stack queue includes multiple full object stacks. The multiple full object stacks are sorted in descending order of the most recent access time, and adjacent full object stacks are connected by a doubly linked list. The empty object stacks in the empty object stack queue are managed through an array. The key of the elements in the array is the identifier of the empty object stack, and the value of the element is the pointer to the corresponding full object stack in the full object stack queue.

[0059] In a specific implementation, the memory pool cache layer combines a hash table and a doubly linked list to implement an efficient LRU (Least Recently Used) cache mechanism. For exampleFigure 6 As shown in the figure: The full object stacks are linked by a doubly linked list respectively, and an array can be used for the empty object stacks. Specifically, the hash table is responsible for quick positioning, where the key is the unique identifier of the memory object stack and the value is a pointer to the corresponding object stack in the doubly linked list. The doubly linked list is used to maintain the access order of the memory object stacks. The head of the linked list always points to the most recently accessed memory object stack, while the tail points to the memory object stack that has not been accessed for the longest time. When a new memory object stack is accessed, first find the node of this object stack in the doubly linked list quickly through the hash table, and then move this node from the current position to the head of the linked list, indicating that it has been recently accessed. If the cache layer is full and a new memory object stack needs to be inserted, the system will remove the least recently used object stack from the tail of the linked list, update the hash table, and free up space for the new object stack. This design ensures that the lookup, insertion, and deletion operations can all be completed in near constant time, thus achieving efficient and stable cache management. At the same time, the LRU strategy ensures that the most recently and most likely to be accessed again memory object stacks are always retained in the cache.

[0060] This embodiment introduces a way to adjust the capacity of the object stack, as Figure 7 shown.

[0061] S201: Determine the access frequency and allocation requirements of the object stack.

[0062] In this step, determine the access frequency and allocation requirements of the object stack. The access frequency refers to the number of times the object stack is accessed within a specific time interval, which reflects the activity level of the object stack. The allocation requirement is a comprehensive evaluation of the number of allocation requests and the number of allocation failures of the object stack within a certain time range. The more allocation failures, it indicates that the object stack cannot meet the memory allocation requirements at the current capacity and needs to be expanded. By counting these data, the system can accurately evaluate the actual usage of each object stack and provide a basis for subsequent capacity adjustment.

[0063] As a feasible implementation method, determining the access frequency of the object stack includes: recording the number of times the objects in the object stack are called within the adjustment period; where the adjustment period includes multiple preset time intervals; calculating the access frequency of the object stack based on the total number of times all objects in the object stack are called within each preset time interval and the time decay factor; where the calculation formula for the access frequency is: ; where is the access frequency, is the total number of times all objects in the object stack are called within the i-th preset time interval, , n is the number of preset time intervals included in the adjustment period, is the time decay factor, .

[0064] In a specific implementation, an access counter is maintained for the object stack of each memory pool, and the specific number of times the objects in the object stack are called to the per-CPU layer is recorded in detail. A preset time interval is set , for example, 10 seconds, to regularly sample the access counter, and the access frequency of each object stack is calculated. The calculation method of the access frequency is the number of accesses to the object stack in the preset time interval divided by the duration of the preset time interval. To more accurately reflect the recent access situation of memory objects, a time decay factor is introduced. The role of this factor is to make the impact of newer accesses on the access frequency greater, so that the system is more sensitive to changes in the access of memory objects. A reasonable adjustment period is set , for example, 1 minute, and within an adjustment period, it contains preset time intervals. The total number of times all objects in the object stack are called in the i-th preset time interval is , and the time decay factor is introduced to more accurately reflect the recent access situation of memory objects.

[0065] As a feasible implementation method, determine the allocation requirements of the object stack, including: recording the number of allocation requests and the number of allocation failures of the object stack within the adjustment period, and using the ratio between the number of allocation failures and the number of allocation requests as the allocation requirements of the object stack.

[0066] In a specific implementation, count the number of allocation requests of the object stack of each memory pool within a certain time range, and at the same time accurately record the number of allocation failures. Allocation failure means that when the memory allocation requirement cannot be met within the object stack, memory needs to be obtained from the memory pool cache layer or the free chain surface layer. The object stack with more allocation failures needs to be increased by a certain amount of capacity. Count the number of allocation requests r of the object stack of each memory pool within an adjustment period , and at the same time accurately record the number of allocation failures . To measure the allocation requirements of the stack, an allocation requirement index is defined. Here, it can be determined whether capacity expansion is needed based on the number of allocation failures, and the allocation requirement D can be further quantified. . When D is larger, it means that the proportion of allocation failures is higher, and the object stack needs to increase its capacity more. .

[0067] S202: Adjust the capacity of the object stack based on the access frequency and allocation requirements of the object stack.

[0068] In this step, the capacity of the object stack is dynamically adjusted according to the access frequency and allocation requirements of the object stack. Specifically, for an object stack with a high access frequency and large allocation requirements, the system will appropriately increase its capacity to better meet the memory allocation requirements. On the contrary, for an object stack with a low access frequency and small allocation requirements, the system will appropriately reduce its capacity to achieve effective memory recycling.

[0069] As a feasible implementation, adjusting the capacity of the object stack based on the access frequency and allocation requirements of the object stack includes: when the access frequency of the object stack is greater than the first preset access frequency threshold and the allocation requirement of the object stack is greater than the first allocation requirement threshold, increasing the capacity of the object stack by the first preset ratio; when the access frequency of the object stack is less than the second preset access frequency threshold and the allocation requirement of the object stack is less than the second allocation requirement threshold, reducing the capacity of the object stack by the second preset ratio; where the first preset access frequency threshold is greater than the second preset access frequency threshold, and the first allocation requirement threshold is greater than the second allocation requirement threshold.

[0070] In a specific implementation, at the end of each adjustment cycle the capacity C of the object stack of each memory pool is adjusted according to the average access frequency and the allocation requirement D. For those stacks with a high access frequency and large allocation requirements, their capacity is appropriately increased. The increase amplitude can be determined according to the frequency of allocation failures. For example, if the number of allocation failures is relatively large, the capacity of the stack can be increased by the first preset ratio , such as 10% or 20%, to ensure that the stack can better meet the memory allocation requirements. When and , , is the first preset access frequency threshold, is the first allocation requirement threshold, is the increased capacity. For stacks with a low access frequency and small allocation requirements, their capacity is appropriately reduced. The reduction amplitude can be comprehensively considered based on the current capacity and access frequency of the stack. For example, if the access frequency is lower than a pre-set threshold, a certain ratio of the stack capacity can be reduced , such as 5% or 10%, so as to achieve effective memory recycling. When and , , is the first preset access frequency threshold, is the first allocation requirement threshold, is the reduced capacity.

[0071] As a feasible implementation, it further includes: setting a maximum threshold and a minimum threshold for the capacity of the object stack; correspondingly, after increasing the capacity of the object stack by a first preset ratio, it further includes: if the capacity of the object stack after the increase is greater than the maximum threshold of the object stack capacity, then setting the capacity of the object stack to the maximum threshold of the object stack capacity; correspondingly, after decreasing the capacity of the object stack by a second preset ratio, it further includes: if the capacity of the object stack after the decrease is less than the minimum threshold of the object stack capacity, then setting the capacity of the object stack to the minimum threshold of the object stack capacity. In specific implementation, by setting the maximum threshold and the minimum threshold for the capacity of the object stack, the adjustment of the object stack capacity should be between the maximum threshold and the minimum threshold of the object stack capacity.

[0072] Thus, in this embodiment, by accurately evaluating the access frequency and allocation requirements of the object stack, and understanding the actual usage of each object stack in real time, a reasonable capacity adjustment decision can be made. This mechanism for dynamically adjusting the capacity of the object stack not only reduces the situation of memory allocation failure, significantly improves the efficiency and flexibility of memory management, but also avoids the waste of memory resources and improves the memory utilization rate.

[0073] This embodiment introduces a method for adjusting the capacity of the memory pool cache layer, as Figure 8 shown.

[0074] S301: Calculate the memory pressure index of the memory pool cache layer based on the memory allocation rate and the memory allocation failure rate of the memory pool cache layer; wherein, the memory allocation rate is used to describe the number of memory object allocations per unit time, and the memory allocation failure rate is used to describe the proportion of memory allocation failure times in the total number of memory allocations.

[0075] In this step, the memory pressure index is calculated by monitoring the memory allocation rate and the memory allocation failure rate of the memory pool cache layer. The memory allocation rate refers to the number of times memory objects are allocated per unit time, and this indicator can intuitively reflect the intensity of the current system's demand for memory resources. The memory allocation failure rate refers to the proportion of memory allocation failure times in the total number of memory allocations, which directly reflects the tightness of the current memory resources. When the memory allocation failure rate is high, it means that the system frequently fails to meet allocation requests due to insufficient memory, which undoubtedly increases the memory pressure of the system. By comprehensively considering these two key indicators, the system can calculate a memory pressure index that reflects the current pressure level of the memory pool cache layer. This index provides an important decision-making basis for subsequent memory pool capacity adjustment, enabling the system to more accurately respond to the dynamic changes of memory resources.

[0076] As a feasible implementation, calculate the memory pressure index of the memory pool cache layer based on the memory allocation rate and memory allocation failure rate of the memory pool cache layer, including: calculating the memory pressure index of the memory pool cache layer based on the memory allocation rate of the memory pool cache layer in the current cycle, the historical average memory allocation rate, and the memory allocation failure rate of the memory pool cache layer in the current cycle; wherein, the calculation formula of the memory pressure index is: ; wherein, M is the memory pressure index, is the memory allocation rate of the memory pool cache layer in the current cycle, is the historical average memory allocation rate, is the number of memory allocation failures of the memory pool cache layer in the current cycle, is the total number of memory allocations of the memory pool cache layer in the current cycle, is the memory allocation failure rate of the memory pool cache layer in the current cycle, , are adjustment parameters, .

[0077] S302: Adjust the capacity of the memory pool cache layer based on the memory pressure index.

[0078] In this step, dynamically adjust the capacity of the memory pool cache layer according to the calculated memory pressure index. When the memory pressure index is high, it indicates that the system is currently facing a large memory demand pressure. At this time, the system will correspondingly increase the capacity of the memory pool cache layer to meet more memory allocation requests and reduce the situation of memory allocation failures. On the contrary, when the memory pressure index is low, it means that the system's demand for memory is relatively reduced and the memory resources are relatively abundant. At this time, the system can appropriately reduce the capacity of the memory pool cache layer to release the excess memory resources and improve the utilization rate of memory resources. This dynamic adjustment mechanism based on the memory pressure index enables the capacity of the memory pool cache layer to be flexibly scaled according to the actual needs of the system, avoiding over-allocation or shortage of memory resources and realizing the refined management of memory resources.

[0079] As a feasible implementation, adjust the capacity of the memory pool cache layer based on the memory pressure index, including: calculating the target capacity of the memory pool cache layer based on the current capacity and memory pressure index of the memory pool cache layer; wherein, the calculation formula of the target capacity is: , is the target capacity, is the current capacity, and M is the memory pressure index; calculate the new capacity value based on the current capacity and target capacity of the memory pool cache layer; wherein, the calculation formula of the new capacity value is: , is the new capacity value, is the influence weight of controlling the historical capacity, When the new capacity value is greater than the first preset multiple of the current capacity, expand the capacity of the memory pool cache layer; where the first preset multiple is greater than 1, and each time expand a preset number of object stacks; when the new capacity value is less than the second preset multiple of the current capacity, shrink the capacity of the memory pool cache layer; where the second preset multiple is less than 1, and each time shrink a preset number of object stacks.

[0080] In a specific implementation, calculate the target capacity based on the current capacity of the memory pool cache layer and the memory pressure index. The greater the memory pressure index, the greater the target capacity, and vice versa. Then, calculate the new capacity value based on the current capacity and the target capacity. By comprehensively considering the current capacity and the target capacity, and giving a certain weight to the historical capacity, calculate a relatively reasonable new capacity value smoothly, and suppress short-term fluctuations through EWMA to avoid unstable adjustment of the capacity of the memory pool cache layer due to drastic changes in the target capacity.

[0081] When the calculated new capacity value is greater than the first preset multiple of the current capacity, the system will expand the capacity of the memory pool cache layer. This first preset multiple is a value greater than 1, used to set an expansion threshold, such as 1.2. Each time of expansion, a preset number of object stacks will be increased, so as to ensure that the memory pool cache layer has enough capacity to cope with the current increase in memory requirements. On the contrary, when the new capacity value is less than the second preset multiple of the current capacity, the system will shrink the capacity of the memory pool cache layer. The second preset multiple is a value less than 1, used to set a shrinkage threshold, such as 0.8. Each time of shrinkage, a preset number of object stacks will also be reduced, so as to release redundant memory resources and improve the utilization rate of memory resources. This expansion and shrinkage strategy based on the multiple relationship between the new capacity value and the current capacity can make the capacity of the memory pool cache layer dynamically adapt to the change of the system's memory requirements, avoiding both the situation of memory shortage and the waste of memory resources, and realizing the efficient management and flexible allocation of memory resources.

[0082] As a feasible implementation manner, this embodiment further includes: setting a maximum threshold and a minimum threshold for the capacity of the memory pool cache layer; when the capacity of the memory pool cache layer is greater than the maximum threshold of the capacity of the memory pool cache layer, shrink the capacity of the memory pool cache layer; where each time shrink a preset number of object stacks; when the capacity of the memory pool cache layer is less than the minimum threshold of the capacity of the memory pool cache layer, expand the capacity of the memory pool cache layer; where each time expand a preset number of object stacks.

[0083] In a specific implementation, a maximum threshold and a minimum threshold for the capacity of the memory pool cache layer are set. When the capacity of the memory pool cache layer is lower than the minimum threshold of the capacity of the memory pool cache layer, the capacity of the memory pool cache layer is expanded, memory blocks are replenished to the free list, and the memory pool cache layer loads memory blocks from the free list. When the capacity of the memory pool cache layer is greater than the maximum threshold of the capacity of the memory pool cache layer, the capacity of the memory pool cache layer is shrunk, that is, a specified internal memory pool recycling mechanism is triggered. When the capacity of the memory pool cache layer is between the maximum and minimum thresholds, the above dynamic adjustment strategy for the capacity of the memory pool cache layer is adopted to adjust the capacity of the memory pool cache layer in real time according to requirements.

[0084] As a feasible implementation manner, it further includes: when initializing the memory pool cache layer, filling the memory pool cache layer so that the capacity of the memory pool cache layer reaches the minimum threshold of the capacity of the memory pool cache layer.

[0085] It should be noted that in traditional memory management systems, there are certain limitations in the management of the cache layer domain. These systems usually only set a fixed threshold in the memory pool cache layer, resulting in that when memory is first allocated, the Per-CPU needs to directly obtain memory from the free list layer. Initially, the free list is also empty, and memory needs to be allocated from the bottom layer. When this memory is used up, it is released to the memory pool cache layer so that subsequent memory allocations can reuse these memory resources. However, this mechanism causes the phenomenon of cross-layer flow, and it takes a long time when allocating memory for the first time or when the memory in the memory pool is used up and then reallocated, affecting the stability and efficiency of the entire system.

[0086] To solve this problem, in the initialization of this embodiment, the memory pool cache layer is filled to reach the set minimum threshold. When allocating memory for the first time, the Per-CPU layer directly obtains memory objects from the cache layer instead of from the free list. After that, the memory pool cache layer uses the dynamic adjustment strategy for the capacity of the memory pool cache layer to keep the memory in the entire cache area between the maximum and minimum thresholds and prefetch or release memory dynamically in real time according to the state. Thereby reducing the performance overhead when allocating memory for the first time or when the memory in the memory pool is used up and then reallocated.

[0087] It can be seen that in this embodiment, by introducing the comprehensive index of memory pressure index to dynamically adjust the capacity of the memory pool cache layer, the efficiency and adaptability of memory management are significantly improved. First of all, the memory pressure index comprehensively considers the two key factors of memory allocation rate and memory allocation failure rate, enabling the system to more comprehensively and accurately evaluate the current memory resource pressure situation. This accurate evaluation provides a reliable basis for the reasonable adjustment of the memory pool capacity, avoiding misjudgment caused by a single index. Secondly, the dynamic adjustment mechanism enables the memory pool cache layer to flexibly change its capacity according to the real-time needs of the system. Whether facing a sudden high memory demand scenario or a period with low memory demand, it can achieve efficient utilization of memory resources. This not only improves the system's response speed and stability but also effectively reduces the situation of memory allocation failure, enhancing the overall performance of the system.

[0088] Based on the above embodiment, as a preferred implementation manner, it further includes: when the number of object stacks included in the memory pool cache layer reaches the maximum number threshold of object stacks, recycle the object stack with the farthest access time.

[0089] It should be noted that in the current memory management system, when dealing with the memory recycling strategy in the memory pool, the recycling operation is usually taken when the memory pool capacity reaches the upper limit. Taking libumem (user-space memory management library) as an example, when the capacity of the memory pool reaches the preset upper limit, it will recycle all the memory in the memory pool. The recycling strategy of Tcmalloc (thread cache memory allocator) is to recycle the objects that have not been used in the cache since the last recycling. However, this recycling strategy has certain problems. For libumem, it is unreasonable to recycle all, and it cannot guarantee the subsequent memory allocation requests after all recycling; for Tcmalloc, if there are no objects marked as unused since the last recycling in the current memory pool, then it cannot perform the memory recycling operation, resulting in the memory amount in the memory pool not being able to be reduced below the maximum threshold.

[0090] To avoid these problems, this embodiment adopts a timestamp-based elimination strategy. That is, when the number of object stacks in the memory pool cache layer reaches the maximum threshold, the system will eliminate the object stacks in the order from the farthest to the nearest access time until the number of object stacks in the cache layer is lower than the maximum threshold. At the same time, the system will also ensure that the number of object stacks in the cache layer is not lower than the minimum threshold to maintain a certain caching basis and avoid frequent elimination and allocation operations from affecting performance. After the elimination operation is completed, the system will insert the newly released object stack into the cache layer and update its access timestamp to the current time to ensure that the newly released object stack can be considered in the next elimination operation. Through this strategy, when the memory pool cache layer reaches its maximum capacity, it can effectively recycle those object stacks that are least likely to be accessed again, making room for new memory allocation requests. At the same time, by keeping the number of object stacks in the cache layer not lower than the minimum threshold, it avoids frequent elimination and allocation operations, reduces performance impact, thereby improving the efficiency of memory allocation and optimizing the management of the memory pool.

[0091] Based on the above embodiments, as a preferred embodiment, it further includes: when the memory pool of the target size cannot respond to a memory allocation request, determining the target memory size requested by the memory allocation request; judging whether the heap memory meets the memory allocation request; if so, recycling the memory of the target size from the heap memory to respond to the memory allocation request; if not, recycling the memory with a size larger than the target size in ascending order of size until the size of the recycled memory reaches the third preset multiple of the target memory size; where the third preset multiple is greater than 1.

[0092] It should be noted that when the current memory management system cannot allocate memory from the memory pool, it will globally recycle all memory pools, which is unnecessary. It is mainly necessary to recycle the memory to meet the current memory usage requirements. Recycling all memory pools may affect the use of other memory pools. Therefore, this embodiment preferentially recycles the memory pool resources that meet the target size and have a larger capacity, ensuring accurate termination when reaching the preset recycling threshold, thereby optimizing the recycling efficiency and system resource utilization. The recycling termination condition is: when the cumulative recycled memory reaches the third preset multiple of the target memory size, for example, 1.2 times the target memory size, ensuring that the recycled memory can meet the current needs and avoid resource waste and system performance degradation caused by over-recycling.

[0093] The specific memory recycling strategy is as follows: First, the system determines the target memory size requested by the memory allocation request. Next, the system checks whether there is enough memory in the heap memory to satisfy this request. If there is enough memory in the heap memory, the system directly allocates the required amount of memory from the heap memory to respond to the memory allocation request. However, if there is not enough memory in the heap memory to meet the request, the system will sequentially recycle the memory pools larger than the target size in ascending order of size. Take the node corresponding to the memory pool of the target size as the current node, and select the right subtree of the current node as the starting point for recycling. The nodes in the subtree represent memory pools of larger sizes. Recycling larger-sized memory pools first can more efficiently meet memory requirements. The traversal and recycling order of the right subtree of the current node is: first traverse and recycle the left subtree of this right subtree (traverse and recycle in ascending order of size), then recycle the root node of this right subtree, and finally traverse and recycle the right subtree of this right subtree (traverse and recycle in ascending order of size). If the recycled memory is still insufficient after traversing the right subtree of the current node, continue to traverse and recycle upward. Check whether the current node is the left child of its parent node. If so, traverse and recycle the memory of the current node's parent node, and traverse and recycle the memory of the right subtree of the current node's parent node. If not, re-use the current node's parent node as the current node and re-enter the step of checking whether the current node is the left child of its parent node, and so on until the recycling termination condition is met. If the recycling of the root node of the memory pool management tree still cannot meet the recycling termination condition, then the memory is insufficient.

[0094] It can be seen that through the above memory recycling strategy, memory can be efficiently recycled when the system memory is tight, ensuring the timeliness of memory allocation and the stability of system operation.

[0095] The following introduces an application embodiment provided by the present invention, as Figure 9 shown.

[0096] Comprehensive management of memory pools: Design strategies comprehensively manage predefined memory pools and custom memory pools, and design methods for creating, deleting, and managing custom memory pools.

[0097] Optimize the memory pool allocation and release process: Optimize the allocation and release process by designing dynamic expansion strategies for memory object stacks in memory pools, dynamic adjustment strategies for the capacity of memory pool cache layers, and optimizing the allocation time-consuming problems in the scenarios of initial allocation or memory exhaustion in memory pools.

[0098] Design a memory pool recycling mechanism: Optimize the memory pool recycling efficiency by designing internal recycling strategies for specified memory pools and passive recycling strategies for global memory pools.

[0099] The flowchart of the memory pool dynamic shrinking and expanding mechanism is as Figure 10As shown in the figure, it includes the following steps: specifying memory pool allocation and release operations; counting the currently available memory in the cache layer of the specified memory pool; obtaining the maximum and minimum thresholds of the cache layer of the specified memory pool; comparing the currently available memory with the maximum and minimum thresholds; if the currently available memory < the minimum threshold, triggering the specified memory pool to supplement memory blocks to the free list and constructing memory objects to be saved in the free list; if the currently available memory > the maximum threshold, triggering the internal recycling mechanism of the specified memory pool; if the currently available memory is between the minimum threshold and the maximum threshold, calculating the appropriate memory according to the memory pool dynamic adjustment strategy and adjusting the cache layer capacity according to the calculation result.

[0100] The flowchart of the internal memory pool recycling strategy is as Figure 11 As shown in the figure, it includes the following steps: triggering the memory recycling mechanism of the specified memory pool; judging whether the available objects in the cache layer of the memory pool are greater than the maximum threshold; if the available objects are greater than the maximum threshold, performing the recycling operation, searching for the doubly linked list of full object stacks, eliminating the full object stack with the longest access time from the tail and putting it into the free list, judging whether the span blocks in the free list reach the full free state, if so, releasing the span blocks to the Heap, if not, not performing any operation and ending the process; if the available objects are not greater than the maximum threshold, not performing the recycling operation and ending the process.

[0101] The flowchart of the passive memory pool recycling mechanism is as Figure 12As shown, it includes the following steps: Determine whether a memory pool of a specific size cannot maintain the minimum threshold; if not, end the process; if so, trigger the passive recycling mechanism. Determine the required memory size N, trigger the replenishment of memory blocks (spans) from the heap memory, and determine whether the heap memory has enough memory to meet the requirements for replenishing a specific memory pool; if so, replenish memory blocks (spans) from the heap memory; if not, recycle the memory of other memory pools (including predefined memory pools and custom memory pools) according to the traversal recycling strategy. Take the node corresponding to the memory pool of the target size as the current node, and select the right subtree of the current node as the starting point for recycling. The traversal recycling order of the right subtree of the current node is: first traverse and recycle the left subtree of the right subtree (traverse and recycle in ascending order of size), then recycle the root node of the right subtree, and finally traverse and recycle the right subtree of the right subtree (traverse and recycle in ascending order of size). Determine whether the recycled memory amount reaches the required size of 1.2N; if so, end the process; if not, determine whether the root node memory pool has been recycled. If so, the memory is insufficient and the process ends; if not, continue to traverse and recycle upward. Determine whether the current node is the left child of its parent node. If so, traverse and recycle the memory of the current node's parent node and the memory of the right subtree of the current node's parent node. If not, re-take the current node's parent node as the current node and re-enter the step of determining whether the current node is the left child of its parent node, and so on until the recycling termination condition is met. If the recycling of the root node of the memory pool management tree is completed and the recycling termination condition still cannot be met, the memory is insufficient.

[0102] It can be seen that through a series of optimization measures, this embodiment significantly improves the efficiency and performance of memory management. First, the object stack dynamic adjustment algorithm is adopted to adjust the stack capacity in real time according to the access frequency and allocation requirements, reducing the time-consuming operation of obtaining memory across layers, thereby improving the memory allocation efficiency. Second, based on the exponentially weighted moving average algorithm, the capacity of the memory pool cache layer is dynamically adjusted. According to key indicators such as the memory allocation rate and object survival time, the memory pool is reasonably expanded and contracted, effectively avoiding memory waste, and thus improving the memory utilization rate. In addition, by optimizing the memory allocation and release process, the frequent flow of memory across layers is reduced, the complexity of memory management is lowered, and the overall performance and stability of the system are enhanced. At the same time, this embodiment supports user-defined memory pools, greatly improving the flexibility of memory management and enabling it to better adapt to various different application scenarios. In terms of memory pool management, this embodiment comprehensively manages predefined memory pools and user-defined memory pools, improving the management efficiency. Finally, the recycling algorithms for individual and global memory pools are optimized. When the memory pool cache layer is full, the memory object stack that has not been used for the longest time is preferentially eliminated; during global recycling, the memory pool resources that meet the target size and have a larger capacity are preferentially recycled, and the recycling is accurately terminated when the preset threshold is reached, thus significantly improving the memory recycling efficiency and system resource utilization rate.

[0103] The following introduces a memory management device provided by an embodiment of the present invention. The memory management device described below can be referred to with the memory management method described above. Refer to Figure 13 , a structural diagram of a memory management device shown according to an exemplary embodiment.

[0104] An adding module 100 is used to receive a creation instruction for a user-defined memory pool and add a node corresponding to the user-defined memory pool to the memory pool management tree based on the size of the user-defined memory pool; wherein, the memory pool management tree is a binary tree, and the nodes in the memory pool management tree are used to manage memory pools of corresponding sizes. The sizes of the memory pools managed by the nodes in the left subtree of the target node in the memory pool management tree are all smaller than the size of the memory pool managed by the target node, and the sizes of the memory pools managed by the nodes in the right subtree of the target node are all larger than the size of the memory pool managed by the target node.

[0105] A reconstruction module 200 is used to, when the memory pool management tree does not meet the balance condition, perform balance reconstruction on the subtree to be adjusted according to the construction method of the memory pool management tree to adjust the memory pool management tree so that the adjusted memory pool management tree meets the balance condition again; wherein, the balance condition is that the height difference of the subtrees of the memory pool management tree is less than or equal to a preset value, and the subtree to be adjusted includes the node to be adjusted and the parent node of the node to be adjusted, and the node to be adjusted is the node that causes the memory pool management tree not to meet the balance condition.

[0106] The memory management device provided by the embodiment of the present invention efficiently manages predefined memory pools and custom memory pools by creating a memory pool management tree, and the memory pool management tree is a binary tree that meets the balance condition. When receiving a creation instruction for a custom memory pool, it can accurately add a corresponding node to the memory pool management tree, thereby supporting users to flexibly create memory pools of different sizes. In addition, when the height difference between the subtrees of the memory pool management tree exceeds a preset value, it can automatically identify the node that causes the imbalance and perform a balance reconstruction on it, readjusting the unbalanced tree into a binary tree that meets the balance condition. This dynamic balance mechanism not only improves the flexibility and efficiency of memory pool management, but also ensures the stability and performance of the memory management structure. In this way, the present invention effectively solves the problems existing in traditional memory management technologies, such as static allocation, inflexible management, and easy imbalance of the structure, and improves the flexibility of memory management.

[0107] On the basis of the above embodiment, as a preferred embodiment, it further includes: a creation module, which is used to create nodes corresponding to each predefined memory pool, sort the nodes corresponding to each predefined memory pool in ascending order according to the size of the corresponding predefined memory pool, select the middle node in the sorting result or the node with the largest size of the corresponding predefined memory pool among the middle two nodes as the root node of the memory pool management tree, and determine the root node as the current node; determine the left subtree node set and the right subtree node set of the current node; wherein, the left subtree node set of the current node includes left subtree nodes, and the nodes corresponding to the predefined memory pools of the left subtree nodes are smaller than the nodes corresponding to the predefined memory pools of the current node. The right subtree node set of the current node includes right subtree nodes, and the nodes corresponding to the predefined memory pools of the right subtree nodes are larger than the nodes corresponding to the predefined memory pools of the current node; sort the left subtree nodes in the left subtree node set in ascending order according to the size of the corresponding predefined memory pool, select the middle node in the sorting result or the node with the largest size of the corresponding predefined memory pool among the middle two nodes as the root node of the left subtree, and sort the left subtree nodes in the right subtree node set in ascending order according to the size of the corresponding predefined memory pool, select the middle node in the sorting result or the node with the largest size of the corresponding predefined memory pool among the middle two nodes as the root node of the right subtree; re-determine the root nodes of the left subtree and the right subtree as the current node, and re-enter the step of determining the left subtree node set and the right subtree node set of the current node until the memory pool management tree contains all the nodes corresponding to the predefined memory pools.

[0108] Based on the above embodiments, as a preferred implementation manner, it further includes: a deletion module, configured to, when receiving a deletion instruction for a custom memory pool to be deleted, determine whether there are child nodes for the node corresponding to the custom memory pool to be deleted in the memory pool management tree; if so, perform a balance reconstruction on the child nodes to obtain a target subtree, delete the node corresponding to the custom memory pool to be deleted in the memory pool management tree, and replace the position corresponding to the node corresponding to the custom memory pool to be deleted in the memory pool management tree with the target subtree; if not, directly delete the node corresponding to the custom memory pool to be deleted in the memory pool management tree.

[0109] Based on the above embodiments, as a preferred implementation manner, the reconstruction module 200 is specifically configured to: sort the nodes in the subtree to be adjusted in ascending order of the sizes of the corresponding memory pools, select the middle node in the sorting result or the node with the largest memory pool size among the middle two nodes as the root node of the reconstructed subtree, and determine the root node as the current node; determine the left subtree node set and the right subtree node set of the current node; wherein, the left subtree node set of the current node includes left subtree nodes, nodes whose corresponding memory pool sizes are smaller than the memory pool size corresponding to the current node, and the right subtree node set of the current node includes right subtree nodes, nodes whose corresponding memory pool sizes are larger than the memory pool size corresponding to the current node; sort the left subtree nodes in the left subtree node set in ascending order of the sizes of the corresponding memory pools, select the middle node in the sorting result or the node with the largest memory pool size among the middle two nodes as the root node of the left subtree, sort the left subtree nodes in the right subtree node set in ascending order of the sizes of the corresponding memory pools, select the middle node in the sorting result or the node with the largest memory pool size among the middle two nodes as the root node of the right subtree; re-determine the root nodes of the left subtree and the right subtree as the current node, and re-enter the step of determining the left subtree node set and the right subtree node set of the current node until all nodes in the subtree to be adjusted are included in the reconstructed subtree.

[0110] Based on the above embodiments, as a preferred implementation manner, in each memory pool of different sizes, the memory pool cache layer includes an empty object stack queue and a full object stack queue. The empty object stack queue includes multiple empty object stacks, and the full object stack queue includes multiple full object stacks. The multiple full object stacks are sorted in descending order of the most recent access time, and adjacent full object stacks are connected by a doubly linked list. The empty object stacks in the empty object stack queue are managed by an array. The key of the element in the array is the identifier of the empty object stack, and the value of the element is the pointer to the corresponding full object stack in the full object stack queue.

[0111] Based on the above embodiments, as a preferred embodiment, it further includes: a determination module for determining the access frequency and allocation requirements of the object stack; an object stack capacity adjustment module for adjusting the capacity of the object stack based on the access frequency and allocation requirements of the object stack.

[0112] Based on the above embodiments, as a preferred embodiment, the determination module is specifically configured to: record the number of times the objects in the object stack are called within an adjustment period; wherein, the adjustment period includes a plurality of preset time intervals; calculate the access frequency of the object stack based on the total number of times all objects in the object stack are called within each preset time interval and a time decay factor; wherein, the calculation formula for the access frequency is: ; wherein, is the access frequency, is the total number of times all objects in the object stack are called within the i-th preset time interval, , n is the number of preset time intervals included in the adjustment period, is the time decay factor, .

[0113] Based on the above embodiments, as a preferred embodiment, the determination module is specifically configured to: record the number of allocation requests and the number of allocation failures of the object stack within an adjustment period, and use the ratio between the number of allocation failures and the number of allocation requests as the allocation requirement of the object stack.

[0114] Based on the above embodiments, as a preferred embodiment, the object stack capacity adjustment module is specifically configured to: when the access frequency of the object stack is greater than a first preset access frequency threshold and the allocation requirement of the object stack is greater than a first allocation requirement threshold, increase the capacity of the object stack by a first preset ratio; when the access frequency of the object stack is less than a second preset access frequency threshold and the allocation requirement of the object stack is less than a second allocation requirement threshold, reduce the capacity of the object stack by a second preset ratio; wherein, the first preset access frequency threshold is greater than the second preset access frequency threshold, and the first allocation requirement threshold is greater than the second allocation requirement threshold.

[0115] Based on the above embodiments, as a preferred embodiment, it further includes: a first setting module for setting the maximum threshold and the minimum threshold of the object stack capacity; correspondingly, the object stack capacity adjustment module is further configured to: if the increased capacity of the object stack is greater than the maximum threshold of the object stack capacity, set the capacity of the object stack to the maximum threshold of the object stack capacity; if the reduced capacity of the object stack is less than the minimum threshold of the object stack capacity, set the capacity of the object stack to the minimum threshold of the object stack capacity.

[0116] On the basis of the above embodiments, as a preferred embodiment, it further includes: a calculation module, configured to calculate the memory pressure index of the memory pool cache layer based on the memory allocation rate and the memory allocation failure rate of the memory pool cache layer; wherein, the memory allocation rate is used to describe the number of memory object allocations per unit time, and the memory allocation failure rate is used to describe the proportion of the number of memory allocation failures to the total number of memory allocations; a memory pool cache layer capacity adjustment module, configured to adjust the capacity of the memory pool cache layer based on the memory pressure index.

[0117] On the basis of the above embodiments, as a preferred embodiment, the calculation module is specifically configured to: calculate the memory pressure index of the memory pool cache layer based on the memory allocation rate of the memory pool cache layer in the current cycle, the historical average memory allocation rate, and the memory allocation failure rate of the memory pool cache layer in the current cycle; wherein, the calculation formula of the memory pressure index is: ; where M is the memory pressure index, is the memory allocation rate of the memory pool cache layer in the current cycle, is the historical average memory allocation rate, is the number of memory allocation failures of the memory pool cache layer in the current cycle, is the total number of memory allocations of the memory pool cache layer in the current cycle, is the memory allocation failure rate of the memory pool cache layer in the current cycle, , is the adjustment parameter, .

[0118] On the basis of the above embodiments, as a preferred embodiment, the memory pool cache layer capacity adjustment module is specifically configured to: calculate the target capacity of the memory pool cache layer based on the current capacity of the memory pool cache layer and the memory pressure index; wherein, the calculation formula of the target capacity is: , is the target capacity, is the current capacity, and M is the memory pressure index; calculate the new capacity value based on the current capacity and the target capacity of the memory pool cache layer; wherein, the calculation formula of the new capacity value is: , is the new capacity value, is the influence weight of the control historical capacity; when the new capacity value is greater than the first preset multiple of the current capacity, expand the capacity of the memory pool cache layer; wherein, the first preset multiple is greater than 1, and each time a preset number of object stacks are expanded; when the new capacity value is less than the second preset multiple of the current capacity, shrink the capacity of the memory pool cache layer; wherein, the second preset multiple is less than 1, and each time a preset number of object stacks are shrunk.

[0119] Based on the above embodiments, as a preferred implementation manner, it further includes: a second setting module, configured to set the maximum threshold and the minimum threshold of the capacity of the memory pool cache layer; correspondingly, the memory pool cache layer capacity adjustment module is further configured to: when the capacity of the memory pool cache layer is greater than the maximum threshold of the capacity of the memory pool cache layer, shrink the capacity of the memory pool cache layer; wherein, each time a preset number of object stacks are shrunk; when the capacity of the memory pool cache layer is less than the minimum threshold of the capacity of the memory pool cache layer, expand the capacity of the memory pool cache layer; wherein, each time a preset number of object stacks are expanded.

[0120] Based on the above embodiments, as a preferred implementation manner, it further includes: an initialization module, configured to fill the memory pool cache layer when initializing the memory pool cache layer, so that the capacity of the memory pool cache layer reaches the minimum threshold of the capacity of the memory pool cache layer.

[0121] Based on the above embodiments, as a preferred implementation manner, it further includes: a first memory recycling module, configured to recycle the object stack with the farthest recent access time when the number of object stacks included in the memory pool cache layer reaches the maximum number threshold of object stacks.

[0122] Based on the above embodiments, as a preferred implementation manner, it further includes: a second memory recycling module, configured to determine the target memory size requested by the memory allocation request when the memory pool of the target size cannot respond to the memory allocation request; judge whether the heap memory satisfies the memory allocation request; if so, recycle the memory of the target size from the heap memory to respond to the memory allocation request; if not, recycle the memory with a size larger than the target size in ascending order of size until the size of the recycled memory reaches the third preset multiple of the target memory size; wherein, the third preset multiple is greater than 1.

[0123] Regarding the device in the above embodiments, the specific manners in which each module performs operations have been described in detail in the embodiments related to the method, and will not be elaborated herein.

[0124] An embodiment of the present invention further provides an electronic device, Figure 14 It is a structural diagram of an electronic device shown according to an exemplary embodiment.

[0125] A communication interface 1, capable of interacting with other devices such as network devices for information.

[0126] A processor 2, connected to the communication interface 1 to achieve information interaction with other devices, and is used to execute the memory management method provided by the above one or more technical solutions when running a computer program. And the computer program is stored on a memory 3.

[0127] Of course, in practical applications, each component in the electronic device is coupled together through the bus system 4. It can be understood that the bus system 4 is used to implement the connection and communication between these components. In addition to the data bus, the bus system 4 also includes a power bus, a control bus, and a status signal bus. However, for the sake of clear illustration, in Figure 14 all kinds of buses are labeled as the bus system 4.

[0128] The memory 3 in the embodiment of the present invention is used to store various types of data to support the operation of the electronic device. Examples of such data include: any computer program for operating on the electronic device.

[0129] It can be understood that the memory 3 can be a volatile memory or a non-volatile memory, or can include both volatile and non-volatile memories. Among them, the non-volatile memory can be a read-only memory (ROM, Read Only Memory), a programmable read-only memory (PROM, Programmable Read-Only Memory), an erasable programmable read-only memory (EPROM, Erasable Programmable Read-Only Memory), an electrically erasable programmable read-only memory (EEPROM, Electrically Erasable Programmable Read-Only Memory), a ferromagnetic random access memory (FRAM, ferromagnetic random access memory), a flash memory (Flash Memory), a magnetic surface memory, an optical disc, or a compact disc read-only memory (CD-ROM, Compact Disc Read-Only Memory); the magnetic surface memory can be a disk memory or a tape memory. The volatile memory can be a random access memory (RAM, Random Access Memory), which is used as an external cache. By way of example but not limitation, many forms of RAM are available, such as a static random access memory (SRAM, Static Random Access Memory), a synchronous static random access memory (SSRAM, Synchronous Static Random Access Memory), a dynamic random access memory (DRAM, Dynamic Random Access Memory), a synchronous dynamic random access memory (SDRAM, Synchronous Dynamic Random Access Memory), a double data rate synchronous dynamic random access memory (DDR SDRAM, Double Data Rate Synchronous Dynamic Random Access Memory), an enhanced synchronous dynamic random access memory (ESDRAM, Enhanced Synchronous Dynamic Random Access Memory), a sync link dynamic random access memory (SLDRAM, SyncLink Dynamic Random Access Memory), and a direct rambus random access memory (DRRAM, Direct Rambus Random Access Memory).The memory 3 described in the embodiments of the present invention is intended to include, but is not limited to, these and any other suitable types of memories.

[0130] The method disclosed in the above embodiments of the present invention can be applied to or implemented by the processor 2. The processor 2 may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by the integrated logic circuit in the hardware of the processor 2 or by instructions in the form of software. The above-mentioned processor 2 may be a general-purpose processor, a DSP, or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The processor 2 can implement or execute each method, step, and logic block diagram disclosed in the embodiments of the present invention. The general-purpose processor may be a microprocessor or any conventional processor, etc. Combining the steps of the method disclosed in the embodiments of the present invention can be directly embodied as being executed by the hardware decoding processor, or by a combination of the hardware and software modules in the decoding processor. The software module may be located in the storage medium, which is located in the memory 3. The processor 2 reads the program in the memory 3 and combines its hardware to complete the steps of the foregoing method.

[0131] When the processor 2 executes the program, it implements the corresponding processes in each method of the embodiments of the present invention. For the sake of brevity, they are not described herein again.

[0132] The embodiments of the present invention also provide a computer-readable storage medium, in which a computer program is stored. Among them, the computer program is set to execute the steps in any of the above embodiments of the memory management method when running.

[0133] In an exemplary embodiment, the above computer-readable storage medium may include, but is not limited to: USB flash drives, read-only memories (ROM for short), random access memories (RAM for short), mobile hard disks, magnetic disks, or optical disks, etc., various media that can store computer programs.

[0134] The embodiments of the present invention also provide a computer program product. The above computer program product includes a computer program, and when the computer program is executed by the processor 2, it implements the steps in any of the above embodiments of the memory management method.

[0135] The embodiments of the present invention also provide another computer program product, including a non-volatile computer-readable storage medium. The non-volatile computer-readable storage medium stores a computer program, and when the computer program is executed by the processor 2, it implements the steps in any of the above embodiments of the memory management method.

[0136] Those skilled in the art may further realize that the units and algorithm steps of each example described in combination with the embodiments disclosed herein can be implemented by electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described according to functions in the above description. Whether these functions are executed in a hardware or software manner depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods for each specific application to implement the described functions, but such implementation should not be considered as exceeding the scope of the present invention.

[0137] The above has introduced in detail a memory management system, method, device, equipment, medium, and product provided by the present invention. Specific examples are used herein to illustrate the principles and implementation manners of the present invention. The description of the above embodiments is only used to help understand the method and its core idea of the present invention. It should be noted that for those of ordinary skill in the art in this technical field, without departing from the principle of the present invention, several improvements and modifications can be made to the present invention, and these improvements and modifications also fall within the protection scope of the present invention.

Claims

1. A memory management method, characterized in that, Including: Receiving a creation instruction for a custom memory pool, and adding a node corresponding to the custom memory pool in a memory pool management tree based on the size of the custom memory pool; wherein, the memory pool management tree is a binary tree, and nodes in the memory pool management tree are used to manage memory pools of corresponding sizes. The sizes of memory pools managed by nodes in the left subtree of a target node in the memory pool management tree are all smaller than the size of the memory pool managed by the target node, and the sizes of memory pools managed by nodes in the right subtree of the target node are all larger than the size of the memory pool managed by the target node. When the memory pool management tree does not meet the balance condition, performing balance reconstruction on an adjustment subtree according to the construction method of the memory pool management tree to adjust the memory pool management tree so that the adjusted memory pool management tree meets the balance condition again; wherein, the balance condition is that the height difference between subtrees of the memory pool management tree is less than or equal to a preset value. The adjustment subtree includes an adjustment node and the parent node of the adjustment node, and the adjustment node is the node that causes the memory pool management tree not to meet the balance condition.

2. The memory management method according to claim 1, wherein Before receiving the creation instruction for the custom memory pool, it further includes: Creating nodes corresponding to each predefined memory pool, sorting the nodes corresponding to each predefined memory pool in ascending order according to the size of the corresponding predefined memory pool, selecting the middle node in the sorting result or the node with the largest size of the corresponding predefined memory pool among the middle two nodes as the root node of the memory pool management tree, and determining the root node as the current node. Determining the left subtree node set and the right subtree node set of the current node; wherein, the left subtree node set of the current node includes left subtree nodes, and the sizes of the predefined memory pools corresponding to the left subtree nodes are smaller than the size of the predefined memory pool corresponding to the current node. The right subtree node set of the current node includes right subtree nodes, and the sizes of the predefined memory pools corresponding to the right subtree nodes are larger than the size of the predefined memory pool corresponding to the current node. Sorting the left subtree nodes in the left subtree node set in ascending order according to the size of the corresponding predefined memory pool, selecting the middle node in the sorting result or the node with the largest size of the corresponding predefined memory pool among the middle two nodes as the root node of the left subtree, sorting the left subtree nodes in the right subtree node set in ascending order according to the size of the corresponding predefined memory pool, and selecting the middle node in the sorting result or the node with the largest size of the corresponding predefined memory pool among the middle two nodes as the root node of the right subtree. Redetermining the root node of the left subtree and the root node of the right subtree as the current node, and re-entering the step of determining the left subtree node set and the right subtree node set of the current node until the memory pool management tree contains all nodes corresponding to the predefined memory pools.

3. The memory management method according to claim 1, wherein Performing balance reconstruction on the adjustment subtree according to the construction method of the memory pool management tree, including: Sort the nodes in the subtree to be adjusted in ascending order according to the sizes of the corresponding memory pools, and select the middle node in the sorting result or the node with the largest corresponding memory pool size among the two middle nodes as the root node of the reconstructed subtree, and determine the root node as the current node; Determine the left subtree node set and the right subtree node set of the current node; wherein, the left subtree node set of the current node includes left subtree nodes, and the nodes whose corresponding memory pool sizes are smaller than the corresponding memory pool size of the current node, and the right subtree node set of the current node includes right subtree nodes, and the nodes whose corresponding memory pool sizes are larger than the corresponding memory pool size of the current node; Sort the left subtree nodes in the left subtree node set in ascending order according to the sizes of the corresponding memory pools, and select the middle node in the sorting result or the node with the largest corresponding memory pool size among the two middle nodes as the root node of the left subtree, and sort the left subtree nodes in the right subtree node set in ascending order according to the sizes of the corresponding memory pools, and select the middle node in the sorting result or the node with the largest corresponding memory pool size among the two middle nodes as the root node of the right subtree; Redetermine the root node of the left subtree and the root node of the right subtree as the current node, and re-enter the step of determining the left subtree node set and the right subtree node set of the current node until all the nodes in the subtree to be adjusted are included in the reconstructed subtree.

4. The memory management method according to claim 1, wherein Further includes: When receiving a deletion instruction for a custom memory pool to be deleted, determine whether there are child nodes for the node corresponding to the custom memory pool to be deleted in the memory pool management tree; If so, perform a balance reconstruction on the child nodes to obtain a target subtree, delete the node corresponding to the custom memory pool to be deleted in the memory pool management tree, and replace the position corresponding to the node corresponding to the custom memory pool to be deleted in the memory pool management tree with the target subtree; If not, directly delete the node corresponding to the custom memory pool to be deleted in the memory pool management tree.

5. The memory management method according to claim 1, wherein In each memory pool of different sizes, the memory pool cache layer includes an empty object stack queue and a full object stack queue. The empty object stack queue includes multiple empty object stacks, and the full object stack queue includes multiple full object stacks. The multiple full object stacks are sorted in descending order according to the most recent access time, and adjacent full object stacks are connected by a doubly linked list. The empty object stacks in the empty object stack queue are managed by an array, and the key of the element in the array is the identifier of the empty object stack, and the value of the element is the pointer to the corresponding full object stack in the full object stack queue.

6. The memory management method according to claim 5, wherein Further includes: Determine the access frequency and allocation requirements of the object stack, and adjust the capacity of the object stack based on the access frequency and allocation requirements of the object stack.

7. The memory management method according to claim 6, wherein Determining the access frequency and allocation requirements of the object stack includes: Record the number of times the objects in the object stack are called during the adjustment period; wherein, the adjustment period includes multiple preset time intervals; Calculate the access frequency of the object stack based on the total number of calls and the time decay factor of all objects in the object stack within each preset time interval; Among them, the calculation formula for the access frequency is: ; wherein, is the access frequency, is the total number of times all objects in the object stack are called within the i-th preset time interval, , n is the number of preset time intervals included in the adjustment period, is the time decay factor, ; Record the number of allocation requests and the number of allocation failures of the object stack during the adjustment period, and use the ratio between the number of allocation failures and the number of allocation requests as the allocation demand of the object stack.

8. The memory management method according to claim 6, wherein Adjust the capacity of the object stack based on the access frequency and allocation demand of the object stack, including: When the access frequency of the object stack is greater than the first preset access frequency threshold and the allocation demand of the object stack is greater than the first allocation demand threshold, increase the capacity of the object stack by the first preset ratio; When the access frequency of the object stack is less than the second preset access frequency threshold and the allocation demand of the object stack is less than the second allocation demand threshold, reduce the capacity of the object stack by the second preset ratio; Among them, the first preset access frequency threshold is greater than the second preset access frequency threshold, and the first allocation demand threshold is greater than the second allocation demand threshold.

9. The memory management method according to claim 8, wherein It also includes: Set the maximum threshold and minimum threshold of the object stack capacity; Correspondingly, after increasing the capacity of the object stack by the first preset ratio, it also includes: If the increased capacity of the object stack is greater than the maximum threshold of the object stack capacity, set the capacity of the object stack to the maximum threshold of the object stack capacity; Correspondingly, after reducing the capacity of the object stack by the second preset ratio, it also includes: If the reduced capacity of the object stack is less than the minimum threshold of the object stack capacity, set the capacity of the object stack to the minimum threshold of the object stack capacity.

10. The memory management method according to claim 5, wherein It also includes: Calculate the memory pressure index of the memory pool cache layer based on the memory allocation rate and memory allocation failure rate of the memory pool cache layer; where the memory allocation rate is used to describe the number of memory object allocations per unit time, and the memory allocation failure rate is used to describe the proportion of the number of memory allocation failures to the total number of memory allocations; Adjust the capacity of the memory pool cache layer based on the memory pressure index.

11. The memory management method according to claim 10, wherein, Calculating the memory pressure index of the memory pool cache layer based on the memory allocation rate and memory allocation failure rate of the memory pool cache layer includes: Calculate the memory pressure index of the memory pool cache layer based on the memory allocation rate of the memory pool cache layer in the current period, the historical average memory allocation rate, and the memory allocation failure rate of the memory pool cache layer in the current period; Among them, the calculation formula for the memory pressure index is: ; Where M is the memory pressure index, is the memory allocation rate of the memory pool cache layer in the current cycle, is the historical average memory allocation rate, is the number of memory allocation failures of the memory pool cache layer in the current cycle, is the total number of memory allocations of the memory pool cache layer in the current cycle, is the memory allocation failure rate of the memory pool cache layer in the current cycle, and are adjustment parameters, .

12. The memory management method according to claim 10, wherein Adjusting the capacity of the memory pool cache layer based on the memory pressure index includes: Calculate the target capacity of the memory pool cache layer based on the current capacity of the memory pool cache layer and the memory pressure index; wherein, the calculation formula of the target capacity is: , is the target capacity, is the current capacity, and M is the memory pressure index; Calculate a new capacity value based on the current capacity and the target capacity of the memory pool cache layer; wherein, the calculation formula for the new capacity value is: , is the new capacity value, is the influence weight for controlling the historical capacity; When the new capacity value is greater than the first preset multiple of the current capacity, expand the capacity of the memory pool cache layer; where the first preset multiple is greater than 1, and a preset number of object stacks are expanded each time; When the new capacity value is less than the second preset multiple of the current capacity, shrink the capacity of the memory pool cache layer; where the second preset multiple is less than 1, and a preset number of object stacks are shrunk each time.

13. The memory management method according to claim 10, wherein It also includes: Set the maximum threshold and minimum threshold of the memory pool cache layer capacity; When the capacity of the memory pool cache layer is greater than the maximum threshold of the capacity of the memory pool cache layer, shrink the capacity of the memory pool cache layer; wherein, shrink a preset number of object stacks each time. When the capacity of the memory pool cache layer is less than the minimum threshold of the capacity of the memory pool cache layer, expand the capacity of the memory pool cache layer; wherein, expand a preset number of object stacks each time.

14. The memory management method according to claim 13, wherein Further included: When initializing the memory pool cache layer, fill the memory pool cache layer so that the capacity of the memory pool cache layer reaches the minimum threshold of the capacity of the memory pool cache layer.

15. The memory management method according to claim 5, wherein Further included: When the number of object stacks included in the memory pool cache layer reaches the maximum number threshold of object stacks, recycle the object stack with the farthest recent access time.

16. The memory management method according to claim 1, wherein Further included: When the memory pool of the target size cannot respond to a memory allocation request, determine the target memory size requested by the memory allocation request. Judge whether the heap memory satisfies the memory allocation request. If so, recycle the memory of the target size from the heap memory to respond to the memory allocation request. If not, recycle the memory with a size larger than the target size in ascending order of size until the size of the recycled memory reaches the third preset multiple of the target memory size; wherein, the third preset multiple is greater than 1.

17. A memory management device, characterized in that, Included: An adding module, configured to receive a creation instruction of a custom memory pool, and add a node corresponding to the custom memory pool in a memory pool management tree based on the size of the custom memory pool; wherein, the memory pool management tree is a binary tree, and the nodes in the memory pool management tree are used to manage memory pools of corresponding sizes. The sizes of the memory pools managed by the nodes in the left subtree of the target node in the memory pool management tree are all smaller than the size of the memory pool managed by the target node, and the sizes of the memory pools managed by the nodes in the right subtree of the target node are all larger than the size of the memory pool managed by the target node. A restructuring module, configured to when the memory pool management tree does not meet the balance condition, perform balance restructuring on the subtree to be adjusted according to the construction method of the memory pool management tree, so as to adjust the memory pool management tree to make the adjusted memory pool management tree meet the balance condition again; wherein, the balance condition is that the height difference of the subtrees of the memory pool management tree is less than or equal to a preset value, and the subtree to be adjusted includes the node to be adjusted and the parent node of the node to be adjusted, and the node to be adjusted is the node that makes the memory pool management tree not meet the balance condition.

18. An electronic device, characterized in that, Included: A memory, configured to store a computer program. A processor, configured to implement the steps executed by the memory management method according to any one of claims 1 to 16 when executing the computer program.

19. A computer-readable storage medium, characterized in that, A computer program is stored on the computer-readable storage medium, and when the computer program is executed, it implements the steps executed by the memory management method according to any one of claims 1 to 16.

20. A computer program product, characterized in that, Included a computer program, and when the computer program is executed, it implements the steps executed by the memory management method according to any one of claims 1 to 16.

Citation Information

Patent Citations

  • Memory management method and device for periodic large big data processing

    CN104090848A

  • Memory pool dynamic management method and device, electronic equipment and storage medium

    CN118916154A

  • Memory allocation method and system, terminal and storage medium

    CN119781979A

  • Method for prefetching recursive data structure traversals

    US20050102294A1

  • Memory allocation method and apparatus based on ion allocator, electronic device, and readable storage medium

    WO2023236930A1