A memory tracking method and device, electronic equipment and storage medium

CN122817044APending Publication Date: 2026-09-25DINGDAO ZHILIAN (BEIJING) TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202610725026.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-05-25
Publication Date
2026-09-25

AI Technical Summary

Technical Problem

然而,插桩式跟踪直接修改业务代码,在关键内存操作处显式添加跟踪语句,破坏了代码的整洁性与可维护性,造成严重的代码污染

Benefits of technology

[0030]第四方面,本发明的实施例还提供一种计算机可读存储介质,所述计算机可读存储介质存储有一个或者多个程序,所述一个或者多个程序可被一个或者多个处理器执行,以实现本发明的实施例提供的任一种所述的内存跟踪方法。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122817044A_ABST
    Figure CN122817044A_ABST
Patent Text Reader

Abstract

Embodiments of the present application disclose a memory tracking method and device, electronic equipment and storage medium, and relate to the technical field of computer software, which can guarantee the accuracy of memory tracking without polluting the source code. The method comprises: in response to a target entity calling a memory management interface, intercepting the calling operation of the target entity on the memory management interface through a first allocator, wherein the target entity is a program entity conforming to a preset process marking rule, and the first allocator is a self-defined global allocator; recording memory operation information of the target entity through the first allocator, the memory operation information being a memory operation to be implemented by the target entity calling the memory management interface; and calling a second allocator through the first allocator to call the memory management interface through the second allocator. The present application is suitable for memory tracking.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer software technology, and in particular to a memory tracking method, apparatus, electronic device, and storage medium. Background Technology

[0002] In software development and maintenance, memory leaks and memory overflows are common causes affecting program stability and performance. To locate these problems, it is often necessary to dynamically trace the program's memory operations to obtain information such as the time, size, and call stack of memory allocation and deallocation.

[0003] Instrumentation tracing is a common memory tracing technique that involves manually inserting memory tracing logic into the target program's business logic (e.g., adding recording functions before and after memory allocation). However, instrumentation tracing directly modifies business logic and explicitly adds tracing statements at critical memory operations, compromising code cleanliness and maintainability, resulting in severe code pollution. Furthermore, in complex execution environments such as multi-process, multi-threaded, and coroutine environments, developers must manually pass tracing parameters layer by layer to each child process, thread, and coroutine, which can easily lead to omissions, causing some execution entities' memory behavior to go unrecorded and affecting the accuracy of the tracing results. Summary of the Invention

[0004] In view of this, embodiments of the present invention provide a memory tracing method, apparatus, electronic device, and storage medium that can ensure the accuracy of memory tracing without contaminating the source code.

[0005] In a first aspect, embodiments of the present invention provide a memory tracing method, the method comprising: in response to a target entity calling a memory management interface, intercepting the target entity's call to the memory management interface via a first allocator, wherein the target entity is a program entity conforming to a preset process marking rule, and the first allocator is a custom global allocator; recording memory operation information of the target entity via the first allocator, the memory operation information being the memory operation to be performed by the target entity when calling the memory management interface; and calling a second allocator via the first allocator to call the memory management interface via the second allocator.

[0006] In one implementation, the first allocator is defined by the properties of the global allocator in the programming language.

[0007] In one implementation, the process marking rule includes a process identifier; before intercepting the target entity's call to the memory management interface via the first allocator in response to the target entity's call to the memory management interface, the method further includes: determining whether the first program entity is the target entity to be tracked based on whether the process marking rule contains the process identifier of the first program entity, wherein the first program entity is any of the program entities.

[0008] In one implementation, the method further includes: in response to the process identifier of the first program entity not being included in the process marking rule, directly invoking the second allocator to perform the memory operation.

[0009] In one implementation, the process marking rule includes a process identifier and at least one of the following rule types: entity type, operation type, memory block size, and time type; before intercepting the target entity's call to the memory management interface via the first allocator in response to the target entity's call to the memory management interface, the method further includes: determining whether the first program entity is a candidate entity based on whether the process marking rule contains the process identifier of the first program entity; if the first program entity is the candidate entity, taking the portion of the first program entity that corresponds to the rule type specified in the process marking rule as the target entity.

[0010] In one implementation, the entity type includes at least one of the following: process, subprocess, thread, coroutine.

[0011] In one implementation, the entity type of the target entity is a coroutine. When the first program entity is the candidate entity, determining the portion of the first program entity corresponding to the rule type specified in the process marking rule as the target entity includes: when the first program entity is the candidate entity, determining the portion of the first program entity corresponding to the entity type of coroutine specified in the process marking rule as a coroutine instance conforming to the process marking rule; and determining the target entity from the coroutine instances based on the coroutine sampling rate, wherein the coroutine sampling rate is a value between 0 and 1.

[0012] In one implementation, the operation type includes at least one of the following: memory allocation and memory release.

[0013] In one implementation, the time type includes at least one of the following: time period, time window.

[0014] In one implementation, the process marking rules are stored in a process rule file. The step of determining whether the first program entity is the target entity to be tracked based on whether the process marking rules contain the process identifier of the first program entity includes: in response to a change in the process rule file, confirming the newly added process marking rules; and determining whether the first program entity is the target entity to be tracked based on whether the newly added process marking rules contain the process identifier of the first program entity.

[0015] In one implementation, the step of calling the second allocator through the first allocator to call the memory management interface through the second allocator includes: calling the second allocator and the third allocator through the first allocator to call a portion of the interfaces in the memory management interface through the second allocator, and calling another portion of the interfaces in the memory management interface through the third allocator.

[0016] In one embodiment, after recording the memory operation information of the target entity through the first allocator, the method further includes: statistically analyzing the memory operation information of the target entity recorded by the first allocator; and analyzing the memory risk of the target entity based on the statistical results, wherein the memory risk includes at least one of the following: memory leak and memory overflow.

[0017] Secondly, embodiments of the present invention also provide a memory tracking device, the device comprising: an interception unit, configured to intercept a target entity's call to a memory management interface via a first allocator in response to the target entity's call to the memory management interface, wherein the target entity is a program entity conforming to a preset process marking rule, and the first allocator is a custom global allocator; a recording unit, configured to record the memory operation information of the target entity via the first allocator, the memory operation information being the memory operation that the target entity intends to perform by calling the memory management interface; and a first calling unit, configured to call a second allocator via the first allocator, so as to call the memory management interface via the second allocator.

[0018] In one implementation, the first allocator is defined by the properties of the global allocator in the programming language.

[0019] In one embodiment, the apparatus further includes: a determining unit, configured to, before intercepting the target entity's call to the memory management interface via a first allocator in response to the target entity's call to the memory management interface, determine whether the first program entity is the target entity to be tracked based on whether the process marking rule contains a process identifier of the first program entity, wherein the first program entity is any of the program entities; wherein the process marking rule includes a process identifier.

[0020] In one embodiment, the apparatus further includes a second invocation unit, configured to directly invoke the second allocator to perform the memory operation in response to the process identifier of the first program entity not being included in the process marking rule.

[0021] In one implementation, the process marking rule includes a process identifier and at least one of the following rule types: entity type, operation type, memory block size, and time type; the determining unit is specifically configured to: before responding to a target entity calling a memory management interface, determine whether the first program entity is a candidate entity based on whether the process marking rule contains the process identifier of the first program entity; if the first program entity is the candidate entity, take the part of the first program entity that corresponds to the rule type specified in the process marking rule as the target entity.

[0022] In one implementation, the entity type includes at least one of the following: process, subprocess, thread, coroutine.

[0023] In one implementation, the entity type of the target entity is a coroutine, and the determining unit is specifically used to: when the first program entity is the candidate entity, determine the part of the first program entity that corresponds to the entity type of coroutine specified in the process marking rule as a coroutine instance that conforms to the process marking rule; determine the target entity from the coroutine instance according to the coroutine sampling rate, wherein the coroutine sampling rate is a value between 0 and 1.

[0024] In one implementation, the operation type includes at least one of the following: memory allocation and memory release.

[0025] In one implementation, the time type includes at least one of the following: time period, time window.

[0026] In one implementation, the determining unit is specifically configured to: in response to a change in the process rule file, confirm the newly added process marking rule; wherein the process marking rule is stored in the process rule file; and determine whether the first program entity is the target entity to be tracked based on whether the newly added process marking rule contains the process identifier of the first program entity.

[0027] In one implementation, the first calling unit is further configured to: call a second allocator and a third allocator through the first allocator, so as to call a portion of the interfaces in the memory management interface through the second allocator, and to call another portion of the interfaces in the memory management interface through the third allocator.

[0028] In one embodiment, the apparatus further includes: a statistics unit, configured to, after the memory operation information of the target entity is recorded by the first allocator, statistically analyze the memory operation information of the target entity recorded by the first allocator; and an analysis unit, configured to, based on the statistical results, analyze the memory risk of the target entity, wherein the memory risk includes at least one of the following: memory leak and memory overflow.

[0029] Thirdly, embodiments of the present invention also provide an electronic device, the electronic device comprising: a processor and a memory, the processor being electrically connected to the memory; the memory being used to store executable program code; the processor running a program corresponding to the executable program code by reading the executable program code stored in the memory, thereby implementing any of the memory tracing methods provided by embodiments of the present invention.

[0030] Fourthly, embodiments of the present invention also provide a computer-readable storage medium storing one or more programs that can be executed by one or more processors to implement any of the memory tracking methods provided in the embodiments of the present invention.

[0031] The memory tracing method, apparatus, electronic device, and storage medium provided in the embodiments of the present invention can respond to a target entity's call to a memory management interface. By intercepting the target entity's call to the memory management interface through a custom first allocator, the device records the target entity's memory operation information. Then, the first allocator calls a second allocator to execute the actual memory operation. Since the first allocator, as a global allocator, automatically intercepts all memory operations at the underlying level, there is no need to manually insert tracing logic into the business code, avoiding the pollution of the source code caused by instrumentation tracing. Furthermore, since the first allocator completely intercepts and records the memory operations of all target entities that conform to the process marking rules, it avoids the omission of memory operation behaviors, thereby ensuring the accuracy of memory tracing. Therefore, it can guarantee the accuracy of memory tracing without polluting the source code. Attached Figure Description

[0032] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0033] Figure 1 A flowchart of a memory tracing method provided for an embodiment of the present invention; Figure 2A schematic diagram of a process marking rule provided for an embodiment of the present invention; Figure 3 Another schematic diagram of the process marking rules provided for embodiments of the present invention; Figure 4 A detailed flowchart of a memory tracing method provided for embodiments of the present invention; Figure 5 A schematic diagram of a memory tracking device provided for an embodiment of the present invention; Figure 6 A schematic diagram of an electronic device provided as an embodiment of the present invention. Detailed Implementation

[0034] The embodiments of the present invention will now be described in detail with reference to the accompanying drawings.

[0035] It should be understood that the described embodiments are merely some, not all, of the embodiments of the present invention. All other embodiments obtained by those skilled in the art based on the embodiments of the present invention without inventive effort are within the scope of protection of the present invention.

[0036] Firstly, embodiments of the present invention provide a memory tracing method that can ensure the accuracy of memory tracing without polluting the source code.

[0037] like Figure 1 As shown, an embodiment of the present invention provides a memory tracing method, the method including: S11, in response to the target entity calling the memory management interface, the first allocator intercepts the target entity's call to the memory management interface, wherein the target entity is a program entity that conforms to a preset process marking rule, and the first allocator is a custom global allocator.

[0038] In this step, the first allocator can intercept the target entity's calls to the memory management interface. The memory management interface can refer to low-level memory operation functions provided by the operating system or runtime library, such as memory allocation (alloc), reallocation (realloc), and deallocation (dealloc). The program completes memory allocation and deallocation by calling these interfaces. The target entity can refer to a program entity that conforms to preset process marking rules, such as a process or child process that conforms to the process marking rules.

[0039] In this embodiment of the invention, the first allocator can be a custom global allocator that implements the same interface as the underlying library. It takes over all memory operations through the compiler's global allocator mechanism, enabling the interception and tracking of memory behavior. Specifically, when a target entity calls the memory management interface through the system memory allocator or a third-party memory allocator, the call will be automatically intercepted by the first allocator because it replaces the memory management interface. For example, in the Rust language, a custom global memory allocator can be set and, through low-level library replacement, intercept calls to the memory management interface.

[0040] S12, the memory operation information of the target entity is recorded by the first allocator, and the memory operation information is the memory operation that the target entity intends to implement by calling the memory management interface.

[0041] In this step, after intercepting the memory management interface call, the first allocator records the memory operation information requested by the target entity. This memory operation information can refer to the allocation or release of memory, the size of the memory block, call stack backtracking, timestamps, etc. This information reflects the specific operations performed by the target entity to request or release memory.

[0042] S13, the first allocator calls the second allocator, so that the memory management interface is called through the second allocator.

[0043] In this step, the first allocator calls the second allocator to execute the memory management interface. The second allocator can refer to the original system memory allocator (system malloc or jemalloc) or a third-party memory allocator. In this embodiment of the invention, the first allocator intercepts and records the request, and then forwards it to the second allocator to perform the actual memory operation.

[0044] The memory tracing method provided in the embodiments of the present invention can respond to a target entity's call to a memory management interface. It intercepts the target entity's call to the memory management interface using a custom first allocator, records the target entity's memory operation information, and then calls a second allocator through the first allocator to execute the actual memory operation. Since the first allocator, as a global allocator, automatically intercepts all memory operations at the underlying level, there is no need to manually insert tracing logic into the business code, avoiding the pollution of the source code caused by instrumentation tracing. Furthermore, since the first allocator completely intercepts and records the memory operations of all target entities that conform to the process marking rules, it avoids the omission of memory operation behaviors, thereby ensuring the accuracy of memory tracing. Therefore, it can guarantee the accuracy of memory tracing without polluting the source code.

[0045] Specifically, in this embodiment of the invention, the first allocator can be defined through the properties of the global allocator in the programming language. For example, in one implementation, the first allocator is defined through the properties of the global allocator in the programming language. For instance, a custom global allocator can be set by replacing the #[global_allocator] property in the Rust language or a similar mechanism.

[0046] In specific implementations, the target entity to be tracked can be defined using process marking rules in this embodiment of the invention. For example, in one implementation, the process marking rule includes a process identifier; before intercepting the target entity's call to the memory management interface via a first allocator in response to the target entity's call to the memory management interface, the method may further include: determining whether the first program entity is the target entity to be tracked based on whether the process marking rule contains the process identifier of a first program entity, wherein the first program entity is any of the program entities.

[0047] Specifically, process tagging rules can be in file format or in formats such as JSON (JavaScript Object Notation). Figure 2 As shown, the process labeling rules include the process identifier. Figure 3 for Figure 2 The corresponding JSON format. Specifically, the process identifier can be the process name or the process ID. For example, Figure 2 Define the tracking target entity in the global process tagging rule file, including the process name attribute "process_name", and limit the process name to data_processor. In this way, the tracking scope is controlled by the global rule file, enabling the tracking of the data_processor process.

[0048] It is understandable that the process tagging rule file is a global rule file. Matching of target entities can use either strict matching or wildcards. The wildcard is used to match multiple names. The specific methods for using wildcards to match target entities and other rules are already detailed in existing technologies, and will not be elaborated upon here.

[0049] The embodiments of the present invention achieve precise filtering based on process identifiers, avoiding indiscriminate tracking of all program entities and reducing unnecessary performance overhead.

[0050] In practice, if the process identifier of a program entity does not conform to the definition of the process marking rule, it can be directly invoked. For example, in one embodiment, the method may further include: in response to the process marking rule not containing the process identifier of the first program entity, directly invoking the second allocator to perform the memory operation. In this embodiment, if the process marking rule does not contain the process identifier of the first program entity, then there is no need to record the memory operation of the program entity, and the second allocator is directly invoked to perform the memory operation. This avoids introducing additional performance overhead and ensures that the program's running efficiency is not affected in non-tracking scenarios.

[0051] Specifically, embodiments of the present invention can also define target entities to be tracked in various complex application scenarios for process marking rules. For example, in one embodiment, the process marking rule includes a process identifier and at least one of the following rule types: entity type, operation type, memory block size, and time type; before intercepting the target entity's call to the memory management interface through the first allocator in response to the target entity's call to the memory management interface, the method may further include: determining whether the first program entity is a candidate entity based on whether the process marking rule contains the process identifier of the first program entity; if the first program entity is the candidate entity, taking the portion of the first program entity that corresponds to the rule type specified in the process marking rule as the target entity.

[0052] In this embodiment, the process marking rules include a process identifier and the following rule types: entity type, operation type, memory block size, and time type. Specifically, based on the process identifier in the process marking rules, it can be determined whether the process to which any program entity belongs matches the process identifier, thus identifying candidate entities. If a match is found, the first program entity is marked as a candidate entity. For example, such as... Figure 2 If the process is identified as “data_processor” in the rules shown, then all execution units such as threads and coroutines belonging to the “data_processor” process become candidate entities.

[0053] Given that the first program entity has been marked as a candidate entity, it is further determined whether the entity satisfies other rule types specified in the process marking rules. If so, the portion of the entity corresponding to the rule type is taken as the final target entity. For example, the rule may also specify that the entity type is "thread" and the thread name pattern is "worker-". If the operation type is "allocation only", the memory block size is not less than 1MB, and the time range is 2:00 AM to 4:00 AM, then only memory allocation operations of 1MB or more executed by threads named "worker-1" or "worker-2" during the period of 2:00 AM to 4:00 AM will be identified as target entities to be tracked. Other threads (such as "gc-thread") or non-allocation operations, small memory operations, and operations outside the specified time period will not be tracked. In this embodiment of the invention, by defining process marking rules, multi-dimensional fine-grained memory tracking control is achieved, which can monitor specific processes, specific operations, or specific memory ranges, reduce redundant data, and improve tracking accuracy.

[0054] Specifically, the entity type of the target entity in the process marking rule can be various forms such as process, child process, thread, coroutine, etc. For example, in one implementation, the entity type includes at least one of the following: process, child process, thread, coroutine. Different entity types correspond to different tracking granularity and coverage, which can be flexibly selected according to actual monitoring needs.

[0055] When the entity type in the process tagging rule is specified as "process", the memory operations of all threads and coroutines within that process are included in the tracking scope. As the basic unit of operating system resource allocation, specifying the entity type as "process" allows for comprehensive tracking of all memory allocation and deallocation during program execution.

[0056] When the entity type is specified as "child process", the child process matching the rules can be automatically identified as the target entity. Child processes are created through operations such as fork, and their memory space is independent of the parent process. In traditional tracing methods, child processes often lose tracing parameters from the parent process; this invention achieves memory tracing of child processes without requiring additional tracing information.

[0057] When the entity type is specified as "thread", the memory behavior of a specific thread can be accurately tracked by using the thread name matching rules, while ignoring other non-target threads.

[0058] When the entity type is specified as "coroutine", the present invention further obtains the unique identifier of the coroutine and associates the identifier in the memory operation record, thereby realizing independent memory statistics for each coroutine in high-concurrency asynchronous scenarios.

[0059] In this embodiment of the invention, lossless tracking of different execution entities such as cross-process, cross-thread, and cross-coroutine is achieved through process marking rules. This solves the problem that tracking parameters cannot be automatically propagated to child processes, child threads, and coroutines in traditional solutions, and can adapt to the needs of various production environments from traditional multi-process architectures to modern high-concurrency asynchronous architectures.

[0060] In practical implementation, for scenarios with a large number of coroutines, this invention provides a sampling-based tracking granularity control mechanism. For example, in one embodiment, where the target entity's entity type is a coroutine, and when the first program entity is the candidate entity, determining the portion of the first program entity corresponding to the rule type specified in the process marking rule as the target entity may include: when the first program entity is the candidate entity, determining the portion of the first program entity corresponding to the entity type of coroutine specified in the process marking rule as a coroutine instance conforming to the process marking rule; determining the target entity from the coroutine instances according to the coroutine sampling rate, wherein the coroutine sampling rate is a value between 0 and 1. For example, it can be configured as 0.01, meaning randomly sampling coroutines at a rate of 1%. Exemplarily, its process marking rule can be set as follows: process_name = "data_processor" entity_type = "coroutine" # Sampling and tracking coroutines at a rate of 1% coroutine_sampling_rate = 0.01 Specifically, this invention further obtains a unique identifier for each coroutine (such as the task ID assigned by the tokio runtime) and associates the identifier with the memory operation record, thereby enabling independent memory statistics for each coroutine in high-concurrency asynchronous scenarios.

[0061] This invention achieves a balance between tracking accuracy and system overhead in a high-concurrency asynchronous environment through a coroutine sampling mechanism. It retains the statistics of memory behavior while effectively avoiding the performance bottleneck caused by full tracking, thereby improving the scalability and sustainability of memory monitoring in production environments.

[0062] Specifically, the operation type of the target entity in the process marking rules can be various forms such as memory allocation and memory release. For example, in one implementation, the operation type includes at least one of the following: memory allocation and memory release. The operation type is used to classify and filter memory operation behaviors. By tracking memory allocation and memory release operations separately or simultaneously, memory anomalies such as memory leaks and abnormal releases can be accurately located, providing fine-grained data support for memory risk analysis and problem investigation.

[0063] When the operation type is specified as memory allocation, memory allocation behavior during program execution can be collected separately, and the number of allocations, allocation size, and allocation location can be statistically analyzed to facilitate the identification of abnormal allocation patterns such as over-allocation and duplicate allocation. When the operation type is specified as memory release, memory reclamation behavior can be monitored separately, and the validity and timing of release can be verified to promptly detect risky operations such as duplicate release, null pointer release, and improper release.

[0064] In this embodiment of the invention, by flexibly configuring operation types, fine-grained screening and targeted tracking of memory operations can be achieved, thereby improving the efficiency of memory problem diagnosis.

[0065] Specifically, the time type of the target entity in the process marking rules can be in various forms such as a time period or a time window. For example, in one implementation, the time type includes at least one of the following: a time period or a time window. The time type is used to filter and limit memory operations from a time dimension, supporting the tracking of memory behavior by fixed time periods or dynamic intervals, which helps diagnose time-related memory problems such as transient memory overflow and periodic memory growth.

[0066] When the time type is specified as a time period, tracking is performed within a pre-defined fixed time range. For example, by setting a time period, tracking can be enabled only during off-peak business hours (such as 2:00 AM to 4:00 AM) to analyze memory issues. Another example is enabling detailed tracking within one hour of a new version release to quickly detect memory leaks or abnormal allocations.

[0067] When the time type is specified as a time window, tracking is performed using a dynamically triggered relative time interval. For example, when memory usage is detected to exceed a threshold (such as 80%), a 10-minute time window is automatically opened to record all memory operations within that window in detail.

[0068] In this embodiment of the invention, by combining time periods and time windows, both static timed tracking and dynamic triggering tracking capabilities are taken into account, covering various timing tracking scenarios such as routine inspections, anomaly reviews, and performance optimization, thereby improving the flexibility and practicality of memory tracking.

[0069] It should be noted that the process marking rules in this application do not rely on a fixed PID (ProcessIdentifier), but instead locate the ID (identifier) ​​of the target entity (process, child process, thread, coroutine, etc.) through various semantic matching methods. Therefore, it can solve the problem of existing memory tracing schemes struggling to obtain IDs in advance when dealing with dynamically generated child processes, threads with extremely short lifecycles, and coroutines, leading to missing or ineffective tracing. For example, the process marking rules can support the following matching techniques: ID range matching (tracing by creation order), name wildcard matching (suitable for scenarios with naming conventions), creation location matching (suitable for scenarios without naming but with module divisions), and sampling rate matching (suitable for high-concurrency production environments). Through these techniques, this invention can accurately identify and track various execution entities without prior knowledge of the specific ID, achieving memory tracing.

[0070] In practical implementation, when process marking rules are stored in a file, updates to the process rule file can be monitored to dynamically determine the target entity to be tracked. For example, in one implementation, the process marking rules are stored in a process rule file. Determining whether the first program entity is the target entity to be tracked based on whether the process marking rules contain the process identifier of the first program entity may include: in response to a change in the process rule file, confirming the newly added process marking rules; and determining whether the first program entity is the target entity to be tracked based on whether the newly added process marking rules contain the process identifier of the first program entity. In this embodiment, the rules can be loaded at program startup, reducing process restarts in the production environment, achieving "zero downtime" adjustment of the tracking strategy, and greatly reducing operational risks.

[0071] Specifically, changes to the process tagging rule file can be monitored using inotify or file polling. When the process tagging rule file is updated, the rules are re-parsed without stopping the process, and the rule matching table in memory is atomically replaced. Subsequent memory operations immediately follow the new rules or stop tracing. It should be noted that in this embodiment, monitoring changes to the process rule file and confirming newly added process tagging rules can be based on whether the newly added process tagging rule contains the process identifier of the first program entity, or it can include the rule type defined in any of the previous embodiments. This invention does not limit this.

[0072] Understandably, memory tracing demands extremely high reliability when deployed in production environments. Production systems need to ensure stable operation with minimal performance overhead; therefore, the memory tracing logic must be implemented in a very lightweight manner, while the core memory allocation process is still executed by a thoroughly validated standard allocator to minimize operational risks. Traditional memory tracing solutions (such as Valgrind and manual instrumentation) are designed for test environments, and their implementation presupposes significant performance degradation, making them difficult to apply directly to production scenarios. Production environments place even more stringent demands on memory tracing methods. This invention achieves low-overhead and non-intrusive tracing through global allocator replacement. Combined with global process marking rules, it meets the aforementioned constraints of production environments, making routine memory tracing possible in production settings.

[0073] In practical implementation, the target entity may also use a third allocator to perform different memory operations during operation. For example, in one embodiment, calling a second allocator through the first allocator to call the memory management interface through the second allocator may include: calling both the second and third allocators through the first allocator, so that the second allocator calls a portion of the interfaces in the memory management interface, and the third allocator calls another portion of the interfaces in the memory management interface. For example, a process may include a second allocator and a third allocator, where the second allocator is jemalloc, managing small memory operations, and the third allocator is mmap, managing large memory operations. In this case, the first allocator can call the second allocator jemalloc and the third allocator mmap respectively to perform the corresponding memory operations according to the original program's calling method. This embodiment can support scenarios where multiple allocators coexist, ensuring the integrity and compatibility of tracing.

[0074] Furthermore, in this embodiment of the invention, statistical analysis can be performed after memory tracking. For example, in one implementation, after recording the memory operation information of the target entity through the first allocator, the method may further include: statistically analyzing the memory operation information of the target entity recorded by the first allocator; and analyzing the memory risk of the target entity based on the statistical results, wherein the memory risk includes at least one of the following: memory leak and memory overflow. This embodiment can transform raw tracking data into a diagnosable memory risk report, helping developers quickly locate the causes of memory leaks and memory overflows in the production environment and improving the efficiency of problem investigation.

[0075] The memory tracing method provided by the embodiments of the present invention will be described in detail below through specific examples.

[0076] like Figure 4 As shown, the memory tracing method provided by the embodiments of the present invention may include: S501. Determine whether the first program entity is a candidate entity based on whether the process identifier of the first program entity is included in the process marking rule. The first program entity can be any program entity. The process marking rule includes a process identifier and at least one of the following rule types: entity type, operation type, memory block size, and time type. Optionally, the entity type includes at least one of the following: process, child process, thread, coroutine; Optionally, the operation type includes at least one of the following: memory allocation, memory release; Optionally, the time type includes at least one of the following: time period, time window; Optionally, the first allocator is defined through the properties of the global allocator in the programming language; S502. When the first program entity is a candidate entity, the part of the first program entity that corresponds to the entity type specified in the process marking rule as a coroutine is determined as a coroutine instance that conforms to the process marking rule, wherein the entity type is a coroutine. S503. Determine the target entity from the coroutine instance based on the coroutine sampling rate, where the coroutine sampling rate is a value between 0 and 1; S504. In response to the target entity calling the memory management interface, the first allocator intercepts the target entity's call to the memory management interface. Here, the target entity is a program entity that conforms to the preset process marking rules, and the first allocator is a custom global allocator. S505. The first allocator records the memory operation information of the target entity. The memory operation information is the memory operation that the target entity intends to implement by calling the memory management interface. S506. Call the second allocator through the first allocator, so as to call the memory management interface through the second allocator; S507. In response to the process identifier of the first program entity not being included in the process marking rule, the second allocator is directly invoked to perform memory operations. S508. Calculate the memory operation information of the target entity recorded by the first allocator; S509. Based on the statistical results, analyze the memory risks of the target entity. Memory risks include at least one of the following: memory leaks and memory overflows.

[0077] Secondly, embodiments of the present invention provide a memory tracking device that can ensure the accuracy of memory tracking without polluting the source code.

[0078] like Figure 5 As shown, embodiments of the present invention also provide a memory tracking device, which may include: Interception unit 31 is used to intercept the target entity's call to the memory management interface in response to the target entity's call to the memory management interface through a first allocator, wherein the target entity is a program entity that conforms to a preset process marking rule, and the first allocator is a custom global allocator; The recording unit 32 is used to record the memory operation information of the target entity through the first allocator, wherein the memory operation information is the memory operation that the target entity intends to implement by calling the memory management interface; The first calling unit 33 is used to call the second allocator through the first allocator, so as to call the memory management interface through the second allocator.

[0079] The memory tracing device provided in the embodiments of the present invention can respond to a target entity's call to the memory management interface, intercept the target entity's call to the memory management interface through a custom first allocator, record the target entity's memory operation information, and then call a second allocator through the first allocator to execute the actual memory operation. Since the first allocator, as a global allocator, automatically intercepts all memory operations at the underlying level, there is no need to manually insert tracing logic into the business code, avoiding the pollution of the source code caused by instrumentation tracing. Furthermore, since the first allocator completely intercepts and records the memory operations of all target entities that conform to the process marking rules, it avoids the omission of memory operation behaviors, thereby ensuring the accuracy of memory tracing. Therefore, it can ensure the accuracy of memory tracing without polluting the source code.

[0080] In one implementation, the first allocator is defined by the properties of the global allocator in the programming language.

[0081] In one embodiment, the apparatus further includes: a determining unit, configured to, before intercepting the target entity's call to the memory management interface via a first allocator in response to the target entity's call to the memory management interface, determine whether the first program entity is the target entity to be tracked based on whether the process marking rule contains a process identifier of the first program entity, wherein the first program entity is any of the program entities; wherein the process marking rule includes a process identifier.

[0082] In one embodiment, the apparatus further includes a second invocation unit, configured to directly invoke the second allocator to perform the memory operation in response to the process identifier of the first program entity not being included in the process marking rule.

[0083] In one implementation, the process marking rule includes a process identifier and at least one of the following rule types: entity type, operation type, memory block size, and time type; the determining unit is specifically configured to: before responding to a target entity calling a memory management interface, determine whether the first program entity is a candidate entity based on whether the process marking rule contains the process identifier of the first program entity; if the first program entity is the candidate entity, take the part of the first program entity that corresponds to the rule type specified in the process marking rule as the target entity.

[0084] In one implementation, the entity type includes at least one of the following: process, subprocess, thread, coroutine.

[0085] In one implementation, the entity type of the target entity is a coroutine, and the determining unit is specifically used to: when the first program entity is the candidate entity, determine the part of the first program entity that corresponds to the entity type of coroutine specified in the process marking rule as a coroutine instance that conforms to the process marking rule; determine the target entity from the coroutine instance according to the coroutine sampling rate, wherein the coroutine sampling rate is a value between 0 and 1.

[0086] In one implementation, the operation type includes at least one of the following: memory allocation and memory release.

[0087] In one implementation, the time type includes at least one of the following: time period, time window.

[0088] In one implementation, the determining unit is specifically configured to: in response to a change in the process rule file, confirm the newly added process marking rule; wherein the process marking rule is stored in the process rule file; and determine whether the first program entity is the target entity to be tracked based on whether the newly added process marking rule contains the process identifier of the first program entity.

[0089] In one implementation, the first invocation unit 33 is further configured to: invoke a second allocator and a third allocator through the first allocator, so as to invoke a portion of the interfaces in the memory management interface through the second allocator, and to invoke another portion of the interfaces in the memory management interface through the third allocator.

[0090] In one embodiment, the apparatus further includes: a statistics unit, configured to, after the memory operation information of the target entity is recorded by the first allocator, statistically analyze the memory operation information of the target entity recorded by the first allocator; and an analysis unit, configured to, based on the statistical results, analyze the memory risk of the target entity, wherein the memory risk includes at least one of the following: memory leak and memory overflow.

[0091] Thirdly, embodiments of the present invention also provide an electronic device that can ensure the accuracy of memory tracking without polluting the source code.

[0092] like Figure 6 As shown, the electronic device provided in the embodiments of the present invention may include: a processor 71 and a memory 72, wherein the processor 71 and the memory 72 are electrically connected; the memory 72 is used to store executable program code; the processor 71 runs a program corresponding to the executable program code by reading the executable program code stored in the memory 72, so as to implement any of the memory tracing methods provided in the foregoing embodiments.

[0093] The specific execution process of the above steps by the processor 71, as well as the steps further executed by the processor 71 by running executable program code, can be found in the description of the foregoing embodiments, and will not be repeated here.

[0094] Fourthly, embodiments of the present invention also provide a computer-readable storage medium storing one or more programs, which can be executed by one or more processors to implement any of the memory tracing methods provided in the foregoing embodiments, thus achieving the corresponding technical effects. This has been described in detail above and will not be repeated here.

[0095] It should be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.

[0096] The various embodiments in this specification are described in a related manner. The same or similar parts between the various embodiments can be referred to each other. Each embodiment focuses on describing the differences from other embodiments.

[0097] In particular, the device embodiment is basically similar to the method embodiment, so the description is relatively simple. For relevant details, please refer to the description of the method embodiment.

[0098] For ease of description, the above apparatus is described by dividing it into various functional units / modules. Of course, in implementing this invention, the functions of each unit / module can be implemented in one or more software and / or hardware.

[0099] Those skilled in the art will understand that all or part of the processes in the above embodiments can be implemented by a computer program instructing related hardware. The program can be stored in a computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. The storage medium can be a magnetic disk, optical disk, read-only memory (ROM), or random access memory (RAM), etc.

[0100] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in the present invention should be included within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.

Claims

1. A memory tracing method, characterized in that, include: In response to a target entity calling a memory management interface, the first allocator intercepts the target entity's call to the memory management interface. The target entity is a program entity that conforms to a preset process marking rule, and the first allocator is a custom global allocator. The first allocator records the memory operation information of the target entity, which is the memory operation that the target entity intends to perform by calling the memory management interface. The first allocator calls the second allocator, which in turn calls the memory management interface through the second allocator.

2. The memory tracing method according to claim 1, characterized in that, The first allocator is defined by the properties of the global allocator in the programming language.

3. The memory tracing method according to claim 1, characterized in that, The process labeling rules include process identifiers; Before intercepting the target entity's call to the memory management interface via the first allocator in response to the target entity's call to the memory management interface, the method further includes: Based on whether the process identification rule contains the process identifier of the first program entity, it is determined whether the first program entity is the target entity to be tracked, and the first program entity can be any of the program entities.

4. The memory tracing method according to claim 3, characterized in that, The method further includes: In response to the fact that the process identifier of the first program entity is not included in the process marking rule, the second allocator is directly invoked to perform the memory operation.

5. The memory tracing method according to claim 3, characterized in that, The process marking rules include process identifiers and at least one of the following rule types: entity type, operation type, memory block size, and time type; Before intercepting the target entity's call to the memory management interface via the first allocator in response to the target entity's call to the memory management interface, the method further includes: Based on whether the process identification rule contains the process identifier of the first program entity, determine whether the first program entity is a candidate entity; When the first program entity is the candidate entity, the portion of the first program entity that corresponds to the rule type specified in the process marking rule is taken as the target entity.

6. The memory tracing method according to claim 5, characterized in that, The entity type includes at least one of the following: process, child process, thread, coroutine.

7. The memory tracing method according to claim 6, characterized in that, The entity type of the target entity is a coroutine. When the first program entity is the candidate entity, the portion of the first program entity corresponding to the rule type specified in the process marking rule is used as the target entity, including: When the first program entity is the candidate entity, the part of the first program entity that corresponds to the entity type of coroutine specified in the process marking rule is determined as a coroutine instance that conforms to the process marking rule; The target entity is determined from the coroutine instance based on the coroutine sampling rate, wherein the coroutine sampling rate is a value between 0 and 1.

8. The memory tracing method according to claim 5, characterized in that, The operation type includes at least one of the following: memory allocation, memory release.

9. The memory tracing method according to claim 5, characterized in that, The time type includes at least one of the following: time period, time window.

10. The memory tracing method according to claim 3, characterized in that, The process marking rules are stored in the process rule file. The step of determining whether the first program entity is the target entity to be tracked based on whether the process identification rule contains the process identifier of the first program entity includes: In response to a change in the process rule file, confirm the newly added process tagging rule; Based on whether the newly added process marking rules contain the process identifier of the first program entity, determine whether the first program entity is the target entity to be tracked.