A memory statistics method, a memory statistics device and a computer readable storage medium
By obtaining the address of the target function in the Linux system, constructing a custom function, and relocating the global offset table, the problem of incomplete memory leak diagnosis in existing technologies is solved. It also enables the statistics of memory usage of executable programs and dynamic libraries, improving diagnostic accuracy and efficiency, and is suitable for embedded devices.
Patent Information
- Application Number
- CN202210442421.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-04-25
- Publication Date
- 2025-11-28
- Estimated Expiration
- 2042-04-25
AI Technical Summary
In the Linux operating system, existing technologies cannot accurately count the memory allocation of executable programs and dynamic link libraries at the same time, resulting in incomplete memory leak diagnosis and requiring modification of source code or compilation and linking, which affects program running efficiency.
By obtaining the address of the target function, a custom function is constructed. The global offset table is then modified using the relocation address of the global offset table to achieve memory usage statistics for the executable program and dynamic libraries. Stack backtracking and relocation techniques are used to generate memory statistics files.
It achieves broad applicability in statistical analysis of memory usage for executable programs and dynamic libraries, improving the accuracy and efficiency of memory leak diagnosis, avoiding modifications to the program's source code, and is suitable for embedded devices.
Smart Images

Figure CN114968702B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer, in particular to a memory statistics method, a memory statistics device and a computer readable storage medium. BACKGROUND
[0002] When developing an application program in a Linux operating system, it is often necessary to pay attention to the size of the virtual memory and physical memory occupied by the process. Too much resource occupation, memory leakage and memory fragmentation can all cause the system to kill the user process due to insufficient memory. When troubleshooting these problems, it is particularly important to accurately count the system memory resource consumption. SUMMARY
[0003] The present application provides a memory statistics method, a memory statistics device and a computer readable storage medium, which can simultaneously count the memory allocation of executable programs and dynamic link libraries.
[0004] To solve the above technical problems, the technical scheme adopted by the present application is to provide a memory statistics method, which comprises: obtaining the address of a target function, the target function including functions in a preset dynamic library and / or functions in an executable program; constructing a custom function based on the prototype of the target function, wherein the custom function calls the target function; obtaining the position of the target function in a global offset table to obtain a relocation address; performing relocation processing on the global offset table based on the relocation address; and obtaining the memory usage of the target function based on the custom function and the relocated global offset table.
[0005] To solve the above technical problems, another technical scheme adopted by the present application is to provide a memory statistics device, which comprises a memory and a processor connected to each other, wherein the memory is used to store a computer program, and the computer program is used to implement the memory statistics method in the above technical scheme when executed by the processor.
[0006] To solve the above technical problems, another technical scheme adopted by the present application is to provide a computer readable storage medium, which is used to store a computer program, and the computer program is used to implement the memory statistics method in the above technical scheme when executed by the processor.
[0007] By the above scheme, the beneficial effects of the present application are: the scheme provided by the present application first acquires the functions in the preset dynamic library and / or the functions in the executable program, obtains the target function and acquires the address of the target function; then constructs a custom function using the prototype of the target function, the custom function calls the target function; then acquires the position of the target function in the global offset table to obtain the relocation address; then performs relocation processing on the global offset table using the relocation address; then uses the custom function and the relocated global offset table to obtain the memory usage of the target function, the present scheme can simultaneously perform memory detection processing on the functions in the executable program and the functions in the dynamic library, realize the statistics of the memory usage, and has relatively wide applicability. BRIEF DESCRIPTION OF DRAWINGS
[0008] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed to be used in the embodiment description will be briefly introduced as follows. Obviously, the drawings in the following description are only some embodiments of the present application, and other drawings can be obtained by those skilled in the art without creative effort on the basis of these drawings. Among them:
[0009] Figure 1 is a flowchart of an embodiment of the memory statistics method provided by the present application;
[0010] Figure 2 is a program jump schematic diagram provided by the present application;
[0011] Figure 3 is a flowchart of another embodiment of the memory statistics method provided by the present application;
[0012] Figure 4 is a corresponding schematic diagram of the relocation entry table, the dynamic link symbol table and the dynamic link string table provided by the present application;
[0013] Figure 5 is a schematic diagram of the memory usage statistics provided by the present application;
[0014] Figure 6 is a schematic diagram of the memory curve provided by the present application;
[0015] Figure 7 is a structural schematic diagram of an embodiment of the memory statistics device provided by the present application;
[0016] Figure 8 is a structural schematic diagram of an embodiment of the computer readable storage medium provided by the present application. DETAILED DESCRIPTION
[0017] The application will be described in further detail below with reference to the drawings and embodiments. It is to be particularly noted that the following embodiments are only illustrative of the application and are not intended to limit the scope of the application. Similarly, the following embodiments are only some of the embodiments of the application and all other embodiments obtained by those of ordinary skill in the art without creative work are within the scope of the application.
[0018] Reference to "an embodiment" or "the embodiment" in this application means that a particular feature, structure, or characteristic described in connection with the embodiment can be included in at least one embodiment of the application. The appearances of the phrase that the phrase in various places in the specification are not necessarily all referring to the same embodiment, or are necessarily referring to some particular embodiment, or are mutually exclusive in reference to some other embodiments.
[0019] It should be noted that the terms "first", "second", "third", etc. in the application are only for the purpose of description, and cannot be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Therefore, the features limited by "first", "second", "third" can explicitly or implicitly include at least one of the features. In the description of the application, the meaning of "a plurality of" is at least two, such as two, three, etc., unless otherwise specifically limited. In addition, the terms "include" and "have" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product or device including a series of steps or units is not limited to the listed steps or units, but can optionally include steps or units not listed, or can optionally include other steps or units inherent to the process, method, product or device.
[0020] Under the existing conditions, the most frequently used command is the top command, which can intuitively print the central processing unit (CPU) and memory resource consumption of a large number of processes, and is useful when roughly checking resource consumption information, and the command operation is simple.
[0021] In addition to the top command, if the developer also wants to obtain more data about the resource consumption of the process, some special tools can also be used, such as the sar command or the pidstat command in the sysstat tool set. By using these tools, the memory consumption at the thread level can be counted.
[0022] For some developers who have a deep understanding of Linux system, they can also print kernel running data through files under / proc path, for example, / proc / meminfo can view detailed memory consumption of the system, which includes physical memory, virtual memory or cache and the like, and the information amount is large; / proc / xxx / status can view consumption values of virtual memory and physical memory.
[0023] The above three methods can realize fast diagnosis when troubleshooting memory leakage, but cannot achieve further viewing of memory consumption data of each sub-module (such as a dynamic library); and in software engineering, software development of each field is usually completed by different teams, and each team provides a respective library file (namely a binary file), and finally the library files are linked into an executable program by a compiling tool; the independent library files generally apply for memory from a heap when the memory is needed, and due to human reasons, the developer does not necessarily release the memory after the memory is used, thereby causing memory leakage.
[0024] For the problem of memory leakage, commonly used memory tools are virgrand and address sanatizer, virgrand occupies a large amount of CPU resources, and seriously reduces program running speed, and when a large program is run on an embedded device, it is difficult to troubleshoot a memory leakage point by using virgrand; address sanatizer also slightly reduces program running speed, but has certain requirements for a version of a compiler, and in addition, the program needs to be completely exited, and in an embedded device, many programs are always running with the device starting, and in this case, address sanatizer cannot be used to troubleshoot a memory leakage problem. Although some solutions in the related art can solve the problem of memory leakage, there are problems such as being easy to miss, needing to modify a source code of a program to be tested, or being unable to simultaneously count memory allocation of an executable program and a dynamic link library. In order to solve the problems in the related art, the present application provides a new solution, and the technical solution adopted by the present application is described in detail below.
[0025] Please refer to Figure 1 , Figure 1 is a flowchart of an embodiment of a memory counting method provided by the present application, and the method comprises the following steps:
[0026] S11: An address of a target function is acquired.
[0027] The target function includes a function in the preset dynamic library and / or a function in an executable program. Specifically, taking the target function as a function in the preset dynamic library (denoted as a dynamic library function) as an example, in order to obtain the real address of the dynamic library function, the preset dynamic library can be first loaded into the memory by using a first preset function; and then the address of the target function is obtained by using a second preset function. For example, assuming that the dynamic library function is a malloc function, the preset dynamic library is a libdl.so library, the first preset function is a dlopen function, and the second preset function is a dlsym function, the libdl.so library provides the dlopen function in a linux environment, which is used to manually load a dynamic library, such as loading libc.so into the memory, and then the address of the malloc function is obtained by using the dlsym function.
[0028] In a specific embodiment, in order to reduce the number of processing tasks, a filtering operation can be performed, that is, all the dynamic libraries loaded into the memory are traversed, and all the functions are filtered by using a preset string name in a fourth preset function to obtain the target function. For example, the fourth preset function is “dl_iterate_phdr”, and the “dl_iterate_phdr” function is provided in the libdl.so library in the linux environment, which is used to traverse all the dynamic libraries loaded into the memory; then, in the callback function, the functions that are not of interest are filtered by using the preset string name to obtain the functions of interest (that is, the target function).
[0029] S12: Constructing a custom function based on the prototype of the target function.
[0030] In order to implement a function customized by a user (denoted as a custom function), the prototype (that is, the set rule or format) corresponding to the target function can be first obtained, and then the custom function is constructed by using the prototype of the target function, and the custom function can call the target function. Specifically, taking the target function as a dynamic library function as an example, the custom function can be defined according to the prototype of the function to be patched (that is, the dynamic library function), and the dynamic library function is called in the custom function, and the address of the dynamic library function has been obtained in S11.
[0031] S13: Obtaining the position of the target function in the global offset table to obtain a relocation address.
[0032] First, a global offset table (got) is acquired, and then a position of a target function in the global offset table (i.e., a relocation address) is acquired; specifically, the global offset table includes multiple relocation entries, when an assembler generates a target module, it does not know specific storage positions of data and code in the memory, and does not know positions of any externally defined functions or global variables referenced by the target module, therefore, when the assembler encounters a target reference whose final position is unknown, a relocation entry is generated to tell the linker to modify the reference when the target file is combined; when a program is loaded into the memory for running, a dynamic library relied on by the program is also loaded into the memory, and when an external symbol is called, dynamic linking is completed to modify the target reference into a real external address.
[0033] In a specific embodiment, taking a dynamic library function as an example, a calling mechanism of the dynamic library function can be used in the process of acquiring the relocation address, and the calling mechanism is introduced as follows:
[0034] When a program is loaded into the memory, a dynamic linker loads and binds a required dynamic library into an address space of the program; subsequently, when a function is called, an address of the function is resolved to achieve the purpose of calling the function. Calling a function in a dynamic library is different from calling an internal function of an executable program, an address of the dynamic library function is unknown before the dynamic library is loaded, therefore, a real address of a to-be-called function is not specified in a code segment of the executable program, but a kind of delay binding mechanism is used to achieve the purpose, symbol resolution and address binding are performed when the function is called, and specifically, two tables are involved: a procedure linkage table (plt) and a global offset table, the global offset table is located in a data segment, and each table entry stores an address; the plt table is located in the code segment, each table entry indicates a plurality of instructions related to a to-be-bound function, and stores code used for delay binding, the code includes a jmp instruction used for jumping to an address stored in a corresponding global offset table entry.
[0035] For example, as shown in a program jump schematic diagram shown in FIG. 1, it is assumed that a function test in an "a.out" program calls a malloc function, and a calling process of the function is as follows: Figure 2
[0036] In the first calling, a second entry of the global offset table stores an address of an instruction in the plt table, the address is jumped to and subsequent linking processing is performed, after the linking is completed, the malloc function in the real glibc library is executed, and a jump path is 1-2-3-4-5, which is briefly described as:
[0037] test-->malloc@plt-->dl_runtime_resolve-->malloc@glibc
[0038] In the second call, first jump to malloc@plt, then jump to the corresponding address in the global offset table, since it has been linked, the global offset table stores the address of the malloc function in the glibc library, then the malloc function can be executed, the jump path is 1-2-5, and the brief description is:
[0039] test-->malloc@plt-->malloc@glibc
[0040] According to the above dynamic library function delay binding mechanism, the value (i.e. the jump address of the plt instruction) in the global offset table can be modified to the address of the user-defined function, for example, the my_malloc function, after the program is started, so as to achieve the purpose of patching the malloc function.
[0041] Understandably, the above method is applicable to executable programs and dynamic libraries; for ease of description, the embodiment takes the malloc function as an example, but is not limited thereto, and the method is applicable to external function calls in any dynamic library.
[0042] S14: Perform relocation processing on the global offset table based on the relocation address.
[0043] After obtaining the relocation address, the relocation address is used to modify the global offset table; for example, the value corresponding to the relocation address in the global offset table is modified to the address of the user-defined custom function, and the relocation is completed.
[0044] S15: Obtain the memory usage of the target function based on the custom function and the global offset table after relocation.
[0045] After obtaining the global offset table after relocation processing, the global offset table and the custom function are processed to generate the memory usage corresponding to the target function, and the automatic detection of memory is realized.
[0046] The embodiment provides a module memory statistics method based on dynamic linking, which can be applied to the field of linux memory management, and relates to dynamic linking, memory management, relocation and delay binding technology; the scheme can simultaneously patch executable programs and dynamic libraries, thereby realizing complete statistics of the memory consumption of each module in the process; in addition, the modules to be relocated in the scheme include executable files and dynamic libraries, which are performed one by one, and the modules that are not interested can be filtered out through name comparison, so that the memory data of certain modules is statistically targeted, resources are saved, and the statistical efficiency is improved.
[0047] Referring to Figure 3 , Figure 3 is a flowchart of another embodiment of the memory statistics method provided in the present application, and the method comprises the following steps:
[0048] S31: Obtain the address of the target function.
[0049] S32: Construct a custom function based on the prototype of the target function.
[0050] S31-S32 are the same as S11-S12 in the above embodiment, and will not be described again here.
[0051] S33: Generate a relocation address based on the relocation entry table, the dynamic link symbol table and the dynamic link string table; and perform relocation processing on the global offset table based on the relocation address.
[0052] Dynamic linking needs to use three tables in the executable program: the relocation entry table, the dynamic link symbol table and the dynamic link string table, so the relocation entry table, the dynamic link symbol table and the dynamic link string table are obtained first; then, a piece of relocation information is selected from the relocation entry table to obtain current relocation information; the current relocation information is matched with the dynamic link symbol table to obtain a symbol name; the symbol name is matched with the dynamic link string table to obtain a current string; and a relocation address is generated based on the current string. Specifically, it can be determined whether the current string is the same as the name of the target function; if the current string is the same as the name of the target function, the position where the current string is located is processed to obtain the relocation address; and if the current string is different from the name of the target function, the step of selecting a piece of relocation information from the relocation entry table is returned until the relocation entry table is traversed completely or the current string is the same as the name of the target function.
[0053] Further, the position of the relocation information corresponding to the position where the current string is located in the relocation entry table is obtained to obtain an offset address; and the base address of the preset dynamic library and the offset address are superimposed to obtain the relocation address, wherein the base address of the preset dynamic library is the first address of the preset dynamic library.
[0054] In an embodiment, taking the malloc function as an example, the above three tables (i.e., the relocation entry table, the dynamic link symbol table and the dynamic link string table) can be used to find the corresponding position of the malloc function in the global offset table, and according to the delay binding mechanism, the value in the global offset table is modified to be the address of the custom function to complete the relocation. Specifically, the relationship among the three tables is as follows: Figure 4As shown, the relocation entry table, the dynamic linking symbol table and the dynamic linking string table are respectively recorded as ".rela.dyn", ".dynsym" and ".dynstr", and the process of relocating the malloc function is as follows:
[0055] The ".rela.dyn" table is traversed, the ".dynsym" table is obtained through the "r_info" field, the string in the ".dynstr" table is obtained through the "st_name" field of the ".dynsym" table, if the string is the same as malloc, the matching relocation table item (i.e. relocation information) is successfully found, the traversal of ".rela.dyn" is ended, and then the relocation address is calculated using the relocation table item, and the calculation method is as follows:
[0056] Relocation address = base address of dynamic library + offset address in relocation table item
[0057] Then, the address of the custom function is written to the relocation address, and the relocation is completed.
[0058] In an embodiment, the name of the dynamic library is also obtained by performing program address space mapping, that is, the name of the preset dynamic library is generated based on the preset dynamic library address and the preset address mapping table. Specifically, the preset file is read to obtain the preset address mapping table, and the preset address mapping table includes a plurality of corresponding relationships; the corresponding relationship of the preset dynamic library is filtered from the preset address mapping table to obtain a target corresponding relationship; and the name of the preset dynamic library is obtained based on the target corresponding relationship, so that it is found which dynamic library the target function belongs to.
[0059] For example, in a linux environment, the preset address mapping table of the program can be obtained by reading the " / proc / ${PID} / maps" file, and then the address mapping of the preset dynamic library is filtered from the preset address mapping table, and the base address of the preset dynamic library can be obtained based on the address mapping, for example, as follows:
[0060] 7b9e128000-7b9e12e000r--p00000000fd:00 3397stem / lib64 / liblog.so
[0061] It can be understood that after obtaining the global offset table after relocation, the custom function can be relocated using the global offset table.
[0062] S34: calling the third preset function in the custom function to obtain the address of the caller of the target function; and calling the target function in the custom function to obtain the memory usage of the target function.
[0063] The third preset function is called in the custom function, and the custom function is used to perform stack backtracking on the target function. For example, in the custom my_malloc function, stack backtracking is performed first, the address of the caller (i.e., the main body of the function called in the my_malloc function) is obtained by calling the built-in function "__builtin_return_address" in the code, and then the real malloc function is called.
[0064] S35: Based on the address of the caller and the memory usage of the target function, a memory statistics file is generated.
[0065] The address of the caller of the target function can be matched with the addresses of all dynamic libraries to obtain the dynamic library to which the caller belongs, that is, to check in which address range of the dynamic library the address of the caller falls; the memory usage (i.e., the memory allocation size) of the target function is added to the information corresponding to the target dynamic library to obtain a memory statistics file. For example, by means of a timing trigger or a manual trigger, the information of the memory usage is printed into the file corresponding to the target dynamic library to generate the memory statistics file.
[0066] Further, the memory usage of the target function is obtained at preset time intervals to obtain memory statistics data, and the memory statistics data includes a plurality of memory usages; based on the memory statistics data, a memory curve graph is generated, and the memory curve graph includes a plurality of collection times and memory usages corresponding to the collection times; based on the memory curve graph, it is determined whether a memory leak event occurs; if it is identified that a memory leak event occurs, a prompt message is generated, and the prompt message is used to indicate that a memory leak event occurs. Specifically, the difference between the memory usages corresponding to two adjacent collection times in the memory curve graph can be calculated; it is determined whether the difference is greater than a preset value; if the difference is greater than the preset value, it is determined that a memory leak event occurs; if the difference is less than or equal to the preset value, it is determined that no memory leak event occurs.
[0067] In an embodiment, the memory statistics data can be output to a certain file at a timing, and the data can be extracted by a script and a line graph can be drawn, as shown in Figure 6 As shown, the memory change trend of each module can be seen, wherein the vertical coordinate is the number of bytes, and the horizontal coordinate is the collection time; in actual application, a python script can be used to draw the memory usage in the "libstdc++.so.6.0.24" library.
[0068] Understandably, when the memory release is performed, the custom free function can be called, and then the related information recorded when the memory is allocated is queried by using the address to be released, and the related information is adaptively deleted.
[0069] The embodiment marks each memory allocation by stack backtracking, and can predict which line of code has a memory leak through data analysis, so that the accuracy of detecting the memory leak is high, and the phenomenon of memory leak is prevented. In addition, the device (i.e., a linkable library file) provided by the embodiment can be directly linked in the library file to take effect, and the function is configured through an environment variable, so that the source code of the measured object does not need to be modified, and no damage is caused, and the implementation is simple and the safety is high. Moreover, the relocation process occurs entirely in the device, and the measured object does not need to intervene, and the global offset table of the measured object is modified during relocation. Since the operation is also completed in the running process, the source code does not need to be modified by the developer, so that the development and use difficulty of the developer is simplified. In addition, since the relocation information exists in the executable program and the dynamic library, the external functions malloc and free can be relocated according to the relocation information, so that the use range is expanded.
[0070] Referring to Figure 7 , Figure 7 is a structural schematic diagram of an embodiment of a memory statistics device provided by the application. The memory statistics device 70 includes a memory 71 and a processor 72 connected to each other. The memory 71 is used to store a computer program. The computer program is used to implement the memory statistics method in the above embodiment when executed by the processor 72. The memory statistics device 70 can be a server or an embedded device.
[0071] The embodiment can be used to count the memory size of a closed-source module. Moreover, the scheme adopted by the embodiment can filter the measured module according to the principle of dynamic linking, so that the memory change of one or more measured modules is monitored, and the pertinence is strong, and the processing pressure is reduced. In addition, the allocator function in the measured module can be relocated according to the principle of dynamic linking, and the source code of the measured module does not need to be modified, and the cross-platform use is realized.
[0072] Referring to Figure 8 , Figure 8 is a structural schematic diagram of an embodiment of a computer readable storage medium provided by the application. The computer readable storage medium 80 is used to store a computer program 81. The computer program 81 is used to implement the memory statistics method in the above embodiment when executed by a processor.
[0073] The computer readable storage medium 80 can be a server, a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, and various media that can store program codes.
[0074] In several embodiments provided in the present application, it should be understood that the disclosed methods and devices can be implemented in other manners. For example, the embodiments of the device described above are merely schematic, and the division of the modules or units is merely logical function division, and there can be another division manner in actual implementation. For example, a plurality of units or components can be combined or integrated into another system, or some features can be ignored or not executed. In this way, the actual implementation can be divided into other forms.
[0075] The units described as separated components can or can not be physically separated, and the components displayed as units can or can not be physical units, i.e., can be located in one place, or can be distributed on a plurality of network units. Some or all of the units can be selected according to actual needs to achieve the purposes of the embodiments.
[0076] In addition, each functional unit in the embodiments of the present application can be integrated in a processing unit, or each unit can exist physically as a separate unit, or two or more units can be integrated in one unit. The integrated unit can be implemented in the form of hardware, or in the form of a software functional unit.
[0077] The above description is merely an example of the embodiments of the present application, and does not limit the patent scope of the present application. Any equivalent structure or equivalent flow transformation based on the content of the present application specification and drawings, or direct or indirect application in other related technical fields, is also included in the patent protection scope of the present application.
Claims
1. A memory statistics method, characterized by, The method comprises the following steps: obtaining an address of a target function, the target function comprising a function in a preset dynamic library and / or a function in an executable program; constructing a custom function based on a prototype of the target function, wherein the custom function calls the target function; obtaining a location of the target function in a global offset table to obtain a relocation address; relocating the global offset table based on the relocation address; obtaining a memory usage of the target function based on the custom function and the relocated global offset table; wherein the memory usage of the target function is obtained by calling a third preset function in the custom function to obtain an address of a caller of the target function, and calling the target function in the custom function to obtain the memory usage of the target function.
2. The memory statistics method of claim 1, wherein, The step of obtaining the location of the target function in the global offset table to obtain the relocation address comprises the following steps: generating the relocation address based on a relocation entry table, a dynamic link symbol table and a dynamic link string table.
3. The memory statistics method of claim 2, wherein, The step of generating the relocation address based on the relocation entry table, the dynamic link symbol table and the dynamic link string table comprises the following steps: selecting a piece of relocation information from the relocation entry table to obtain current relocation information; matching the current relocation information with the dynamic link symbol table to obtain a symbol name; matching the symbol name with the dynamic link string table to obtain a current string; generating the relocation address based on the current string.
4. The memory statistics method of claim 3, wherein, The step of generating the relocation address based on the current string comprises the following steps: determining whether the current string is the same as the name of the target function; if yes, processing a location where the current string is located to obtain the relocation address; if no, returning to the step of selecting a piece of relocation information from the relocation entry table until the relocation entry table is traversed completely or the current string is the same as the name of the target function.
5. The memory statistics method of claim 4, wherein, The step of processing the location where the current string is located to obtain the relocation address comprises the following steps: obtaining a location of the relocation information corresponding to the location where the current string is located in the relocation entry table to obtain an offset address; superimposing a base address in the preset dynamic library and the offset address to obtain the relocation address.
6. The memory statistics method of claim 5, wherein, Before the step of superimposing the base address in the preset dynamic library and the offset address, the method comprises the following steps: reading a preset file to obtain a preset address mapping table, the preset address mapping table comprising a plurality of corresponding relationships; filtering the corresponding relationship of the preset dynamic library from the preset address mapping table to obtain a target corresponding relationship; obtaining the name of the preset dynamic library based on the target corresponding relationship.
7. The memory statistics method of claim 1, wherein, The step of obtaining the address of the target function comprises the following steps: loading the preset dynamic library into memory by using a first preset function; obtaining the address of the target function by using a second preset function.
8. The memory statistics method of claim 1, wherein, The step of relocating the global offset table based on the relocation address comprises the following steps: write the address of the custom function to a relocation address in the global offset table.
9. The memory statistics method of claim 1, wherein, The method further comprises: calling a third preset function in the custom function to obtain an address of a caller of the target function; calling the target function in the custom function to obtain a memory usage of the target function; generating a memory statistics file based on the address of the caller and the memory usage of the target function.
10. The memory statistics method of claim 9, wherein, The step of generating the memory statistics file based on the address of the caller and the memory usage of the target function comprises: matching the address of the caller with addresses of all dynamic libraries to obtain a dynamic library to which the caller belongs, to obtain a target dynamic library; adding the memory usage of the target function to information corresponding to the target dynamic library to obtain the memory statistics file.
11. The memory statistics method of claim 10, wherein, The method further comprises: obtaining the memory usage of the target function at preset time intervals to obtain memory statistics data, the memory statistics data comprising a plurality of memory usages; generating a memory curve graph based on the memory statistics data, the memory curve graph comprising a plurality of collection times and memory usages corresponding to the collection times; judging whether a memory leak event occurs based on the memory curve graph; if yes, generating a prompt message, the prompt message being used to indicate that the memory leak event occurs currently.
12. The memory statistics method of claim 11, wherein, The step of judging whether the memory leak event occurs based on the memory curve graph comprises: calculating a difference value of memory usages corresponding to adjacent two collection times in the memory curve graph; judging whether the difference value is greater than a preset value; if yes, determining that the memory leak event occurs.
13. The memory statistics method of claim 1, wherein, The method further comprises: traversing all dynamic libraries loaded into memory and filtering all functions using a preset string name in a fourth preset function to obtain the target function.
14. A memory statistics device, comprising: A computer program product comprising a memory and a processor connected to each other, wherein the memory is configured to store a computer program, and the computer program, when executed by the processor, is configured to implement the memory statistics method in any one of claims 1-13.
15. A computer readable storage medium for storing a computer program, characterized in that, The computer program, when executed by the processor, is configured to implement the memory statistics method in any one of claims 1-13.
Citation Information
Patent Citations
Tracking method and device for dynamic link library function
CN111290952A
Memory allocation information processing method, device, electronic equipment and storage medium
CN113778687A