Memory usage tracking method and storage medium

By introducing a unified structure for the header space and data space and a circular doubly linked list for management during the memory allocation phase, the problem of lacking full lifecycle tracking in memory management is solved, achieving secure, reliable, and efficient management of memory usage, and providing precise methods for memory leak location and optimization.

CN122152566APending Publication Date: 2026-06-05GUANGZHOU THINKER TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
GUANGZHOU THINKER TECH CO LTD
Filing Date
2026-01-29
Publication Date
2026-06-05

AI Technical Summary

Technical Problem

Existing memory management technologies lack a continuous tracking and unified management mechanism for the entire lifecycle of memory blocks, making it difficult to monitor memory usage in real time. This leads to security risks such as out-of-bounds access, illegal access, or repeated releases. Furthermore, existing analysis methods are inefficient, costly, and difficult to locate memory leaks.

Method used

By introducing a unified structure including header space and data space during the memory allocation phase, memory management information is recorded, and the target memory block is linked to a circular doubly linked list, thus achieving orderly organization and efficient traversal of memory blocks. Combined with the verification and safe release process of memory access and release, the linked list is periodically traversed to output memory usage tracking results.

Benefits of technology

It achieves traceability and consistency of memory usage, improves security and reliability, detects abnormal states in a timely manner, prevents problems such as memory leaks, provides accurate memory usage tracking results, and reduces system operating burden.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122152566A_ABST
    Figure CN122152566A_ABST
Patent Text Reader

Abstract

The application relates to the technical field of memory detection, in particular to a memory usage tracking method and a storage medium. The method constructs a target memory block containing a header space and a data space through obtaining a memory allocation request, records memory management information in the header space, and hangs the target memory block to a ring-shaped bidirectional linked list; in a memory access process, the validity of the target memory block is checked and access time information is updated, and safe release is performed according to the memory management information when releasing; through periodic traversal of the ring-shaped bidirectional linked list, in combination with the time information and the reference state of the target memory block, a memory usage tracking result is generated, and continuous monitoring and abnormal occupation analysis of the memory life cycle are realized. The application realizes traceability, checkability and analysis of the memory usage state, thereby effectively improving the safety, reliability and problem positioning efficiency of memory management.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of memory detection technology, and in particular to a memory usage tracking method and storage medium. Background Technology

[0002] In related technologies, with the rapid development of IoT systems, cloud computing platforms, and various embedded devices, software systems are increasingly reliant on memory resources. Memory management has become a crucial foundational technology for ensuring stable system operation and service continuity. Existing software systems typically use dynamic memory allocation mechanisms to request and release memory space during program execution to meet the operational needs of different functional modules. They also manage memory resources uniformly through the operating system or runtime environment to support high-concurrency, high-throughput application scenarios.

[0003] However, existing memory management and usage monitoring typically focus only on memory allocation and release, lacking a continuous tracking and unified management mechanism for the entire lifecycle of memory blocks, making it difficult to grasp memory usage status in real time. On the one hand, in existing solutions, memory management information and memory data are often stored in a scattered manner or maintained only in local stages, failing to perform consistency checks at critical stages such as memory access and release, thus making it difficult to detect security risks such as out-of-bounds access, illegal access, or duplicate release in a timely manner. On the other hand, existing memory usage analysis mostly relies on post-event statistics or coarse-grained scanning methods, which have low traversal efficiency and poor real-time performance, and can easily introduce significant performance overhead when the memory scale is large, affecting system operating efficiency. In addition, for issues such as memory leaks and long-term unreleased memory, existing technologies lack fine-grained judgment methods that combine access time and reference status, resulting in difficulties in locating abnormal memory and high investigation costs.

[0004] In summary, the technical problems existing in the relevant technologies need to be improved. Summary of the Invention

[0005] The main objective of this application is to propose a memory usage tracking method and storage medium to enable the tracking, verification, and analysis of memory usage status, thereby effectively improving the security, reliability, and problem location efficiency of memory management.

[0006] To achieve the above objectives, one aspect of this application proposes a memory usage tracking method, the method comprising the following steps:

[0007] Get memory allocation request; A target memory block is constructed based on the memory allocation request; the target memory block includes a header space and a data space. The header space records the memory management information corresponding to the target memory block; The target memory block is attached to a circular doubly linked list; When accessing the target memory block, the validity of the target memory block is verified, and the corresponding access time information is updated. When releasing the target memory block, a safe release process is performed on the target memory block according to the memory management information; The circular doubly linked list is periodically traversed, and memory usage tracking results are output based on the time information and reference status of the target memory block.

[0008] In some embodiments, the memory management information includes memory block valid identification information, memory block request location information, memory block access time information, memory block reference count information, and data space offset information.

[0009] In some embodiments, attaching the target memory block to a circular doubly linked list includes: After creating the target memory block, the forward and backward pointers corresponding to the target memory block are initialized based on the header space; Set the forward pointer of the target memory block to the head node of the circular doubly linked list, and set the backward pointer of the target memory block to the next node after the head node; Update the pointer information of the head node and the next node so that the target memory block is inserted into the circular doubly linked list to form a doubly linked structure.

[0010] In some embodiments, the circular doubly linked list includes a head node and multiple memory nodes. The forward and backward pointers of the head node initially point to itself. Each memory node corresponds to a target memory block and forms a circular doubly linked structure with other memory nodes or the head node through the forward and backward pointers.

[0011] In some embodiments, when accessing the target memory block, verifying the validity of the target memory block and updating the corresponding access time information includes: When accessing the target memory block, obtain the memory management information corresponding to the target memory block, and read the valid identification information of the memory block; The valid identifier information of the memory block is compared with a preset valid identifier value to determine whether the target memory block is in a valid state. When it is determined that the target memory block is in a valid state, the access time information is obtained and the access time information is written into the memory block access time information in the memory management information; Based on the data space offset information, calculate the starting address of the data space of the target memory block, and return the starting address of the data space for access; If it is determined that the target memory block is not in a valid state, the access operation to the target memory block is terminated.

[0012] In some embodiments, when releasing the target memory block, performing a safe release process on the target memory block according to the memory management information includes: When releasing the target memory block, verify the valid identifier information of the memory block in the memory management information; When the target memory block is determined to be in a valid state, the valid identification information of the memory block is updated to an invalid state, and the target memory block is removed from the circular doubly linked list; Obtain the destructor associated with the target memory block in the header space; When the destructor is not empty, the corresponding destructor is called to release the resources associated with the target memory block; After the destructor call is completed, the memory space occupied by the target memory block is released.

[0013] In some embodiments, the destructor is managed by a virtual function table stored in the header space and is used to perform destructing and releasing operations on external resources or sub-memory blocks associated with the target memory block before releasing the target memory block.

[0014] In some embodiments, the periodic traversal of the circular doubly linked list, based on the time information and reference status of the target memory block, outputs memory usage tracking results, including: The circular doubly linked list is traversed according to a preset period to obtain the target memory block corresponding to each memory node. Read the access time information and reference count information corresponding to each target memory block, and calculate the lifespan of the target memory block based on the current time; Based on the survival time, access time information, and reference count information, the corresponding memory usage tracking results are output.

[0015] To achieve the above objectives, another aspect of this application proposes a memory usage tracking system, the system comprising: The acquisition module is used to acquire memory allocation requests; A memory construction module is used to construct a target memory block based on the memory allocation request; the target memory block includes a header space and a data space. The information recording module is used to record memory management information corresponding to the target memory block according to the header space; The linked list management module is used to attach the target memory block to a circular doubly linked list. The access control module is used to verify the validity of the target memory block when accessing the target memory block and update the corresponding access time information. The release module is used to perform a safe release process on the target memory block according to the memory management information when releasing the target memory block; The output module is used to periodically traverse the circular doubly linked list and output memory usage tracking results based on the time information and reference status of the target memory block.

[0016] To achieve the above objectives, another aspect of this application provides an electronic device, which includes a memory and a processor. The memory stores a computer program, and the processor executes the computer program to implement the above-described method.

[0017] To achieve the above objectives, another aspect of the embodiments of this application proposes a computer-readable storage medium storing a computer program that, when executed by a processor, implements the above-described method.

[0018] To achieve the above objectives, another aspect of this application provides a computer program product, including a computer program that, when executed by a processor, implements the above-described method.

[0019] The embodiments of this application include at least the following beneficial effects: This application provides a memory usage tracking method and storage medium. This scheme introduces a unified structure including a header space and a data space for the target memory block during the memory allocation phase, and centrally records memory management information in the header space, making the memory allocation, access, and release process traceable and consistent. By attaching the target memory block to a circular doubly linked list, the orderly organization and efficient traversal of in-use memory blocks are achieved, avoiding the performance overhead of traditional linear scanning. During memory access, by real-time verification of the validity of the target memory block and synchronously updating the access time information, abnormal memory states such as out-of-bounds access, illegal access, or long-term inaccessibility can be detected in a timely manner, improving the security and reliability of memory usage. During the memory release phase, safe release processing is performed in conjunction with memory management information, effectively preventing problems such as duplicate release, dangling pointers, and memory leaks. Furthermore, by periodically traversing the circular doubly linked list and comprehensively outputting the memory usage tracking results based on the time information and reference status of the target memory block, accurate basis can be provided for memory leak location, performance analysis, and resource optimization, thereby achieving continuous monitoring and fine-grained management of memory usage status without significantly increasing the system's operating burden. Attached Figure Description

[0020] Figure 1 This is a flowchart illustrating a memory usage tracking method provided in an embodiment of this application; Figure 2 yes Figure 1 A flowchart illustrating step S5 in the process; Figure 3 yes Figure 1 A flowchart illustrating step S6 in the process; Figure 4 This is a schematic diagram of a memory usage tracking system provided in an embodiment of this application. Detailed Implementation

[0021] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of this application and are not intended to limit it. In the following description, when referring to the accompanying drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with those of this application; they are merely examples of apparatuses and methods consistent with some aspects of the embodiments of this application as detailed in the appended claims.

[0022] It is understood that the terms “first,” “second,” etc., used in this application may be used herein to describe various concepts, but unless otherwise stated, these concepts are not limited by these terms. These terms are only used to distinguish one concept from another. For example, without departing from the scope of the embodiments of this application, first information may also be referred to as second information, and similarly, second information may also be referred to as first information. Depending on the context, the words “if,” “when,” or “in response to a determination” as used herein may be interpreted as “when…” or “when…” or “in response to a determination.”

[0023] As used in this application, the terms "at least one", "multiple", "each", "any", etc., "at least one" includes one, two or more, "multiple" includes two or more, "each" refers to each of the corresponding multiples, and "any" refers to any one of the multiples.

[0024] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application belongs. The terminology used herein is for the purpose of describing embodiments of this application only and is not intended to limit this application.

[0025] This application provides a memory usage tracking method and storage medium. This scheme introduces a unified structure including a header space and a data space for the target memory block during the memory allocation phase, and centrally records memory management information in the header space, making the memory allocation, access, and release processes traceable and consistent. By linking the target memory block to a circular doubly linked list, it achieves ordered organization and efficient traversal of in-use memory blocks, avoiding the performance overhead of traditional linear scanning. During memory access, by real-time verification of the validity of the target memory block and synchronously updating access time information, abnormal memory states such as out-of-bounds access, illegal access, or long-term inaccessibility can be detected in a timely manner, improving the security and reliability of memory usage. During the memory release phase, safe release processing is performed in conjunction with memory management information, effectively preventing problems such as duplicate releases, dangling pointers, and memory leaks. Furthermore, by periodically traversing the circular doubly linked list and comprehensively analyzing the time information and reference status of the target memory block to output memory usage tracking results, it can provide accurate basis for memory leak location, performance analysis, and resource optimization, thereby achieving continuous monitoring and refined management of memory usage status without significantly increasing the system's operational burden.

[0026] This application provides a memory usage tracking method, relating to the field of memory detection technology. The memory usage tracking method provided in this application can be applied to a terminal, a server, or software running on a terminal or server. In some embodiments, the terminal can be a smartphone, tablet, laptop, desktop computer, smart speaker, smartwatch, or in-vehicle terminal, but is not limited to these. The server can be configured as an independent physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms. The server can also be a node server in a blockchain network. The software can be an application implementing a memory usage tracking method, but is not limited to the above forms.

[0027] This application can be used in a wide variety of general-purpose or special-purpose computer system environments or configurations. Examples include: personal computers, server computers, handheld or portable devices, tablet devices, multiprocessor systems, microprocessor-based systems, set-top boxes, programmable consumer electronics devices, network PCs, minicomputers, mainframe computers, and distributed computing environments including any of the above systems or devices. This application can be described in the general context of computer-executable instructions executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, etc., that perform specific tasks or implement specific abstract data types. This application can also be practiced in distributed computing environments where tasks are performed by remote processing devices connected via a communication network. In distributed computing environments, program modules can reside in local and remote computer storage media, including storage devices.

[0028] Figure 1 This is an optional flowchart of a memory usage tracking method provided in an embodiment of this application. Figure 1 The method may include, but is not limited to, steps S1 to S7: S1: Obtain a memory allocation request; In this embodiment, obtaining a memory allocation request refers to the memory request operation initiated to the memory tracking module through a unified encapsulated memory allocation interface when the business logic needs to dynamically create data objects, cache data, or temporarily store calculation results during the application's operation.

[0029] Unlike the traditional direct invocation of system memory allocation functions, this embodiment centrally intercepts and processes memory allocation requests, ensuring that each memory request is uniformly managed through a memory usage tracking method.

[0030] Specifically, when a memory allocation request is triggered, in addition to the size information of the target data space to be requested, the program context information corresponding to the request is also collected simultaneously. The program context information includes, but is not limited to: the data structure type and size of the currently requested memory, the name of the source code file that triggered the memory allocation, the name of the function it belongs to, and the corresponding line number. This information is automatically obtained through compile-time macros or predefined interfaces, thus avoiding omissions or inconsistencies caused by manual input.

[0031] In this way, the memory allocation request carries complete information about the size and location of the memory request before entering the subsequent processing flow, providing a reliable data foundation for the subsequent construction of the target memory block and the recording of memory management information.

[0032] S2: Construct the target memory block based on the memory allocation request; the target memory block includes header space and data space; In this embodiment, the actual total size of the target memory block is calculated based on the target data space size information carried in the memory allocation request and in conjunction with the predefined memory header structure size. The target memory block is a contiguous memory region consisting of a header space and a data space arranged sequentially.

[0033] The header space, located at the beginning of the target memory block, stores management and control information for the memory block. The data space, immediately following the header space, stores the data actually accessed and used by the application. By allocating contiguous memory in a single request, the complexity of address management and performance degradation caused by the dispersed allocation of header information and data space are avoided.

[0034] In the specific implementation process, the underlying memory allocation function is called to request the target memory block calculated above. After the memory allocation is successful, the starting offset of the data space is calculated and set according to the structural length of the header space, so that the actual address of the data space can be quickly located by the offset when accessing the target memory block later.

[0035] By constructing the target memory blocks as described above, each allocated memory block naturally possesses a unified structure of "management information + data content," providing a structural foundation for subsequent memory management information recording, access verification, lifecycle tracking, and secure release.

[0036] Specifically, the header space is used to store the doubly linked list pointer, the memory block validity flag MAGIC, the memory block allocation location constant L, the reason for the memory block allocation, the memory block allocation and access time, the address of the related parent memory block, the virtual function list V, the number of memory block references R, the offset of the data space, and the size of the data space. The data space is used to store user data.

[0037] S3: Record the memory management information corresponding to the target memory block according to the header space record; among which, the memory management information includes memory block valid identification information, memory block allocation location information, memory block access time information, memory block reference count information, and data space offset information.

[0038] In this embodiment, the header space of the target memory block is used to initialize and record the memory management information corresponding to the target memory block. The header space is located before the data space of the target memory block and is used to centrally store control data related to the memory block's lifecycle, access status, and source information, so as to ensure independent management of a single memory block without introducing additional external structures.

[0039] Specifically, in this embodiment, memory block validity identification information is written into the header space to indicate whether the target memory block is currently in a valid state. When the target memory block is successfully allocated, the validity identification information is set to a predetermined valid value; during subsequent release or invalidation processing, the validity identification information is updated to an invalid value, so that the legality of the target memory block can be determined by verifying the validity identification information before performing access or release operations. At the same time, the memory block allocation location information corresponding to the target memory block is written into the header space, establishing a one-to-one correspondence between the target memory block and its allocation location in the source code.

[0040] Furthermore, the access time information, reference count information, and data space offset information of the target memory block are recorded in the header space. The access time information records the timestamps corresponding to the creation and each access of the target memory block, and is updated during the access process. The reference count information reflects the current reference status of the target memory block and is adjusted accordingly when reference relationships change. The data space offset information indicates the starting position of the data space relative to the header space, enabling accurate calculation and acquisition of the data space address based on the offset information when accessing the data space of the target memory block. By recording the above memory management information, validity verification, access tracking, and safe release processing of the target memory block can be performed in subsequent steps.

[0041] S4: Add the target memory block to the circular doubly linked list; The process of attaching the target memory block to the circular doubly linked list includes: S41: After creating the target memory block, initialize the forward and backward pointers corresponding to the target memory block based on the header space; S42: Set the forward pointer of the target memory block to the head node of the circular doubly linked list, and set the backward pointer of the target memory block to the next node after the head node; S43: Update the pointer information of the head node and the next node so that the target memory block is inserted into the circular doubly linked list to form a doubly linked structure; wherein, the circular doubly linked list includes a head node and multiple memory nodes. The forward pointer and backward pointer of the head node initially point to itself. Each memory node corresponds to a target memory block and forms a circular doubly linked structure with other memory nodes or the head node through the forward pointer and backward pointer.

[0042] In this embodiment, after creating the target memory block and writing the basic information of the header space, the target memory block is attached to a pre-maintained circular doubly linked list so that all allocated but not released target memory blocks can be tracked uniformly by traversing the list. The circular doubly linked list has a fixed head node. The head node does not carry actual data space and is only used to mark the boundary between the start and end of the list. During initialization, the forward and backward pointers of the head node both point to itself, so that the structure remains closed even when the list is empty.

[0043] Specifically, after creating the target memory block, the forward and backward pointers corresponding to the target memory block are initialized based on the header space of the target memory block. The forward and backward pointers are linked list pointer fields in the header space, used to establish bidirectional links with other memory nodes. Subsequently, the forward pointer of the target memory block is set to point to the head node, and the backward pointer of the target memory block is set to point to the next node after the head node. In the scenario where the linked list is empty, the next node after the head node is the head node itself, so the backward pointer of the target memory block will point to the head node, thus still satisfying the pointing constraint of the circular structure.

[0044] Furthermore, to ensure the target memory block is inserted and forms a circular doubly linked list, the pointer information of the head node and the next node is updated. This ensures that the head node's backward pointer points to the target memory block, and the next node's forward pointer points to the target memory block. This positions the target memory block between the head node and the next node, establishing a bidirectional link with both nodes. After insertion, the circular doubly linked list remains closed: each memory node corresponds to a target memory block and is connected to other memory nodes or the head node via forward and backward pointers. When inserting a new target memory block, the insertion process is repeated to expand the linked list without disrupting the circular structure. This also provides a basis for fast removal during subsequent releases and for periodic traversal output of tracking results.

[0045] S5: When accessing the target memory block, verify the validity of the target memory block and update the corresponding access time information; Among them, reference Figure 2 As shown, when accessing the target memory block, the validity of the target memory block is verified, and the corresponding access time information is updated, including: S51: When accessing the target memory block, obtain the memory management information corresponding to the target memory block and read the valid identification information of the memory block; S52: Compare the valid identifier information of the memory block with the preset valid identifier value to determine whether the target memory block is in a valid state; S53: When it is determined that the target memory block is in a valid state, the access time information is obtained and the access time information is written into the memory block access time information in the memory management information; S54: Based on the data space offset information, calculate the starting address of the data space of the target memory block and return the starting address of the data space for access; S55: If it is determined that the target memory block is not in a valid state, terminate the access operation to the target memory block.

[0046] In this embodiment, when the program needs to perform read or write operations on the data space of the target memory block, it first enters the memory access verification process instead of directly returning the data space address to the caller. By introducing a unified verification step before actual access, access to memory blocks that have been released, corrupted, or illegally constructed can be avoided, thereby reducing runtime risks such as memory out-of-bounds access and dangling pointer access, and ensuring the security and controllability of memory access behavior.

[0047] Specifically, when accessing a target memory block, the system first obtains the memory management information corresponding to that target memory block and then reads the memory block validity identifier information from the header space of the target memory block. The memory block validity identifier information is set to a preset valid identifier value when the target memory block is created and updated to an invalid value when the target memory block is released. Therefore, it can accurately reflect whether the current target memory block is still within its legal lifecycle. By reading this validity identifier information, the validity of a single target memory block can be independently determined without relying on external states.

[0048] Subsequently, the read memory block validity identifier information is compared with a preset validity identifier value to determine whether the target memory block is in a valid state. If the comparison result indicates that the target memory block is not in a valid state, the access operation to the target memory block is directly terminated, and subsequent data address calculation and return processing are no longer performed, thereby avoiding access to freed memory or illegal memory regions. In this way, abnormal access behavior can be blocked in time at the access entry point, improving the security of memory use.

[0049] When the target memory block is determined to be in a valid state, the access record update operation continues. Specifically, the current access time information is obtained and written into the memory block access time information in the memory management information to record the time node when the target memory block was last accessed. This access time information can serve as an important basis for determining whether the target memory block has not been accessed for a long time when traversing the circular doubly linked list subsequently, thereby helping to identify memory blocks suspected of memory leaks or abnormal usage.

[0050] After updating the access time information, the starting address of the target memory block's data space is calculated based on the data space offset information in the memory management information, and this starting address is returned for access. The data space offset information indicates the starting position of the data space relative to the header space, providing the caller with a valid access address pointing only to the data space without exposing the header space, thus achieving logical isolation between memory management information and user data. Through the above access process, both the convenience of data access and the traceability and security of memory usage are ensured.

[0051] S6: When releasing the target memory block, perform a safe release process on the target memory block according to the memory management information; Among them, reference Figure 3 As shown, when releasing the target memory block, a safe release process is performed on the target memory block according to the memory management information, including: S61: When releasing the target memory block, verify the valid identification information of the memory block in the memory management information; S62: When the target memory block is determined to be in a valid state, update the memory block validity identification information to invalid state and remove the target memory block from the circular doubly linked list; S63: Retrieve the destructor associated with the target memory block in the header space; S64: When the destructor is not empty, call the corresponding destructor to release the resources associated with the target memory block; the destructor is managed by the virtual function table stored in the header space, and is used to perform destructing and releasing operations on the external resources or sub-memory blocks associated with the target memory block before releasing the target memory block.

[0052] S65: After the destructor call is completed, release the memory space occupied by the target memory block.

[0053] In this embodiment, when the program finishes using the data space of the target memory block and triggers a release operation, it does not directly call the underlying release function to reclaim the memory. Instead, it first executes a safe release process based on the memory management information recorded in the header space of the target memory block. By introducing steps such as validity verification, linked list consistency maintenance, and destructing release at the release entry point, problems such as duplicate release, dangling pointer release, and resource leaks can be avoided. Furthermore, it ensures that subsequent traversal of the circular doubly linked list and output of the tracking results will not include any released invalid nodes.

[0054] Specifically, when releasing a target memory block, the memory management information corresponding to that target memory block is first obtained, and the memory block validity identification information within it is verified. The validity identification information is set to a preset validity identification value after the target memory block is created, indicating that the memory block is in a usable state; when the target memory block has been released or an exception occurs, this validity identification information will no longer meet the validity conditions. By verifying the validity identification information, it is determined whether the release request is for a target memory block that is still in a legitimate lifecycle, thereby blocking duplicate or illegal releases at the source.

[0055] When the target memory block is determined to be valid, its validity flag is updated to invalid, ensuring it is unavailable during any subsequent access or release operations. The target memory block is then removed from the circular doubly linked list to maintain its structure and prevent further access during traversal. Specifically, the removal process updates the pointer relationships between adjacent nodes based on the forward and backward pointers maintained in the target memory block's header space, ensuring direct connection between the released node's preceding and following nodes. This allows the list removal operation to be completed in constant time while maintaining the closed circular doubly linked list.

[0056] After removing the linked list, the destructors associated with the target memory block are retrieved from the header space. These destructors are managed by a virtual function table stored in the header space. This table records callback entries used to release resources, adapting to the cleanup requirements of different data structures before release. This approach allows for the execution of external resource release operations or linked release operations of sub-memory blocks related to the target memory block before reclaiming memory space, avoiding the loss of handles, caches, file descriptors, or associated memory blocks while only reclaiming memory.

[0057] When the destructor is not null, the corresponding destructor is called to release the resources associated with the target memory block. Specifically, the destructor can clean up the resources bound to the target memory block item by item based on the association information recorded in the header space. In scenarios where there are parent and child memory block associations, the destructor can also trigger the release operation of the relevant child memory blocks, thereby ensuring that no dangling references or indirect leaks are left after the target memory block is released. If the destructor is null, the destructor step is skipped, and the memory space reclamation process is entered directly to reduce unnecessary call overhead.

[0058] After the destructor call is completed, the memory space occupied by the target memory block is released. Since the target memory block consists of a contiguous header space and data space, the entire memory block can be reclaimed by performing a one-time release at its starting address. At the same time, since the valid identification information has been invalidated and the linked list has been removed before the release, even if there is subsequent misuse, it can be detected and blocked in time during the access verification phase, thereby achieving safe release and controllable management of the target memory block throughout its entire lifecycle.

[0059] S7: Periodically traverse the circular doubly linked list, and output memory usage tracking results based on the time information and reference status of the target memory block.

[0060] The process involves periodically traversing a circular doubly linked list and, based on the time information and reference status of the target memory block, outputting memory usage tracking results, including: S71: Traverse the circular doubly linked list according to a preset period to obtain the target memory block corresponding to each memory node; S72: Read the access time information and reference count information corresponding to each target memory block, and calculate the lifespan of the target memory block based on the current time; S73: Based on the lifespan, access time, and reference count information, output the corresponding memory usage tracking results.

[0061] In this embodiment, a traversal operation is performed on the circular doubly linked list according to a preset period to check all memory nodes currently in the list. During traversal, the head node is used as the start and end marker, and the target memory block corresponding to each memory node is obtained sequentially starting from the next node after the head node; when the traversal returns to the head node again, the traversal is considered complete. By adopting a circular doubly linked list structure and using the head node as a sentinel node, stable traversal can be completed without introducing additional boundary checks.

[0062] When traversing each target memory block, the access time information and reference count information recorded in the header space of that target memory block are read, and the lifespan of the target memory block is determined by combining this information with the current time. The access time information reflects the time when the target memory block was last accessed or created, and the reference count information reflects the current reference status of the target memory block. By reading and analyzing the above information, the activity level of each target memory block during operation can be intuitively reflected.

[0063] After acquiring the above information, based on the lifetime, access time, and reference count, the corresponding memory usage tracking results are output. These results characterize the usage status of the target memory block and may include information for locating memory allocation positions. This helps developers quickly pinpoint potential memory leaks or abnormal memory usage when a target memory block has been inaccessible for an extended period or has an abnormal reference status, thus enabling continuous tracking and analysis of memory usage.

[0064] This embodiment specifically illustrates the use of memory blocks in C language, as follows: Initialize the resource ID when allocating memory blocks, and set the type, size, line number, file name, function name, and data structure type; Calculate the actual size S of the requested memory block, which is the size of the requested memory space plus the size of the header data structure; Call the system function to request a memory block M of size S; Set the MAGIC value of memory block M to 0xA8B4C2D1; Save the address of the resource ID to memory space M; Set the timestamp T for the allocation of memory block M; Set the offset of memory block M; Set the data space size of memory block M; Add the memory block to the circular linked list; The memory block access process is as follows: Check if the MAGIC value of memory block M is 0xA8B4C2D1. If not, exit access; otherwise, continue access. Set the access timestamp T for memory block M; It calculates and returns the data space, allowing programs to read and write the actual data space content.

[0065] The memory block is released as follows: Check if the MAGIC value of memory block M is 0xA8B4C2D1. If not, exit the deletion process; otherwise, continue the deletion process. Set the MAGIC value of memory block M to 0; Remove memory block M from the circular linked list; Check if the destructor in memory block M is not empty; if not, call the destructor first. Call the system function to release the memory block.

[0066] Traversing all memory blocks in the circular doubly linked list is as follows: Read the start header node; this node is only used as the start and end node and does not store memory blocks. Read the next node. If it is equal to the starting node, exit. Otherwise, output the memory block information. Repeat this step until exit.

[0067] Please see Figure 4 This application also provides a memory usage tracking system, the system comprising: The acquisition module is used to acquire memory allocation requests; The memory construction module is used to construct a target memory block based on a memory allocation request; the target memory block includes header space and data space. The information recording module is used to record memory management information corresponding to the target memory block based on the header space; The linked list management module is used to attach target memory blocks to a circular doubly linked list; The access control module is used to verify the validity of the target memory block when accessing it, and to update the corresponding access time information. The release module is used to perform safe release processing on the target memory block based on memory management information when releasing the target memory block; The output module is used to periodically traverse the circular doubly linked list and output memory usage tracking results based on the time information and reference status of the target memory block.

[0068] It is understood that the content of the above method embodiments is applicable to this system embodiment. The specific functions implemented in this system embodiment are the same as those in the above method embodiments, and the beneficial effects achieved are also the same as those achieved in the above method embodiments.

[0069] This application also provides an electronic device, which includes a memory and a processor. The memory stores a computer program, and the processor executes the computer program to implement the above-described method. This electronic device can be any smart terminal, including tablet computers, in-vehicle computers, etc.

[0070] It is understood that the content of the above method embodiments is applicable to this device embodiment. The specific functions implemented by this device embodiment are the same as those of the above method embodiments, and the beneficial effects achieved are also the same as those achieved by the above method embodiments.

[0071] This application also provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the above-described method.

[0072] It is understood that the content of the above method embodiments is applicable to this storage medium embodiment. The specific functions implemented in this storage medium embodiment are the same as those in the above method embodiments, and the beneficial effects achieved are also the same as those achieved in the above method embodiments.

[0073] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the above-described method.

[0074] It is understood that the content of the above method embodiments is applicable to the embodiments of this program product. The specific functions implemented by the embodiments of this program product are the same as those of the above method embodiments, and the beneficial effects achieved are also the same as those achieved by the above method embodiments.

[0075] Memory, as a non-transitory computer-readable storage medium, can be used to store non-transitory software programs and non-transitory computer-executable programs. Furthermore, memory may include high-speed random access memory, and may also include non-transitory memory, such as at least one disk storage device, flash memory device, or other non-transitory solid-state storage device. In some embodiments, memory may optionally include memory remotely located relative to the processor, and these remote memories can be connected to the processor via a network. Examples of such networks include, but are not limited to, the Internet, intranets, local area networks, mobile communication networks, and combinations thereof.

[0076] This application provides a memory usage tracking method and storage medium. This scheme introduces a unified structure including a header space and a data space for the target memory block during the memory allocation phase, and centrally records memory management information in the header space, making the memory allocation, access, and release processes traceable and consistent. By linking the target memory block to a circular doubly linked list, it achieves ordered organization and efficient traversal of in-use memory blocks, avoiding the performance overhead of traditional linear scanning. During memory access, by real-time verification of the validity of the target memory block and synchronously updating access time information, abnormal memory states such as out-of-bounds access, illegal access, or long-term inaccessibility can be detected in a timely manner, improving the security and reliability of memory usage. During the memory release phase, safe release processing is performed in conjunction with memory management information, effectively preventing problems such as duplicate releases, dangling pointers, and memory leaks. Furthermore, by periodically traversing the circular doubly linked list and comprehensively analyzing the time information and reference status of the target memory block to output memory usage tracking results, it can provide accurate basis for memory leak location, performance analysis, and resource optimization, thereby achieving continuous monitoring and refined management of memory usage status without significantly increasing the system's operational burden.

[0077] The embodiments described in this application are for the purpose of more clearly illustrating the technical solutions of the embodiments of this application, and do not constitute a limitation on the technical solutions provided by the embodiments of this application. As those skilled in the art will know, with the evolution of technology and the emergence of new application scenarios, the technical solutions provided by the embodiments of this application are also applicable to similar technical problems.

[0078] Those skilled in the art will understand that the technical solutions shown in the figures do not constitute a limitation on the embodiments of this application, and may include more or fewer steps than shown, or combine certain steps, or different steps.

[0079] Those skilled in the art will understand that all or some of the steps in the methods disclosed above, as well as the functional modules / units in the systems and devices, can be implemented as software, firmware, hardware, or suitable combinations thereof.

[0080] The terms “first,” “second,” “third,” “fourth,” etc. (if present) in the specification and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this application described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms “comprising” and “having,” and any variations thereof, are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.

[0081] The preferred embodiments of the present application have been described above with reference to the accompanying drawings, but this does not limit the scope of the claims of the present application. Any modifications, equivalent substitutions, and improvements made by those skilled in the art without departing from the scope and substance of the embodiments of the present application shall be within the scope of the claims of the present application.

Claims

1. A memory usage tracking method, characterized in that, The method includes the following steps: Get memory allocation request; A target memory block is constructed based on the memory allocation request; the target memory block includes a header space and a data space. The header space records the memory management information corresponding to the target memory block; The target memory block is attached to a circular doubly linked list; When accessing the target memory block, the validity of the target memory block is verified, and the corresponding access time information is updated. When releasing the target memory block, a safe release process is performed on the target memory block according to the memory management information; The circular doubly linked list is periodically traversed, and memory usage tracking results are output based on the time information and reference status of the target memory block.

2. The method according to claim 1, characterized in that, The memory management information includes memory block valid identification information, memory block request location information, memory block access time information, memory block reference count information, and data space offset information.

3. The method according to claim 1, characterized in that, The step of attaching the target memory block to the circular doubly linked list includes: After creating the target memory block, the forward and backward pointers corresponding to the target memory block are initialized based on the header space; Set the forward pointer of the target memory block to the head node of the circular doubly linked list, and set the backward pointer of the target memory block to the next node after the head node; Update the pointer information of the head node and the next node so that the target memory block is inserted into the circular doubly linked list to form a doubly linked structure.

4. The method according to claim 3, characterized in that, The circular doubly linked list includes a head node and multiple memory nodes. The forward and backward pointers of the head node initially point to itself. Each memory node corresponds to a target memory block and forms a circular doubly linked structure with other memory nodes or the head node through the forward and backward pointers.

5. The method according to claim 2, characterized in that, The step of verifying the validity of the target memory block and updating the corresponding access time information when accessing the target memory block includes: When accessing the target memory block, obtain the memory management information corresponding to the target memory block, and read the valid identification information of the memory block; The valid identifier information of the memory block is compared with a preset valid identifier value to determine whether the target memory block is in a valid state. When it is determined that the target memory block is in a valid state, the access time information is obtained and the access time information is written into the memory block access time information in the memory management information; Based on the data space offset information, calculate the starting address of the data space of the target memory block, and return the starting address of the data space for access; If it is determined that the target memory block is not in a valid state, the access operation to the target memory block is terminated.

6. The method according to claim 2, characterized in that, When releasing the target memory block, the step of performing a safe release process on the target memory block according to the memory management information includes: When releasing the target memory block, verify the valid identifier information of the memory block in the memory management information; When the target memory block is determined to be in a valid state, the valid identification information of the memory block is updated to an invalid state, and the target memory block is removed from the circular doubly linked list; Obtain the destructor associated with the target memory block in the header space; When the destructor is not empty, the corresponding destructor is called to release the resources associated with the target memory block; After the destructor call is completed, the memory space occupied by the target memory block is released.

7. The method according to claim 6, characterized in that, The destructor is managed through a virtual function table stored in the header space and is used to perform destructing and releasing operations on external resources or sub-memory blocks associated with the target memory block before releasing the target memory block.

8. The method according to claim 1, characterized in that, The periodic traversal of the circular doubly linked list, based on the time information and reference status of the target memory block, outputs memory usage tracking results, including: The circular doubly linked list is traversed according to a preset period to obtain the target memory block corresponding to each memory node. Read the access time information and reference count information corresponding to each target memory block, and calculate the lifespan of the target memory block based on the current time; Based on the survival time, access time information, and reference count information, the corresponding memory usage tracking results are output.

9. A memory usage tracking system, characterized in that, The system includes: The acquisition module is used to acquire memory allocation requests; A memory construction module is used to construct a target memory block based on the memory allocation request; the target memory block includes a header space and a data space. The information recording module is used to record memory management information corresponding to the target memory block according to the header space; The linked list management module is used to attach the target memory block to a circular doubly linked list. The access control module is used to verify the validity of the target memory block when accessing the target memory block and update the corresponding access time information. The release module is used to perform a safe release process on the target memory block according to the memory management information when releasing the target memory block; The output module is used to periodically traverse the circular doubly linked list and output memory usage tracking results based on the time information and reference status of the target memory block.

10. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it implements the method of any one of claims 1 to 8.