Memory statistics method and apparatus, electronic device, and computer-readable storage medium

By obtaining the functional modules and their memory sizes during memory allocation and deallocation, and using the PC and FP registers to calculate and update memory records in real time, the problem of existing tools being unable to locate memory issues in resource-constrained systems is solved, enabling rapid location and processing of functional module memory.

CN115168051BActive Publication Date: 2026-02-24BEIJING TOPSEC NETWORK SECURITY TECH +2
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202210898380.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-07-28
Publication Date
2026-02-24
Estimated Expiration
2042-07-28

AI Technical Summary

Technical Problem

Existing memory analysis tools consume excessive system resources in resource-constrained systems and cannot accurately locate the memory usage of functional modules, making memory problems difficult to solve.

Method used

By obtaining the calling functional module and its memory size when the memory allocation and release interfaces are invoked, updating the memory record, calculating the functional module using the PC and FP registers, and setting a threshold for the number of times to avoid complex tracing, real-time monitoring and location of the functional module's memory can be achieved.

Benefits of technology

It enables real-time monitoring and rapid location of functional module memory in resource-constrained systems, solving the problem that existing tools cannot locate problematic modules, and does not consume a large amount of system resources.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115168051B_ABST
    Figure CN115168051B_ABST
Patent Text Reader

Abstract

The application provides a memory statistics method and device, electronic equipment and computer readable storage medium. The method comprises: when a memory application interface or a memory release interface is called, a function module calling the memory application interface or the memory release interface is acquired, and a memory size applied by the memory application interface or a memory size released by the memory release interface is acquired; and according to the applied memory size or the released memory size, a total occupied memory value in a memory record corresponding to the function module is updated. The scheme of the application can continuously update the latest memory occupation of the function module, realize real-time monitoring of the memory occupied by each function module, quickly locate the function module with problems, and thus timely processing. Moreover, the scheme of the application is not complex in program implementation, and does not need to occupy a large amount of resources.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of computer, in particular, relates to a memory statistics method and device, electronic equipment and computer readable storage medium. BACKGROUND

[0002] Memory is an important component in the system, with the continuous innovation of technology, the continuous enrichment of system function, the reasonable allocation and use of memory is particularly important. In the system, there will be various memory problems, the common memory problems mainly have the following two kinds:

[0003] The first kind: in the system development process, due to unreasonable software design, some functional modules occupy too much memory, thereby reducing the overall performance of the system. For example: a functional module does not set the upper limit value of the memory it occupies, the program keeps allocating memory in the process of running, until the end of the memory is released. For a functional module, the running time may be several days, several weeks or even several months, due to no upper limit value, no timely release of memory, may lead to the final exhaustion of all memory of the system, resulting in the system cannot be used.

[0004] The second kind: in the system development process, due to the use of memory is not standard, no release of the allocated memory, resulting in memory leak problem. When memory leak occurs, the available memory in the system will become less and less, eventually leading to system crash and unable to use normally.

[0005] In order to solve the above problems, it is necessary to count the memory usage in order to find the problem in time and handle it in time. At present, the common memory analysis tools, such as kmemleak, kasan, etc., due to the function is too large, too many dependencies, resulting in these tools itself need to occupy a lot of system resources, not suitable for use in resource shortage system (such as embedded system), and these tools are analyzed from the system as a whole, these tools can not count the memory occupied by each functional module, so it is difficult to solve the above problems. SUMMARY

[0006] The purpose of the embodiment of the present application is to provide a memory statistics method, device, electronic equipment and computer readable storage medium, to solve the above problems.

[0007] This application provides a memory statistics method, including: when a memory request interface or a memory release interface is called, obtaining the functional module that calls the memory request interface or the memory release interface, and obtaining the memory size requested by calling the memory request interface, or obtaining the memory size released by calling the memory release interface; updating the total memory occupied value in the memory record corresponding to the functional module according to the requested memory size or the released memory size.

[0008] Through the above implementation process, when the memory allocation or release interface is called, the function module that called the interface is identified, along with the size of the memory allocated or released. This updates the total memory usage in the memory record corresponding to the function module. Each time a function module allocates or releases memory, the total memory usage is updated, continuously providing real-time monitoring of memory usage by each module. This allows for rapid identification of the problematic function module when memory issues arise, based on its memory records, enabling timely intervention and resolving the limitation of existing memory analysis tools in locating faulty modules. Furthermore, this implementation is not complex and does not require significant resources, addressing the issue that existing memory analysis tools consume substantial system resources and are unsuitable for resource-constrained systems (such as embedded systems).

[0009] Furthermore, when a memory allocation interface or a memory release interface is invoked, obtaining the functional module that invokes the memory allocation interface or the memory release interface includes: obtaining target data when the memory allocation interface or the memory release interface is invoked; the target data includes the calling function that invokes the memory allocation interface or the memory release interface, and the address of the function code segment stored in the PC (Program Counter) register corresponding to the calling function; calculating the functional module to which the calling function belongs based on the function code segment address; the functional module to which the calling function belongs is the functional module that invokes the memory allocation interface or the memory release interface.

[0010] In the above implementation, the functional module to which the calling function belongs can be determined by calling the memory allocation interface or the memory release interface, and by using the function code segment address stored in the PC register corresponding to the calling function, thus achieving accurate determination of the functional module that calls the memory allocation interface or the memory release interface.

[0011] Furthermore, the target data also includes the value of the FP register corresponding to the called function; calculating the functional module to which the called function belongs based on the function code segment address includes: if the calculation result obtained based on the function code segment address is empty, then calculating a new function code segment address based on the value of the FP register; and calculating the functional module to which the called function belongs based on the new function code segment address.

[0012] In a system, complex call relationships may exist, which could lead to a situation where calculating the address of the function's code segment stored in the PC register yields a null result. In the implementation described above, if the calculation of the function's code segment address stored in the PC register is null, the address of the new function's code segment can be recalculated based on the value of the FP register. This allows for tracing back to the functional module to which the calling function belongs.

[0013] Furthermore, the method further includes: if the calculation result obtained based on the new function code segment address is still empty, then taking a value upward in the FP register, calculating a new function code segment address based on the value, and calculating the functional module to which the called function belongs based on the new function code segment address; repeating the above process until the calculation result obtained based on the function code segment address is not empty, or the number of repetitions reaches a threshold; if the number of repetitions reaches the threshold, then determining that the requested memory size or the released memory size is the memory size corresponding to the kernel.

[0014] In the above implementation process, by setting a threshold for the number of repetitions, once the threshold is reached, the source will no longer be traced upwards. Instead, the size of the requested or released memory will be attributed to the kernel. This can avoid the large system overhead caused by tracing the source of overly complex call relationships, which would affect system performance.

[0015] Further, updating the total memory occupied value in the memory record corresponding to the functional module according to the requested memory size or the released memory size includes: determining whether the called function exists in the memory record corresponding to the functional module; if not, associating the called function with the requested memory size or the released memory size in the memory record; if it exists, updating the total memory occupied value corresponding to the called function in the memory record according to the requested memory size or the released memory size.

[0016] In the above implementation process, the called functions are recorded, along with the total memory usage of each called function. This allows for the location of the problematic called function when a problem is identified in a functional module, based on the total memory usage of each called function, thus facilitating problem handling.

[0017] Furthermore, the target data also includes the line number of the function line that calls the memory allocation interface or the memory release interface in the calling function;

[0018] The memory record associates the called function with the requested memory size or the released memory size, including: associating the called function, the line number, and the memory size requested by the function line calling the memory request interface or the memory size released by calling the memory release interface in the memory record;

[0019] Correspondingly, based on the requested memory size or the released memory size, the total memory occupied value corresponding to the called function in the memory record is updated, including: based on the memory size requested by the memory request interface or the memory size released by the memory release interface for each line number, the total memory occupied value corresponding to each line number is updated respectively.

[0020] In the above implementation process, by associating the total memory occupied value corresponding to each line number, when locating the problematic function call function in the problematic functional module, it is possible to further locate the function line that may be causing the problem based on the total memory occupied value corresponding to each line number, thus making it easier to handle the problem.

[0021] Furthermore, the method further includes: redirecting the function pointer in the memory allocation interface or memory release interface; and obtaining target data when the memory allocation interface or memory release interface is called, including: obtaining the target data through the redirected function pointer when the memory allocation interface or memory release interface is called.

[0022] In the above implementation process, by redirection, the target data related to the call to the memory allocation interface or memory release interface can be easily obtained, thus eliminating the need to design complex data acquisition logic and reducing the resource consumption of the system.

[0023] This application embodiment also provides a memory statistics device, including: an acquisition module, configured to acquire, when a memory allocation interface or a memory release interface is invoked, an acquisition module, and acquire the size of memory allocated by the memory allocation interface or the size of memory released by the memory release interface; and a processing module, configured to update the total memory occupied value in the memory record corresponding to the acquisition module based on the allocated memory size or the released memory size.

[0024] This application also provides an electronic device, including a processor and a memory; the processor is used to execute one or more instructions stored in the memory to implement any of the methods described above.

[0025] This application also provides a computer-readable storage medium storing one or more programs that can be executed by one or more processors to implement any of the methods described above. Attached Figure Description

[0026] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments of this application will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.

[0027] Figure 1 A flowchart illustrating a memory statistics method provided in an embodiment of this application;

[0028] Figure 2 A schematic diagram illustrating the process of obtaining a functional module that calls a memory allocation interface or a memory release interface, as provided in an embodiment of this application;

[0029] Figure 3 A schematic diagram illustrating the first data association and statistical method provided in the embodiments of this application;

[0030] Figure 4 A schematic diagram illustrating the second data association and statistical method provided in the embodiments of this application;

[0031] Figure 5 A schematic diagram illustrating the third data association and statistical method provided in the embodiments of this application;

[0032] Figure 6 This is a schematic diagram of the structure of a memory statistics device provided in an embodiment of this application;

[0033] Figure 7This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation

[0034] The technical solutions in the embodiments of this application will now be described with reference to the accompanying drawings.

[0035] To address the issues of commonly used memory analysis tools being overly complex and having numerous dependencies, resulting in high system resource consumption and unsuitability for resource-constrained systems (such as embedded systems), and because these tools analyze the entire system and cannot track the memory usage of individual functional modules, thus failing to pinpoint the modules causing memory issues, this application provides a memory statistics method. (See [link to relevant documentation]). Figure 1 As shown, the memory statistics method includes:

[0036] S101: When the memory allocation interface or memory release interface is called, obtain the functional module that called the memory allocation interface or memory release interface, and obtain the size of the memory allocated by the memory allocation interface or the size of the memory released by the memory release interface.

[0037] It is understood that the system provides interfaces for memory allocation and memory release. In this embodiment, the interface for memory allocation is referred to as the memory allocation interface, and the interface for memory release is referred to as the memory release interface.

[0038] In some embodiments, the memory allocation interface and the memory release interface may correspond to the same interface function. By calling this interface function, a memory allocation operation or a memory release operation can be performed. In other embodiments, the memory allocation interface and the memory release interface may correspond to different interface functions. Thus, when memory allocation is needed, the interface function corresponding to the memory allocation interface is called, and when memory release is needed, the interface function corresponding to the memory release interface is called.

[0039] Considering that in the system, functional modules typically call memory allocation or memory release interfaces through interface functions (for ease of description, the interface functions that call the memory allocation or memory release interfaces are referred to as calling functions in this embodiment), when a memory allocation or memory release interface is called, only information about the calling function can usually be obtained, and information about the functional module that called the memory allocation or memory release interface cannot be directly obtained. Therefore, in order to obtain the functional module that called the memory allocation or memory release interface, in this embodiment, it is possible to... Figure 2 Obtain it in the manner shown:

[0040] S201: Obtain the target data when the memory allocation interface or memory release interface is called.

[0041] In this embodiment, the target data may include the calling function of the memory allocation interface or the memory release interface, and the address of the function code segment stored in the PC register corresponding to the calling function. In this embodiment, the calling function can be obtained using the FUNCTION macro provided by the system, and the address of the function code segment stored in the PC register can be obtained using the current_text_addr() function provided by the system.

[0042] S202: Calculate the functional module to which the called function belongs based on the function code segment address stored in the PC register.

[0043] In this embodiment, the function code segment stored in the PC register can be calculated using the module_text_address() function to obtain the functional module to which the calling function belongs. Here, the parameters within parentheses are the function parameters, and during step S202, the parameters within the parentheses are the addresses of the function code segment stored in the PC register.

[0044] It should be understood that the functional module to which the function being called belongs is the same functional module that calls the memory allocation or memory deallocation interface. This allows for the rapid identification of the functional module that calls the memory allocation or memory deallocation interface.

[0045] It is important to note that in practical applications, complex call relationships may exist when calling memory allocation or deallocation interfaces. This could lead to a situation where calculating the address of the function's code segment stored in the PC register yields an empty result. To obtain the functional module to which the called function belongs, the acquired target data may also include the value of the FP register corresponding to the called function. The value of the FP register corresponding to the called function can be obtained, but is not limited to, through the built-in function `builtin_frame_address(0)`.

[0046] If the calculation result obtained from the function code segment address stored in the PC register is empty, a new function code segment address can be calculated based on the value of the FP register, and then the functional module to which the calling function belongs can be recalculated based on the new function code segment address.

[0047] If the calculation result obtained based on the new function code segment address is still empty, then the value can be taken up from the FP register, the new function code segment address can be calculated based on the value of the FP register, and the functional module to which the calling function belongs can be calculated based on the new function code segment address.

[0048] In one implementation, the above process can be repeated until the calculation result obtained from the function code segment address is not empty.

[0049] It should be understood that the FP register records the call relationships of each function call in a stacked manner. Therefore, by recalculating the function code segment address by taking values ​​from the bottom up in the FP register, the call relationship of the functions can be traced upwards level by level, thus tracing back to the functional module that called the function.

[0050] Considering that overly complex call relationships might require significant time and resources for tracing, another implementation can repeat the above process. However, the cutoff condition can include not only that the calculation result obtained from the function code segment address is not empty, but also that the number of repetitions reaches a threshold. That is, the above process can be repeated until the calculation result obtained from the function code segment address is not empty, or the number of repetitions reaches the threshold.

[0051] If the call ends because the number of repetitions has reached a threshold, then the requested or released memory size can be determined to be the kernel's corresponding memory size. In other words, the requested or released memory size can be attributed to the kernel to avoid excessive system overhead and impact on system performance caused by tracing overly complex call relationships.

[0052] The number of attempts threshold can be set by the engineer according to the actual situation of the system. For example, it can be set to 5, but it is not a limit.

[0053] S102: Update the total memory occupied value in the memory record corresponding to this function module based on the requested or released memory size.

[0054] In one optional embodiment of this application, it can be carried out according to... Figure 3 The method shown only associates and records each functional module, as well as the total memory occupied by each functional module.

[0055] It is understood that in this optional implementation, it can be first determined whether a memory record corresponding to the functional module exists. If it does not exist, a blank record can be created, and the identification information of the functional module (e.g., the name of the functional module) and the requested memory size or released memory size obtained in step S101 can be filled into the blank record. At this time, the requested memory size or released memory size is the total memory occupied by the functional module. If it exists, the total memory occupied in the memory record corresponding to the functional module can be updated according to the requested memory size or released memory size. When updating, if it is the requested memory size, the requested memory size is added to the original total memory occupied value; if it is the released memory size, the requested memory size is subtracted from the original total memory occupied value.

[0056] In another optional embodiment of the present application, it can also be carried out as follows Figure 4 The method shown associates and records each functional module, the functions called by each functional module, and the total memory usage corresponding to each function call. Thus, the sum of the total memory usage corresponding to all functions called by each functional module is the total memory usage of that functional module.

[0057] In the above optional implementation, when updating the total memory occupied by the functional module, it can be determined first whether there is a memory record corresponding to the functional module.

[0058] If it does not exist, a blank record can be created and filled with the identification information of the functional module (e.g., the name of the functional module), the identification information of the called function obtained this time (e.g., the name of the called function), and the memory size requested or released by the called function. At this time, the memory size requested or released is the total memory occupied by the functional module.

[0059] If the function exists, first check if the called function exists in the memory record corresponding to that functional module. If the called function does not exist, associate the called function with the requested or released memory size in the memory record. If the called function exists, update the total memory occupied by the called function in the memory record according to the requested or released memory size. During the update, if it is the requested memory size, add the requested memory size to the total memory occupied by the called function; if it is the released memory size, subtract the released memory size from the total memory occupied by the called function.

[0060] Thus, through Figure 4The table shown allows for the identification of problematic functions within functional modules when an issue is identified. By calculating the total memory usage of each called function, the problematic function can be located, enabling direct analysis and adjustment to quickly eliminate memory problems.

[0061] In another optional embodiment of the present application, it can also be carried out as follows Figure 5 The method shown associates and records each functional module, the functions called by each functional module, the line number of the function used when calling the memory allocation or memory release interface, and the total memory usage corresponding to each line number. Thus, the sum of the total memory usage corresponding to all line numbers of all function calls for each functional module is the total memory usage of that functional module.

[0062] In the above optional implementation, when obtaining the target data, the line number of the function line used when calling the memory allocation interface or memory release interface can be included as part of the target data and obtained together. For example, the line number of the function line used when calling the memory allocation interface or memory release interface can be obtained through the LINE macro, but this is not a limitation.

[0063] In the above optional implementation, when updating the total memory occupied by the functional module, it can be determined first whether there is a memory record corresponding to the functional module.

[0064] If it does not exist, a blank record can be created, and the blank record should be filled with the identification information of the functional module (e.g., the name of the functional module), the identification information of the function call obtained this time (e.g., the name of the function call), the line number of the function line used when calling the memory allocation interface or memory release interface, and the memory size allocated or released by each function line. At this time, the memory size allocated or released by each function line is the total memory occupied by the functional module.

[0065] If it exists, we can first check if the called function exists in the memory record corresponding to the functional module. If the called function does not exist, we then record the called function, the line number obtained by the called function, and the memory size allocated or released for each line number in the memory record.

[0066] If the called function exists, then it is determined whether the memory record associated with the called function has each row number.

[0067] For any given line number, if it is not recorded, then the line number is associated with the called function in the memory record, along with the allocated or freed memory size corresponding to that line number. If the line number is already recorded, then the total memory usage corresponding to that line number is updated based on the allocated or freed memory size. During the update, if it's allocated memory, the allocated memory size is added to the total memory usage corresponding to that line number; if it's freed memory, the freed memory size is subtracted from the total memory usage corresponding to that line number.

[0068] Thus, through Figure 5 The table shown illustrates how, when a problem is identified in a functional module, the problematic function can be located based on the total memory usage of each called function (the sum of the total memory usage corresponding to each line number of the same called function). Furthermore, based on the total memory usage corresponding to each line number of that called function, the potentially problematic function line can be located. This allows for direct contextual analysis and adjustment of the problematic function line to quickly eliminate memory issues.

[0069] In this embodiment of the application, when recording associations, methods such as hash buckets and hash lists can be used, but are not limited to. For example, the name of the functional module can be used as the key, and a hash bucket structure can be used to organize the data. The function name, line number, and total memory usage corresponding to the line number of the called function can be counted in the hash list of the module in the form of a structure.

[0070] In this embodiment of the application, the target data can be obtained by redirecting the function pointers in the memory allocation interface or memory release interface.

[0071] For example, a functional module for implementing the memory statistics method described above can be pre-defined, and the function pointers in the memory allocation and memory release interfaces can be assigned the address of this functional module. Thus, when the memory allocation or memory release interface is called, the target data can be transferred to this functional module through the redirected function pointer, thereby executing the memory statistics method. In this way, by redirecting, the acquisition of target data related to the calls to the memory allocation or memory release interfaces can be easily achieved, eliminating the need for complex data acquisition logic and reducing system resource consumption.

[0072] In this embodiment, the stored memory records can be sorted from largest to smallest using heap sort, and a display service function can be provided to show the memory usage of each functional module after sorting to the user. For example, the memory usage of each functional module after sorting can be displayed using the meminfo command. The meminfo command is used to display the memory usage of each functional module in the current system (including the module name and the total memory usage of the functional module). By using the meminfo command, the total memory usage of each functional module can be viewed in real time.

[0073] The memory statistics method provided in this application update the total memory usage value in the memory record corresponding to the functional module when the memory allocation or release interface is called. This update retrieves the memory size allocated or released by the functional module each time it allocates or releases memory, thus continuously updating the total memory usage of each module. This allows for real-time monitoring of memory usage by each functional module. Consequently, when a memory problem occurs, the problematic functional module can be quickly located based on its memory record, enabling timely intervention and resolving the issue of existing memory analysis tools being unable to pinpoint the problematic module. Furthermore, the implementation is not complex and does not require significant resources, thus addressing the problem that existing memory analysis tools consume substantial system resources and are unsuitable for resource-constrained systems (such as embedded systems).

[0074] Example 2:

[0075] This embodiment, based on Embodiment 1, provides a specific implementation process as an example to further illustrate this application.

[0076] In the system, a kernel patch can be added to redirect memory allocation and deallocation interfaces, for example, redirecting the kmalloc interface to tos_kmalloc. Furthermore, a function pointer tos_kmalloc_hook can be set in the redirected interface.

[0077] A new module, memwatch.ko, is added. After the kernel boots, this module, memwatch.ko, is loaded first to assign a value to tos_kmalloc_hook, which is the address of memwatch.ko.

[0078] After loading the memwatch.ko module, the memory usage of each functional module in the system is monitored in real time.

[0079] When a functional module calls the memory allocation interface or memory release interface (i.e., kmalloc) during operation, the actual runtime will call the redirected interface (tos_kmalloc), and finally pass the data back to the memwatch.ko module by calling the function pointer tos_kmalloc_hook.

[0080] The returned data includes: the calling function of the kmalloc interface (obtained using the FUNCTION macro), the line number of the function line used by the calling function to call the kmalloc interface (obtained using the LINE macro), the address of the function code segment stored in the PC register corresponding to the calling function (obtained using the current_text_addr() function) and the value of the FP register (obtained using the built-in function builtin_frame_address(0)), and the memory size allocated or freed for each function line used by the calling function to call the kmalloc interface (obtained through the kmalloc interface).

[0081] The `memwatch.ko` module uses the function `module_text_address(function code segment address)` to calculate the functional module to which the function code segment address stored in the PC register belongs. If the calculation result is not empty, the result is the functional module to which the called function belongs. If the calculation result is empty, a new function code segment address is calculated based on the value of the FP register, and the functional module to which the called function belongs is recalculated using `module_text_address(function code segment address)`. If the calculation result is still empty, the value in the FP register is taken up, a new function code segment address is recalculated, and the functional module to which the called function belongs is recalculated using `module_text_address(function code segment address)`. If the calculation result of `module_text_address(function code segment address)` is empty for 5 consecutive times, the memory size allocated or freed is determined to be the memory occupied by the kernel and is not used to update the total memory occupied by functional modules.

[0082] After determining the functional module to which the called function belongs, the memwatch.ko module locates the corresponding functional module in the record table based on the module name, finds the calling function in the record table based on the function name, and then finds the line numbers corresponding to the calling function. It then updates the total memory usage value corresponding to each line number. Specifically, it performs addition operations on the allocated memory size and subtraction operations on the freed memory size.

[0083] In the record table, the module name of the functional module is used as the key, and a hash bucket structure is used to organize the records. The function name of the called function and the line number of the function line calling the kmalloc interface are counted in the hash chain list of the module in the form of a structure.

[0084] The above solution allows for continuous updates to the memory usage of functional modules, enabling real-time monitoring of memory consumption by each module. This allows for rapid identification of problematic modules and timely intervention. Furthermore, this solution does not require significant resource consumption, making it suitable for use in resource-constrained systems (such as embedded systems).

[0085] Example 3:

[0086] Based on the same inventive concept, this application also provides a memory statistics device 600. Please refer to... Figure 6 As shown, Figure 6 It shows the use of Figure 1 The method illustrated uses a memory statistics device. It should be understood that the specific functions of device 600 are described above; to avoid repetition, detailed descriptions are omitted here. Device 600 includes at least one software function module that can be stored in memory or embedded in the operating system of device 600 in the form of software or firmware. Specifically:

[0087] See Figure 6 As shown, the device 600 includes: an acquisition module 601 and a processing module 602. Wherein:

[0088] The acquisition module 601 is used to acquire the functional module that calls the memory allocation interface or the memory release interface when the memory allocation interface or the memory release interface is called, and to acquire the memory size allocated by the memory allocation interface or the memory size released by the memory release interface.

[0089] The processing module 602 updates the total memory occupied value in the memory record corresponding to the functional module according to the requested memory size or the released memory size.

[0090] In one feasible embodiment of this application, the acquisition module 601 is specifically used for:

[0091] When the memory allocation interface or memory release interface is invoked, the target data is obtained; the target data includes the calling function that invokes the memory allocation interface or the memory release interface, and the address of the function code segment stored in the PC register corresponding to the calling function;

[0092] The functional module to which the called function belongs is calculated based on the address of the function code segment; the functional module to which the called function belongs is the functional module that calls the memory allocation interface or the memory release interface.

[0093] In this embodiment of the application, the target data further includes the value of the FP register corresponding to the called function;

[0094] The acquisition module 601 is specifically used for:

[0095] If the calculation result obtained based on the address of the function code segment is empty, then a new address of the function code segment is calculated based on the value of the FP register;

[0096] The functional module to which the called function belongs is calculated based on the new function code segment address.

[0097] In this embodiment of the application, the acquisition module 601 is further configured to: if the calculation result obtained based on the new function code segment address is still empty, then take a value upward in the FP register, calculate the new function code segment address based on the value, and calculate the functional module to which the called function belongs based on the new function code segment address; repeat the above process until the calculation result obtained based on the function code segment address is not empty, or the number of repetitions reaches a threshold.

[0098] The processing module 602 is further configured to, if the number of repetitions reaches the number threshold, determine that the requested memory size or the released memory size is the memory size corresponding to the kernel.

[0099] In this embodiment of the application, the processing module 602 is specifically used for:

[0100] Determine whether the called function exists in the memory record corresponding to the functional module;

[0101] If it does not exist, the memory record associates the called function with the requested memory size or the freed memory size;

[0102] If it exists, update the total memory occupied value corresponding to the called function in the memory record according to the requested memory size or the released memory size.

[0103] In this embodiment, the target data further includes the line number of the function line that calls the memory allocation interface or the memory release interface in the calling function; the processing module 602 is specifically used for:

[0104] The memory record associates the called function, the line number, and the memory size requested by the memory request interface or released by the memory release interface for each line number.

[0105] Based on the memory size requested by the memory request interface or the memory size released by the memory release interface according to the function line corresponding to each line number, update the total memory occupied value corresponding to each line number.

[0106] In this embodiment of the application, the processing module 602 is further configured to redirect the function pointer in the memory allocation interface or memory release interface; the acquisition module 601 is specifically configured to acquire the target data through the redirected function pointer when the memory allocation interface or memory release interface is called.

[0107] It should be understood that, for the sake of brevity, some of the content described in Embodiment 1 will not be repeated in this embodiment.

[0108] Example 4:

[0109] This embodiment provides an electronic device, see [link / reference] Figure 7 As shown, it includes a processor 701 and a memory 702. Wherein:

[0110] The processor 701 is used to execute one or more instructions stored in the memory 702 to implement the memory statistics method in Embodiment 1 and / or Embodiment 2 described above.

[0111] Understandable. Figure 7 The structure shown is for illustrative purposes only; the electronic device may also include components that are more advanced than those shown. Figure 7 The more or fewer components shown, or having the same Figure 7 The different configurations shown.

[0112] For example, the electronic device may also have a communication bus to enable communication between the processor 701 and the memory 702. Alternatively, the electronic device may have an external communication interface, such as a USB interface, to facilitate data transfer. Furthermore, the electronic device may have an information display component, such as a screen, for displaying information. Additionally, the electronic device may have an information input component, such as a keyboard, for inputting information.

[0113] Optionally, the electronic device may be a device with data processing capabilities, such as a computer, tablet, or server, and may be able to install layout design software, but this is not a limitation.

[0114] This embodiment also provides a computer-readable storage medium, such as a floppy disk, optical disk, hard disk, flash memory, USB flash drive, SD (Secure Digital Memory Card), MMC (Multimedia Card), etc., in which one or more programs implementing the above steps are stored. These one or more programs can be executed by one or more processors to implement the memory statistics method in Embodiment 1 and / or Embodiment 2. Further details will not be provided here.

[0115] In the embodiments provided in this application, it should be understood that the disclosed apparatus and methods can be implemented in other ways. The apparatus embodiments described above are merely illustrative. For example, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. Furthermore, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Additionally, the displayed or discussed mutual couplings, direct couplings, or communication connections may be through some communication interfaces; indirect couplings or communication connections between devices or units may be electrical, mechanical, or other forms.

[0116] Furthermore, the units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.

[0117] Furthermore, the functional modules in the various embodiments of this application can be integrated together to form an independent part, or each module can exist independently, or two or more modules can be integrated to form an independent part.

[0118] In this document, relational terms such as first and second are used only to distinguish one entity or operation from another entity or operation, without necessarily requiring or implying any such actual relationship or order between these entities or operations.

[0119] In this article, "multiple" refers to two or more.

[0120] The above description is merely an embodiment of this application and is not intended to limit the scope of protection of this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the scope of protection of this application.

Claims

1. A memory statistics method, characterized in that, include: When the memory allocation interface or memory release interface is called, obtain the functional module that called the memory allocation interface or the memory release interface, and obtain the memory size allocated by calling the memory allocation interface, or obtain the memory size released by calling the memory release interface; Update the total memory occupied value in the memory record corresponding to the functional module according to the requested memory size or the released memory size; When a memory allocation interface or a memory release interface is invoked, the function module that invokes the memory allocation interface or the memory release interface is retrieved, including: When the memory allocation interface or memory release interface is invoked, the target data is obtained; the target data includes the calling function that invokes the memory allocation interface or the memory release interface, and the address of the function code segment stored in the PC register corresponding to the calling function; The functional module to which the calling function belongs is calculated based on the address of the function code segment; the functional module to which the calling function belongs is the functional module that calls the memory allocation interface or the memory release interface. The target data also includes the value of the FP register corresponding to the called function; Calculating the functional module to which the called function belongs based on the address of the function code segment includes: If the calculation result obtained based on the address of the function code segment is empty, then a new address of the function code segment is calculated based on the value of the FP register; Calculate the functional module to which the called function belongs based on the new function code segment address; The method further includes: If the calculation result obtained based on the new function code segment address is still empty, then the value is taken up from the FP register, the new function code segment address is calculated based on the value, and the functional module to which the called function belongs is calculated based on the new function code segment address; Repeat the above process until the calculation result obtained from the address of the function code segment is not empty, or the number of repetitions reaches the threshold. If the number of repetitions reaches the threshold, then the requested memory size or the released memory size is determined to be the memory size corresponding to the kernel.

2. The method as described in claim 1, characterized in that, Based on the requested memory size or the released memory size, update the total memory usage value in the memory record corresponding to the functional module, including: Determine whether the called function exists in the memory record corresponding to the functional module; If it does not exist, the memory record associates the called function with the requested memory size or the freed memory size; If it exists, update the total memory occupied value corresponding to the called function in the memory record according to the requested memory size or the released memory size.

3. The method as described in claim 2, characterized in that, The target data also includes the line number of the function line that calls the memory allocation interface or the memory release interface in the calling function; The memory record associates the called function with the requested memory size or the freed memory size, including: The memory record associates the called function, the line number, and the memory size requested by the memory request interface or released by the memory release interface for each line number. Correspondingly, based on the requested memory size or the released memory size, the total memory occupied value corresponding to the called function in the memory record is updated, including: Based on the memory size requested by the memory request interface or the memory size released by the memory release interface according to the function line corresponding to each line number, update the total memory occupied value corresponding to each line number.

4. The method according to any one of claims 1, characterized in that, The method further includes: redirecting the function pointer in the memory allocation interface or memory release interface; When the memory allocation or memory release interface is called, obtain the target data, including: When the memory allocation interface or memory release interface is called, the target data is obtained through the redirected function pointer.

5. A memory statistics device, characterized in that, include: The acquisition module is used to acquire the functional module that calls the memory allocation interface or the memory release interface when the memory allocation interface or the memory release interface is called, and to acquire the memory size allocated by the memory allocation interface or the memory size released by the memory release interface. The processing module updates the total memory occupied value in the memory record corresponding to the functional module according to the requested memory size or the released memory size; The acquisition module is specifically used for: acquiring target data when the memory allocation interface or memory release interface is called; the target data includes the calling function that calls the memory allocation interface or the memory release interface, and the function code segment address stored in the PC register corresponding to the calling function; calculating the functional module to which the calling function belongs based on the function code segment address; the functional module to which the calling function belongs is the functional module that calls the memory allocation interface or the memory release interface; The target data also includes the value of the FP register corresponding to the called function; the acquisition module is specifically used to: if the calculation result obtained based on the function code segment address is empty, calculate a new function code segment address based on the value of the FP register; and calculate the functional module to which the called function belongs based on the new function code segment address. The acquisition module is further configured to: if the calculation result obtained based on the new function code segment address is still empty, then take a value upward in the FP register, calculate the new function code segment address based on the value, and calculate the functional module to which the called function belongs based on the new function code segment address; Repeat the above process until the calculation result obtained from the address of the function code segment is not empty, or the number of repetitions reaches the threshold. The processing module is further configured to, if the number of repetitions reaches the threshold, determine that the requested memory size or the released memory size is the memory size corresponding to the kernel.

6. An electronic device, characterized in that, It includes a processor and a memory; the processor is configured to execute one or more instructions stored in the memory to implement the method as described in any one of claims 1-4.

7. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores one or more programs, which can be executed by one or more processors to implement the method as described in any one of claims 1-4.

Citation Information

Patent Citations

  • Memory operation recording method and device of dynamic library, equipment and storage medium

    CN111274155A

  • Memory monitoring method and device, medium and electronic equipment

    CN112084024A

  • Method and apparatus for locating memory leakage

    WO2017035785A1