Memory allocation system with memory monitoring and early warning system

By designing a memory allocation system with memory monitoring and early warning system in embedded systems, the problem of lack of fine-grained monitoring and exception management in traditional memory management is solved, and the rapid positioning and resolution of memory leaks, wild pointers and other problems are achieved, which significantly improves the reliability and stability of the system.

CN119883814AActive Publication Date: 2025-04-25GUANGZHOU SENYANG ELECTRONIC TECH CO LTD
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202411960818.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-27
Publication Date
2025-04-25
Estimated Expiration
2044-12-27

AI Technical Summary

Technical Problem

Memory management in traditional embedded systems lacks fine-grained monitoring and exception management, which makes it difficult to quickly locate and solve problems such as memory leaks, wild pointers, null pointers and secondary releases.

Method used

A memory allocation system with memory monitoring and early warning system was designed, including memory pool management module, memory pool debugging module, memory monitoring and early warning module, memory leak detection module, dual release detection module, memory usage statistics module and field pointer positioning module. Through the coordinated work of these modules, real-time monitoring, abnormal detection and rapid positioning problems are achieved.

Benefits of technology

It significantly improves the transparency, efficiency and security of memory management, can monitor and locate memory leaks, wild pointers and other problems in real time, avoid memory resource waste and program crashes, and improves the reliability and stability of embedded systems.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119883814A_ABST
    Figure CN119883814A_ABST
Patent Text Reader

Abstract

The invention discloses a memory allocation system with a memory monitoring and early warning system, and relates to the technical field of memory management. Comprising a memory pool management module, a memory pool debugging module, a memory monitoring and early warning module, a memory leak detection module, a dual release detection module, a memory usage amount statistics module and a wild pointer positioning module. By integrating the memory pool management module, the debugging module and the monitoring and early warning module, memory management in the embedded system is optimized, the system can dynamically allocate memories, record operation, monitor abnormal use of the memories in real time and position the problems of memory leakage, wild pointers and the like, the safety and efficiency of memory use are improved, the positioning and solving of the problems are accelerated, and the system is suitable for large-scale popularization and application. And the system stability is enhanced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The 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 usually relies on standard malloc and free functions. Although these functions can meet basic memory allocation and release requirements, they lack fine-grained monitoring and exception management of memory usage. This approach is prone to memory leaks, wild pointers, null pointers, and secondary releases when faced with complex memory usage scenarios. The lack of effective monitoring and early warning mechanisms makes it difficult to quickly locate and resolve these problems. Therefore, the prior art urgently needs a memory management solution that can provide group statistics, monitoring, and abnormal operation monitoring to improve the security and efficiency of memory usage. Summary of the invention

[0003] The purpose of the present invention is to provide a memory allocation system with a memory monitoring and early warning system, which solves many problems in traditional memory management methods and improves the transparency, efficiency and security of memory management. Developers can monitor the memory usage of the system in real time, quickly locate and repair problems such as memory leaks, wild pointers, and repeated releases, avoid wasting memory resources and program crashes, and significantly improve 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 code module of the memory pool, and helps developers detect memory management problems by tracking and recording the usage of the memory pool;

[0008] 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, 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, by simulating the memory leak scenario, uses the memory pool management module and the memory pool debugging module to identify and debug the memory leak problem, simulates the memory leak by deliberately not releasing the memory block, and uses the memory_pool_debug_trace function to output the current state of the memory pool, and analyzes the debugging information to confirm the memory leak;

[0010] The double release detection module simulates releasing the same memory block twice in the memory pool and how to use the memory pool debugging module to identify and debug double release, by deliberately releasing the same memory block multiple times to simulate the problem of double release, and analyzing debugging information to identify double release;

[0011] 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 the memory usage, and analyzing the allocation and release of memory blocks by printing the memory pool usage;

[0012] The wild pointer location 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] Further, the memory pool management module includes: initializing the memory pool according to the size of the memory pool and the specifications of the memory block, dividing the memory pool into a plurality of memory blocks, and setting the state of the memory block to free, and when memory allocation is required, traversing the memory blocks in the memory pool, finding the first free memory block, and setting its state 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 status information of the memory pool is updated. During the memory allocation process, the allocation of 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 status information of the memory pool is updated;

[0015] Scan the memory pool regularly and release the 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 the usage of the memory pool and dynamically adjust the size of the memory pool and the specifications of the memory blocks.

[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, and using the initialized tracer_node array as a linked list to track free debugging nodes, and then setting the statistical information and flag bits in tracer_list, and creating a mutex to protect concurrent access to debugging information, and setting tracer_list.init to true, indicating 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 lock and check the validity of the memory block, and allocate a debugging node 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;

[0019] The allocation and release statistics are obtained, and the call is made when the memory pool releases memory, the released memory block information is recorded, the mutex is locked to reduce the count of memory allocation and release, and the corresponding memory block is searched and removed from the used linked list. When the corresponding memory block cannot be found, the repeated release information is recorded, and 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 counts of current memory allocation and release, 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 of memory usage, memory allocation and release, locking the mutex to protect the reading of the debugging information, and then collecting statistical information and outputting the detailed status of the memory pool. The output includes the usage of each memory pool, statistical information of repeated allocation of memory blocks, and when there is repeated release, listing 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 the memory usage, recording the memory peak usage data within a period of time, and judging whether the current memory peak is abnormal by comparing it with the historical memory peak data, and triggering a memory peak warning when it is abnormal;

[0023] When memory usage exceeds the threshold or the memory peak is abnormal, use memory analysis tools to locate and analyze memory usage, obtain the distribution of memory usage, and determine 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] Use pointer analysis technology to scan the usage of pointers in the code. Use data flow analysis and symbolic execution technology to determine whether there are wild pointers or null pointers in the code. If so, record the relevant code location. Insert memory operation recording code at the code location of memory allocation and release. By recording the allocation and release of memory objects, determine whether there is a memory secondary release problem. If so, record the code location of the secondary release.

[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 release, and provides location information of 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 annotating a function call for releasing memory in a memory pool management module so that block1 and block2 will not be released, simulating a memory leak scenario that causes memory exhaustion in an actual application, and outputting the current state of the memory pool by executing a memory_pool_debug_trace function, including key information such as the number of unreleased memory blocks, the number of unused and used nodes, a 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] Further, the double release detection module includes: in the memory pool management module, a release mark is maintained for each memory block, the initial value is 0, indicating that the memory block has not been released, when a memory block is applied, its corresponding release mark is set to 0, indicating that the memory block is in an allocated state, when a memory block is released, first determine whether its release mark is 0, when it is 0, then set its mark 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 release error. In the memory pool debugging module, the application and release operations of each memory block are recorded. When a double release 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] Further, the memory usage statistics module includes: initializing the 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, and if there are free memory blocks, allocating them to the requester 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 usage of the memory pool 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 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.

[0033] Furthermore, 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 in 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 to record the relevant information of the write operation 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. Through multiple modules working in collaboration, the security and efficiency of memory use are significantly improved. 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 recycles 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. 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 codes 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 A schematic diagram of the structure of a memory allocation system with a memory monitoring and early warning system provided in this application. DETAILED DESCRIPTION

[0039] In order to further explain the technical means and effects taken by the present invention to achieve the predetermined invention purpose, exemplary embodiments will be described in detail here, and examples thereof are shown in the accompanying drawings. When the following description refers to the drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The implementations described in the following exemplary embodiments do not represent all implementations consistent with the present application. Instead, they are only examples of methods and systems consistent with some aspects of the present application as detailed in the attached 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. The singular forms of "a", "said" and "the" used in this application and the appended claims are also intended to include plural forms unless the context clearly indicates other meanings. It should also be understood that the term "and / or" used herein refers to and includes any or all possible combinations of one or more associated listed items.

[0041] The specific implementation methods, features and effects of the present invention are described in detail below 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, allocating memory, releasing memory 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 area; the mpool_alloc function is responsible for allocating a memory block of a specified size from the memory pool; the mpool_free function returns the released memory block to the memory pool;

[0045] The mpool_status function provides information about the current usage of the memory pool. In addition, it also includes error handling mechanisms, such as insufficient memory and illegal operation processing.

[0046] Further, the memory pool management module includes: initializing the memory pool according to the size of the memory pool and the specifications of the memory block, dividing the memory pool into a plurality of memory blocks, and setting the state of the memory block to free, and when memory allocation is required, traversing the memory blocks in the memory pool, finding the first free memory block, and setting its state 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 status information of the memory pool is updated. During the memory allocation process, the allocation of memory blocks is recorded through data structures such as bitmaps or linked lists to quickly find free memory blocks and recycle 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 status information of the memory pool is updated;

[0048] Scan the memory pool regularly and release the 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 the usage of the memory pool, including the total memory size, allocated memory size, free memory size, etc. Based on this information, dynamically adjust the size of the memory pool and the specifications of the memory blocks to meet different memory requirements.

[0049] Specifically, through its memory pool management module, an efficient and dynamic memory management mechanism is implemented, which 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 when allocating memory, and record necessary allocation information. When memory is insufficient, the system can dynamically expand the memory pool and update the status, and use data structures such as bitmaps or linked lists to efficiently track the allocation and release of memory blocks to 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 the status acquisition interface, and dynamically adjusts the memory pool configuration based on real-time data to adapt to changing memory requirements, 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 problems by tracking and recording the usage of memory pools;

[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, and using the initialized tracer_node array as a linked list to track free debugging nodes, and then setting the statistical information and flag bits in tracer_list, and creating a mutex to protect concurrent access to debugging information, and setting tracer_list.init to true, indicating that the debugging information has been initialized;

[0053] The recording memory allocation and release function is called when the memory pool allocates memory, records the allocated memory block, file name, line number and other information, locks the mutex to prevent data inconsistency caused by concurrent access, checks the validity of the memory block, and allocates a debugging node 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 is unlocked to release the lock;

[0054] The allocation and release statistics are obtained, and the call is made when the memory pool releases memory, the released memory block information is recorded, the mutex is locked, the count of memory allocation and release is reduced, and the corresponding memory block is searched and removed from the used linked list. When the corresponding memory block cannot be found, the repeated release information is recorded, and 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 counts of current memory allocation and release, 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 of memory usage, memory allocation and release, locking the mutex to protect the reading of the debugging information, and then collecting statistical information and outputting the detailed status of the memory pool, including the usage of each memory pool, repeated allocation statistics of memory blocks, and other information. When there is repeated release, the repeated release records of all memory blocks are listed.

[0057] It also includes data structures and macro definitions, in which tracer_node_t is used to record detailed information of 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 used and unused node lists, 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, identify memory leaks, repeated releases and other memory management problems 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 memory peak usage data within 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] When memory usage exceeds the threshold or the memory peak is abnormal, use memory analysis tools to locate and analyze memory usage, obtain the distribution of memory usage, and determine 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] Use pointer analysis technology to scan the usage of pointers in the code. Use data flow analysis and symbolic execution to determine whether there are wild pointers or null pointers in the code. If so, record the relevant code location. Insert memory operation recording code at the code location of memory allocation and release. By recording the allocation and release of memory objects, determine whether there is a memory secondary release problem. If so, record the code location of the secondary release.

[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 release, and provides location information of the problem code. The information is sent to relevant personnel through the early warning system to help quickly locate and solve memory problems.

[0066] Specifically, the memory monitoring and early warning module realizes 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 detect anomalies and trigger early warnings in a timely manner by comparing with preset thresholds and historical data. 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, the use of wild pointers and null pointers is scanned and recorded, and the problem of secondary memory release is detected by inserting memory operation recording code. Finally, a diagnostic report containing detailed information and code location of memory problems is generated, and relevant personnel are notified in a timely manner through the early warning system, thereby greatly improving the location speed and solution efficiency of memory problems and ensuring the stability and reliability of the system.

[0067] The memory leak detection module uses the memory pool management module and the memory pool debugging module to identify and debug memory leaks by simulating memory leak scenarios. 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 analyzes the debugging information 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 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 annotating the function call for releasing memory in the memory pool management module so that block1 and block2 will not be 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, so that developers can accurately identify memory blocks that have not been correctly released, confirm the existence of memory leaks, and repair them accordingly to prevent waste of memory resources and avoid potential program crash risks.

[0069] Specifically, the effect of the memory leak detection module is reflected in its ability to accurately simulate and identify memory leak problems 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. These 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 release 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] Further, the double release detection module includes: in the memory pool management module, a release mark is maintained for each memory block, the initial value is 0, indicating that the memory block has not been released, when a memory block is applied, its corresponding release mark is set to 0, indicating that the memory block is in an allocated state, when a memory block is released, first determine whether its release mark is 0, if it is 0, then set its mark 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 release error. In the memory pool debugging module, the application and release operations of each memory block are recorded, including operation type, memory address, operation time and other information. When a double release 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 application and release operations, we can determine the cause of double release, such as program logic errors, multi-threaded competition, etc. Based on the analysis results, we can repair the code to ensure that each memory block is released only once to avoid 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 release detection module sets a release mark for each memory block in the memory pool management module to ensure the legitimacy of each release operation and the correct release of the memory block. Ultimately, the double release 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] Further, the memory usage statistics module includes: initializing the 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, and if there are free memory blocks, allocating them to the requester and marking the memory blocks as allocated, and when memory release is required, marking the allocated memory blocks as free 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 usage of the memory pool 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 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 to optimize the memory allocation and release efficiency and improve memory utilization.

[0078] The dangling pointer location module simulates the detection and avoidance of dangling pointer problems by using the memory pool for memory management, writing to the freed memory blocks, simulating dangling pointer problems, and using 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 dangling pointers can lead to unpredictable behavior and possible program crashes.

[0079] 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, 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 in the memory allocation and release process, recording the allocation and release information of each memory block, 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, write data, etc., 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 wild pointer location module effectively tracks and manages the life cycle of memory blocks by creating memory pool objects, defining memory block structures, and recording detailed allocation and release information, so that the detection mechanism can be quickly triggered and key information can be recorded when wild pointer writes occur. This module uses the memory pool debugging module to deeply analyze this information, accurately locate the location and cause of wild pointer problems, and provide practical debugging information and repair suggestions, thereby helping developers quickly solve problems and optimize code, enhancing the robustness and security of the program.

[0082] Through its modular architecture, it achieves efficient memory management and problem diagnosis: the memory pool management module (mpool) is responsible for basic memory allocation and release operations, while maintaining the management structure of memory blocks; the memory pool debugging module (mpool_debug) works closely with mpool to track memory usage by recording detailed information on each memory allocation and release, and provides debugging support for issues such as memory leaks, double releases, and wild pointers; the memory monitoring and early warning module monitors memory usage in real time, and once an anomaly such as a memory leak or illegal operation is detected, the early warning mechanism is immediately triggered to quickly locate the source of the problem. These modules work together to not only optimize the efficiency of memory resource use, but also significantly improve the stability and reliability of the system through real-time monitoring and early warning mechanisms.

[0083] The above description is only a preferred embodiment of the present invention and does not limit the present invention in any form. Although the present invention has been disclosed as a preferred embodiment as above, it is not used to limit the present invention. Any technical personnel in this field can make some changes or modify the technical contents disclosed above into equivalent embodiments without departing from the scope of the technical solution of the present invention. 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 early 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 code module of the memory pool, and helps developers detect memory management problems by tracking and recording the usage of the memory pool; 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, monitor memory leaks, wild pointers, null pointers and secondary releases, and quickly locate problems through the early warning system; The memory leak detection module, by simulating the memory leak scenario, uses the memory pool management module and the memory pool debugging module to identify and debug the memory leak problem, simulates the memory leak by deliberately not releasing the memory block, and uses the memory_pool_debug_trace function to output the current state of the memory pool, and analyzes the debugging information to confirm the memory leak; The double release detection module simulates releasing the same memory block twice in the memory pool and how to use the memory pool debugging module to identify and debug double release, by deliberately releasing the same memory block multiple times to simulate the problem of double release, and analyzing debugging information to identify double release; 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 the memory usage, and analyzing the allocation and release of memory blocks by printing the memory pool usage; The wild pointer location 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. The memory allocation system with memory monitoring and early warning system according to claim 1, characterized in that: The memory pool management module comprises: initializing the memory pool according to the size of the memory pool and the specification of the memory block, dividing the memory pool into a plurality of memory blocks, and setting the state of the memory block to free; when memory allocation is required, traversing the memory blocks in the memory pool, finding the first free memory block, setting its state 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 status information of the memory pool is updated. During the memory allocation process, the allocation of 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 status information of the memory pool is updated; Scan the memory pool regularly and release the 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 the usage of the memory pool and dynamically adjust the size of the memory pool and the specifications of the memory blocks.

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, and using the initialized tracer_node array as a linked list to track free debugging nodes, and then setting the statistical information and flag bits in tracer_list, and creating a mutex to protect concurrent access to debugging information, and setting tracer_list.init to true, indicating 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 lock and check the validity of the memory block, and allocate a debugging node 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; The allocation and release statistics are obtained, and the call is made when the memory pool releases memory, the released memory block information is recorded, the mutex is locked to reduce the count of memory allocation and release, and the corresponding memory block is searched and removed from the used linked list. When the corresponding memory block cannot be found, the repeated release information is recorded, and 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 counts of current memory allocation and release, 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 is 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 statistical information on memory usage, memory allocation and release, locking the mutex to protect the reading of the debugging information, and then collecting statistical information and outputting the detailed status of the memory pool. The output includes the usage of each memory pool, the repeated allocation statistics of memory blocks, and 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 memory peak usage data within 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; When memory usage exceeds the threshold or the memory peak is abnormal, use memory analysis tools to locate and analyze memory usage, obtain the distribution of memory usage, and determine 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. Use pointer analysis technology to scan the usage of pointers in the code. Use data flow analysis and symbolic execution technology to determine whether there are wild pointers or null pointers in the code. If so, record the relevant code location. Insert memory operation recording code at the code location of memory allocation and release. By recording the allocation and release of memory objects, determine whether there is a memory secondary release problem. If so, record the code location of the secondary release. 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 release, and provides location information of 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 and the file name and line number at the time of allocation, annotating a function call for releasing memory in a memory pool management module so that block1 and block2 will not be released, simulating a memory leak scenario that causes memory exhaustion in an actual application, and outputting the current state of the memory pool by executing a memory_pool_debug_trace function, so that 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, a release mark is maintained for each memory block, the initial value is 0, indicating that the memory block is not released, when a memory block is applied, the corresponding release mark is set to 0, indicating that the memory block is in an allocated state, when a memory block is released, firstly judging whether its release mark is 0, when it is 0, then setting its mark 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 release error. In the memory pool debugging module, the application and release operations of each memory block are recorded. When a double release 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 double release can be determined, and the code can be 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 the 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, and when there are free memory blocks, allocating them to the requester 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 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 state 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 an allocated state, returning the starting address of the memory block, marking the specified memory block as a released state through a memory release function, and re-adding it to the memory pool, wherein in 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 to record the relevant information of the write operation 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

  • Method for capturing function for resulting in memory rewriting by wild pointer

    CN101169752A

  • Memory leak detecting method and device

    CN106610892A

  • Memory management method and device

    CN116382919A

  • Method for positioning memory leak

    CN119149285A