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

Through the dynamic management mechanism of the memory pool management tree, the problems of insufficient memory resource utilization and inflexible management are solved, the flexibility and stability of memory management are achieved, and the memory requirements of different application scenarios are adapted.

CN120407204BActive Publication Date: 2025-09-05LANGCHAO ELECTRONIC INFORMATION IND CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

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

AI Technical Summary

Technical Problem

The memory management system in the prior art cannot dynamically adjust the size of the memory pool, resulting in insufficient utilization of memory resources and inflexible management, making it difficult to adapt to the memory requirements of different application scenarios.

Method used

The dynamic management mechanism of the memory pool management tree is adopted. By creating custom memory pools and binary tree structures, users can flexibly create memory pools of different sizes and perform balanced reconstruction when imbalanced to ensure the balance of the tree.

Benefits of technology

It improves the flexibility and efficiency of memory management, ensures the stability and performance of the memory management structure, and adapts to diverse memory requirements.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120407204B_ABST
    Figure CN120407204B_ABST
Patent Text Reader

Abstract

The present invention discloses a memory management method, apparatus, device, storage medium, and computer program product, relating to the field of computer technology. The method comprises: receiving a creation instruction for a custom memory pool; adding a node corresponding to the custom memory pool in a memory pool management tree based on the size of the custom memory pool; 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; when the memory pool management tree does not meet a balance condition, performing a balanced reconstruction on a 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; 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, the subtree to be adjusted includes a node to be adjusted and a parent node of the node to be adjusted, and the node to be adjusted is a node that causes the memory pool management tree to not meet the balance condition. The present invention improves the flexibility of memory management.
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 specifically, to a memory management method, apparatus and device, storage medium, and computer program product. Background Art

[0002] In computer systems, memory management is a key factor affecting system performance. In related technologies, memory management systems are typically based on predefined memory pools, whose sizes are determined during system initialization and cannot be adjusted dynamically. This static memory management approach has many limitations. First, the sizes of predefined memory pools are fixed and cannot be dynamically adjusted based on actual workloads. This results in underutilized memory resources in some cases and potentially insufficient memory in others. Second, memory management systems have difficulty managing custom memory pools, preventing users from flexibly creating and managing memory pools of different sizes.

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

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

[0005] To achieve the above-mentioned object, the present invention provides a memory allocation method, comprising: 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 the nodes in the memory pool management tree are used to manage memory pools of corresponding sizes, the size of the memory pool managed by each node in the left subtree of a target node in the memory pool management tree is smaller than the size of the memory pool managed by the target node, and the size of the memory pool managed by each node in the right subtree of the target node is larger than the size of the memory pool managed by the target node; when the memory pool management tree does not meet a balance condition, balancing and reconstructing a subtree to be adjusted according to a 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, 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 to not meet the balance condition.

[0006] To achieve the above-mentioned objectives, the present invention provides a memory allocation device, comprising: an adding module, configured to receive a creation instruction for a custom memory pool, and to 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, and the size of the memory pool managed by each node in the left subtree of a target node in the memory pool management tree is smaller than the size of the memory pool managed by the target node, and the size of the memory pool managed by each node in the right subtree of the target node is larger than the size of the memory pool managed by the target node; and a reconstruction module, configured to, when the memory pool management tree does not meet a balance condition, balance and reconstruct a subtree to be adjusted according to a construction method of the memory pool management tree, so as 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, 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 to not meet the balance condition.

[0007] To achieve the above object, the present invention provides an electronic device, comprising: a memory for storing a computer program; and a processor for implementing the steps of the above memory allocation method when executing the computer program.

[0008] To achieve the above objectives, the present invention provides a computer-readable storage medium having a computer program stored thereon. 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, including a computer program, which implements the steps of the above memory allocation method when executed by a processor.

[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, the 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 of the subtrees of the memory pool management tree exceeds a preset value, the nodes that cause 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 balancing 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 structure in traditional memory management technology, and improves the flexibility of memory management. The present invention also discloses a memory management device and an electronic device, a computer-readable storage medium, and a computer program product, which can also achieve the above-mentioned technical effects.

[0011] It should be understood that the foregoing general description and the following detailed description are exemplary only and are not restrictive of the invention. BRIEF DESCRIPTION OF THE DRAWINGS

[0012] In order to more clearly illustrate the embodiments of the present invention, the following is a brief introduction to the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.

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

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

[0015] Figure 3 The figure is a schematic diagram showing a method of adding a node corresponding to a custom memory pool in a memory pool management tree according to an exemplary embodiment.

[0016] Figure 4 The figure is a schematic diagram showing a balanced reconstruction of a memory pool management tree according to an exemplary embodiment.

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

[0018] Figure 6 The figure is a schematic diagram showing links between empty object stacks and full object stacks according to an exemplary embodiment.

[0019] Figure 7 The figure is a flowchart of a method for adjusting the capacity of an object stack according to an exemplary embodiment.

[0020] Figure 8 The present invention is a flowchart of a method for adjusting the capacity of a memory pool cache layer according to an exemplary embodiment.

[0021] Figure 9 This is a flowchart of the memory management method in the application embodiment provided by the present invention.

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

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

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

[0025] Figure 13 The figure is a structural diagram of a memory management device according to an exemplary embodiment.

[0026] Figure 14 The figure is a structural diagram of an electronic device according to an exemplary embodiment. DETAILED DESCRIPTION

[0027] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of them. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making any creative efforts shall fall within the scope of protection of the present invention.

[0028] It should be noted that, in the description of the present invention, the terms "comprises," "includes," or any other variations thereof are intended to encompass non-exclusive inclusion, such that a process, method, article, or apparatus comprising a series of elements includes not only those elements but also other elements not explicitly listed, or elements inherent to such process, method, article, or apparatus. The terms "first," "second," etc., in the present invention are used to distinguish similar objects, and are not used to describe a particular order or precedence.

[0029] In order to enable those skilled in the art to better understand the solutions of the present invention, the present invention is further described in detail below with reference to the accompanying drawings and specific implementation methods.

[0030] In modern computer systems, the operating system, as the core system software, is responsible for managing and scheduling the computer's various hardware resources to support the efficient operation of applications. Memory management is one of the key tasks of the operating system. With the continuous development of computer technology, the memory requirements of applications are becoming increasingly diverse. This requires the operating system to not only efficiently allocate and reclaim memory, but also be flexible enough to adapt to memory usage patterns in different application scenarios.

[0031] In related technologies, memory management in operating systems typically uses a static allocation approach, where the size of the memory pool is determined during system initialization and cannot be dynamically adjusted based on the actual workload. This static memory management approach has many limitations. First, the size of the predefined memory pool is fixed and cannot be dynamically adjusted based on the actual workload, resulting in underutilized memory resources in some cases and potentially insufficient memory in other cases. Second, operating systems have difficulty managing custom memory pools, and users cannot flexibly create and manage memory pools of different sizes. Furthermore, with the increase in multitasking and high-concurrency application scenarios, operating systems need to manage memory resources more efficiently to meet the dynamic memory demands of different processes and applications.

[0032] In this context, the present invention proposes a memory management method, which aims to solve the problems existing in the memory management of operating systems 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 needs by introducing a dynamic management mechanism of custom memory pools and memory pool management trees, while ensuring the stability and efficiency of the memory management structure through a balanced reconstruction mechanism. In addition, the method further optimizes the allocation and recovery process of memory resources through dynamic capacity adjustment of the object stack and the dynamic capacity adjustment strategy of the memory pool cache layer, reduces memory fragmentation, and improves memory utilization. This memory management method can better adapt to the high requirements of modern operating systems for memory management and provide strong support for the efficient operation of the operating system.

[0033] The embodiment of the present invention provides a memory management method, and the method is described in detail in conjunction with the execution process of the memory management method. Figure 1 , a flowchart of a memory management method according to an exemplary embodiment is shown.

[0034] S101: Receive a creation instruction for 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, and the size of the memory pool managed by each node in the left subtree of the target node in the memory pool management tree is smaller than the size of the memory pool managed by the target node, and the size of the memory pool managed by each node in the right subtree of the target node is larger than the size of the memory pool managed by the target node.

[0035] As a feasible implementation method, this embodiment also includes: creating a memory pool management tree based on the size of each predefined memory pool; wherein the memory pool management tree is a binary tree that meets the balance condition. Among them, the predefined memory pool is a memory pool predefined when the system is initialized, and its size is fixed, which is used to meet common memory allocation requirements. The size of the memory pool refers to the size of the memory objects therein. The memory pool management tree is a data structure for managing memory pools. This embodiment adopts a binary tree that meets the balance condition. It is a special binary tree, in which 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, which is used to manage a memory pool of a specific size.

[0036] In practice, we first collect the sizes of all predefined memory pools and then construct a memory pool management tree based on these sizes. During this construction process, we select an appropriate root node, typically a memory pool of intermediate size, to ensure tree balance. For each node, its left subtree contains all memory pools smaller than the node's size, while its right subtree contains all memory pools larger than the node's size. Recursively, we select an appropriate root node for each subtree, ultimately constructing the entire memory pool management tree.

[0037] As a feasible implementation method, a memory pool management tree is created based on the size of each predefined memory pool, including: creating a node corresponding to each predefined memory pool, sorting the nodes corresponding to each predefined memory pool from small to large 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 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 the left subtree node, and the size of the predefined memory pool corresponding to the left subtree node is 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 the right subtree node, and the size of the predefined memory pool corresponding to the right subtree node is larger than The node of the size of the predefined memory pool corresponding to the current node; sort the left subtree nodes in the left subtree node set from small to large 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 two middle nodes as the root node of the left subtree, sort the left subtree nodes in the right subtree node set from small to large 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 two middle nodes as the root node of the right subtree; redefine 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 the memory pool management tree contains nodes corresponding to all predefined memory pools.

[0038] In specific implementations, a corresponding node is first created for each predefined memory pool. These nodes serve as the basic building blocks of the memory pool management tree. Next, these nodes are sorted from smallest to largest according to the size of their corresponding predefined memory pools. This facilitates the selection of appropriate nodes as the tree root in subsequent steps, thereby constructing a balanced binary tree. From the sorted node list, the middle node or the larger of the two middle nodes is selected as the root node of the memory pool management tree. This step ensures 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. Next, the left and right subtree node sets of the current node need to be determined. Specifically, all nodes are traversed, and nodes with memory pool sizes smaller than the current node's memory pool size are assigned to the left subtree node set, while nodes with memory pool sizes larger than the current node's memory pool size are assigned to the right subtree node set. Next, the same operation is performed on the left and right subtree node sets. For the nodes in the left subtree node set, sort them from small to large according to the size of their corresponding predefined memory pools, and then select the middle node in the sorted result or the larger node between the two middle nodes as the root node of the left subtree. Similarly, for the nodes in the right subtree node set, sort them from small to large according to the size of their corresponding predefined memory pools, and select the middle node in the sorted result or the larger node between the two middle nodes as the root node of the right subtree. Finally, mark the root node of the left subtree and the root node of the right subtree as the new current node respectively, and then repeat the above steps for each new current node, that is, determine its left subtree node set and right subtree node set, and continue to build 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 predefined memory pools from small to large are: 8byte, 16byte, 32byte, 64byte, 128byte, 256byte, 512byte, 1KB, 4KB, 8KB, 16KB, 32KB. Take 512byte as the root node, and the left subtree is: 8byte, 16byte, 32byte, 64byte, 128byte, 256byte. Then the root node of the left subtree is 64byte; similarly, the root node of the right subtree is 8KB. And so on, a complete memory pool management tree is constructed as follows: Figure 2 shown.

[0040] As can be seen, this step, by constructing a balanced binary tree as the memory pool management tree, allows for rapid location and management of memory pools of varying sizes. The balanced binary tree structure ensures efficient operations, reducing the time complexity of lookups, insertions, and deletions. This improves memory management efficiency, particularly when the number of memory pools is large, significantly reducing operation time and improving system performance.

[0041] In step S101, after the system receives a user-issued instruction 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 based on specific needs. 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 into the memory pool management tree at a suitable location. The insertion process follows the rules of a binary search tree, i.e., 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, a user needs to create a custom memory pool with an object size of 18KB and use standard binary search tree (BST) logic to insert custom size nodes. Tree structure traversal: Starting from the root node (256 bytes): 18KB>256byte 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 Need to be inserted into the right subtree of the 16KB node, such as Figure 3 shown.

[0043] As you can see, this step supports the creation of user-defined memory pools, greatly increasing the flexibility of memory management. Users can dynamically create memory pools of varying sizes based on different application scenarios and requirements, without being restricted to predefined memory pool sizes. This flexibility enables memory management to better adapt to diverse workloads, improve memory resource utilization, and meet the needs of diverse 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: memory pool size, which is used to clarify the size of the memory block managed by the node; the subtree pointer contains pointers to the left subtree and the right subtree. The left subtree corresponds to a smaller memory pool, and the right subtree corresponds to a larger memory pool, forming an ordered tree structure, which is convenient for quickly finding and locating memory pools of different sizes. At the same time, the node is associated with a linked list head pointer of a memory pool of the current size to manage the memory pool of that size; the node-level spin lock is used for concurrency control to ensure the atomicity and consistency of memory pool operations (such as allocation, recycling, insertion, deletion, etc.) in a multi-threaded environment, avoid data competition and conflicts, and ensure the correctness and stability of memory management.

[0045] As a feasible implementation method, this embodiment also 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 a child node; if so, performing balanced reconstruction on the child node 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 specific implementations, upon receiving a command to delete a custom memory pool, the system first checks whether the node corresponding to the custom memory pool to be deleted has any child nodes in the memory pool management tree. If the node does have child nodes, the system initiates a rebalancing and reconstruction process. This process ensures that the memory pool management tree remains balanced after the node is deleted, preventing the tree structure from becoming unbalanced due to the node removal, which could affect memory management efficiency. During the rebalancing and reconstruction, the system processes the child nodes of the node to be deleted and reorganizes them using a specific algorithm to construct a new balanced subtree, 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 executes the deletion operation, removing the node corresponding to the custom memory pool to be deleted from the memory pool management tree. The system then replaces the target subtree with the original location of the node to be deleted, completing the deletion and reconstruction process. In this way, the memory pool management tree remains balanced after the node is deleted, ensuring efficient and stable memory management operations. If the node to be deleted has no children, it is directly deleted without the need for complex rebalancing operations. This is because deleting a node without children does not affect the balance of the memory pool management tree. This direct deletion method simplifies the operation process, improves processing efficiency, and reduces unnecessary computational overhead.

[0047] For example, if you need to delete a 1KB memory pool, start from the root node (512byte), 1KB>512byte Enter the right subtree. At the right subtree root node (8KB), 1KB<8KB Enter the left subtree (4KB). At the 4KB node, 1KB < 4KB Enter the left subtree (1KB) and delete the 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 this embodiment uses this flexible node deletion method to ensure the balance of the memory pool management tree while also improving the overall performance and response speed of the memory pool management system.

[0049] S102: When the memory pool management tree does not meet the balance condition, the subtree to be adjusted is balanced and reconstructed 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, 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.

[0050] The subtree height difference is the difference between the heights of a node's left and right subtrees, and is used to measure the tree's balance. The default value is a pre-set threshold, such as 2. When the subtree height difference exceeds this value, the tree is considered unbalanced. Nodes to be adjusted are those that cause the memory pool management tree to become unbalanced and require adjustment to restore the tree's balance.

[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 balance and needs to be adjusted. In related technologies, 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 structure of the tree. However, these rotation operations may be triggered more 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, if Figure 4 As shown, the nodes to be adjusted that make the memory pool management tree unbalanced are 18KB and 20KB, and their parent node is 16KB. Therefore, the subtree to be adjusted is the subtree containing the nodes 16KB, 18KB, and 20KB.

[0052] During reconstruction, all nodes in the subtree to be adjusted are first collected and reordered according to their corresponding memory pool sizes. Once sorted, a suitable node is selected as the new root node, following the binary tree construction method. Typically, the node in the middle of the sorted nodes is chosen to minimize the height difference between the left and right subtrees, thereby achieving good balance. The same operation is then recursively performed on the left and right subtrees until the entire subtree to be adjusted is reorganized into a balanced binary tree structure.

[0053] As a feasible implementation method, the subtree to be adjusted is balanced and reconstructed according to the construction method of the memory pool management tree, including: sorting the nodes in the subtree to be adjusted from small to large according to the size of the corresponding memory pool, selecting the middle node in the sorting result or the node with the largest size of the corresponding memory pool among the two middle nodes as the root node of the reconstructed subtree, 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 the left subtree node, and the size of the memory pool corresponding to the left subtree node is smaller than the size of the memory pool corresponding to the current node, and the right subtree node set of the current node includes the right subtree node, and the size of the memory pool corresponding to the right subtree node is larger than the size of the memory pool corresponding to the current node. The left subtree nodes in the left subtree node set are sorted from small to large according to the sizes of the corresponding memory pools, and the middle node in the sorted result or the node with the largest size of the corresponding memory pool among the two middle nodes is selected as the root node of the left subtree. The left subtree nodes in the right subtree node set are sorted from small to large according to the sizes of the corresponding memory pools, and the middle node in the sorted result or the node with the largest size of the corresponding memory pool among the two middle nodes is selected as the root node of the right subtree. The root node of the left subtree and the root node of the right subtree are re-determined as the current node, and the step of determining the left subtree node set and the right subtree node set of the current node is re-entered until the reconstructed subtree contains all the nodes in the subtree to be adjusted.

[0054] In a specific implementation, the nodes in the subtree to be adjusted are first sorted from smallest to largest according to the size of their corresponding memory pools. From the sorted node list, the middle node or the larger of 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. Next, the left and right subtree node sets of the current node need to be determined. Specifically, all nodes are traversed, and nodes with memory pool sizes smaller than the current node's memory pool size are assigned to the left subtree node set, while nodes with memory pool sizes larger than the current node's memory pool size are assigned to the right subtree node set. Next, the same operation is performed on the left and right subtree node sets. The nodes in the left subtree node set are sorted from smallest to largest according to the size of their corresponding memory pools. The middle node or the larger of the two middle nodes in the sorted result is then selected as the root node of the left subtree. Similarly, the nodes in the right subtree node set are sorted from smallest to largest according to the size of their corresponding memory pools. The middle node or the larger of the two middle nodes in the sorted result is selected as the root node of the right subtree. Finally, the root node of the left subtree and the root node of the right subtree are marked as the new current node, respectively. The above steps are then repeated for each new current node, that is, its left subtree node set and right subtree node set are determined, and the subtree construction continues 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, thereby restoring the balance of the entire memory pool management tree. This reconstruction method effectively avoids the frequent adjustment problems that may be caused by traditional rotation operations, reducing the number and complexity of adjustments. At the same time, it can ensure that the height of the tree remains within a reasonable range, thereby 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 an 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 a creation instruction for a custom memory pool is received, the 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 of the subtrees of the memory pool management tree exceeds a preset value, the nodes that cause 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 balancing 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 structure in traditional memory management technology, and improves the flexibility of memory management.

[0057] The memory management method provided in the above embodiment is applied to a memory management system, the structure of which is shown in FIG. Figure 5As shown, the system comprises an interface layer, a memory pool layer, and heap memory. The memory pool layer includes multiple memory pools. Memory pooling is a memory management technology that preallocates a large block of memory space and divides it into multiple fixed-size memory objects for reuse by the system or applications during runtime. This avoids frequent calls to the operating system's memory allocation and deallocation functions, reduces memory fragmentation, and improves memory allocation and deallocation efficiency. Specifically, the size of a memory pool is the size of the memory objects within it. A memory object is the basic allocation unit within the memory pool and the specific entity that an application operates on when requesting or releasing memory from the memory pool. The memory pool comprises 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: a currently loaded object stack and a standby object stack. Within memory pool management, an object stack is a collection of multiple memory objects of the same size organized in a stack structure. Each object stack contains the same size, with the allocatable memory object at the top of the stack. Memory objects are removed from the top of the stack when allocated and returned to the top when released. This last-in, first-out (LIFO) principle facilitates efficient management of reusable memory objects. CPU cores can quickly access the currently loaded object stack to allocate and release memory objects without cross-core contention, improving cache and memory allocation speeds and reducing access latency. The memory pool cache layer, located between the per-CPU layer and the free list layer, serves as an intermediate layer for memory pool management. It stores full and empty object stacks, and each interaction with the per-CPU layer is performed on the basis of the entire object stack, improving efficiency. The free list layer obtains memory blocks consisting of multiple memory pages from the heap layer and divides them into fixed-size memory objects. The free list layer is responsible for interacting with the operating system's heap layer. The heap layer is the operating system's dynamically allocated memory area. Programs request memory releases at runtime, but frequent operations can lead to fragmentation and high management overhead. Therefore, the memory pool often pre-allocates large blocks of memory from the heap and then breaks them down into memory objects for application use. This reduces direct heap operations, improving memory management efficiency and system performance.

[0058] As a feasible implementation method, 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 from near to far according to the most recent access time. Adjacent full object stacks are connected by a bidirectional 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 of the full object stack corresponding to the object stack in the full object stack queue.

[0059] In the specific implementation, the memory pool cache layer combines the hash table with the bidirectional linked list to implement an efficient LRU (Least Recently Used) cache mechanism. Figure 6 As shown, full object stacks are linked by doubly linked lists, while empty object stacks can be linked by arrays. Specifically, a hash table is used for fast location. Its key is the unique identifier of a memory object stack, and its value is a pointer to the corresponding object stack in the doubly linked list. The doubly linked list maintains the order of access to memory object stacks. The head of the list always points to the most recently accessed memory object stack, while the tail points to the least recently accessed memory object stack. When a new memory object stack is accessed, the hash table is first used to quickly locate the node for that object stack in the doubly linked list. This node is then moved from its current position to the head of the linked list, indicating that it has been recently accessed. If the cache level is full and a new memory object stack needs to be inserted, the system removes the least recently accessed object stack from the tail of the linked list and updates the hash table to free up space for the new object stack. This design ensures that lookup, insertion, and deletion operations complete in near-constant time, achieving efficient and stable cache management. Furthermore, the least-recently-used (LRU) strategy ensures that the cache always retains the memory object stack that is most likely to be accessed again.

[0060] This embodiment introduces a method for adjusting the capacity of an object stack, such as Figure 7 shown.

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

[0062] In this step, the object stack's access frequency and allocation demand are determined. Access frequency refers to the number of times an object stack is accessed within a specific time interval, reflecting its activity level. Allocation demand, on the other hand, is a comprehensive assessment of the number of allocation requests and allocation failures for an object stack within a certain timeframe. A higher number of allocation failures indicates that the object stack cannot meet its memory allocation needs within its current capacity and requires expansion. By compiling this data, the system can accurately assess the actual usage of each object stack, providing a basis for subsequent capacity adjustments.

[0063] As a feasible implementation, determining the access frequency of an object stack includes: recording the number of times an object in the object stack is called during an adjustment period; wherein the adjustment period includes a plurality of preset time intervals; and calculating the access frequency of the object stack based on the total number of times all objects in the object stack are called during each preset time interval and a time decay factor; wherein the calculation formula for the access frequency is: ;in, 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 cycle, is the time decay factor, .

[0064] In the specific implementation, an access counter is maintained for the object stack of each memory pool, which records in detail the specific number of times the object in the object stack is called to the per-CPU layer. Set a preset time interval , for example, every 10 seconds, the access counter is sampled regularly, and the access frequency of each object stack is calculated. The access frequency is calculated by dividing the number of accesses to the object stack in the preset time interval by the length of the preset time interval. In order to more accurately reflect the recent access to memory objects, a time decay factor is introduced. The role of this factor is to make newer accesses have a greater impact on the access frequency, thereby making the system more sensitive to changes in access to memory objects. Reasonably set an adjustment cycle , for example 1 minute, within one adjustment cycle includes The total number of times all objects in the object stack are called during the i-th preset time interval is , introducing the time decay factor , to more accurately reflect recent access to memory objects.

[0065] As a feasible implementation, determining the allocation requirement of the object stack includes: recording the number of allocation requests and allocation failures of the object stack during the adjustment period, and using the ratio of the number of allocation failures to the number of allocation requests as the allocation requirement of the object stack.

[0066] In the specific implementation, the number of allocation requests for the object stack of each memory pool within a certain time range is counted, and the number of allocation failures is accurately recorded. Allocation failure refers to the situation where the object stack cannot meet the memory allocation demand and needs to obtain memory from the memory pool cache layer or free list layer. The object stack with more allocation failures needs to be expanded to a certain extent. The number of allocation requests within r, , while accurately recording the number of allocation failures In order to measure the allocation demand of the stack, an allocation demand indicator is defined. Here, the number of allocation failures can be used to determine whether capacity expansion is needed, and the allocation demand D can be further quantified. ,When D is larger, it means that the proportion of allocation failure is higher, and the object stack needs to increase its capacity.

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

[0068] In this step, the object stack capacity is dynamically adjusted based on its access frequency and allocation needs. Specifically, for object stacks with high access frequency and large allocation needs, the system will appropriately increase their capacity to better meet memory allocation needs. Conversely, for object stacks with low access frequency and small allocation needs, the system will appropriately reduce their capacity to achieve efficient memory reclamation.

[0069] As a feasible implementation method, the capacity of the object stack is adjusted based on the access frequency and allocation demand of the object stack, including: when the access frequency of the object stack is greater than a first preset access frequency threshold and the allocation demand of the object stack is greater than the first allocation demand threshold, the capacity of the object stack is increased by a first preset proportion; when the access frequency of the object stack is less than a second preset access frequency threshold and the allocation demand of the object stack is less than the second allocation demand threshold, the capacity of the object stack is reduced by a second preset proportion; wherein 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.

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

[0071] As a feasible implementation, the method further includes: setting a maximum threshold and a minimum threshold for the object stack capacity; accordingly, after increasing the capacity of the object stack by a first preset ratio, the method further includes: if the increased capacity of the object stack is greater than the maximum threshold for the object stack capacity, setting the capacity of the object stack to the maximum threshold for the object stack capacity; accordingly, after reducing the capacity of the object stack by a second preset ratio, the method further includes: if the reduced capacity of the object stack is less than the minimum threshold for the object stack capacity, setting the capacity of the object stack to the minimum threshold for the object stack capacity. In a specific implementation, the maximum threshold and the minimum threshold for the object stack capacity are set, and the object stack capacity should be adjusted between the maximum threshold and the minimum threshold for the object stack capacity.

[0072] This demonstrates that this embodiment accurately assesses object stack access frequency and allocation requirements, providing real-time insights into each object stack's actual usage, thereby enabling appropriate capacity adjustment decisions. This dynamic adjustment of object stack capacity not only reduces memory allocation failures, significantly improving the efficiency and flexibility of memory management, but also avoids wasted memory resources and enhances memory utilization.

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

[0074] S301: 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; 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 ratio of memory allocation failures to the total number of memory allocations.

[0075] In this step, the memory pressure index is calculated by monitoring the memory allocation rate and 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. This indicator can intuitively reflect the intensity of the current system's demand for memory resources. The memory allocation failure rate refers to the ratio of memory allocation failures to the total number of memory allocations. It directly reflects the current level of tension in 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 combining 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 adjustments, allowing the system to more accurately respond to dynamic changes in memory resources.

[0076] As a feasible implementation method, a memory pressure index of the memory pool cache layer is calculated 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: ; Where M is the memory pressure index, The memory allocation rate of the memory pool cache layer in the current cycle, is the historical average memory allocation rate, The number of memory allocation failures of the memory pool cache layer in the current cycle. The total amount of memory allocated for the memory pool cache layer in the current cycle. The memory allocation failure rate of the memory pool cache layer in the current cycle. 、 To adjust the parameters, .

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

[0078] In this step, the capacity of the memory pool cache layer is dynamically adjusted based on 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 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 memory resources are relatively abundant. At this time, the system can appropriately reduce the capacity of the memory pool cache layer, thereby releasing excess memory resources and improving the utilization 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, avoids over-allocation or under-allocation of memory resources, and realizes refined management of memory resources.

[0079] As a feasible implementation, adjusting the capacity of the memory pool cache layer based on the memory pressure index includes: calculating a 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 target capacity is calculated as follows: , is the target capacity, is the current capacity, M is the memory pressure index; the new capacity value is calculated based on the current capacity and target capacity of the memory pool cache layer; the calculation formula for the new capacity value is: , is the new capacity value, To control the impact weight of historical capacity, ; When the new capacity value is greater than the first preset multiple of the current capacity, the capacity of the memory pool cache layer is expanded; wherein, the first preset multiple is greater than 1, and the 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, the capacity of the memory pool cache layer is shrunk; wherein, the second preset multiple is less than 1, and the preset number of object stacks are shrunk each time.

[0080] In practice, the target capacity is calculated based on the current capacity of the memory pool cache layer and the memory pressure index. A higher memory pressure index indicates a higher target capacity, and vice versa. Next, the new capacity is calculated based on the current and target capacities. By comprehensively considering the current and target capacities, and weighting historical capacity, a more reasonable new capacity value is smoothly calculated. EWMA (Excessive Wavelength Memory) is used to suppress short-term fluctuations, preventing unstable adjustments to the memory pool cache layer capacity caused by drastic changes in the target capacity.

[0081] When the calculated new capacity value is greater than a first preset multiple of the current capacity, the system expands the capacity of the memory pool cache layer. This first preset multiple is a value greater than 1 that sets a threshold for expansion, such as 1.2. Each expansion adds a preset number of object stacks to ensure the memory pool cache layer has sufficient capacity to meet the current increase in memory demand. Conversely, when the new capacity value is less than a second preset multiple of the current capacity, the system shrinks the capacity of the memory pool cache layer. The second preset multiple is a value less than 1 that sets a threshold for shrinkage, such as 0.8. Each shrink also reduces a preset number of object stacks, freeing up excess memory resources and improving memory resource utilization. This expansion and shrinking strategy, based on the relationship between the new capacity value and the current capacity multiple, enables the capacity of the memory pool cache layer to dynamically adapt to changes in the system's memory needs, avoiding memory shortages and wasting memory resources, thereby achieving efficient management and flexible allocation of memory resources.

[0082] As a feasible implementation method, this embodiment also includes: setting a maximum threshold and a 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 memory pool cache layer capacity, shrinking the capacity of the memory pool cache layer; wherein, each time the capacity is shrunk by a preset number of object stacks; when the capacity of the memory pool cache layer is less than the minimum threshold of the memory pool cache layer capacity, expanding the capacity of the memory pool cache layer; wherein, each time the capacity is expanded by a preset number of object stacks.

[0083] In the specific implementation, the maximum threshold and minimum threshold of the memory pool cache layer capacity are set. When the capacity of the memory pool cache layer is lower than the minimum threshold of the memory pool cache layer capacity, the capacity of the memory pool cache layer is expanded, memory blocks are added 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 memory pool cache layer capacity, the capacity of the memory pool cache layer is reduced, which triggers the internal recycling mechanism of the specified memory pool; when the capacity of the memory pool cache layer is between the maximum and minimum thresholds, the above-mentioned memory pool cache layer capacity dynamic adjustment strategy is adopted to adjust the capacity of the memory pool cache layer in real time according to demand.

[0084] As a feasible implementation method, it also 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 a minimum threshold of the memory pool cache layer capacity.

[0085] It's important to note that traditional memory management systems have limitations in managing cache layer domains. These systems typically only set a fixed threshold at the memory pool cache layer. Consequently, for the first memory allocation, each CPU must directly obtain memory from the free list layer. The free list is initially empty, requiring memory to be allocated from the bottom layer. Once 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 introduces cross-layer flow, resulting in significant time-consuming initial allocations or subsequent allocations after the memory pool runs out of memory, impacting the stability and efficiency of the entire system.

[0086] To address this issue, this embodiment initializes the memory pool cache layer to a set minimum threshold. During the first allocation, the per-CPU layer retrieves memory objects directly from the cache layer, rather than from the free list. Subsequently, the memory pool cache layer utilizes a dynamic capacity adjustment strategy to maintain the entire cache area between the minimum and maximum thresholds, dynamically prefetching or releasing memory in real time based on the state. This reduces the performance overhead of initial memory allocations and subsequent allocations after the memory pool runs out of memory.

[0087] It can be seen that this embodiment significantly improves the efficiency and adaptability of memory management by introducing the memory pressure index as a comprehensive indicator to dynamically adjust the capacity of the memory pool cache layer. First, the memory pressure index comprehensively considers the two key factors of memory allocation rate and memory allocation failure rate, so that the system can more comprehensively and accurately evaluate the pressure status of current memory resources. This precise assessment provides a reliable basis for the reasonable adjustment of memory pool capacity and avoids misjudgment caused by a single indicator. 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 sudden high memory demand scenarios or in periods of low memory demand, it can achieve efficient use of memory resources. This not only improves the response speed and stability of the system, but also effectively reduces the occurrence of memory allocation failures and improves the overall performance of the system.

[0088] Based on the above embodiment, as a preferred implementation, the method further includes: when the number of object stacks included in the memory pool cache layer reaches a maximum number threshold of object stacks, reclaiming the object stack with the longest most recent access time.

[0089] It's important to note that in current memory management systems, when handling memory reclamation strategies within a memory pool, reclamation is typically initiated when the pool capacity reaches its upper limit. For example, libumem (the user-space memory management library) reclaims all memory in the pool when the capacity reaches its preset upper limit. Tcmalloc (the thread-cached memory allocator) reclaims only cached objects that have not been used since the last reclamation. However, this reclamation strategy presents certain issues. For libumem, a full reclamation is unreasonable, as it cannot guarantee future memory allocation requests. For Tcmalloc, if there are no objects in the current memory pool marked as unused since the last reclamation, it cannot reclaim memory, resulting in the pool's memory capacity failing to fall below the maximum threshold.

[0090] To avoid these problems, this embodiment adopts a timestamp-based eviction strategy. That is, when the number of object stacks in the memory pool cache layer reaches the maximum threshold, the system will evict the object stacks in order from the most recent access time to the most recent, until the number of object stacks in the cache layer falls below the maximum threshold. At the same time, the system will ensure that the number of object stacks in the cache layer does not fall below the minimum threshold to maintain a certain cache foundation and prevent frequent eviction and allocation operations from affecting performance. After completing the eviction operation, the system will insert the newly released object stack into the cache layer and update its access timestamp to the current time, ensuring that the newly released object stack can be considered in the next eviction operation. Through this strategy, the memory pool cache layer can effectively reclaim the object stacks that are least likely to be accessed again when it reaches maximum capacity, making room for new memory allocation requests. At the same time, by keeping the number of object stacks in the cache layer above the minimum threshold, frequent eviction and allocation operations are avoided, reducing performance impact, thereby improving memory allocation efficiency and optimizing memory pool management.

[0091] Based on the above embodiment, as a preferred implementation method, it also includes: when the memory pool of the target size cannot respond to the memory allocation request, determining the target memory size requested by the memory allocation request; judging whether the heap memory satisfies the memory allocation request; if so, reclaiming the memory of the target size from the heap memory to respond to the memory allocation request; if not, reclaiming the memory larger than the target size in order from small to large until the reclaimed memory size reaches a third preset multiple of the target memory size; wherein the third preset multiple is greater than 1.

[0092] It should be noted that the current memory management system will globally recycle all memory pools when a memory pool cannot apply for memory. This is unnecessary. The main purpose of reclaiming memory is to meet the current memory usage requirements. Recycling all memory pools may affect the use of other memory pools. Therefore, this embodiment gives priority to recycling memory pool resources that meet the target size and have a larger capacity, ensuring accurate termination when the preset recycling threshold is reached, thereby optimizing 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 current needs while avoiding resource waste and system performance degradation caused by excessive recycling.

[0093] The specific reclamation strategy is as follows: First, the system determines the target memory size requested by the memory allocation request. Next, the system determines whether there is sufficient memory in the heap to satisfy the request. If there is sufficient memory in the heap, the system allocates the required memory size directly from the heap to satisfy the memory allocation request. However, if there is insufficient memory in the heap to satisfy the request, the system reclaims memory larger than the target size in ascending order. The node corresponding to the memory pool of the target size is used as the current node, and the right subtree of the current node is selected as the reclamation starting point. Nodes in the subtree represent larger memory pools, and prioritizing the reclamation of larger memory pools can more efficiently meet memory requirements. The reclamation order for the right subtree of the current node is as follows: first, the left subtree of the right subtree is traversed and reclaimed (reclaiming in ascending order of size), then the root node of the right subtree, and finally the right subtree of the right subtree (reclaiming in ascending order of size). If the memory reclaimed after traversing the right subtree of the current node is still insufficient, continue to traverse upwards for recycling to determine whether the current node is the left child of its parent node. If so, traverse and recycle the memory of the parent node of the current node, and traverse and recycle the memory of the right subtree of the parent node of the current node. If not, set the parent node of the current node as the current node again, and re-enter the step of determining whether the current node is the left child of its parent node. Repeat this process until the recycling termination condition is met. If the recycling of the root node of the memory pool management tree is completed but the recycling termination condition is still not met, then there is insufficient memory.

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

[0095] An application example provided by the present invention is described below. Figure 9 shown.

[0096] Comprehensive management of memory pools: Design strategies for comprehensive management of 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 a dynamic expansion strategy for the memory object stack in the memory pool, a dynamic adjustment strategy for the memory pool cache layer capacity, and optimizing the allocation time problem in the memory pool's first allocation or memory exhaustion scenarios.

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

[0099] The flowchart of the dynamic shrinkage and expansion mechanism of the memory pool is as follows Figure 10As shown, the following steps are included: allocating and releasing operations on a specified memory pool; counting the current available memory of the specified memory pool cache layer; obtaining the maximum and minimum thresholds of the specified memory pool cache layer; comparing the current available memory with the maximum and minimum thresholds; if the current available memory is less than the minimum threshold, triggering the specified memory pool to add memory blocks to the free list, constructing memory objects and saving them to the free list; if the current available memory is greater than the maximum threshold, triggering the internal recycling mechanism of the specified memory pool; if the current 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 memory pool internal recycling strategy is as follows Figure 11 As shown, the following steps are included: triggering the memory recycling mechanism of the specified memory pool; judging whether the available objects in the memory pool cache layer are greater than the maximum threshold; if the available objects are greater than the maximum threshold, executing the recycling operation, searching the bidirectional linked list of the full object stack, eliminating the full object stack with the longest access time from the tail, and putting it into the free list, judging whether the free list span block has reached a full free state, if so, releasing the span block 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 executing the recycling operation and ending the process.

[0101] The flowchart of the memory pool passive recycling mechanism is as follows Figure 12As shown, the following steps are included: determining whether a memory pool of a specific size cannot maintain the minimum threshold; if not, terminating the process; if so, triggering a passive reclamation mechanism. The required memory size N is determined, triggering the replenishment of memory blocks (spans) from the heap memory, and determining whether the heap memory (heap) has sufficient memory to meet the replenishment requirements of the specific memory pool; if so, replenishing the memory blocks (spans) from the heap memory; if not, reclaiming memory from other memory pools (including predefined memory pools and custom memory pools) according to the traversal reclamation strategy. The node corresponding to the memory pool of the target size is used as the current node, and the right subtree of the current node is selected as the recycling starting point. The traversal and recycling order of the right subtree of the current node is as follows: first traverse and reclaim the left subtree of the right subtree (traverse and reclaim from small to large size), then reclaim the root node of the right subtree, and finally traverse and reclaim the right subtree of the right subtree (traverse and reclaim from small to large size). Determine whether the amount of reclaimed memory reaches the required size of 1.2N. If so, end the process. If not, determine whether it is reclaimed to the root node memory pool. If so, then there is insufficient memory and end the process. If not, continue to traverse upward for recycling. Determine whether the current node is the left child node of its parent node. If so, traverse and reclaim the memory of the parent node of the current node, and traverse and reclaim the right subtree memory of the parent node of the current node. If not, set the parent node of the current node as the current node again, and re-enter the step of determining whether the current node is the left child node of its parent node. Repeat this process 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 is still not met, then there is insufficient memory.

[0102] It can be seen that this embodiment significantly improves the efficiency and performance of memory management through a series of optimization measures. First, an 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 efficiency of memory allocation. Secondly, 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 memory allocation rate and object survival time, the memory pool is reasonably expanded and contracted, effectively avoiding memory waste and thus improving memory utilization. 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 reduced, and the overall performance and stability of the system are enhanced. At the same time, this embodiment supports user-defined memory pools, which greatly improves the flexibility of memory management and enables it to better adapt to various application scenarios. In terms of memory pool management, this embodiment comprehensively manages predefined memory pools and custom memory pools to improve management efficiency. Finally, the recycling algorithms for individual and global memory pools have been optimized. When the memory pool cache layer is full, memory object stacks that have not been used for a long time are prioritized for elimination. During global recycling, memory pool resources that meet the target size and have a larger capacity are prioritized for recycling, and recycling is terminated accurately when the preset threshold is reached, thereby significantly improving memory recycling efficiency and system resource utilization.

[0103] The following is an introduction to a memory management device provided by an embodiment of the present invention. The memory management device described below and the memory management method described above can be referenced to each other. Figure 13 , a structural diagram of a memory management device according to an exemplary embodiment.

[0104] The adding module 100 is used to receive an instruction to create 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 size of the memory pool managed by each node in the left subtree of the target node in the memory pool management tree is smaller than the size of the memory pool managed by the target node, and the size of the memory pool managed by each node in the right subtree of the target node is larger than the size of the memory pool managed by the target node.

[0105] The reconstruction module 200 is used to balance and reconstruct the subtree to be adjusted according to the construction method of the memory pool management tree when the memory pool management tree does not meet the balance condition, so as 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, 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 to not meet the balance condition.

[0106] The memory management device provided by an 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 a creation instruction for a custom memory pool is received, the 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 of the subtrees of the memory pool management tree exceeds a preset value, the nodes that cause 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 balancing 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 structure in traditional memory management technology, and improves the flexibility of memory management.

[0107] On the basis of the above embodiment, as a preferred implementation manner, it also includes: a creation module, used to create nodes corresponding to each predefined memory pool, sort the nodes corresponding to each predefined memory pool from small to large 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 two middle 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 the left subtree node, and the size of the predefined memory pool corresponding to the left subtree node is 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 the right subtree node, and the size of the predefined memory pool corresponding to the right subtree node is larger than the current node. The node of the size of the predefined memory pool corresponding to the previous node; sort the left subtree nodes in the left subtree node set from small to large 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 two middle nodes as the root node of the left subtree, sort the left subtree nodes in the right subtree node set from small to large 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 two middle nodes as the root node of the right subtree; redefine 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 the memory pool management tree contains nodes corresponding to all predefined memory pools.

[0108] Based on the above embodiment, as a preferred implementation, it further includes: a deletion module, which is used to, upon receiving a deletion instruction for the custom memory pool to be deleted, determine whether the node corresponding to the custom memory pool to be deleted in the memory pool management tree has child nodes; if so, perform balanced 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] On the basis of the above embodiment, as a preferred implementation mode, the reconstruction module 200 is specifically used to: sort the nodes in the subtree to be adjusted from small to large according to the size of the corresponding memory pool, select the middle node in the sorting result or the node with the largest size of the corresponding memory pool 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 the left subtree node, and the size of the memory pool corresponding to the left subtree node is smaller than the size of the memory pool corresponding to the current node, and the right subtree node set of the current node includes the right subtree node, and the size of the memory pool corresponding to the right subtree node is larger than the size of the memory pool corresponding to the current node The left subtree nodes in the left subtree node set are sorted from small to large according to the size of the corresponding memory pool, and the middle node in the sorted result or the node with the largest size of the corresponding memory pool among the two middle nodes is selected as the root node of the left subtree. The left subtree nodes in the right subtree node set are sorted from small to large according to the size of the corresponding memory pool, and the middle node in the sorted result or the node with the largest size of the corresponding memory pool among the two middle nodes is selected as the root node of the right subtree. The root node of the left subtree and the root node of the right subtree are re-determined as the current node, and the step of determining the left subtree node set and the right subtree node set of the current node is re-entered until the reconstructed subtree contains all the nodes in the subtree to be adjusted.

[0110] Based on the above embodiments, as a preferred 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 from near to far according to the most recent access time. Adjacent full object stacks are connected by a bidirectional 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 of the full object stack corresponding to the object stack in the full object stack queue.

[0111] Based on the above embodiment, as a preferred implementation, it further includes: a determination module for determining the access frequency and allocation requirements of the object stack; and 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 embodiment, as a preferred implementation, the determination module is specifically configured to: record the number of times an object in the object stack is called during an adjustment period; wherein the adjustment period includes multiple preset time intervals; and calculate the access frequency of the object stack based on the total number of times all objects in the object stack are called during each preset time interval and a time decay factor; wherein the calculation formula for the access frequency is: ;in, 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 cycle, is the time decay factor, .

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

[0114] Based on the above embodiments, as a preferred implementation, the object stack capacity adjustment module is specifically used to: when the access frequency of the object stack is greater than a 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 a first preset proportion; when the access frequency of the object stack is less than a 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 a second preset proportion; wherein 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.

[0115] Based on the above embodiment, as a preferred implementation mode, it further includes: a first setting module, used to set the maximum threshold and minimum threshold of the object stack capacity; accordingly, the object stack capacity adjustment module is also used to: if the capacity of the object stack after the increase is greater than the maximum threshold of the object stack capacity, then set the capacity of the object stack to the maximum threshold of the object stack capacity; if the capacity of the object stack after the decrease is less than the minimum threshold of the object stack capacity, then set the capacity of the object stack to the minimum threshold of the object stack capacity.

[0116] Based on the above embodiment, as a preferred implementation, it also includes: a calculation module, which is used to 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; 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 failures to the total number of memory allocations; a memory pool cache layer capacity adjustment module, which is used to adjust the capacity of the memory pool cache layer based on the memory pressure index.

[0117] Based on the above embodiment, as a preferred implementation, the calculation module is specifically used 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, The memory allocation rate of the memory pool cache layer in the current cycle, is the historical average memory allocation rate, The number of memory allocation failures of the memory pool cache layer in the current cycle. The total amount of memory allocated for the memory pool cache layer in the current cycle. The memory allocation failure rate of the memory pool cache layer in the current cycle. 、 To adjust the parameters, .

[0118] Based on the above embodiment, as a preferred implementation, 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 target capacity is calculated using the formula: , is the target capacity, is the current capacity, M is the memory pressure index; the new capacity value is calculated based on the current capacity and target capacity of the memory pool cache layer; the calculation formula for the new capacity value is: , is the new capacity value, To control the influence weight of historical capacity; when the new capacity value is greater than the first preset multiple of the current capacity, the capacity of the memory pool cache layer is expanded; wherein, 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, the capacity of the memory pool cache layer is shrunk; wherein, the second preset multiple is less than 1, and a preset number of object stacks are shrunk each time.

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

[0120] Based on the above embodiment, as a preferred implementation, it also includes: an initialization module, which is used 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 memory pool cache layer capacity.

[0121] Based on the above embodiment, as a preferred implementation, it also includes: a first memory recycling module, which is used to recycle the object stack with the longest recent access time when the number of object stacks contained in the memory pool cache layer reaches the maximum number threshold of object stacks.

[0122] On the basis of the above embodiment, as a preferred implementation manner, it also includes: a second memory recovery module, which is used 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, reclaim the memory of the target size from the heap memory to respond to the memory allocation request; if not, reclaim the memory larger than the target size in order from small to large until the recovered memory size reaches a third preset multiple of the target memory size; wherein the third preset multiple is greater than 1.

[0123] Regarding the apparatus in the above embodiment, the specific manner in which each module performs operations has been described in detail in the embodiment of the method, and will not be elaborated here.

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

[0125] Communication interface 1 can exchange information with other devices such as network devices.

[0126] The processor 2 is connected to the communication interface 1 to implement information exchange with other devices and is used to execute the memory management method provided by one or more of the above technical solutions when running a computer program. The computer program is stored in the memory 3.

[0127] Of course, in actual application, the various components in the electronic device are coupled together through the bus system 4. It can be understood that the bus system 4 is used to realize 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 clarity, Figure 14 Various buses are labeled as 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 used to operate on the electronic device.

[0129] It is 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), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), a magnetic random access memory (FRAM), a flash memory, a magnetic surface memory, an optical disc, or a compact disc read-only memory (CD-ROM); the magnetic surface memory can be a magnetic disk or a magnetic tape. The volatile memory can be a random access memory (RAM), which is used as an external cache. By way of example and not limitation, many forms of RAM are available, such as static random access memory (SRAM), synchronous static random access memory (SSRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDRSDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous link dynamic random access memory (SLDRAM), and direct rambus random access memory (DRRAM).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 embodiment of the present invention can be applied to processor 2 or implemented by processor 2. Processor 2 may be an integrated circuit chip with signal processing capabilities. During implementation, each step of the above method can be completed by the hardware integrated logic circuit in processor 2 or by instructions in software form. The above processor 2 can be a general-purpose processor, a DSP, or other programmable logic device, discrete gate or transistor logic device, discrete hardware component, etc. Processor 2 can implement or execute the various methods, steps and logic block diagrams disclosed in the embodiment of the present invention. A general-purpose processor can be a microprocessor or any conventional processor, etc. The steps of the method disclosed in the embodiment of the present invention can be directly embodied as being executed by a hardware decoding processor, or can be executed by a combination of hardware and software modules in the decoding processor. The software module can be located in a storage medium, which is located in memory 3. Processor 2 reads the program in memory 3 and completes the steps of the above method in combination with its hardware.

[0131] When the processor 2 executes the program, the corresponding processes in the various methods of the embodiments of the present invention are implemented, which will not be described here for the sake of brevity.

[0132] An embodiment of the present invention further provides a computer-readable storage medium, in which a computer program is stored. The computer program is configured to execute the steps of any one of the above-mentioned memory management method embodiments when running.

[0133] In an exemplary embodiment, the computer-readable storage medium may include, but is not limited to, various media that can store computer programs, such as a USB flash drive, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk, or an optical disk.

[0134] An embodiment of the present invention further provides a computer program product, which includes a computer program. When the computer program is executed by the processor 2, the steps of any one of the above memory management method embodiments are implemented.

[0135] An embodiment of the present invention further provides another computer program product, including a non-volatile computer-readable storage medium, wherein the non-volatile computer-readable storage medium stores a computer program, and when the computer program is executed by the processor 2, the steps of any of the above-mentioned memory management method embodiments are implemented.

[0136] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the above description has generally described the components and steps of each example according to their functions. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of the present invention.

[0137] The above is a detailed introduction to a memory management system, method, apparatus and equipment, medium and product provided by the present invention. Specific examples are used herein to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea. It should be pointed out that for ordinary technicians in this technical field, without departing from the principles of the present invention, the present invention can also be improved and modified, and these improvements and modifications also fall within the scope of protection of the present invention.

Claims

1. A memory management method, characterized in that: include: Receive a creation instruction for 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, and the size of the memory pool managed by each node in the left subtree of a target node in the memory pool management tree is smaller than the size of the memory pool managed by the target node, and the size of the memory pool managed by each node in the right subtree of the target node is larger than the size of the memory pool managed by the target node; When the memory pool management tree does not satisfy a balance condition, reconstructing the subtree to be adjusted in a balanced manner 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 again satisfies the balance condition; wherein the balance condition is that a height difference of 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 a parent node of the node to be adjusted, and the node to be adjusted is a node that causes the memory pool management tree to not satisfy the balance condition; Among them, also include: 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; 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 ratio of memory allocation failures to the total number of memory allocations; Adjusting the capacity of the memory pool cache layer based on the memory pressure index; 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: Calculating a memory pressure index of the memory pool cache layer based on a memory allocation rate of the memory pool cache layer in a current cycle, a historical average memory allocation rate, and a memory allocation failure rate of the memory pool cache layer in the current cycle; 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, The number of memory allocation failures of the memory pool cache layer in the current cycle, The total amount of memory allocated for 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, 、 To adjust the parameters, ; The step of adjusting the capacity of the memory pool cache layer based on the memory pressure index includes: The target capacity of the memory pool cache layer is calculated 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; A new capacity value is calculated 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, To control the impact weight of historical capacity; When the new capacity value is greater than a first preset multiple of the current capacity, the capacity of the memory pool cache layer is expanded; wherein 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 a second preset multiple of the current capacity, the capacity of the memory pool cache layer is shrunk; wherein, the second preset multiple is less than 1, and a preset number of object stacks are shrunk each time.

2. The memory management method according to claim 1, wherein: Before receiving the instruction to create a custom memory pool, it also includes: Creating a node corresponding to each predefined memory pool, sorting the nodes corresponding to each predefined memory pool from small to large according to the size of the corresponding predefined memory pool, selecting the middle node in the sorting result or the node between the two middle nodes whose corresponding predefined memory pool has the largest size as the root node of the memory pool management tree, and determining the root node as the current node; Determine a left subtree node set and a right subtree node set of the current node; wherein the left subtree node set of the current node includes a left subtree node, and a node whose corresponding predefined memory pool has a size smaller than that of the current node; and the right subtree node set of the current node includes a right subtree node, and a node whose corresponding predefined memory pool has a size larger than that of the current node; Sort the left subtree nodes in the left subtree node set according to the sizes of the corresponding predefined memory pools from small to large, and select 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; sort the left subtree nodes in the right subtree node set according to the sizes of the corresponding predefined memory pools from small to large, and select 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; The root node of the left subtree and the root node of the right subtree are re-determined as the current node, and the step of determining the left subtree node set and the right subtree node set of the current node is re-entered 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: The subtree to be adjusted is reconstructed in a balanced manner according to the construction method of the memory pool management tree, including: Sort the nodes in the subtree to be adjusted from small to large according to the size of the corresponding memory pool, select the middle node in the sorting result or the node with the largest corresponding memory pool size between the two middle nodes as the root node of the reconstructed subtree, and determine the root node as the current node; Determine a left subtree node set and a right subtree node set of the current node; wherein the left subtree node set of the current node includes a left subtree node, and a node whose corresponding memory pool size is smaller than the size of the memory pool corresponding to the current node; and the right subtree node set of the current node includes a right subtree node, and a node whose corresponding memory pool size is larger than the size of the memory pool corresponding to the current node; Sort the left subtree nodes in the left subtree node set according to the sizes of the corresponding memory pools from small to large, and select the middle node in the sorted result or the node with the largest corresponding memory pool size among the two middle nodes as the root node of the left subtree; Sort the left subtree nodes in the right subtree node set according to the sizes of the corresponding memory pools from small to large, and select the middle node in the sorted result or the node with the largest corresponding memory pool size among the two middle nodes as the root node of the right subtree; The root node of the left subtree and the root node of the right subtree are re-determined as the current node, and the step of determining the left subtree node set and the right subtree node set of the current node is re-entered until the reconstructed subtree contains all the nodes of the subtree to be adjusted.

4. The memory management method according to claim 1, wherein: Also includes: When receiving a deletion instruction for a custom memory pool to be deleted, determining whether a node corresponding to the custom memory pool to be deleted in the memory pool management tree has a child node; If so, reconstruct the child nodes in a balanced manner to obtain a target subtree, delete the node corresponding to the to-be-deleted custom memory pool in the memory pool management tree, and replace the position corresponding to the node corresponding to the to-be-deleted custom memory pool in the memory pool management tree with the target subtree; If not, directly delete the node corresponding to the to-be-deleted custom memory pool 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 from near to far according to the most recent access time. Adjacent full object stacks are connected by a bidirectional 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 of the full object stack corresponding to the object stack in the full object stack queue.

6. The memory management method according to claim 5, characterized in that: Also includes: An access frequency and allocation requirement of an object stack are determined, and a capacity of the object stack is adjusted based on the access frequency and allocation requirement of the object stack.

7. The memory management method according to claim 6, characterized in that: Determine object stack access frequency and allocation requirements, including: Recording the number of times an object in the object stack is called during an adjustment period; wherein the adjustment period includes a plurality of 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 a time decay factor; The calculation formula for access frequency is: ; in, 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 cycle, is the time decay factor, ; The number of allocation requests and the number of allocation failures of the object stack are recorded during the adjustment period, and the ratio between the number of allocation failures and the number of allocation requests is used as the allocation demand of the object stack.

8. The memory management method according to claim 6, characterized in that: 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 a first preset access frequency threshold and the allocation demand of the object stack is greater than a first allocation demand threshold, increasing 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 demand of the object stack is less than a second allocation demand threshold, reducing the capacity of the object stack by a second preset ratio; 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.

9. The memory management method according to claim 8, characterized in that: Also includes: Set the maximum and minimum thresholds for object stack capacity; Correspondingly, after increasing the capacity of the object stack by a first preset ratio, the method further includes: If the increased capacity of the object stack is greater than the maximum threshold of the object stack capacity, setting 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 a second preset ratio, the method further includes: If the reduced capacity of the object stack is less than the minimum threshold of the object stack capacity, the capacity of the object stack is set to the minimum threshold of the object stack capacity.

10. The memory management method according to claim 1, wherein: Also includes: Set the maximum and minimum thresholds for the memory pool cache layer capacity; When the capacity of the memory pool cache layer is greater than the maximum threshold of the memory pool cache layer capacity, shrinking the capacity of the memory pool cache layer; wherein a preset number of object stacks are shrunk each time; When the capacity of the memory pool cache layer is less than a minimum threshold of the capacity of the memory pool cache layer, the capacity of the memory pool cache layer is expanded; wherein a preset number of object stacks are expanded each time.

11. The memory management method according to claim 10, characterized in that: Also includes: When initializing the memory pool cache layer, the memory pool cache layer is filled so that the capacity of the memory pool cache layer reaches a minimum threshold of the memory pool cache layer capacity.

12. The memory management method according to claim 5, characterized in that: Also includes: When the number of object stacks included in the memory pool cache layer reaches a maximum threshold of the number of object stacks, the object stack with the longest most recent access time is recycled.

13. The memory management method according to claim 1, wherein: Also includes: When the memory pool of the target size cannot respond to the memory allocation request, determining the target memory size requested by the memory allocation request; Determine whether the heap memory satisfies the memory allocation request; If so, reclaiming memory of the target size from the heap memory in response to the memory allocation request; If not, reclaim memory with a size larger than the target size in order from small to large until the reclaimed memory size reaches a third preset multiple of the target memory size; wherein the third preset multiple is greater than 1.

14. A memory management device, characterized in that: include: An adding module is configured to receive a creation instruction for a custom memory pool and, based on the size of the custom memory pool, add a node corresponding to the custom memory pool in a memory pool management tree; 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, and the size of the memory pool managed by each node in the left subtree of a target node in the memory pool management tree is smaller than the size of the memory pool managed by the target node, and the size of the memory pool managed by each node in the right subtree of the target node is larger than the size of the memory pool managed by the target node; a reconstruction module configured to, when the memory pool management tree does not satisfy a balance condition, reconstruct the subtree to be adjusted in a balanced manner according to the construction method of the memory pool management tree, so as to adjust the memory pool management tree so that the adjusted memory pool management tree again satisfies the balance condition; 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, the subtree to be adjusted includes a node to be adjusted and a parent node of the node to be adjusted, and the node to be adjusted is the node that causes the memory pool management tree to not satisfy the balance condition; Among them, also include: a calculation module, configured to calculate a memory pressure index of the memory pool cache layer based on a memory allocation rate and a 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 ratio 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; The calculation module is specifically used 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: ; 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, The number of memory allocation failures of the memory pool cache layer in the current cycle, The total amount of memory allocated for 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, 、 To adjust the parameters, ; The memory pool cache layer capacity adjustment module is specifically configured to calculate a 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 for the target capacity is: , is the target capacity, is the current capacity, M is the memory pressure index; a new capacity value is calculated 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, To control the influence weight of historical capacity; when the new capacity value is greater than a first preset multiple of the current capacity, the capacity of the memory pool cache layer is expanded; wherein, 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 a second preset multiple of the current capacity, the capacity of the memory pool cache layer is shrunk; wherein, the second preset multiple is less than 1, and a preset number of object stacks are shrunk each time.

15. An electronic device, characterized in that: include: memory for storing computer programs; A processor, configured to implement the steps of the memory management method according to any one of claims 1 to 13 when executing the computer program.

16. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, which, when executed, implements the steps of the memory management method according to any one of claims 1 to 13.

17. A computer program product, characterized in that The invention comprises a computer program, which implements the steps performed by the memory management method according to any one of claims 1 to 13 when the computer program is executed.

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