A memory allocation system with memory monitoring and early warning system
Through the collaborative work of multiple modules of the memory monitoring and early warning system, the shortcomings of memory management in traditional embedded systems are solved, the security and efficiency of memory use are improved, and problems such as memory leaks are quickly located and repaired, ensuring the stability and reliability of the system.
Patent Information
- Application Number
- CN202411960818.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-27
- Publication Date
- 2025-09-26
- Estimated Expiration
- 2044-12-27
AI Technical Summary
Memory management in traditional embedded systems lacks fine-grained monitoring and exception management, making it difficult to quickly locate and resolve problems such as memory leaks, wild pointers, null pointers, and secondary releases.
A memory monitoring and early warning system is adopted, including a memory pool management module, a memory pool debugging module, a memory monitoring and early warning module, a memory leak detection module, a double release detection module, and a wild pointer positioning module. It provides real-time monitoring, positioning, and early warning mechanisms, and identifies and analyzes memory problems through simulated scenarios.
It significantly improves the transparency, efficiency and security of memory management, quickly locates and fixes problems such as memory leaks and wild pointers, avoids resource waste, and improves the reliability and stability of embedded systems.
Smart Images

Figure CN119883814B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of memory management, in particular to a memory allocation system with a memory monitoring and early warning system. Background Art
[0002] In traditional embedded systems, memory management typically relies on standard malloc and free functions. While these functions meet basic memory allocation and deallocation requirements, they lack fine-grained monitoring and exception management of memory usage. This approach can easily lead to problems such as memory leaks, wild pointers, null pointers, and double frees in complex memory usage scenarios. The lack of effective monitoring and early warning mechanisms makes these issues difficult to quickly identify and resolve. Therefore, there is an urgent need for a memory management solution that can provide group statistics, monitoring, and exception operation detection to improve the security and efficiency of memory usage. Summary of the Invention
[0003] The present invention aims to provide a memory allocation system with a memory monitoring and early warning system, which addresses many issues with traditional memory management and improves the transparency, efficiency, and security of memory management. Developers can monitor the system's memory usage in real time and quickly locate and fix issues such as memory leaks, stray pointers, and duplicate frees, thus avoiding memory resource waste and program crashes, significantly improving the reliability and stability of embedded systems.
[0004] The purpose of the present invention can be achieved through the following technical solutions:
[0005] The present application provides a memory allocation system with a memory monitoring and early warning system, a memory allocation system with a memory monitoring and early warning system, including a memory pool management module, a memory pool debugging module, a memory monitoring and early warning module, a memory leak detection module, a double release detection module, a memory usage statistics module and a wild pointer positioning module;
[0006] The memory pool management module is responsible for the basic operations of the memory pool and is used to dynamically allocate and manage memory, including initialization, allocation, release and status acquisition;
[0007] The memory pool debugging module is used to debug the memory pool code module, and helps developers detect memory management problems by tracking and recording the usage of the memory pool;
[0008] The memory monitoring and warning module is used to provide real-time memory usage monitoring, peak memory usage monitoring, memory usage location, memory usage distribution and recycling statistics, monitor memory leaks, wild pointers, null pointers and secondary releases, and quickly locate problems through the early warning system;
[0009] The memory leak detection module simulates a memory leak scenario and uses the memory pool management module and the memory pool debugging module to identify and debug memory leaks. It simulates memory leaks by deliberately not releasing memory blocks, and uses the memory_pool_debug_trace function to output the current state of the memory pool and analyze the debugging information to confirm memory leaks.
[0010] The double-free detection module simulates releasing the same block of memory twice in the memory pool and how to use the memory pool debugging module to identify and debug double free. By deliberately releasing the same block of memory multiple times, the double-free problem is simulated and debugging information is analyzed to identify double free.
[0011] The memory usage statistics module simulates the memory usage statistics in the memory pool, including initializing the memory pool, allocating and releasing memory blocks, and using the memory pool debugging module to count the memory usage, and analyzing the allocation and release of memory blocks by printing the memory pool usage;
[0012] The wild pointer positioning module simulates the detection and avoidance of wild pointer problems by using the memory pool for memory management, writing to the released memory blocks, simulating wild pointer problems, and using the memory pool debugging module to locate and debug wild pointers.
[0013] Furthermore, the memory pool management module includes: initializing the memory pool according to the size of the memory pool and the specifications of the memory blocks, dividing the memory pool into multiple memory blocks, and setting the status of the memory blocks to free; when memory allocation is required, traversing the memory blocks in the memory pool, finding the first free memory block, setting its status to allocated, and recording the allocated memory size and starting address;
[0014] When there are not enough free memory blocks in the memory pool, the size of the memory pool is dynamically expanded, the memory blocks are re-divided, and the memory pool status information is updated. During the memory allocation process, the allocation status of the memory blocks is recorded through data structures such as bitmaps or linked lists. When memory needs to be released, the corresponding memory block status is set to free according to the starting address and size of the memory block, and the memory pool status information is updated;
[0015] Scan the memory pool regularly and release memory blocks that have not been used for a long time back to the system to reduce memory fragmentation and improve memory utilization. Use the memory pool status acquisition interface to monitor memory pool usage and dynamically adjust the memory pool size and memory block specifications.
[0016] Furthermore, the memory pool debugging module includes initializing debugging information, recording memory allocation and release, obtaining allocation and release statistics, and outputting debugging information of the memory pool.
[0017] The initialization debugging information is responsible for initializing the debugging data structure, preparing the memory blocks and statistical information required for memory pool debugging, checking whether tracer_list has been initialized, and returning directly if it has been initialized. The tracer_node array is used as a linked list to track idle debugging nodes, and then the statistical information and flag bits in tracer_list are set, and a mutex is created to protect concurrent access to the debugging information. Setting tracer_list.init to true indicates that the debugging information has been initialized;
[0018] The record memory allocation and release is called when the memory pool allocates memory, and is used to record the allocation information, lock the mutex and check the validity of the memory block, and allocate a debug node from the free list. If there is no free node, the error flag is set, the allocation information is recorded, including the pointer, file name and line number of the memory allocation, and the node is added to the use list, and then the mutex is unlocked to release the lock;
[0019] The allocation and release statistics are obtained, called when the memory pool releases memory, the released memory block information is recorded, the mutex is locked to reduce the memory allocation and release counts, and the corresponding memory block is searched and removed from the used linked list. If the corresponding memory block cannot be found, the repeated release information is recorded. When the node is found, it is moved from the used linked list back to the free linked list;
[0020] The debugging information of the output memory pool is used to obtain the current memory allocation and release counts, lock the mutex to protect the reading of the counts, and then obtain and return the current allocation and release counts.
[0021] Furthermore, the memory pool debugging module also includes outputting debugging information of the memory pool, which is used to output status information of the current memory pool, including statistical information on memory usage, memory allocation and release, lock the mutex to protect the reading of the debugging information, and then collect statistical information and output the detailed status of the memory pool. The output includes the usage of each memory pool, repeated allocation statistics of memory blocks, and when there is repeated release, lists the repeated release records of all memory blocks.
[0022] Furthermore, the memory monitoring and warning module includes: obtaining real-time usage data of the system memory, judging whether the current memory usage exceeds the threshold according to a preset memory usage threshold, and triggering a memory warning when the threshold is exceeded; continuously monitoring memory usage, recording peak memory usage data over a period of time, and judging whether the current memory peak is abnormal by comparing it with historical memory peak data, and triggering a memory peak warning when it is abnormal;
[0023] If memory usage exceeds the threshold or peaks abnormally, use memory analysis tools to locate and analyze memory usage, obtain the distribution of memory usage, and identify the code modules or variables with concentrated memory usage.
[0024] Then, a memory leak detection algorithm is used to determine whether there is a memory leak by analyzing the object reference relationship graph and memory allocation and release records. If there is a memory leak, the leak object and the leak code location are recorded.
[0025] Pointer analysis technology is used to scan the pointer usage in the code. Through data flow analysis and symbolic execution technology, it is determined whether there are wild pointers or null pointers in the code. If so, the relevant code locations are recorded. Memory operation recording code is inserted at the code locations where memory is allocated and released. By recording the allocation and release of memory objects, it is determined whether there is a memory double release problem. If so, the code location of the double release is recorded.
[0026] Based on the results of memory analysis and positioning, memory leak detection, pointer analysis, and memory operation records, a memory problem diagnosis report is generated. The report lists problems such as memory usage exceeding the threshold, memory leaks, wild pointers, null pointers, and secondary releases, and provides location information for the problem code, which is sent to relevant personnel through the early warning system.
[0027] Furthermore, the memory leak detection module includes initializing a memory pool of a specified size and number of blocks to provide space for memory allocation, then using a debugging function to activate a memory pool debugging module, preparing to record detailed information on memory allocation and release, allocating two fixed-size memory blocks block1 and block2 from the memory pool through a memory block allocation function, and using a memory allocation information recording function to record detailed information on the allocation operation, including the size and address of the memory block, and the file name and line number at the time of allocation, and commenting on the function call for releasing memory in the memory pool management module so that block1 and block2 will not be released, simulating a memory leak scenario that causes memory exhaustion in actual applications, and outputting the current status of the memory pool through executing the memory_pool_debug_trace function, including key information such as the number of unreleased memory blocks, the number of unused and used nodes, the memory pool status flag, and the allocation location and number of memory blocks, so that developers can accurately identify memory blocks that have not been correctly released, confirm the existence of memory leaks, and repair them.
[0028] Furthermore, the double release detection module includes: in the memory pool management module, maintaining a release flag for each memory block, with an initial value of 0, indicating that the memory block has not been released; when a memory block is applied for, setting its corresponding release flag to 0, indicating that the memory block is in an allocated state; when a memory block is released, first determining whether its release flag is 0; if it is 0, setting its flag to 1, indicating that the memory block has been released;
[0029] When it is not 0, it means that the memory block has been released, triggering a double free error. In the memory pool debugging module, the allocation and release operations of each memory block are recorded. When a double free error is detected, the debugging module locates the erroneous code position and the corresponding memory block based on the recorded operation information;
[0030] By analyzing the memory block allocation and release operation sequence, the cause of the double release is determined, and the code is repaired based on the analysis results.
[0031] Furthermore, the memory usage statistics module includes: initializing a memory pool according to a preset memory pool size, dividing the memory pool into a plurality of fixed-size memory blocks, searching for free memory blocks from the memory pool when memory allocation is required, allocating free memory blocks to the requester if any, and marking the memory blocks as allocated; and marking the allocated memory blocks as free when memory release is required;
[0032] Through the memory pool debugging module, the initialization information of the memory pool is recorded, including the memory pool size, the number of memory blocks, etc. During the memory allocation and release process, the number of allocated memory blocks and the number of free memory blocks are counted in real time, the memory utilization rate of the memory pool is calculated, and the memory pool usage is printed regularly, including the number of allocated memory blocks, the number of free memory blocks, the memory utilization rate and other information, to facilitate the analysis of memory usage. According to the memory pool usage, it is determined whether the memory pool size needs to be expanded or the memory block size needs to be adjusted.
[0033] Furthermore, the wild pointer location module includes: creating a memory pool object, pre-allocating a certain number of memory blocks for subsequent memory management and allocation, defining a memory block structure for recording and managing the status of each memory block, searching for a suitable free memory block from the memory pool according to the requested memory size through a memory allocation function, marking it as allocated, returning the starting address of the memory block, marking the specified memory block as released through a memory release function, and re-adding it to the memory pool, wherein during the memory allocation and release process, the allocation and release information of each memory block is recorded;
[0034] When writing to a released memory block, the wild pointer detection mechanism is triggered and relevant information of the write operation is recorded to simulate the wild pointer problem. The memory pool debugging module is used to analyze the recorded memory allocation, release and write information to locate the location and cause of the wild pointer problem.
[0035] The beneficial effects of the present invention are:
[0036] The present invention effectively solves the shortcomings of memory management in traditional embedded systems and significantly improves the security and efficiency of memory use through multiple collaborative modules. The memory pool management module is responsible for basic memory operations, including initialization, allocation, release and status acquisition, and can dynamically allocate and manage memory. At the same time, it records the allocation of memory blocks through data structures such as bitmaps or linked lists, quickly searches for free memory blocks and reclaims allocated memory blocks, and dynamically expands the size of the memory pool to adapt to different memory requirements; the memory pool debugging module provides detailed memory allocation and release records, including information such as operation type, memory address and operation time, so that developers can accurately track memory usage and identify memory management problems such as memory leaks and repeated releases; memory The monitoring and early warning module uses real-time monitoring and threshold judgment to promptly capture anomalies in memory usage and peak data, and uses memory analysis tools and leak detection algorithms to accurately locate code modules and variables with concentrated memory usage, and identify the source of memory leaks; at the same time, pointer analysis technology is used to scan and record the use of wild pointers and null pointers, insert memory operation recording code to detect problems with secondary memory release, and generate a diagnostic report containing detailed information on memory problems and code location, and promptly notify relevant personnel through the early warning system; the combination of these technologies not only optimizes the use of memory resources, but also provides strong support for the rapid location and processing of memory-related problems, greatly improving the location speed and solution efficiency of memory problems, and ensuring the stability and reliability of the system. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] For better understanding and implementation, the technical solution of the present application is described in detail below with reference to the accompanying drawings.
[0038] Figure 1 This is a structural diagram of a memory allocation system with a memory monitoring and early warning system provided in this application. DETAILED DESCRIPTION
[0039] To further illustrate the technical means and effectiveness of the present invention in achieving its intended purpose, exemplary embodiments will be described in detail herein, with examples illustrated in the accompanying drawings. In the following description, when referring to the drawings, identical numerals in different figures represent identical or similar elements, unless otherwise indicated. The embodiments described in the following exemplary embodiments are not intended to represent all possible embodiments consistent with the present application. Rather, they are merely examples of methods and systems consistent with certain aspects of the present application, as detailed in the appended claims.
[0040] The terms used in this application are for the purpose of describing specific embodiments only and are not intended to limit this application. As used in this application and the appended claims, the singular forms "a," "an," "the," and "the" are intended to include the plural forms, unless the context clearly indicates otherwise. It should also be understood that the term "and / or" as used herein refers to and encompasses any and all possible combinations of one or more of the associated listed items.
[0041] The following describes in detail the specific implementation methods, features and effects of the present invention in conjunction with the accompanying drawings and preferred embodiments.
[0042] See also Figure 1 This embodiment provides a memory allocation system with a memory monitoring and early warning system, including a memory pool management module, a memory pool debugging module, a memory monitoring and early warning module, a memory leak detection module, a double release detection module, a memory usage statistics module, and a wild pointer positioning module.
[0043] The memory pool management module is responsible for the basic operations of the memory pool and is used to dynamically allocate and manage memory, including initialization, allocation, release and status acquisition.
[0044] Specifically, the memory pool management module includes initializing the memory pool, memory allocation, memory release, and obtaining the memory pool status. The mpool_init function is used to initialize the memory pool, set the basic structure of the memory pool, and manage the required memory areas; the mpool_alloc function is responsible for allocating memory blocks of a specified size from the memory pool; and the mpool_free function returns the released memory blocks to the memory pool.
[0045] The mpool_status function provides information about the current usage of the memory pool. It also includes error handling mechanisms such as out-of-memory and illegal operation handling.
[0046] Furthermore, the memory pool management module includes: initializing the memory pool according to the size of the memory pool and the specifications of the memory blocks, dividing the memory pool into multiple memory blocks, and setting the status of the memory blocks to free; when memory allocation is required, traversing the memory blocks in the memory pool, finding the first free memory block, setting its status to allocated, and recording the allocated memory size and starting address;
[0047] When there are not enough free memory blocks in the memory pool, the size of the memory pool is dynamically expanded, the memory blocks are re-divided, and the memory pool status information is updated. During the memory allocation process, the allocation status of the memory blocks is recorded through data structures such as bitmaps or linked lists to quickly find free memory blocks and reclaim allocated memory blocks. When memory needs to be released, the corresponding memory block status is set to free according to the starting address and size of the memory block, and the memory pool status information is updated;
[0048] Scan the memory pool regularly and release memory blocks that have not been used for a long time back to the system to reduce memory fragmentation and improve memory utilization. Use the memory pool status acquisition interface to monitor memory pool usage, including total memory size, allocated memory size, and free memory size. Based on this information, dynamically adjust the memory pool size and memory block specifications to meet different memory requirements.
[0049] Specifically, its memory pool management module implements an efficient and dynamic memory management mechanism. It can not only initialize the memory pool and divide it into multiple standardized memory blocks, but also quickly find and mark free memory blocks as allocated during memory allocation, and record necessary allocation information. When memory is insufficient, the system can dynamically expand the memory pool and update the status, using data structures such as bitmaps or linked lists to efficiently track the allocation and release of memory blocks and optimize memory utilization. In addition, the system also regularly scans and releases memory blocks that have not been used for a long time to reduce memory fragmentation, monitors memory usage through a status acquisition interface, and dynamically adjusts the memory pool configuration based on real-time data to adapt to changing memory needs, thereby significantly improving the efficiency and flexibility of memory management.
[0050] Memory pool debugging module, a code module for debugging memory pools, helps developers detect memory management issues by tracking and recording memory pool usage;
[0051] Furthermore, the memory pool debugging module includes initializing debugging information, recording memory allocation and release, obtaining allocation and release statistics, and outputting debugging information of the memory pool.
[0052] The initialization debugging information is responsible for initializing the debugging data structure, preparing the memory blocks and statistical information required for memory pool debugging, checking whether tracer_list has been initialized, and returning directly if it has been initialized. The tracer_node array is used as a linked list to track idle debugging nodes, and then the statistical information and flag bits in tracer_list are set, and a mutex is created to protect concurrent access to the debugging information. Setting tracer_list.init to true indicates that the debugging information has been initialized;
[0053] The recording memory allocation and release function is called when memory is allocated in the memory pool, and the allocated memory block, file name, line number and other information are recorded. The mutex lock is locked to prevent data inconsistency caused by concurrent access, the validity of the memory block is checked, and a debug node is allocated from the free list. When there is no free node, the error flag is set, the allocation information is recorded, including the pointer, file name and line number of the memory allocation, and the node is added to the use list, and then the mutex lock is unlocked to release the lock;
[0054] The allocation and release statistics are obtained, called when the memory pool releases memory, the released memory block information is recorded, the mutex is locked, the memory allocation and release counts are reduced, and the corresponding memory block is searched and removed from the used linked list. If the corresponding memory block cannot be found, the duplicate release information is recorded. When the node is found, it is moved from the used linked list back to the free linked list;
[0055] The debugging information of the output memory pool is used to obtain the current memory allocation and release counts, lock the mutex to protect the reading of the counts, and then obtain and return the current allocation and release counts.
[0056] Furthermore, the memory pool debugging module also includes outputting debugging information of the memory pool, which is used to output status information of the current memory pool, including statistical information on memory usage, memory allocation and release, lock the mutex to protect the reading of the debugging information, and then collect statistical information and output the detailed status of the memory pool, including information such as the usage of each memory pool, repeated allocation statistics of memory blocks, etc. When there is repeated release, the repeated release records of all memory blocks are listed.
[0057] It also includes data structures and macro definitions, where tracer_node_t is used to record detailed information about each memory block, such as file name, line number, allocation pointer, memory size, etc., and tracer_list_t is used to maintain the overall structure of debugging information, including linked lists of used and unused nodes, statistical information, etc.
[0058] Macros such as TRACER_MEMX_NUM, TRACER_NODE_NUM, TRACER_REPEAT_NUM, and TRACER_REFREE_NUM define the number of nodes to debug and other parameters.
[0059] Specifically, the memory pool debugging module provides developers with a comprehensive set of memory management problem diagnostic tools. It enables developers to accurately track memory usage and identify memory management problems such as memory leaks and repeated releases through functions such as initializing debugging information, recording memory allocation and release in detail, counting allocation and release times, and outputting detailed debugging information of the memory pool. It outputs the memory pool status through the memory_pool_debug_trace function, helping developers analyze and solve complex memory management problems, thereby improving system stability and reliability.
[0060] The memory monitoring and early warning module is used to provide real-time memory usage monitoring, peak memory usage monitoring, memory usage location, memory usage distribution and recycling statistics. It can monitor memory leaks, wild pointers, null pointers, secondary releases and other problems, and quickly locate problems through the early warning system.
[0061] Furthermore, the memory monitoring and warning module includes: obtaining real-time usage data of system memory, including total memory, used memory, available memory, etc., judging whether the current memory usage exceeds the threshold according to a preset memory usage threshold, and triggering a memory warning when the threshold is exceeded; continuously monitoring memory usage, recording peak memory usage data over a period of time, and judging whether the current memory peak is abnormal by comparing it with historical memory peak data, and triggering a memory peak warning when it is abnormal;
[0062] If memory usage exceeds the threshold or peaks abnormally, use memory analysis tools to locate and analyze memory usage, obtain the distribution of memory usage, and identify the code modules or variables with concentrated memory usage.
[0063] Then, a memory leak detection algorithm is used to determine whether there is a memory leak by analyzing the object reference relationship graph and memory allocation and release records. If there is a memory leak, the leak object and the leak code location are recorded.
[0064] Pointer analysis technology is used to scan the pointer usage in the code. Through data flow analysis and symbolic execution, it is determined whether there are wild pointers or null pointers in the code. If so, the relevant code locations are recorded. Memory operation recording code is inserted at the memory allocation and release code locations. By recording the allocation and release of memory objects, it is determined whether there is a memory double release problem. If so, the code location of the double release is recorded.
[0065] Based on the results of memory analysis and positioning, memory leak detection, pointer analysis, and memory operation records, a memory problem diagnosis report is generated. The report lists problems such as memory usage exceeding the threshold, memory leaks, wild pointers, null pointers, and secondary releases, and provides location information of the problem code. The report is sent to relevant personnel through the early warning system to assist in quickly locating and resolving memory problems.
[0066] Specifically, the memory monitoring and early warning module achieves comprehensive monitoring and management of system memory usage by comprehensively using technical means such as real-time monitoring, threshold judgment, peak analysis, memory usage location, distribution statistics, and leak detection. This module can capture memory usage and peak data in real time, and by comparing with preset thresholds and historical data, promptly detect anomalies and trigger early warnings. At the same time, memory analysis tools and leak detection algorithms are used to accurately locate code modules and variables with concentrated memory usage and identify the source of memory leaks. Combined with pointer analysis technology, it scans and records the use of wild pointers and null pointers, and detects problems with secondary memory release by inserting memory operation recording code. Ultimately, a diagnostic report containing detailed information about memory problems and code location is generated, and relevant personnel are promptly notified through the early warning system, thereby greatly improving the speed of locating memory problems and the efficiency of solving them, ensuring the stability and reliability of the system.
[0067] The memory leak detection module simulates memory leak scenarios and uses the memory pool management module and memory pool debugging module to identify and debug memory leaks. This module simulates memory leaks by intentionally not releasing memory blocks and uses the memory_pool_debug_trace function to output the current state of the memory pool. The debug information is then analyzed to confirm memory leaks.
[0068] Furthermore, the memory leak detection module includes initializing a memory pool of a specified size and number of blocks to provide space for memory allocation, then using a debug function to activate the memory pool debugging module to prepare to record detailed information on memory allocation and release, allocating two fixed-size memory blocks, block1 and block2, from the memory pool using the allocate memory block function, and using the record memory allocation information function to record detailed information on the allocation operation, including the size and address of the memory blocks, as well as the file name and line number at the time of allocation. By annotating the function call for releasing memory in the memory pool management module, block1 and block2 are prevented from being released, thereby simulating a memory leak scenario that causes memory exhaustion in actual applications. By executing the memory_pool_debug_trace function, the current state of the memory pool is output, including key information such as the number of unreleased memory blocks, the number of unused and used nodes, the memory pool status flag, and the allocation location and number of memory blocks. This allows developers to accurately identify memory blocks that have not been correctly released, confirm the existence of memory leaks, and repair them accordingly to prevent the waste of memory resources and avoid potential program crashes.
[0069] Specifically, the effectiveness of the memory leak detection module is reflected in its ability to accurately simulate and identify memory leak problems. It does this by actually creating a memory pool with a specific size and number of blocks, and activating the memory pool debugging module to record in detail each memory allocation and release behavior. The module simulates a memory leak by deliberately not releasing the allocated memory blocks block1 and block2, and then uses the memory_pool_debug_trace function to output the memory pool status in detail, including the number of unreleased memory blocks, the usage of memory nodes, the memory pool status flag, and the specific allocation location and number of memory blocks. This detailed debugging information enables developers to quickly locate incorrectly released memory blocks, confirm the exact location of the memory leak, and take timely measures to repair it, effectively preventing the waste of memory resources and reducing the risk of program crashes due to memory leaks, thereby enhancing the stability and reliability of the program.
[0070] The double-free detection module simulates the release of the same block of memory twice in the memory pool and how to use the memory pool debugging module to identify and debug double release. It simulates the problem of double release by deliberately releasing the same block of memory multiple times and analyzes the debugging information to identify double release.
[0071] Furthermore, the double release detection module includes: in the memory pool management module, maintaining a release flag for each memory block, with an initial value of 0, indicating that the memory block has not been released; when a memory block is applied for, setting its corresponding release flag to 0, indicating that the memory block is in an allocated state; when a memory block is released, first determining whether its release flag is 0; if it is 0, setting its flag to 1, indicating that the memory block has been released;
[0072] When it is not 0, it means that the memory block has been released, triggering a double free error. In the memory pool debugging module, the allocation and release operations of each memory block are recorded, including information such as the operation type, memory address, and operation time. When a double free error is detected, the debugging module locates the erroneous code position and the corresponding memory block based on the recorded operation information;
[0073] By analyzing the sequence of memory block allocation and release operations, we can determine the causes of double release, such as program logic errors and multi-threaded contention. Based on the analysis results, we can repair the code to ensure that each memory block is released only once, avoiding double release errors. At the same time, we can optimize the design of the memory pool to improve the efficiency and reliability of memory management.
[0074] Specifically, the double-free detection module sets a release flag for each memory block in the memory pool management module, ensuring the legitimacy of each release operation and the correct release of the memory block. Ultimately, the double-free detection module not only improves the security of memory management but also enhances the stability and reliability of the entire system by optimizing the memory pool design.
[0075] The memory usage statistics module simulates the statistics of memory usage in the memory pool, including initializing the memory pool, allocating and releasing memory blocks, and using the memory pool debugging module to count memory usage. By printing the memory pool usage, it analyzes the allocation and release of memory blocks.
[0076] Furthermore, the memory usage statistics module includes: initializing a memory pool according to a preset memory pool size, dividing the memory pool into a plurality of fixed-size memory blocks, searching for free memory blocks from the memory pool when memory allocation is required, allocating free memory blocks to the requester if any, and marking the memory blocks as allocated; and marking the allocated memory blocks as free when memory release is required for subsequent reallocation;
[0077] Through the memory pool debugging module, the initialization information of the memory pool is recorded, including the memory pool size, the number of memory blocks, etc. During the memory allocation and release process, the number of allocated memory blocks and the number of free memory blocks are counted in real time, the memory utilization rate of the memory pool is calculated, and the memory pool usage is printed regularly, including the number of allocated memory blocks, the number of free memory blocks, memory utilization rate and other information, to facilitate the analysis of memory usage. According to the memory pool usage, it is determined whether the memory pool size needs to be expanded or the memory block size needs to be adjusted to optimize memory allocation and release efficiency and improve memory utilization.
[0078] The dangling pointer location module simulates detection and prevention of dangling pointer issues by using a memory pool for memory management, writing to freed memory blocks, simulating dangling pointer issues, and utilizing the memory pool debugging module to locate and debug dangling pointers. A dangling pointer is a pointer that points to freed or unallocated memory. Using a dangling pointer can lead to unpredictable behavior and possible program crashes.
[0079] Furthermore, the wild pointer locating module includes: creating a memory pool object, pre-allocating a certain number of memory blocks for subsequent memory management and allocation, defining a memory block structure, including the starting address, size, status and other information of the memory block, for recording and managing the status of each memory block, searching for a suitable free memory block from the memory pool according to the requested memory size through a memory allocation function, marking it as allocated, returning the starting address of the memory block, marking the specified memory block as released through a memory release function, and re-adding it to the memory pool, wherein during the memory allocation and release process, the allocation and release information of each memory block is recorded, including the call stack at the time of allocation, the call stack at the time of release, etc., for subsequent debugging and problem location;
[0080] When writing to a released memory block, the wild pointer detection mechanism is triggered to record relevant information of the write operation, such as the write address and write data, to simulate the wild pointer problem. The memory pool debugging module is used to analyze the recorded memory allocation, release, and write information, locate the location and cause of the wild pointer problem, and provide corresponding debugging information and suggestions to assist developers in problem repair and optimization.
[0081] Specifically, the orphan pointer location module effectively tracks and manages the lifecycle of memory blocks by creating memory pool objects, defining memory block structures, and recording detailed allocation and release information. This allows detection mechanisms to be quickly triggered and critical information to be recorded when orphan pointer writes occur. This module leverages the memory pool debugging module to deeply analyze this information, pinpointing the location and cause of orphan pointer issues and providing practical debugging information and remediation suggestions, helping developers quickly resolve issues and optimize code, enhancing program robustness and security.
[0082] Its modular architecture enables efficient memory management and problem diagnosis: the memory pool management module (mpool) is responsible for basic memory allocation and release operations, while maintaining the memory block management structure; the memory pool debugging module (mpool_debug) works closely with mpool to track memory usage by recording detailed information about each memory allocation and release, providing debugging support for issues such as memory leaks, double frees, and wild pointers; the memory monitoring and warning module monitors memory usage in real time. Once an anomaly such as a memory leak or illegal operation is detected, it immediately triggers an alert mechanism to quickly locate the source of the problem. These modules work together to not only optimize the efficiency of memory resource utilization but also significantly improve system stability and reliability through real-time monitoring and early warning mechanisms.
[0083] The above description is merely a preferred embodiment of the present invention and does not constitute any form of limitation to the present invention. Although the present invention has been disclosed as above in terms of a preferred embodiment, it is not intended to limit the present invention. Any person skilled in the art can, without departing from the scope of the technical solution of the present invention, make some changes or modifications to equivalent embodiments using the technical contents disclosed above. However, any brief modifications, equivalent changes and modifications made to the above embodiments based on the technical essence of the present invention without departing from the content of the technical solution of the present invention are still within the scope of the technical solution of the present invention.
Claims
1. A memory allocation system with a memory monitoring and early warning system, characterized in that: It includes memory pool management module, memory pool debugging module, memory monitoring and warning module, memory leak detection module, double release detection module, memory usage statistics module and wild pointer positioning module; The memory pool management module is responsible for the basic operations of the memory pool and is used to dynamically allocate and manage memory, including initialization, allocation, release and status acquisition; The memory pool debugging module is used to debug the memory pool code module, and helps developers detect memory management problems by tracking and recording the usage of the memory pool; The memory monitoring and warning module is used to provide real-time memory usage monitoring, peak memory usage monitoring, memory usage location, memory usage distribution and recycling statistics, monitor memory leaks, wild pointers, null pointers and secondary releases, and quickly locate problems through the early warning system; The memory leak detection module simulates a memory leak scenario and uses the memory pool management module and the memory pool debugging module to identify and debug memory leaks. It simulates memory leaks by deliberately not releasing memory blocks, and uses the memory_pool_debug_trace function to output the current state of the memory pool and analyze the debugging information to confirm memory leaks. The double-free detection module simulates releasing the same block of memory twice in the memory pool and how to use the memory pool debugging module to identify and debug double free. By deliberately releasing the same block of memory multiple times, the double-free problem is simulated and debugging information is analyzed to identify double free. The memory usage statistics module simulates the memory usage statistics in the memory pool, including initializing the memory pool, allocating and releasing memory blocks, and using the memory pool debugging module to count the memory usage, and analyzing the allocation and release of memory blocks by printing the memory pool usage; The wild pointer positioning module simulates the detection and avoidance of wild pointer problems by using the memory pool for memory management, writing to the released memory blocks, simulating wild pointer problems, and using the memory pool debugging module to locate and debug wild pointers.
2. A memory allocation system with a memory monitoring and early warning system according to claim 1, characterized in that: The memory pool management module includes: initializing the memory pool according to the size of the memory pool and the specifications of the memory blocks, dividing the memory pool into multiple memory blocks, and setting the status of the memory blocks to free; when memory allocation is required, traversing the memory blocks in the memory pool, finding the first free memory block, setting its status to allocated, and recording the allocated memory size and starting address; When there are not enough free memory blocks in the memory pool, the size of the memory pool is dynamically expanded, the memory blocks are re-divided, and the memory pool status information is updated. During the memory allocation process, the allocation status of the memory blocks is recorded through a bitmap or linked list data structure. When memory needs to be released, the corresponding memory block status is set to free according to the starting address and size of the memory block, and the memory pool status information is updated; Scan the memory pool regularly and release memory blocks that have not been used for a long time back to the system to reduce memory fragmentation and improve memory utilization. Use the memory pool status acquisition interface to monitor memory pool usage and dynamically adjust the memory pool size and memory block specifications.
3. The memory allocation system with memory monitoring and early warning system according to claim 1, characterized in that: The memory pool debugging module includes initializing debugging information, recording memory allocation and release, obtaining allocation and release statistics, and outputting debugging information of the memory pool. The initialization debugging information is responsible for initializing the debugging data structure, preparing the memory blocks and statistical information required for memory pool debugging, checking whether tracer_list has been initialized, and returning directly if it has been initialized. The tracer_node array is used as a linked list to track idle debugging nodes, and then the statistical information and flag bits in tracer_list are set, and a mutex is created to protect concurrent access to the debugging information. Setting tracer_list.init to true indicates that the debugging information has been initialized; The record memory allocation and release is called when the memory pool allocates memory, and is used to record the allocation information, lock the mutex and check the validity of the memory block, and allocate a debug node from the free list. If there is no free node, the error flag is set, the allocation information is recorded, including the pointer, file name and line number of the memory allocation, and the node is added to the use list, and then the mutex is unlocked to release the lock; The allocation and release statistics are obtained, called when the memory pool releases memory, the released memory block information is recorded, the mutex is locked to reduce the memory allocation and release counts, and the corresponding memory block is searched and removed from the used linked list. If the corresponding memory block cannot be found, the repeated release information is recorded. When the node is found, it is moved from the used linked list back to the free linked list; The debugging information of the output memory pool is used to obtain the current memory allocation and release counts, lock the mutex to protect the reading of the counts, and then obtain and return the current allocation and release counts.
4. The memory allocation system with memory monitoring and early warning system according to claim 3, characterized in that: The memory pool debugging module also includes outputting debugging information of the memory pool, which is used to output the status information of the current memory pool, including memory usage, memory allocation and release statistics, lock the mutex to protect the reading of the debugging information, and then collect statistics and output the detailed status of the memory pool. The output includes the usage of each memory pool and repeated allocation statistics of memory blocks. When there is repeated release, the repeated release records of all memory blocks are listed.
5. The memory allocation system with memory monitoring and early warning system according to claim 1, characterized in that: The memory monitoring and warning module includes: obtaining real-time usage data of system memory, judging whether the current memory usage exceeds the threshold according to a preset memory usage threshold, and triggering a memory warning when the threshold is exceeded; continuously monitoring memory usage, recording peak memory usage data over a period of time, and comparing it with historical memory peak data to judge whether the current memory peak is abnormal, and triggering a memory peak warning when it is abnormal; If memory usage exceeds the threshold or peaks abnormally, use memory analysis tools to locate and analyze memory usage, obtain the distribution of memory usage, and identify the code modules or variables with concentrated memory usage. Then, a memory leak detection algorithm is used to determine whether there is a memory leak by analyzing the object reference relationship graph and memory allocation and release records. If there is a memory leak, the leak object and the leak code location are recorded. Pointer analysis technology is used to scan the pointer usage in the code. Through data flow analysis and symbolic execution technology, it is determined whether there are wild pointers or null pointers in the code. If so, the relevant code locations are recorded. Memory operation recording code is inserted at the code locations where memory is allocated and released. By recording the allocation and release of memory objects, it is determined whether there is a memory double release problem. If so, the code location of the double release is recorded. Based on the results of memory analysis and positioning, memory leak detection, pointer analysis, and memory operation records, a memory problem diagnosis report is generated. The report lists problems such as memory usage exceeding the threshold, memory leaks, wild pointers, null pointers, and secondary releases, and provides location information for the problem code, which is sent to relevant personnel through the early warning system.
6. The memory allocation system with memory monitoring and early warning system according to claim 1, characterized in that: The memory leak detection module includes: initializing a memory pool of a specified size and number of blocks to provide space for memory allocation, then using a debugging function to activate a memory pool debugging module, preparing to record detailed information on memory allocation and release, allocating two fixed-size memory blocks block1 and block2 from the memory pool through a memory block allocation function, and using a memory allocation information recording function to record detailed information on the allocation operation, including the size and address of the memory block, as well as the file name and line number at the time of allocation. By commenting on the function call for releasing memory in the memory pool management module, block1 and block2 will not be released, simulating a memory leak scenario that causes memory exhaustion in actual applications, and outputting the current state of the memory pool by executing the memory_pool_debug_trace function, developers can accurately identify memory blocks that have not been correctly released, confirm the existence of memory leaks, and repair them.
7. The memory allocation system with memory monitoring and early warning system according to claim 1, characterized in that: The double release detection module comprises: in the memory pool management module, maintaining a release flag for each memory block, wherein the initial value is 0, indicating that the memory block is not released; when a memory block is applied for, the corresponding release flag is set to 0, indicating that the memory block is in an allocated state; when a memory block is released, first determining whether its release flag is 0; if it is 0, then setting its flag to 1, indicating that the memory block has been released; When it is not 0, it means that the memory block has been released, triggering a double free error. In the memory pool debugging module, the allocation and release operations of each memory block are recorded. When a double free error is detected, the debugging module locates the erroneous code position and the corresponding memory block based on the recorded operation information; By analyzing the memory block allocation and release operation sequence, the cause of the double release is determined, and the code is repaired based on the analysis results.
8. The memory allocation system with memory monitoring and early warning system according to claim 1, characterized in that: The memory usage statistics module includes: initializing a memory pool according to a preset memory pool size, dividing the memory pool into a plurality of fixed-size memory blocks, searching for free memory blocks from the memory pool when memory allocation is required, allocating free memory blocks to requesters when there are free memory blocks, and marking the memory blocks as allocated; and marking the allocated memory blocks as free when memory release is required; Through the memory pool debugging module, the initialization information of the memory pool is recorded. During the memory allocation and release process, the number of allocated memory blocks and the number of free memory blocks are counted in real time, the memory utilization rate of the memory pool is calculated, and the usage of the memory pool is printed regularly. Based on the usage of the memory pool, it is determined whether the memory pool size needs to be expanded or the memory block size needs to be adjusted.
9. The memory allocation system with a memory monitoring and early warning system according to claim 1, characterized in that: The wild pointer positioning module includes: creating a memory pool object, pre-allocating a certain number of memory blocks for subsequent memory management and allocation, defining a memory block structure for recording and managing the status of each memory block, searching for a suitable free memory block from the memory pool according to the requested memory size through a memory allocation function, marking it as allocated, returning the starting address of the memory block, marking the specified memory block as released through a memory release function, and re-adding it to the memory pool, wherein during the memory allocation and release process, the allocation and release information of each memory block is recorded; When writing to a released memory block, the wild pointer detection mechanism is triggered and relevant information of the write operation is recorded to simulate the wild pointer problem. The memory pool debugging module is used to analyze the recorded memory allocation, release and write information to locate the location and cause of the wild pointer problem.
Citation Information
Patent Citations
Memory management method and device
CN116382919A
Method for positioning memory leak
CN119149285A