A memory management system, a leakage detection method and a storage medium
By using a red and black tree to record the application information and number of applications of the call points in the memory management system, comparing the differences between snapshots, the problem of untimely memory leakage detection in the existing technology is solved, and the function of timely detecting memory leakage during the operation of the application is realized.
Patent Information
- Application Number
- CN202211084067.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-09-06
- Publication Date
- 2025-04-01
- Estimated Expiration
- 2042-09-06
AI Technical Summary
The prior art can only detect whether there is a leak problem after the application exits, resulting in untimely detection of leaks.
Provide a memory management system and leakage detection method. Through the red and black tree, it records the application information and number of applications of the call point, obtains snapshots of the red and black tree during the application operation, and compares the differences between the snapshots to determine the target call point with leakage risk.
It realizes the timely detection of memory leaks during the application operation, does not need to exit the application, nor does it affect the normal operation of the application, and can automatically locate the leaked call points.
Smart Images

Figure CN115617504B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of memory detection technology, and in particular to a memory management system, a leakage detection method and a storage medium. Background Art
[0002] Memory is also called internal storage. It is used to temporarily store the calculation data in the processor and the data exchanged with external storage such as hard disk. When the application starts running, the processor will allocate part of the memory to the application to meet the data calculation needs of the application. Therefore, whether the memory is normal determines whether the application can run stably.
[0003] However, in the process of embedded software development, some unavoidable anomalies often occur in the memory, such as memory leaks. Memory leaks are one of the main problems that often occur and bother software developers, and memory leaks are difficult to detect. In the existing technology, memory health detection tools can be used to detect memory, but memory health detection tools require the application to run in its context environment and can only detect whether the memory is abnormal after the application exits.
[0004] It can be seen that, since the prior art can detect whether there is a memory leak only after the application program exits, the prior art has the problem of delayed leak detection. Summary of the invention
[0005] Based on this, the present application provides a memory management system, a leakage detection method and a storage medium to improve the problem of untimely leakage detection in the prior art.
[0006] In a first aspect, the present application provides a memory management system, which includes: a memory operation interface for receiving memory operation instructions of a call point, wherein the memory operation interface includes a memory allocation interface and a memory release interface, and the memory operation instructions include memory allocation instructions and memory release instructions; a memory pool for allocating memory blocks to the call point, wherein the memory pool includes at least one memory block, each memory block includes first characteristic information, and the first characteristic information is used to indicate the allocation status of the memory block; a red-black tree for recording second characteristic information of the call point, wherein the second characteristic information includes application information and application times of the call point, the application information includes the code file name, code line number and / or task identifier of the call point, and the application times is the number of memory blocks occupied by the call point.
[0007] In a second aspect, the present application provides a leakage detection method, which includes: when the application is running stably, obtaining a first snapshot of the red-black tree of the application; when the occupancy rate of the memory pool is greater than or equal to a preset value, obtaining a second snapshot of the red-black tree of the application; determining the growth amount of the number of applications of each call point based on the first snapshot and the second snapshot to determine the target call point with leakage risk.
[0008] In combination with the second aspect, in a first possible implementation of the second aspect, after the step of determining the target call point with a leakage risk, it also includes: searching for the second characteristic information of the target call point in the red-black tree; printing out the second characteristic information of the target call point to indicate that the target call point has a leakage risk.
[0009] In combination with the second aspect, in a second feasible implementation of the second aspect, when a memory release instruction of a call point is received, the method also includes: searching for application information and application count of the call point in a red-black tree; reducing the application count of the call point by one; if the application count of the call point is equal to zero after reduction by one, deleting the second characteristic information of the call point in the red-black tree; modifying the first characteristic information in the target memory block corresponding to the call point, and hanging the memory block corresponding to the call point to the unallocated memory linked list in the memory pool.
[0010] In combination with the second possible implementation method of the second aspect, in a third possible implementation method of the second aspect, when a memory release instruction of a call point is received, the method also includes: reading first characteristic information of the target memory block, wherein the first characteristic information includes a characteristic code, usage information and index information, the characteristic code includes the starting point and end point of the memory block, the usage information includes the memory size, whether it has been allocated, whether it has been released and release point information, and the index information is used to search for the corresponding second characteristic information in the red-black tree; determining whether the target memory block has a risk of repeated release based on the first characteristic information.
[0011] In combination with the third feasible implementation method of the second aspect, in the fourth feasible implementation method of the second aspect, after the step of determining whether the target memory block has a risk of repeated release according to the first characteristic information, it also includes: when determining whether the target memory block has a risk of repeated release, taking the call point corresponding to the memory block released last time as the target call point; and prompting that there is a risk of repeated release of the memory.
[0012] In combination with the second aspect, in a fifth possible implementation of the second aspect, when a memory allocation instruction of a call point is received, the method also includes: allocating an unallocated target memory block in a memory pool to the call point, and hanging the target memory block in an allocated memory linked list; searching for application information of the call point in a red-black tree, and if the search fails, recording the application information and application count of the call point in the red-black tree, and setting the application count to one; if the search is successful, adding one to the application count of the call point; modifying the first characteristic information in the target memory block, and associating the first characteristic information with the second characteristic information of the call point in the red-black tree.
[0013] On the third aspect, the present application provides a memory management system, which includes: an acquisition unit, used to obtain a first snapshot of the red-black tree of an application when the application is running stably; when the occupancy rate of the memory pool is greater than or equal to a preset value, obtain a second snapshot of the red-black tree of the application; a determination unit, used to determine the increase in the number of applications of each call point based on the first snapshot and the second snapshot, so as to determine the target call point with leakage risk.
[0014] In combination with the third aspect, in a first feasible implementation of the third aspect, the memory management system also includes a prompt unit, which is used to: search for the second characteristic information of the target call point in the red-black tree; print out the second characteristic information of the target call point to prompt that there is a leakage risk at the target call point.
[0015] In combination with the third aspect, in a second feasible implementation of the third aspect, the memory management system also includes a release unit, which is used to: search for application information and application count of the call point in the red-black tree; reduce the application count of the call point by one; if the application count of the call point is equal to zero after reduction by one, then delete the second characteristic information of the call point in the red-black tree; modify the first characteristic information in the target memory block corresponding to the call point, and hang the memory block corresponding to the call point to the unallocated memory linked list in the memory pool.
[0016] In combination with the second feasible implementation method of the third aspect, in the third feasible implementation method of the third aspect, the determination unit is also used to: read first characteristic information of the target memory block, wherein the first characteristic information includes a characteristic code, usage information and index information, the characteristic code includes the starting point and end point of the memory block, the usage information includes the memory size, whether it has been allocated, whether it has been released and release point information, and the index information is used to search for the corresponding second characteristic information in the red-black tree; determine whether the target memory block has a risk of repeated release based on the first characteristic information.
[0017] In combination with the third possible implementation of the third aspect, in a fourth possible implementation of the third aspect, the memory management system also includes a prompt unit, which is used to: when determining whether there is a risk of repeated release of the target memory block, use the call point corresponding to the memory block released last time as the target call point; and prompt that there is a risk of repeated release of the memory.
[0018] In combination with the third aspect, in a fifth possible implementation of the third aspect, the memory management system also includes an allocation unit, which is used to: allocate an unallocated target memory block in the memory pool to the call point, and hang the target memory block in an allocated memory linked list; search for application information of the call point in the red-black tree, and if the search fails, record the application information and application count of the call point in the red-black tree, and set the application count to one; if the search is successful, increase the application count of the call point by one; modify the first feature information in the target memory block, and associate the first feature information with the second feature information of the call point in the red-black tree.
[0019] In a fourth aspect, the present application also provides a memory management system, which includes a processor and a memory, wherein the processor and the memory are connected via a bus; the processor is used to execute multiple instructions; the memory is used to store multiple instructions, and the instructions are suitable for being loaded by the processor and executed by the leakage detection method such as the first aspect or any one of the embodiments of the first aspect.
[0020] In a fifth aspect, the present application further provides a computer-readable storage medium, in which a plurality of instructions are stored, and the instructions are suitable for being loaded by a processor and executed by a leakage detection method such as the first aspect or any one embodiment of the first aspect.
[0021] In summary, the present application provides a memory management system, a leakage detection method and a storage medium, wherein the leakage detection method is applied to the memory management system. The memory management system of the present application realizes memory leakage detection during the operation of the application by comparing the first snapshot and the second snapshot of the red-black tree. In this process, there is no need to exit the application, and the operation of the application will not be affected, so the functional implementation of the application will not be affected. In general, the problem of untimely leakage detection in the prior art can be improved by adopting the memory management system and leakage detection method provided by the present application. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] Figure 1 is a schematic block diagram of a memory management system in one embodiment;
[0023] Figure 2 A schematic diagram of a flow chart of a leakage detection method in an embodiment;
[0024] Figure 3A schematic block diagram of a memory management system provided by the present application;
[0025] Figure 4 A structural block diagram of a memory management system provided in this application. DETAILED DESCRIPTION
[0026] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0027] Since the embodiments of the present application involve relatively many professional terms, in order to facilitate understanding, the relevant terms and concepts that may be involved in the embodiments of the present application are first introduced below.
[0028] 1. Memory leak
[0029] Memory leak refers to the situation where the dynamically allocated heap memory in the program is not released or cannot be released by the program for some reason, resulting in a waste of system memory, slowing down the program running speed or even causing serious consequences such as system crash.
[0030] 2. Memory allocation instructions
[0031] Memory allocation instructions refer to instructions used to apply for memory space for the application's call point, such as the malloc instruction. The full name of malloc is memory allocation, which is called dynamic memory allocation in Chinese. It is used to apply for a continuous memory block area of a specified size and return the allocated memory area address as a void* type.
[0032] 3. Memory release instructions
[0033] Memory release instructions refer to instructions used to release memory space for the call point of the application, such as the free instruction. The free instruction is used to release a continuous memory block of a specified size obtained by malloc.
[0034] 4. Repeated release of memory
[0035] Duplicate release refers to releasing the same memory block twice. Specifically, after an application allocates a piece of memory, it releases the memory after using it, but does not clear or recycle all pointers pointing to the memory, so that the same memory block is released next time.
[0036] 5. Red Black Tree
[0037] A red-black tree is a self-balancing binary search tree with high read performance and higher efficiency in inserting or deleting nodes. In this application, a red-black tree is used to store application information and application times of application call points to improve data search and storage efficiency.
[0038] It should be noted that the processors involved in the following of this application may include but are not limited to central processing units (CPU), general processors, coprocessors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field programmable gate arrays (FPGA) or other programmable logic devices, transistor logic devices, hardware components or any combination thereof. The processor can implement the methods described in this application, such as responding to memory operation instructions at the call point of the application, etc., and this application will not go into details.
[0039] It should also be noted that the diagrams provided in this embodiment are only used to illustrate the basic concept of the present invention in a schematic manner, so the diagrams only show the components related to the present invention rather than being drawn according to the number, shape and size of the components in actual implementation. The type, quantity and proportion of each component in actual implementation can be changed at will, and the component layout type may also be more complicated. The structure, proportion, size, etc. shown in the drawings attached to this specification are only used to match the content disclosed in the specification for people familiar with this technology to understand and read, and are not used to limit the limiting conditions that the present invention can be implemented, so they have no technical substantive significance. Any modification of the structure, change of the proportional relationship or adjustment of the size should still fall within the scope of the technical content disclosed by the present invention without affecting the effect and purpose that can be achieved by the present invention. At the same time, the directions or positional relationships indicated by the terms "upper", "lower", "left", "right", "middle", "longitudinal", "lateral", "horizontal", "inner", "outer", "radial", "circumferential" and the like in this specification are based on the directions or positional relationships shown in the drawings, and are only for the convenience of simplifying the description, and do not indicate or imply that the devices or components referred to must have a specific direction, be constructed and operated in a specific direction. Changes or adjustments in their relative relationships, without substantial changes in the technical content, should also be regarded as the scope of the present invention. Therefore, they cannot be understood as limitations on the present invention. In addition, the terms "first" and "second" are used for descriptive purposes only and cannot be understood as indicating or implying relative importance.
[0040] At present, the prior art can detect whether the memory is leaked only after the application is exited, and cannot detect the memory in time during the program running without affecting the normal operation of the application. Therefore, the prior art still has the problem of untimely leakage detection.
[0041] In this regard, the present application provides a memory management system and a leakage detection method, which can improve the problem of untimely leakage detection in the prior art. Among them, the leakage detection method is applied to a memory management system, which includes a memory operation interface, a memory pool, and a red-black tree. Specifically:
[0042] like Figure 1 As shown, the memory management system includes a memory operation interface, a memory pool and a red-black tree. Specifically: the memory operation interface 110 is used to receive the memory operation instruction of the call point, the memory operation interface includes a memory allocation interface and a memory release interface, and the memory operation instruction includes a memory allocation instruction and a memory release instruction. The memory pool 120 is used to allocate a memory block for the call point, the memory pool includes at least one memory block, each memory block includes the first feature information, the first feature information includes the front feature part located at the head of the memory block and / or the rear feature part located at the tail of the memory block (each part is 16 bytes), the first feature information includes a feature code, usage information and index information, the feature code includes the starting point and the end point of the memory block, the usage information includes the memory size, whether it has been allocated, whether it has been released and the release point information, and the index information is used to find the corresponding second feature information in the red-black tree. The red-black tree 130 is used to record the second feature information of the call point, the second feature information is used to indicate the application status of the call point, the second feature information includes application information and application times, the application information includes the code file name, code line number and / or task identifier of the call point, and the application times are the number of memory blocks occupied by the call point. It should be noted that when a memory block is not allocated, some fields in its feature information are empty values. Specific values, such as the index information of the call point and the task identifier, are added only after the memory block is allocated. Correspondingly, some fields in the feature information of a memory block are always not empty, such as the feature code.
[0043] The leakage detection method of the present application is implemented during the operation of the application, and can improve the problem of untimely leakage detection in the prior art. Specifically, when the memory management system implements the leakage detection method, it first obtains the first snapshot of the red-black tree of the application when the application is running stably, and then obtains the second snapshot of the red-black tree of the application when the occupancy rate of the memory pool is greater than or equal to the preset value; finally, the growth amount of the number of applications of each call point is determined according to the first snapshot and the second snapshot to determine the target call point with leakage risk. It can be seen that the present application realizes timely memory leakage detection during the operation of the application. In this process, there is no need to exit the application, and it will not affect the operation of the application, so it will not affect the functional implementation of the application. In addition, the leakage detection method provided by the present application can also automatically locate the leaked call point.
[0044] It should be noted that in addition to leak detection, the memory management system can also implement functions such as memory pool initialization, creation of red-black trees, and response to memory operation instructions. Next, this application will explain some of the functions that the memory management system can implement, specifically:
[0045] When the application starts running, the memory management system first initializes the memory, that is, the memory management system creates a memory pool including multiple memory blocks according to the usage characteristics of the application, and the memory blocks with the same memory size in the multiple memory blocks are stored in the same linked list of unallocated memory. The memory blocks in the same unallocated memory linked list have the same size, so that when allocating memory blocks for the call points of the application later, free memory blocks can be quickly obtained from the corresponding unallocated memory linked list according to the memory size required by the call point. In addition, when there are not enough memory blocks in the memory pool, the memory management system can also dynamically allocate memory from the system memory automatically, and hang it in the corresponding unallocated memory linked list according to the size of the memory block.
[0046] When the application starts running, the memory management system also creates a red-black tree, which is used to store the second characteristic information of the application's call point. When responding to the memory operation instructions of the memory management system, the memory management system not only allocates and releases memory blocks, but also modifies the red-black tree accordingly. It should be noted that the purpose of recording the application information and application times of the call point through the red-black tree in this application is to improve the efficiency of leak detection by improving the search efficiency. For example, when responding to the memory allocation instruction, the application information of the call point can be searched in the red-black tree to quickly find out whether there is an application record for the call point, thereby modifying the application times of the call point, which is convenient for the memory management system to detect whether there is a risk of memory leakage. This will be described in detail later.
[0047] After the application starts running, the application's call point sends a memory operation instruction to the memory management system through the memory operation interface of the memory management system to instruct the memory pool in the memory management system to allocate or release a memory block for the application's call point. When the memory allocation interface (malloc) receives the memory allocation instruction of the call point, the memory pool allocates a memory block for the call point, that is, obtains a free memory block from the unallocated memory list and stores it in the allocated memory list; when the memory allocation interface (free) receives the memory release instruction of the call point, the memory pool releases the memory block for the call point, that is, takes the memory block occupied by the call point from the allocated memory list and re-hangs it to the unallocated memory list. Among them, the memory allocation interface and the memory release interface can be encapsulated in a memory management module, and the memory management module abstracts the memory allocation interface (malloc) and the memory release interface (free). The memory management module is used to replace the system's memory management interface, so that the application's application and release of memory are performed through the interface of the memory management module. When the application calls the interface of the memory management module, the interface automatically and invisiblely conveys the application information of the call point (such as file name, line number and task identifier, etc.) to the memory management system.
[0048] In one embodiment, Figure 2 As shown, based on the above description, the present application provides a leakage detection method, which is applied to Figure 1 The memory management system shown in the figure is described as an example of an execution subject, and includes the following steps:
[0049] 201: When the application is running stably, obtain a first snapshot of the red-black tree of the application.
[0050] Among them, the red-black tree is used to record the number of applications and application information of the call point. Stable operation of the application refers to when most of the call points in the application are allocated memory blocks, such as when the application runs for a preset time, the occupancy rate of the memory pool (the ratio of the size of all allocated memory blocks in the memory pool to the total size of the memory pool) reaches 50%, etc. This application does not impose any restrictions on this. When the application starts to run stably, the memory management system takes a snapshot of the red-black tree of the application, obtains the first snapshot of the red-black tree, and saves it to the flash memory.
[0051] 202: When the occupancy rate of the memory pool is greater than or equal to a preset value, obtain a second snapshot of the red-black tree of the application.
[0052] The memory management system monitors the occupancy rate of the memory pool. If the occupancy rate of the memory pool reaches a preset value (eg, 85%), a snapshot is taken of the red-black tree of the application program to obtain a second snapshot of the red-black tree and save it in the flash memory.
[0053] 203: Determine the growth amount of the number of applications of each call point according to the first snapshot and the second snapshot, so as to determine the target call point with the risk of leakage.
[0054] Among them, since the first snapshot and the second snapshot respectively record the data of the red-black tree of the application at two moments, namely, when the application starts to run stably and when the occupancy rate of the memory pool reaches a preset value, the memory management system can determine the growth amount of the number of applications of each call point by comparing the first snapshot and the second snapshot after obtaining the first snapshot and the second snapshot of the red-black tree of the application from the flash memory, and obtain the call points ranked in the top N (for example, the top ten) in terms of growth amount as target call points.
[0055] For example, the memory management system reads the second application count of each call point in the second snapshot, and searches for the first application count of each call point in the second snapshot in the first snapshot, subtracts the first application count from the second application count to obtain the increase in the application count of each call point in the second snapshot, and takes the call point with the largest increase in application count in the second snapshot as the target call point. For a more detailed example, assume that the second snapshot records the application counts of three call points A, B, and C, and the application counts of the three call points are a1, b1, and c1, respectively. In the first snapshot, the application counts of A and B are found to be a2 and b2, respectively, and the application count of C is not found, so the increase in the application counts of A, B, and C are (a1-a2), (b1-b2), and c1, respectively, and the call point with the largest increase in application count among A, B, and C is taken as the target call point.
[0056] In an practicable manner, after the step of determining the target call point with leakage risk, the method further includes: searching for the second characteristic information of the target call point in the red-black tree; and printing out the second characteristic information of the target call point to indicate that the target call point has leakage risk.
[0057] Among them, the second characteristic information includes application information and application times. The application times are the number of memory blocks occupied by the call point. The application information includes the code file name, code line number and task identifier, so that the user can quickly locate the target call point with leakage risk according to the application information and take effective countermeasures. It should be noted that, because the memory management system obtains the application information and application times of the call point through the interface of the memory management module while allocating memory blocks for the call point, and records the application information and application times of the call point in the red-black tree, the second characteristic information of each call point that is occupying the memory block is recorded in the red-black tree, so that the memory management system can find the second characteristic information of the target call point in the red-black tree, and print out the second characteristic information of the target call point to prompt the user that the target call point has a leakage risk.
[0058] In one implementable manner, the memory management system can also receive and respond to memory operation instructions of the call point through the memory operation interface to implement memory allocation and memory release. Memory allocation means: when the memory management system receives a memory allocation instruction of the call point through the memory allocation interface, it allocates a free memory block to the call point. Memory release means that when the memory management system receives a memory release instruction of the call point through the memory release interface, the memory block occupied by the call point is released as a free memory block. Next, this application will explain the process of memory allocation and memory release in detail, specifically:
[0059] Memory allocation process: When receiving the memory release instruction of the call point, the memory management system searches the red-black tree for the application information and application count of the call point; reduces the application count of the call point by one; if the application count of the call point is equal to zero after reduction by one, delete the second characteristic information of the call point in the red-black tree; modify the first characteristic information in the target memory block corresponding to the call point, and hang the memory block corresponding to the call point in the unallocated memory linked list in the memory pool.
[0060] Among them, when the memory operation instruction is a memory allocation instruction, the memory management system obtains an idle memory block from the unallocated memory linked list as the target memory block, allocates it to the call point, and hangs the target memory block into the allocated memory linked list. On the one hand, the memory management system modifies the first characteristic information in the target memory block, and modifies whether it has been allocated in the first characteristic information to "yes". On the other hand, the application information of the call point is searched in the red-black tree, so as to find the leaf node corresponding to the call point. The leaf node records the second characteristic information of the call point (including application information and application times). If the application information of the call point is found in the red-black tree, the application times of the call point are increased by one. If the application information of the call point is not found in the red-black tree, a new leaf node is created in the red-black tree, and the application information and application times of the call point are recorded in the newly created leaf node. Finally, the first characteristic information is associated with the second characteristic information of the call point in the red-black tree, that is, the position of the application information and application times of the call point in the red-black tree is recorded in the index information of the characteristic information, so that the application information and application times of the call point recorded in the red-black tree can be quickly found later through the index information.
[0061] Memory release process: When receiving the memory allocation instruction of the call point, the memory management system allocates the unallocated target memory block in the memory pool to the call point, and hangs the target memory block in the allocated memory linked list; searches for the application information of the call point in the red-black tree. If the search fails, the application information and application count of the call point are recorded in the red-black tree, and the application count is set to one; if the search is successful, the application count of the call point is increased by one; the first feature information in the target memory block is modified, and the first feature information is associated with the second feature information of the call point in the red-black tree.
[0062] Among them, when the memory operation instruction is a memory release instruction, the memory management system first obtains the application information of the call point through the interface of the memory management module, and then searches the application information and application times of the call point in the red-black tree, and reduces the application times of the call point by one. If the application times of the call point are equal to zero after being reduced by one, it means that there are no other occupied memory blocks at the call point. Therefore, on the one hand, the application information and application times of the call point are deleted in the red-black tree, and on the other hand, the first feature information in the memory block corresponding to the call point is modified, for example, whether it has been allocated in the first feature information is modified to "no", the index information is cleared, whether it has been released is marked as released, and the application information of the released call point is recorded at the release point information. Finally, the memory management system re-hangs the released memory block to the unallocated memory linked list in the memory pool. Among them, whether it has been released and the release point information in the first feature information are used to determine whether the memory block has been released repeatedly.
[0063] In one implementable manner, when a memory management system receives a memory release instruction at a call point, it reads first characteristic information of a target memory block, wherein the first characteristic information includes a characteristic code, usage information, and index information, the characteristic code includes a starting point and an end point of the memory block, the usage information includes memory size, whether it has been allocated, whether it has been released, and release point information, and the index information is used to search for corresponding second characteristic information in a red-black tree; and determines whether there is a risk of duplicate release of the target memory block based on the first characteristic information.
[0064] Among them, when the memory operation instruction is a memory allocation instruction, the memory management system allocates a free memory block to the call point, and uses the free memory block as the target memory block, and detects whether the feature code and usage information in the feature information in the target memory block are damaged, such as whether the feature code is consistent with the preset string, whether the usage information is correct, etc.; when the memory operation instruction is a memory release instruction, the memory management system uses the memory block allocated to the call point as the target memory block. At this time, in addition to detecting whether the feature code and usage information in the feature information are damaged, the index information of the call point can also be detected, such as detecting whether the index information of the call point is valid, etc. It can be seen that this embodiment can perform repeated release detection on the memory in real time when the memory is released, further improving the detection efficiency and real-time performance of the memory.
[0065] In one feasible method, after the step of determining whether the target memory block has a risk of repeated release according to the first characteristic information, the memory management system, when determining whether the target memory block has a risk of repeated release, uses the call point corresponding to the memory block released last time as the target call point, thereby prompting that there is a risk of repeated release of the memory.
[0066] In summary, the memory management system of the present application realizes memory leak detection during the running of the application by comparing the first snapshot and the second snapshot of the red-black tree. In this process, there is no need to exit the application, and the running of the application will not be affected, so the functional implementation of the application will not be affected. In general, the problem of untimely leak detection in the prior art can be improved by adopting the memory management system and leak detection method provided by the present application.
[0067] In one embodiment, the present invention also provides a memory management system. Figure 3 . The embodiment of the present invention can divide the functional units of the device according to the above method example. For example, each functional unit can be divided according to each function, or two or more functions can be integrated into one unit. The above integrated unit can be implemented in the form of hardware or in the form of software functional units. It should be noted that the division of units in the embodiment of the present invention is schematic and is only a logical function division. There may be other division methods in actual implementation. Figure 3 As shown, the memory management system includes an acquisition unit 310 and a determination unit 320. Specifically, the acquisition unit 310 is used to acquire a first snapshot of the red-black tree of the application when the application is running stably; the acquisition unit 310 is also used to acquire a second snapshot of the red-black tree of the application when the occupancy rate of the memory pool is greater than or equal to a preset value; the determination unit 320 is used to determine the growth amount of the number of applications of each call point according to the first snapshot and the second snapshot, so as to determine the target call point with leakage risk.
[0068] In an implementable manner, the memory management system further includes a prompt unit 330, which is used to: search for the second characteristic information of the target call point in the red-black tree; and print out the second characteristic information of the target call point to prompt that the target call point has a leakage risk.
[0069] In one practicable manner, the memory management system also includes a release unit 340, which is used to: search for application information and application count of the call point in the red-black tree; reduce the application count of the call point by one; if the application count of the call point is equal to zero after reduction by one, delete the second characteristic information of the call point in the red-black tree; modify the first characteristic information in the target memory block corresponding to the call point, and hang the memory block corresponding to the call point in the unallocated memory linked list in the memory pool.
[0070] In an implementable manner, the determination unit 320 is also used to: read first characteristic information of the target memory block, wherein the first characteristic information includes a characteristic code, usage information and index information, the characteristic code includes the starting point and the ending point of the memory block, the usage information includes the memory size, whether it has been allocated, whether it has been released and the release point information, and the index information is used to find the corresponding second characteristic information in the red-black tree; determine whether the target memory block has a risk of repeated release based on the first characteristic information.
[0071] In one implementable manner, the memory management system also includes a prompt unit 330, which is used to: when determining whether there is a risk of repeated release of the target memory block, use the call point corresponding to the memory block released last time as the target call point; and prompt that there is a risk of repeated release of the memory.
[0072] In one practicable manner, the memory management system also includes an allocation unit 350, which is used to: allocate an unallocated target memory block in the memory pool to the call point, and hang the target memory block in the allocated memory linked list; search for the application information of the call point in the red-black tree, and if the search fails, record the application information and application count of the call point in the red-black tree, and set the application count to one; if the search is successful, increase the application count of the call point by one; modify the first feature information in the target memory block, and associate the first feature information with the second feature information of the call point in the red-black tree.
[0073] In summary, the memory management system first obtains the first snapshot and the second snapshot of the red-black tree of the application program through the acquisition unit 310, and then determines the increase in the number of applications of each call point according to the first snapshot and the second snapshot through the determination unit 320 to determine the target call point with a leakage risk. It can be seen that the problem of delayed leakage detection in the prior art can be improved by adopting the memory management system and leakage detection method provided by the present application.
[0074] In one embodiment, the present application also provides a memory management system, see Figure 4 . The memory management system can be, but is not limited to, various personal computers, laptops, smart phones, tablet computers, portable wearable devices and servers. The server can be implemented as an independent server or a server cluster consisting of multiple servers. The memory management system in this embodiment as shown in the figure may include: a processor 410 and a memory 420. The above-mentioned processor 410 and memory 420 are connected via a bus 430. The processor 410 is used to execute multiple instructions; the memory 420 is used to store multiple instructions, which are suitable for being loaded by the processor 410 and executing the leakage detection method in the above-mentioned embodiment.
[0075] Among them, the processor 410 can be an electronic control unit (ECU), a central processing unit (CPU), a general processor, a coprocessor, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, transistor logic devices, hardware components or any combination thereof. The processor 410 can also be a combination that realizes computing functions, such as a combination of one or more microprocessors, a combination of DSP and microprocessors, etc. In this embodiment, the processor 410 can use a single-chip microcomputer, and various control functions can be realized by programming the single-chip microcomputer. For example, in this embodiment, the collection, processing and demodulation functions of the capacity of the battery cell and the cumulative total amount of charging are realized. The processor has the advantages of strong computing power and fast processing. Specifically: the processor 410 is used to execute the function of the acquisition unit 310, for obtaining a first snapshot of the red-black tree of the application when the application is running stably; it is also used to obtain a second snapshot of the red-black tree of the application when the occupancy rate of the memory pool is greater than or equal to a preset value; it is also used to execute the function of the determination unit 320, for determining the increase in the number of applications of each call point based on the first snapshot and the second snapshot, so as to determine the target call point with a leakage risk.
[0076] In an practicable manner, the processor 410 is also used to execute the function of the prompt unit 330, which is used to: search for the second characteristic information of the target call point in the red-black tree; and print out the second characteristic information of the target call point to prompt that the target call point has a leakage risk.
[0077] In one practicable manner, the processor 410 is also used to execute the functions of the release unit 340, which is used to: search for the application information and application count of the call point in the red-black tree; reduce the application count of the call point by one; if the application count of the call point is equal to zero after being reduced by one, then delete the second characteristic information of the call point in the red-black tree; modify the first characteristic information in the target memory block corresponding to the call point, and hang the memory block corresponding to the call point in the unallocated memory linked list in the memory pool.
[0078] In one practicable manner, the processor 410 is also used to: read first characteristic information of the target memory block, wherein the first characteristic information includes a characteristic code, usage information, and index information, the characteristic code includes the starting point and the ending point of the memory block, the usage information includes the memory size, whether it has been allocated, whether it has been released, and the release point information, and the index information is used to search for the corresponding second characteristic information in the red-black tree; and determine whether the target memory block has a risk of repeated release based on the first characteristic information.
[0079] In an implementable manner, the processor 410 is further used to: when determining whether the target memory block has a risk of duplicate release, use the call point corresponding to the memory block released last time as the target call point; and prompt that there is a risk of duplicate release of the memory.
[0080] In one practicable manner, the processor 410 is also used to execute the functions of the allocation unit 350, which is used to: allocate an unallocated target memory block in the memory pool to the call point, and hang the target memory block in the allocated memory linked list; search for the application information of the call point in the red-black tree, and if the search fails, record the application information and application count of the call point in the red-black tree, and set the application count to one; if the search is successful, increase the application count of the call point by one; modify the first feature information in the target memory block, and associate the first feature information with the second feature information of the call point in the red-black tree.
[0081] In one possible implementation, the present application further provides a computer-readable storage medium, in which a plurality of instructions are stored, and the instructions are suitable for being loaded by a processor and executing the method in any of the foregoing embodiments. The processor 410 is used to execute the plurality of instructions; the memory 420 is used to store the plurality of instructions, and the instructions are suitable for being loaded by the processor 410 and executing the method for uploading faults in the foregoing embodiments.
[0082] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0083] The above embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the invention patent. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the patent of the present application shall be subject to the attached claims.
Claims
1. A leakage detection method, applied to a memory management system, characterized in that: include: When receiving a memory allocation instruction of a call point, searching the red-black tree for the application information of the call point, if the search fails, recording the application information and the application count of the call point in the red-black tree, and setting the application count to one; if the search succeeds, increasing the application count of the call point by one; When receiving a memory release instruction of a call point, searching the red-black tree for application information and application times of the call point; Decrease the number of applications for the call point by one; If the number of applications of the call point is equal to zero after being decremented by one, deleting the second characteristic information of the call point in the red-black tree; When the application is running stably, a first snapshot of the red-black tree of the application is obtained; when the occupancy rate of the memory pool is greater than or equal to a preset value, a second snapshot of the red-black tree of the application is obtained, wherein the red-black tree is used to record second characteristic information of the call point, the second characteristic information is used to indicate the application status of the call point, the second characteristic information includes the application information and the number of applications of the call point, and the number of applications is the number of memory blocks occupied by the call point; the growth amount of the number of applications of each call point is determined according to the first snapshot and the second snapshot, and the call points ranked top N in terms of growth amount are obtained as target call points with leakage risks.
2. The method according to claim 1, characterized in that After the step of determining the target call point with leakage risk, the method further includes: Searching the red-black tree for second characteristic information of the target call point; The second characteristic information of the target calling point is printed out to indicate that the target calling point has a leakage risk.
3. The method according to claim 1, characterized in that In the case where a memory release instruction of the call point is received, the method further includes: The first characteristic information in the target memory block corresponding to the call point is modified, and the memory block corresponding to the call point is hung in the unallocated memory linked list in the memory pool.
4. The method according to claim 3, characterized in that In the case where a memory release instruction of the call point is received, the method further includes: Reading first characteristic information of the target memory block, wherein the first characteristic information includes a characteristic code, usage information and index information, the characteristic code includes a starting point and an end point of the memory block, the usage information includes a memory size, whether it has been allocated, whether it has been released and release point information, and the index information is used to find the corresponding second characteristic information in the red-black tree; Determine whether the target memory block has a risk of duplicate release according to the first characteristic information.
5. The method according to claim 4, characterized in that After the step of determining whether the target memory block has a risk of repeated release according to the first characteristic information, the method further includes: In the case of determining whether the target memory block has a risk of repeated release, taking the call point corresponding to the memory block released last time as the target call point; It is indicated that there is a risk of repeated release of the memory.
6. The method according to claim 1, characterized in that In the case where a memory allocation instruction of a call point is received, the method further comprises: Allocating an unallocated target memory block in the memory pool to the call point, and hanging the target memory block into an allocated memory linked list; The first characteristic information in the target memory block is modified, and the first characteristic information is associated with the second characteristic information of the call point in the red-black tree.
7. A memory management device, characterized in that: include: an allocation unit, configured to search for application information of the call point in the red-black tree when receiving a memory allocation instruction of the call point, and if the search fails, record the application information and application count of the call point in the red-black tree and set the application count to one; if the search succeeds, increase the application count of the call point by one; A release unit, configured to search the red-black tree for application information and application times of the call point when receiving a memory release instruction of the call point; Decrease the number of applications for the call point by one; If the number of applications of the call point is equal to zero after being decremented by one, deleting the second characteristic information of the call point in the red-black tree; An acquisition unit is used to acquire a first snapshot of a red-black tree of the application when the application is running stably; and acquire a second snapshot of the red-black tree of the application when the occupancy rate of the memory pool is greater than or equal to a preset value, wherein the red-black tree is used to record second characteristic information of the call point, the second characteristic information is used to indicate the application status of the call point, the second characteristic information includes application information and application times of the call point, and the application times is the number of memory blocks occupied by the call point; A determination unit is used to determine the growth of the number of applications of each call point according to the first snapshot and the second snapshot, and obtain the top N call points ranked by the growth as target call points with leakage risk.
8. A memory management device, characterized in that: The device includes a processor and a memory, which are connected via a bus; the processor is used to execute multiple instructions; and a storage medium is used to store the multiple instructions, which are suitable for being loaded by the processor and executing the leakage detection method described in any one of claims 2-6.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a plurality of instructions, and the instructions are suitable for being loaded by a processor and executing the leakage detection method according to any one of claims 2 to 6.
Citation Information
Patent Citations
Memory detecting method, device and system
CN106484580A
Memory leak detecting method and device
CN106610892A
Memory leakage repairing method and device
CN108108258A