A memory leak monitoring method, a memory leak detection method and corresponding apparatuses
By intercepting the memory operation information when the program under test calls the memory monitoring dynamic library and sending it to the server, the problem of memory leak detection having a significant impact on program execution in existing technologies is solved, and continuous memory leak monitoring and detection is achieved, especially for long-running service processes.
Patent Information
- Application Number
- CN202111412997.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-11-25
- Publication Date
- 2025-12-12
- Estimated Expiration
- 2041-11-25
AI Technical Summary
Existing memory leak detection methods, while achieving memory leak monitoring and detection, have a significant impact on the execution of the program being tested.
By intercepting and acquiring memory operation information when the program under test calls the memory monitoring dynamic library through a preloading mechanism, and sending it to the server, complex processing operations are avoided on the terminal device, and memory leak monitoring and detection are performed using the server.
It reduces the impact on the execution of the detection program itself, enabling continuous memory leak monitoring and detection, especially for long-running service processes.
Smart Images

Figure CN113918373B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer memory management, memory leak monitoring and memory leak detection, in particular to a memory leak monitoring method, a memory leak detection method and corresponding devices. BACKGROUND
[0002] Memory leak, also known as memory leak, is a resource leak in computer science. Memory leak does not mean the physical disappearance of memory, but the application program is not released after use, which causes the memory space to be unable to be used by the program again, resulting in waste of memory. Memory leak can usually only be analyzed by programmers who have the source code of the program.
[0003] At present, the mainstream memory leak detection methods are static code analysis and dynamic code analysis. Static code analysis mostly refers to analyzing the source code of the process in the compilation phase of the program with the help of compilers and other tools to detect possible leak points. Dynamic code analysis analyzes the memory allocation and release of the program when the program to be detected is running. Usually, function rewriting, code insertion and other methods are used to collect memory allocation and release information of the program, and then further analyze whether the process has memory leak.
[0004] In the specific practice process, it is found that both methods have a great influence on the execution of the program to be detected. Taking dynamic code analysis as an example, dynamic code analysis usually needs to manually link the program to the library of the analysis tool or modify the code of the program itself to call the specified allocation and release, initialization interface, etc. That is, the current memory leak detection method has a great influence on the execution of the program to be detected while implementing memory leak monitoring and detection. SUMMARY
[0005] The purpose of the embodiments of the present application is to provide a memory leak monitoring method, a memory leak detection method and corresponding devices, which can improve the problem that the execution of the program to be detected is greatly influenced while implementing memory leak monitoring and detection.
[0006] The embodiment of the present application provides a memory leakage monitoring method, which is applied to a terminal device and comprises the following steps: when a program to be detected calls a memory monitoring dynamic library through a preloading mechanism, intercepting memory operation information of the program to be detected, wherein memory allocation functions and memory release functions in the memory monitoring dynamic library are rewritten; and sending the memory operation information of the program to be detected to a server. In the implementation process, the memory operation information of the program to be detected is intercepted when the program to be detected calls the memory monitoring dynamic library through the preloading mechanism, and the memory operation information of the program to be detected is sent to the server, so that redundant storage, query, retrieval, symbol analysis and other complex processing operations on the terminal device are avoided, thereby realizing memory leakage monitoring and detection while greatly reducing the influence on the execution of the program to be detected.
[0007] Optionally, in the embodiment of the present application, the memory operation information comprises memory allocation information and memory release information; and the step of sending the memory operation information of the program to be detected to the server comprises the step of sending the memory allocation information and the memory release information to the server through a shared queue. In the implementation process, the memory allocation information and the memory release information are sent to the server through the shared queue, so that the server can receive the memory allocation information and the memory release information, thereby improving the communication efficiency between the server and the terminal device.
[0008] Optionally, in the embodiment of the present application, before the memory allocation information and the memory release information are sent to the server through the shared queue, the method further comprises the steps of: judging whether the program to be detected meets a preset condition, wherein the preset condition is that the program to be detected is in a monitoring list and calls the memory monitoring dynamic library for the first time; and if yes, initializing the shared queue. In the implementation process, the shared queue is initialized only when the program to be detected is in the monitoring list and calls the memory monitoring dynamic library for the first time, and the monitoring list (also referred to as a white list) is a configuration file provided for a user to configure a process to be detected, thereby realizing the function of memory monitoring and memory detection on a specified application program.
[0009] Optionally, in the embodiment of the present application, the memory allocation information comprises an allocated memory size, an allocated return address, a stack backtracking address and process ID information; and the memory release information comprises a released memory address, a stack backtracking address and process ID information.
[0010] The embodiment of the present application further provides a memory leakage detection method, which is applied to a server and comprises the following steps: receiving memory operation information of a program to be detected sent by a terminal device, the memory operation information of the program to be detected being obtained by interception when the program to be detected calls a memory monitoring dynamic library through a preloading mechanism, memory allocation functions and memory release functions in the memory monitoring dynamic library being rewritten; and analyzing and storing the memory operation information of the program to be detected in a hash linked list. In the implementation process, compared with a mode of only providing short-term memory leakage detection and then exiting after obtaining a detection report, the server is used to receive and analyze the memory operation information, so that the server can continuously monitor a long-term running service process (such as a daemon process), thereby achieving the effect of providing continuous memory leakage monitoring and detection.
[0011] Optionally, in the embodiment of the present application, the memory operation information of the program to be detected is analyzed and stored in the hash linked list, comprising: analyzing a stack backtracking address from the memory operation information, and calculating a hash value according to the stack backtracking address; and storing or updating the memory operation information in the hash linked list by taking the hash value as a node identifier.
[0012] Optionally, in the embodiment of the present application, the method further comprises the following steps: obtaining a memory viewing command request sent by a command terminal program; and sending a detection result of the memory leakage to the command terminal program according to the memory viewing command request, so that the command terminal program outputs the detection result of the memory leakage.
[0013] The embodiment of the present application further provides a memory leakage monitoring device, which is applied to a terminal device and comprises: a memory operation interception module, configured to intercept memory operation information of a program to be detected when the program to be detected calls a memory monitoring dynamic library through a preloading mechanism, memory allocation functions and memory release functions in the memory monitoring dynamic library being rewritten; and an operation information sending module, configured to send the memory operation information of the program to be detected to a server.
[0014] Optionally, in the embodiment of the present application, the memory operation information comprises memory allocation information and memory release information; and the operation information sending module comprises a memory information sending module, configured to send the memory allocation information and the memory release information to the server through a shared queue.
[0015] Optionally, in the embodiment of the present application, the memory leak monitoring device further comprises a detection program judgment module configured to judge whether the to-be-detected program meets a preset condition, the preset condition being that the to-be-detected program is in the monitoring list and is called for the first time to call the memory monitoring dynamic library; and a ring queue initialization module configured to initialize the shared queue if the to-be-detected program meets the preset condition. Optionally, in the embodiment of the present application, the memory allocation information comprises an allocated memory size, an allocated return address, a stack backtracking address and process ID information; and the memory release information comprises a released memory address, a stack backtracking address and process ID information.
[0016] The embodiment of the present application further provides a memory leak detection device applied to a server, comprising: an operation information receiving module configured to receive memory operation information of a to-be-detected program sent by a terminal device, the memory operation information of the to-be-detected program being intercepted and acquired when the to-be-detected program calls a memory monitoring dynamic library through a preloading mechanism, memory allocation functions and memory release functions in the memory monitoring dynamic library being rewritten; and an operation information storage module configured to analyze and store the memory operation information of the to-be-detected program in a hash chain table.
[0017] Optionally, in the embodiment of the present application, the operation information storage module comprises: a node hash value calculation module configured to analyze a stack backtracking address from the memory operation information and calculate a hash value according to the stack backtracking address; and an element node storage module configured to store or update the memory operation information in the hash chain table by taking the hash value as a node identifier.
[0018] Optionally, in the embodiment of the present application, the memory leak detection device further comprises: a viewing command obtaining module configured to obtain a memory viewing command request sent by a command terminal program; and a detection result obtaining module configured to send a detection result of memory leak to the command terminal program according to the memory viewing command request, so that the command terminal program outputs the detection result of memory leak.
[0019] The embodiment of the present application further provides an electronic device, comprising a processor and a memory, the memory storing machine readable instructions executable by the processor, the machine readable instructions being executed by the processor to perform the method as described above.
[0020] The embodiment of the present application further provides a computer readable storage medium, the computer readable storage medium storing a computer program, the computer program being executed by a processor to perform the method as described above. BRIEF DESCRIPTION OF DRAWINGS
[0021] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the following will briefly introduce the drawings needed to be used in the embodiments of the present application. It should be understood that the following drawings only show some of the embodiments of the present application, and therefore should not be regarded as a limitation on the scope, and for those skilled in the art, other related drawings can also be obtained without creative labor on the basis of these drawings.
[0022] Figure 1 The flowchart of the memory leakage monitoring method executed by the terminal device provided by the embodiments of the present application is shown;
[0023] Figure 2 The flowchart of the memory leakage detection method executed by the server provided by the embodiments of the present application is shown;
[0024] Figure 3 The network diagram of the interaction between the server and the terminal device provided by the embodiments of the present application is shown;
[0025] Figure 4 The flowchart of the server providing memory viewing service provided by the embodiments of the present application is shown;
[0026] Figure 5 The structural diagram of the memory leakage monitoring device provided by the embodiments of the present application is shown;
[0027] Figure 6 The structural diagram of the electronic device provided by the embodiments of the present application is shown. DETAILED DESCRIPTION
[0028] The technical solutions in the embodiments of the present application will be described clearly and completely below with reference to the drawings in the embodiments of the present application. Obviously, the described embodiments are only some of the embodiments of the present application, but not all the embodiments. The components of the embodiments of the present application described and shown in the drawings herein can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present application provided in the drawings is not intended to limit the scope of the claimed present application, but only represents selected embodiments of the present application. Based on the present application, all other embodiments obtained by those skilled in the art without creative labor are within the scope of the present application.
[0029] Before introducing the memory leakage monitoring and detection method provided by the embodiments of the present application, some concepts involved in the embodiments of the present application are introduced:
[0030] The memory operation information refers to information related to operations on a memory of the electronic device. The memory operation information includes memory allocation information and memory release information. The memory allocation information can include an allocated memory size, an allocated return address, a stack backtracking address, and process ID information. The memory release information can include a released memory address, a stack backtracking address, and process ID information.
[0031] The PreLoad mechanism refers to an LD_PRELOAD mechanism under a Linux operating system. The LD_PRELOAD is an environment variable, and the LD_PRELOAD is used for loading of a dynamic library. The environment variable is a directory address for loading of a dynamic library with the highest priority. Normally, when the memory operation functions are called in a program, the glibc library is searched. If the LD_PRELOAD mechanism of the Linux system is used, the memory allocation and release functions are searched in the self-implemented memory monitoring dynamic library. Taking malloc as an example, when the application program calls malloc, the memory monitoring dynamic library is searched for whether the malloc function exists. If the malloc function exists, the malloc function is used. Through the mechanism, specific functions can be added or joined in the memory allocation function and the memory release function of the memory monitoring dynamic library. The specific functions can be used to collect the memory allocation information and the memory release information of the program.
[0032] It should be noted that the memory leak monitoring method provided in the embodiments of the present application can be executed by a device terminal, such as a smart phone, a personal computer, a tablet computer, a personal digital assistant, or a mobile Internet device. The memory leak detection method provided in the embodiments of the present application can be executed by a server, such as an x86 server and a non-x86 server. The non-x86 server includes a mainframe, a minicomputer, and a UNIX server. Of course, in the specific implementation process, the above memory leak monitoring method can be executed by an electronic device to be monitored. The electronic device can also be the above server (i.e., self-monitoring), and in general, it can be another server.
[0033] The application scenarios to which the memory leak monitoring and detection method is applicable are described below. The application scenarios include, but are not limited to, the following: the memory leak monitoring method is used to monitor the memory of a target host to determine whether there is a memory leak on the target host; and the memory leak detection method is used to deploy a memory leak detection service program on a server to provide a memory leak detection service for users in the manner of the server. The user does not need to manually link to the library of the analysis tool or modify the code of the program to call the specified allocation and release interfaces, and the user can avoid causing a large impact on the execution of the program to be detected while implementing the memory leak monitoring and detection.
[0034] See Figure 1 The terminal device provided by the embodiment of the present application provides a flowchart of a memory leakage monitoring method executed by a terminal device. The memory leakage monitoring method can be applied to a terminal device. The main idea of the method is to intercept memory operation information of a program to be detected when the program to be detected calls a memory monitoring dynamic library through a preloading mechanism, and send the memory operation information of the program to be detected to a server, so as to avoid performing complex and time-consuming processing operations such as storage, query, retrieval, and symbol analysis on the terminal device, thereby greatly reducing the influence on the execution of the program to be detected while achieving memory leakage monitoring and detection. The implementation of the memory leakage monitoring method described above can include:
[0035] Step S110: The terminal device intercepts memory operation information of a program to be detected when the program to be detected calls a memory monitoring dynamic library through a preloading mechanism. Memory allocation functions and memory release functions in the memory monitoring dynamic library are rewritten.
[0036] The implementation of step S110 described above is, for example: The terminal device intercepts memory operation information of a program to be detected when the program to be detected calls a memory monitoring dynamic library through a preloading mechanism (i.e., a directory address pointed to by an LD_PRELOAD environment variable). Since the memory allocation functions and the memory release functions in the memory monitoring dynamic library are rewritten, the memory operation information of the program to be detected is intercepted and memory allocation operations or memory release operations are performed. Therefore, when the functions in the memory monitoring dynamic library in the directory address corresponding to the environment variable are called, the memory operation information of the program to be detected can be intercepted. In this way, the memory leakage of the program to be detected can be monitored and detected without modifying the application code (without referencing header files or linking dynamic libraries).
[0037] It can be understood that the memory monitoring dynamic library described above can be implemented by rewriting (or overloading) the memory allocation functions and the memory release functions in the glibc library. For example, generally, a program calls the malloc library in the glibc library. At this time, the malloc function rewritten (or overloaded) in the memory monitoring dynamic library can be called by using the LD_PRELOAD preloading mechanism, so that when the memory allocation functions and the memory release functions are called by all application programs, the malloc function rewritten (or overloaded) in the memory monitoring dynamic library is called.
[0038] After step S110, step S120 is performed: The terminal device sends the memory operation information of the program to be detected to the server.
[0039] Optionally, before sending the memory allocation information and the memory release information to the server through the shared queue, the shared queue can also be initialized, and the embodiment can comprise: judging whether the to-be-detected program meets a preset condition; if the to-be-detected program meets the preset condition, initializing the shared queue; wherein the preset condition is that the to-be-detected program is in a monitoring list (i.e. a whitelist) and calls a memory monitoring dynamic library for the first time. The monitoring list (also referred to as the whitelist) is a configuration file provided to a user to configure the processes to be detected, thereby realizing the function of memory monitoring and memory detection on the specified application program. That is, when a process running on the terminal device calls a memory allocation function for the first time, it is first judged whether the process is in the monitoring list (i.e. the whitelist). If the process is not in the whitelist, the memory operation information of the process is no longer monitored, i.e. all subsequent memory allocation information and memory release information of the process are no longer acquired. If the process is in the whitelist, it is indicated that the process needs to be monitored, and subsequent initialization operation is performed. Since the initialization operation is relatively complex, the initialization operation will be described in detail below.
[0040] The above initialization work can specifically comprise: creating a process ring queue (for example, a Posix ring queue) for transmitting the memory allocation and release information of the process, the Posix ring queue being used for transmitting the memory allocation information and the memory release information of the to-be-monitored process to a process running on the server, and sending the ring queue name to the server. Before sending the ring queue name to the server, the terminal device first needs to establish a communication connection with the server. The server newly creates a specific ring queue (for example, referred to as ring queue 0) after the communication connection is established. The ring queue 0 is used to receive the ring queue names sent by the processes running on the terminal devices. After receiving the ring queue names sent by the terminal devices through the ring queue 0, the server opens the ring queues of the terminal devices, then adds the ring queues to a ring queue pool, and finally acquires the memory operation information from the ring queue pool, in other words, the memory operation information is acquired from the opened ring queues. Thereafter, all memory allocation information and memory release information of the to-be-detected program are transmitted to the server through the ring queue pool. In short, the above specific ring queue (for example, referred to as ring queue 0) is only responsible for transmitting the ring queue names (i.e. the names of the ring queues) created by the terminal devices, so as to establish a communication connection between the server and the terminal devices, whereas the real data communication is transmitted through the ring queues created by the terminal devices and the server.
[0041] The implementation of the above step S120 is specifically, for example: the above-mentioned shared queue can adopt a ring queue, first, it is judged whether the to-be-detected program meets the preset condition; if the to-be-detected program meets the preset condition, the ring queue is initialized; after the ring queue is initialized, the memory allocation information and the memory release information are sent to the server through the shared queue. The preset condition is that the program is in the monitoring list and is called for the first time. The above-mentioned shared queue can adopt a ring queue (Ring Queue), including but not limited to the following implementation manners:
[0042] The first implementation manner is that the client (i.e., the monitoring and collecting program and the sending program of the memory operation information) runs on the terminal device, and the server (i.e., the receiving program and the analysis and detection service program of the memory operation information) runs on the server. Then, the ring queue can be arranged only on the server, and not on the terminal device. After the network card of the server receives the memory operation information, the memory operation information can be directly stored in the ring queue of the server. The server can also maintain a ring queue for each monitored process.
[0043] The second implementation manner is that the client runs on the terminal device, and the server runs on the server. Then, the ring queue can be arranged only on the terminal device, and not on the server. After the terminal device monitors the memory operation information, the memory operation information can be stored in the local ring queue. Then, the memory operation information is read from the ring queue by the network card of the terminal device, and the read memory operation information is sent to the server.
[0044] The third implementation manner combines the first implementation manner and the second implementation manner. The client runs on the terminal device, and the server runs on the server. Then, the ring queue is arranged on both the terminal device and the server. The specific implementation manner can refer to the first implementation manner and the second implementation manner, which will not be described here.
[0045] The fourth implementation manner is that the client and the server run on the same electronic device (for example, a terminal device or a server). Then, at least one ring queue can be arranged on the electronic device. After the client monitors the memory operation information, the memory operation information is stored in the ring queue. The server reads the memory operation information from the ring queue.
[0046] In the implementation process, the memory operation information of the to-be-detected program is intercepted when the to-be-detected program calls the memory monitoring dynamic library through the preloading mechanism, and the memory operation information of the to-be-detected program is sent to the server, thereby avoiding complex processing operations such as storage, query, retrieval, and symbol analysis on the terminal device, and thereby greatly reducing the influence on the execution of the to-be-detected program while achieving memory leak monitoring and detection. Since the memory monitoring dynamic library is used to monitor and intercept the memory operation information of the to-be-detected program, any time-consuming operation (such as storing, retrieving, and analyzing the processing message) is avoided, and the memory operation information of the to-be-detected program is directly sent to the server, so that the server performs leak detection according to the memory operation information, thereby greatly reducing the influence on the execution efficiency of the to-be-detected program. The terminal device can run the memory leak monitoring process for a long time, and the memory leak monitoring process is used to continuously monitor the long-running service process, for example, a daemon (nohup) process. Compared with the short-term memory leak detection, after obtaining the detection report, the memory leak monitoring and detection method in the embodiment of the application can continuously monitor the long-running service process (for example, a daemon process), thereby achieving the effect of providing continuous memory leak monitoring and detection.
[0047] Referring to Figure 2 The server executes the memory leak detection method provided by the embodiment of the application, and a flowchart of the memory leak detection method executed by the server is shown. The embodiment of the application also provides a memory leak detection method applied to a server, which can include the following steps:
[0048] In step S210, the server receives the memory operation information of the to-be-detected program sent by the terminal device.
[0049] For example, the implementation of step S210 is as follows: Referring to Figure 3 The server and the terminal device interact with each other, and a network diagram of the interaction is shown. The server receives the memory operation information of the to-be-detected program sent by the terminal device through the ring queue, for example, the server can receive the queue name and the memory operation information sent by each process program (including the to-be-detected program) through the ring queue cycle, and open and add the queue name and the memory operation information to the queue pool, and then receive the memory operation message sent by each process program (including the to-be-detected program) by traversing the ring queue in the queue pool, thereby improving the communication efficiency between the server and the terminal device. The memory operation information of the to-be-detected program is intercepted when the to-be-detected program calls the memory monitoring dynamic library through the preloading mechanism, and the memory allocation function and the memory release function in the memory monitoring dynamic library are rewritten.
[0050] After step S210, step S220 is performed: the server parses the memory operation information of the program to be detected and stores it in the hash linked list.
[0051] The implementation of step S220 can include: the server processes the received memory operation information and stores it in the hash linked list, specifically for example: parsing the stack backtracking address from the memory operation information, and calculating the hash value according to the stack backtracking address. If the node identifier of the hash value is not found in the hash linked list, a new element node can be created and the memory operation information is parsed and stored in the new element node. The specific process of creating the new element node is for example: constructing the element node with the hash value as the node identifier and the memory operation information as the node content, parsing and storing the memory operation information in the newly created element node, and then associating the element node with the hash linked list. If the node identifier of the hash value is found in the hash linked list, the element node content of the hash value as the node identifier can be updated. The specific process of updating the element node is for example: since the program to be detected may call the memory allocation function repeatedly in the same place (i.e. the same line in the source code) and not release, the memory allocation information of these repeated calls should be stored in the same element node. This element node will be created when the memory allocation is called for the first time, and when the memory allocation is called again, only the content of the element node needs to be updated. Each element node in the hash linked list includes: total memory allocation size, memory release size, memory allocation times, memory release times, stack backtracking address and stack backtracking symbol information, etc.
[0052] It can be understood that, in the specific practice, the server retains a memory space for each process to be detected, and the memory space is used to store memory allocation and memory release information of each process to be detected, which includes total memory allocation, release times, total memory allocation and release size, and the like. Meanwhile, the server also maintains a hash linked list for each monitored process, and only memory allocation operations create element nodes in the hash linked list, and the element nodes store memory allocation information or memory release information. If the server receives memory release information sent by the terminal device, the server can find the element node corresponding to the memory allocation, and modify the total memory release size and release times of the element node. The server takes each memory allocation call point as a single node in the memory hash linked list, and takes the nodes with the same call stack backtracking address as the same node. The content of each node includes memory allocation, release times, memory allocation, release size, stack backtracking address and stack backtracking symbol, and the like. The stack backtracking symbol information herein refers to symbol information parsed by the server according to the stack backtracking address, and the address can be uniformly parsed by using the gdb tool and the addr2line tool. The parsed symbol (which can be a file name + function name + line number, or a dynamic library name) is saved in the node. The purpose of this is that symbol parsing is time-consuming, and placing it in the server can greatly reduce the influence on the execution efficiency of the monitored program.
[0053] After step S220, step S230 is performed: the server obtains memory operation information from the hash linked list, and performs memory leak detection analysis on the memory operation information to obtain a detection result.
[0054] The implementation of step S230 is, for example: the server obtains memory operation information from the hash linked list, wherein the memory allocation information can include: allocation memory size, allocation return address, stack backtracking address, process ID information; and the memory release information can include: release memory address, stack backtracking address, process ID information. Then, the memory operation information is subjected to memory leak detection analysis to obtain a detection result, which is, for example: parsing detailed stack backtracking function call information of a leak point, accurately pointing out function call processes of the leak point (from which interface to start, which interfaces are called in the middle, and which interface is finally called to cause the leak), and allocation, release size and times of the leak point, and the like.
[0055] Please refer to Figure 4 The server provided by the embodiment of the application provides a flowchart of a process of providing a memory viewing service; optionally, the server can also provide a memory viewing service for the terminal device or other terminal devices, and the memory viewing service can specifically include:
[0056] Step S310: The server acquires the memory viewing command request sent by the command terminal program.
[0057] For example, in the embodiment of step S310, the user sends the memory viewing command to the server through the command terminal program on the device, and the server receives the memory viewing command sent by the command terminal program. The memory viewing command can include a specified parameter, for example, a process ID, which can be used by the user to view the memory leakage of a specified process. The command terminal program herein can be understood as a related program of the device through a graphical user interface (GUI) or a command-line interface (CLI), that is, the user sends the memory viewing command to the server through the GUI or the CLI on the device, and then the server receives the memory viewing command sent by the device of the user.
[0058] After step S310, step S320 is performed: after receiving the memory viewing command request sent by the command terminal program, the server generates a detection result of memory leakage according to the memory viewing command request, and sends the detection result of memory leakage to the command terminal program.
[0059] The implementation of step S320 can be, for example, that after receiving the memory viewing command request sent by the terminal program, the server generates the detection result of the memory leak according to the memory viewing command request. The memory viewing command request can include query parameters. After receiving the memory viewing command request sent by the terminal program, the server can parse the query parameters from the memory viewing command request, and perform snapshot parsing filtering processing and text formatting processing on the data of the specified process to be queried in the memory according to the query parameters. Specifically, the snapshot parsing filtering processing can include four stages. In the first stage, the memory allocation and release information of the specified process is snapped. Since the server continuously records the memory allocation and release data of the monitored process, snapping the memory allocation and release information of the specified process can effectively ensure that the data at the time of querying will not change. In the second stage, nodes that do not leak are filtered. The method for determining whether a node leaks is that if the allocation size of a node is greater than the release size, it means that the node leaks, otherwise it does not leak. In the third stage, the gdb and addr2line tools are used to uniformly analyze the stack backtracking addresses of each node, and the symbol information of each address is parsed and stored in the memory node. In the fourth stage, some functions that the user wants to actively shield are filtered according to the false detection configuration file. The false detection configuration file functions as follows: the user can configure this file to not detect the specified functions of the detected program and the memory allocation and release in the specified file code segment. For example, the thread creation function pthread_create will apply for a part of space, but this is unnecessary leakage, so this function can be configured in the configuration file, so that the memory leak information caused by this function is filtered when processing. The text formatting processing includes text formatting of the memory allocation and release, the number of times, the size, etc. of the entire process, and then text formatting of the node data in each hash linked list, to form a readable memory leak report and write it into the memory detection leak report, so as to obtain the detection result.
[0060] It can be understood that the detection result has many forms of embodiment, which can be a memory detection leak report in the form of a webpage or a document. The memory detection leak report can include the total number of memory applications, the total number of memory releases, the total memory allocation size, the total memory release size, and the size of the un-released memory of the entire process. At the same time, the memory detection leak report can also include the memory application number, the memory release number, the memory allocation size, the memory release size, the size of the un-released memory, and the stack address backtracking symbol of the leak point (i.e. the node that leaks).
[0061] After step S320, step S330 is performed: the server sends the detection result to the terminal program.
[0062] The implementation of the step S330 is, for example, that the server sends the detection result of the memory leak to the command terminal program. If the command terminal program receives the detection result sent by the server through the CLI, the command terminal program can further display the detection result on the command line console after receiving the detection result sent by the server. If the command terminal program receives the detection result sent by the server through the GUI, the command terminal program can further display the detection result on the graphical interface corresponding to the GUI. In this way, the memory leak information of all monitored processes or the memory leak information of a specified process can be viewed.
[0063] Please refer to Figure 5 The memory leak monitoring device provided by the embodiment of the application is shown in the structural schematic diagram; the embodiment of the application provides a memory leak monitoring device 400 applied to a terminal device, comprising:
[0064] The memory operation interception module 410 is configured to intercept the memory operation information of the to-be-detected program when the to-be-detected program calls the memory monitoring dynamic library through the preloading mechanism, wherein the memory allocation function and the memory release function in the memory monitoring dynamic library are rewritten.
[0065] The operation information sending module 420 is configured to send the memory operation information of the to-be-detected program to the server.
[0066] Optionally, in the embodiment of the application, the memory operation information comprises memory allocation information and memory release information; and the operation information sending module comprises:
[0067] The memory information sending module is configured to send the memory allocation information and the memory release information to the server through the shared queue.
[0068] Optionally, in the embodiment of the application, the memory leak monitoring device further comprises:
[0069] The detection program judging module is configured to judge whether the to-be-detected program meets a preset condition, wherein the preset condition is that the to-be-detected program is in the monitoring list and calls the memory monitoring dynamic library for the first time.
[0070] The ring queue initialization module is configured to initialize the shared queue if the to-be-detected program meets the preset condition.
[0071] Optionally, in the embodiment of the application, the memory allocation information comprises an allocated memory size, an allocated return address, a stack backtracking address and process ID information; and the memory release information comprises a released memory address, a stack backtracking address and process ID information.
[0072] The embodiment of the application provides a memory leak detection device applied to a server, comprising:
[0073] An operation information receiving module is configured to receive memory operation information of a to-be-detected program sent by a terminal device, the memory operation information of the to-be-detected program being intercepted and acquired when the to-be-detected program calls a memory monitoring dynamic library through a preloading mechanism, and memory allocation functions and memory release functions in the memory monitoring dynamic library being rewritten.
[0074] An operation information storing module is configured to parse the memory operation information of the to-be-detected program and store the memory operation information in a hash linked list.
[0075] Optionally, in the embodiment of the present application, the operation information storing module comprises:
[0076] A node hash value calculating module is configured to parse a stack backtracking address from the memory operation information and calculate a hash value according to the stack backtracking address.
[0077] An element node storing module is configured to store or update the memory operation information in the hash linked list by taking the hash value as a node identifier.
[0078] Optionally, in the embodiment of the present application, the memory leak detection device further comprises:
[0079] A viewing command obtaining module is configured to obtain a memory viewing command request sent by a command terminal program.
[0080] A detection result obtaining module is configured to send a detection result of memory leak to the command terminal program according to the memory viewing command request, so that the command terminal program outputs the detection result of memory leak.
[0081] It should be understood that the device corresponds to the memory leak monitoring and detection method embodiments described above, and can perform each step involved in the above method embodiments. The specific functions of the device can be referred to the description above. To avoid repetition, the detailed description is appropriately omitted here. The device comprises at least one software function module stored in the memory in the form of software or firmware or solidified in the operating system (OS) of the device.
[0082] Please refer to Figure 6 An electronic device 500 provided by an embodiment of the present application is shown in a structural schematic diagram. The electronic device 500 provided by an embodiment of the present application comprises a processor 510 and a memory 520. The memory 520 stores machine readable instructions executable by the processor 510. The machine readable instructions are executed by the processor 510 to perform the method described above.
[0083] An embodiment of the present application further provides a computer readable storage medium 530, which stores a computer program. The computer program is run by the processor 510 to perform the method described above.
[0084] The computer readable storage medium 530 can be implemented by any type of volatile or nonvolatile storage devices or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read only memory (EPROM), programmable read only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic or optical disk.
[0085] It should be understood that the disclosed apparatus and method can also be implemented in other manners. The embodiments described above are merely exemplary embodiments of the present application. In the embodiments of the present application, the apparatus embodiment described above is merely schematic. For example, the flow chart and block diagram in the drawings show the possible implementation architectures, functions and operations of the apparatus, method and computer program product according to the embodiments of the present application. In this regard, each block in the flow chart or block diagram can represent a module, a program segment or a part of code, which contains one or more executable instructions for implementing the specified logical function. It should also be noted that, in some alternative implementations, the functions noted in the blocks can be performed in a different order from that shown in the drawings. For example, two consecutive blocks can actually be executed substantially in parallel, and sometimes in reverse order, depending on the functions involved.
[0086] In addition, each functional module in the various embodiments of the present application can be integrated together to form a separate part, or each module can exist independently, or two or more modules can be integrated to form a separate part.
[0087] In this document, the terms such as first and second are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply that there is any such actual relationship or order between these entities or operations.
[0088] The above description is only optional implementation of the embodiments of the present application, but the protection scope of the embodiments of the present application is not limited thereto, any person skilled in the art can easily think of changes or replacements within the technical range disclosed by the embodiments of the present application, which should be covered in the protection scope of the embodiments of the present application.
Claims
1. A memory leak monitoring method, characterized by, The application is applied to a terminal device and comprises: When a to-be-detected program calls a memory monitoring dynamic library function through a preloading mechanism, memory operation information of the to-be-detected program is intercepted, memory allocation functions and memory release functions in the memory monitoring dynamic library are rewritten, the preloading mechanism is a mechanism of an LD_PRELOAD environment variable for dynamic library loading under a Linux operating system, and the memory monitoring dynamic library function comprises a malloc function rewritten in a directory address pointed by the LD_PRELOAD environment variable, and the malloc function is used to collect memory operation information of a program; The memory operation information of the to-be-detected program is sent to a server, the memory operation information is used to generate a memory leakage detection result according to a memory viewing command request sent by a terminal program, the memory viewing command request is parsed to obtain query parameters, and data of a specified query process in memory of the terminal device is subjected to snapshot analysis and filtering processing according to the query parameters; the memory operation information comprises memory allocation information and memory release information; the snapshot analysis and filtering processing comprises snapshotting memory allocation information and memory release information of a specified process, and / or using gdb and addr2line tools to uniformly analyze stack backtracking addresses of each node in the memory allocation information.
2. The method of claim 1, wherein, The memory operation information of the to-be-detected program is sent to a server, and the memory operation information comprises: The memory allocation information and the memory release information are sent to the server through a shared queue.
3. The method of claim 2, wherein, Before the memory allocation information and the memory release information are sent to the server through the shared queue, the method further comprises: It is judged whether the to-be-detected program meets a preset condition, the preset condition is that the to-be-detected program is in a monitoring list and calls the memory monitoring dynamic library for the first time; If yes, the shared queue is initialized.
4. The method according to claim 2 or 3, characterized in that, The memory allocation information comprises allocation memory size, allocation return addresses, stack backtracking addresses and process ID information; and the memory release information comprises release memory addresses, stack backtracking addresses and process ID information.
5. A memory leak detection method characterized by, The application is applied to a server and comprises: Memory operation information of a to-be-detected program sent by a terminal device is received, the memory operation information of the to-be-detected program is intercepted when the to-be-detected program calls a memory monitoring dynamic library function through a preloading mechanism, memory allocation functions and memory release functions in the memory monitoring dynamic library are rewritten, the preloading mechanism is a mechanism of an LD_PRELOAD environment variable for dynamic library loading under a Linux operating system, and the memory monitoring dynamic library function comprises a malloc function rewritten in a directory address pointed by the LD_PRELOAD environment variable, and the malloc function is used to collect memory operation information of a program; The memory operation information of the program to be detected is parsed and stored in a hash linked list, and the memory operation information is used to generate a memory leakage detection result according to a memory viewing command request sent by a server according to a command terminal program, comprising: parsing a query parameter from the memory viewing command request, and performing snapshot parsing filtering processing on data of a specified query process in the memory of the terminal device according to the query parameter; the memory operation information comprises memory allocation information and memory release information; the snapshot parsing filtering processing comprises: snapshotting memory allocation information and memory release information of a specified process, and / or uniformly parsing stack backtracking addresses of each node in the memory allocation information using gdb and addr2line tools.
6. The method of claim 5, wherein, The memory operation information of the program to be detected is parsed and stored in a hash linked list, comprising: The stack backtracking addresses are parsed from the memory operation information, and a hash value is calculated according to the stack backtracking addresses; The memory operation information is stored or updated in the hash linked list with the hash value as a node identifier.
7. The method of claim 5, wherein, Further comprising: Obtaining a memory viewing command request sent by a command terminal program; Sending the memory leakage detection result to the command terminal program according to the memory viewing command request, so that the command terminal program outputs the memory leakage detection result.
8. A memory leak monitoring apparatus, characterized by, Applied to a terminal device, comprising: A memory operation interception module, configured to intercept memory operation information of a program to be detected when the program to be detected calls a memory monitoring dynamic library function through a preloading mechanism, memory allocation functions and memory release functions in the memory monitoring dynamic library are rewritten, the preloading mechanism is a mechanism of an LD_PRELOAD environment variable for dynamic library loading under a Linux operating system, and the memory monitoring dynamic library function comprises a malloc function rewritten in a directory address pointed by the LD_PRELOAD environment variable, and the malloc function is used to collect memory operation information of a program; An operation information sending module, configured to send the memory operation information of the program to be detected to a server, and the memory operation information is used to generate a memory leakage detection result according to a memory viewing command request sent by a server according to a command terminal program, comprising: parsing a query parameter from the memory viewing command request, and performing snapshot parsing filtering processing on data of a specified query process in the memory of the terminal device according to the query parameter; the memory operation information comprises memory allocation information and memory release information; the snapshot parsing filtering processing comprises: snapshotting memory allocation information and memory release information of a specified process, and / or uniformly parsing stack backtracking addresses of each node in the memory allocation information using gdb and addr2line tools.
9. An electronic device, comprising: Comprising: A processor and a memory, the memory stores machine readable instructions executable by the processor, and the machine readable instructions are executed by the processor to perform the method in any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that, The computer readable storage medium stores a computer program, and the computer program is run by the processor to perform the method in any one of claims 1 to 7.
Citation Information
Patent Citations
Memory leak detecting method and device
CN106610892A