CN114546795A - Memory leak detection method, electronic device, and computer-readable storage medium - Google Patents
Memory leak detection method, electronic device, and computer-readable storage medium Download PDFInfo
- Publication number
- CN114546795A CN114546795A CN202111614917.7A CN202111614917A CN114546795A CN 114546795 A CN114546795 A CN 114546795A CN 202111614917 A CN202111614917 A CN 202111614917A CN 114546795 A CN114546795 A CN 114546795A
- Authority
- CN
- China
- Prior art keywords
- memory
- application module
- library
- detection
- call stack
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F11/00—Error detection; Error correction; Monitoring
- G06F11/30—Monitoring
- G06F11/34—Recording or statistical evaluation of computer activity, e.g. of down time, of input/output operation ; Recording or statistical evaluation of user activity, e.g. usability assessment
- G06F11/3466—Performance evaluation by tracing or monitoring
- G06F11/3476—Data logging
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F11/00—Error detection; Error correction; Monitoring
- G06F11/30—Monitoring
- G06F11/34—Recording or statistical evaluation of computer activity, e.g. of down time, of input/output operation ; Recording or statistical evaluation of user activity, e.g. usability assessment
- G06F11/3466—Performance evaluation by tracing or monitoring
- G06F11/3471—Address tracing
Landscapes
- Engineering & Computer Science (AREA)
- General Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Computer Hardware Design (AREA)
- Quality & Reliability (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- Debugging And Monitoring (AREA)
Abstract
The application discloses a memory leak detection method, electronic equipment and a computer readable storage medium, wherein the method comprises the following steps: the detection library acquires a corresponding memory from the memory management library based on the memory application instruction, returns a memory address corresponding to the memory to the application module, and records a mapping relation between the memory address and a call stack corresponding to the application module to acquire recorded data; acquiring a memory address specified by the application module based on the memory release instruction, determining a memory released by the application module based on the memory address, and deleting a mapping relation between the memory address corresponding to the released memory and a call stack corresponding to the application module from the current recorded data; and in response to the preset period, generating a current recording file based on the recording data at the current moment so that the statistical unit obtains the current recording file and determines the position of memory leakage based on the current recording file and a historical recording file before the current recording file. By the scheme, the adaptability and the efficiency of detecting the memory leakage can be improved.
Description
Technical Field
The present application relates to the field of computer technologies, and in particular, to a memory leak detection method, an electronic device, and a computer-readable storage medium.
Background
The memory leak refers to that the memory applied in the process of calling the memory operation by the application module has no way to be released because the memory address is lost, or the memory which should be released is always held by the application module and the application module continuously applies for the memory, so that the system memory approaches to be full. The existing memory leak detection method usually needs to search for the location of the memory leak by repeatedly debugging with a compiling tool, and the method has low efficiency and poor compatibility of different compiling tools to various types of programming languages. In view of this, how to improve the adaptability and efficiency of detecting memory leakage becomes an urgent problem to be solved.
Disclosure of Invention
The technical problem mainly solved by the application is to provide a memory leak detection method, an electronic device and a computer-readable storage medium, which can improve the adaptability and efficiency of detecting memory leak.
In order to solve the foregoing technical problem, a first aspect of the present application provides a memory leak detection method, including: in response to receiving a memory application instruction of an application module, a detection library acquires a corresponding memory from a memory management library based on the memory application instruction, returns a memory address corresponding to the memory to the application module, and records a mapping relation between the memory address and a call stack corresponding to the application module to acquire recorded data; in response to receiving a memory release instruction of an application module, the detection library acquires a memory address specified by the application module based on the memory release instruction, determines a memory released by the application module based on the memory address, and deletes a mapping relation between the memory address corresponding to the released memory and a call stack corresponding to the application module from current recorded data; and responding to the fact that a preset period passes, the detection library generates a current record file based on the record data at the current moment, so that a statistical unit obtains the current record file and determines the position of memory leakage based on the current record file and a historical record file before the current record file.
In order to solve the above technical problem, a second aspect of the present application provides a memory leak detection method, including: responding to a memory application instruction sent by an application module to a detection library, enabling the detection library to obtain a corresponding memory from a memory management library based on the memory application instruction, returning a memory address corresponding to the memory to the application module, and recording a mapping relation between the memory address and a call stack corresponding to the application module to obtain recorded data; responding to a memory release instruction sent by the application module to a detection library, so that the detection library obtains a memory address specified by the application module based on the memory release instruction, determines a memory released by the application module based on the memory address, and deletes a mapping relation between the memory address corresponding to the released memory and a call stack corresponding to the application module from current recorded data; and in response to the preset period, the application module sends a file acquisition instruction to the detection library so that the detection library generates a current record file based on the current record data, and then the statistical unit acquires the current record file and determines the position of memory leakage based on the current record file and a historical record file before the current record file.
In order to solve the foregoing technical problem, a third aspect of the present application provides a memory leak detection method, including: acquiring a current recording file sent by an application module, and extracting a historical recording file before the current recording file; wherein the current recording file is obtained based on the method of the first aspect or the second aspect; when the current recording file is larger than the last adjacent historical recording file, extracting a call stack set formed by call stacks in at least one recording file, and determining the occurrence frequency of the call stacks in the call stack set in all the recording files; and arranging all the record files according to a time sequence, acquiring a call stack with the increased frequency appearing in the record files arranged according to the time sequence as a target call stack, and analyzing the memory leakage position corresponding to the application module from the target call stack.
In order to solve the above technical problem, a fourth aspect of the present application provides an electronic device, including: a memory and a processor coupled to each other, wherein the memory stores program data, and the processor calls the program data to execute the method of the first aspect, the second aspect, or the third aspect.
In order to solve the above technical problem, a fifth aspect of the present application provides a computer-readable storage medium having stored thereon program data, which when executed by a processor, implements the method of the first aspect or the second aspect or the third aspect.
According to the scheme, if the detection library receives a memory application instruction of the application module, the detection library obtains a corresponding memory from the memory management library and returns a memory address corresponding to the memory to the application module, and records a mapping relation between the memory address and a call stack corresponding to the application module to obtain recorded data, if the detection library receives a memory release instruction of the application module, the detection library obtains the memory released by the application module, and deletes the mapping relation between the memory address corresponding to the released memory and the call stack corresponding to the application module from the current recorded data, so that the mapping relation between the memory address corresponding to the memory and the call stack which are not released by the application module is kept in the recorded data at the current moment and are always recorded, when a preset period passes, the detection library generates a current recorded file based on the recorded data at the current moment, and then the statistical unit can obtain the current recorded file and then can be based on the current recorded file and the history before the current recorded file And recording the change of the file and finding out the position of memory leakage. Therefore, no matter which type of programming language the application module runs based on, after the mapping relation between the memory address and the call stack corresponding to the application module is recorded and the recording file is output at regular time, the position of memory leakage can be efficiently analyzed by the statistical unit, and the adaptability and the efficiency of detecting the memory leakage are improved.
Drawings
In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed to be used in the description of the embodiments are briefly introduced below, and it is obvious that the drawings in the following description are only some embodiments of the present application, and it is obvious for those skilled in the art to obtain other drawings based on these drawings without creative efforts. Wherein:
FIG. 1 is a schematic flow chart diagram illustrating an embodiment of a memory leak detection method according to the present application;
FIG. 2 is a schematic topology diagram of an embodiment of a memory leak detection method according to the present application;
FIG. 3 is a schematic flowchart illustrating a memory leak detection method according to another embodiment of the present disclosure;
FIG. 4 is a schematic flow chart diagram illustrating a memory leak detection method according to another embodiment of the present disclosure;
FIG. 5 is a schematic flow chart diagram illustrating a memory leak detection method according to another embodiment of the present disclosure;
FIG. 6 is a schematic structural diagram of an embodiment of an electronic device of the present application;
FIG. 7 is a schematic structural diagram of an embodiment of a computer storage medium according to the present application.
Detailed Description
The technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application, and it is obvious that the described embodiments are only a part of the embodiments of the present application, and not all the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
The terms "system" and "network" are often used interchangeably herein. The term "and/or" herein is merely an association describing an associated object, meaning that three relationships may exist, e.g., a and/or B, may mean: a exists alone, A and B exist simultaneously, and B exists alone. In addition, the character "/" herein generally indicates that the former and latter related objects are in an "or" relationship. Further, the term "plurality" herein means two or more than two.
Referring to fig. 1, fig. 1 is a schematic flowchart illustrating a memory leak detection method according to an embodiment of the present application, where the method uses a detection library disposed between an application module and a memory management library as an execution end, and includes:
s101: and in response to the received memory application instruction of the application module, the detection library acquires a corresponding memory from the memory management library based on the memory application instruction, returns a memory address corresponding to the memory to the application module, and records the mapping relation between the memory address and a call stack corresponding to the application module to acquire recorded data.
Specifically, a memory application instruction of the application module is received, the detection library obtains a corresponding memory after applying from the memory management library based on the memory application instruction and returns a memory address corresponding to the memory to the application module, and a mapping relation between the memory address corresponding to the memory and a call stack corresponding to the application module is obtained and recorded to obtain recorded data.
Optionally, before obtaining the mapping relationship between the memory address corresponding to the memory and the call stack corresponding to the application module, the method further includes: and judging that the size of the memory applied by the application module exceeds a preset threshold, if so, acquiring the mapping relation between the memory address corresponding to the memory and the call stack corresponding to the application module and recording the mapping relation to acquire recorded data, otherwise, not recording the mapping relation, thereby filtering smaller memory occupation and reducing storage pressure and memory called during storage.
Further, the detection library corresponds to a detection state, the detection state defaults to an unopened state when the application module starts to run, the detection state is opened after the application module is initialized and an interactive relation is established between the application module and the detection module, the detection library records the detection state, and then the detection library determines whether to record a mapping relation between a memory address corresponding to the memory and a call stack corresponding to the application module based on the detection state. The call stack corresponds to the application module, and the call stack corresponding to the application module when the application module runs among threads is kept unchanged. When the detection state is turned on and a memory application instruction of the application module is received, step S101 is entered.
In one application, referring to fig. 2, fig. 2 is a schematic topology diagram of an embodiment of a memory leak detection method according to the present application, wherein, the detection library comprises a replacement module and a recording module, when the detection state is started, the application module sends a memory application instruction to the replacement module, the replacement module receives the memory application instruction and then applies for the memory for the application module from the memory management library through a custom function, and the memory address corresponding to the memory applied from the memory management library is fed back to the application module, the process of the application module applying for the memory corresponds to the process of returning to the application module for applying for the memory in fig. 2, when the size of the memory applied by the application module exceeds the preset threshold value, the replacement module applies for the memory from the memory management library so that the recording module stores the mapping relationship with the memory applied by the replacement module, the process of the replacement module applying for the memory corresponds to the process of returning to the replacement module applying for the memory in fig. 2.
In a specific application scenario, when the minimum record memory applied by the recording module during library compilation is detected to be 1024 bytes, the preset threshold is set to be 1024 bytes, so that the memory applied by the application module is collected only in the recorded data to be a call stack with more than 1024 bytes, thereby reducing the recording of a smaller memory application process and reducing the influence on the program performance. It is understood that the preset threshold may be set to other values in other application scenarios, which is not specifically limited in this application.
S102: in response to receiving a memory release instruction of the application module, the detection library acquires a memory address specified by the application module based on the memory release instruction, determines a memory released by the application module based on the memory address, and deletes a mapping relation between the memory address corresponding to the released memory and a call stack corresponding to the application module from current recorded data.
Specifically, a memory release instruction of the application module is received, the detection library determines a memory address specified by the application model based on the memory release instruction, releases a memory corresponding to the memory address from the memory management library, deletes a mapping relationship between the memory address corresponding to the released memory and a call stack corresponding to the application module from current recorded data if the memory address corresponding to the released memory is recorded in the recorded data, and does not process the recorded data if the memory address corresponding to the released memory is not recorded in the recorded data, indicating that the size of the memory corresponding to the released memory is smaller than a preset threshold value, thereby keeping the memory address corresponding to the memory which is not released and is larger than the preset threshold value recorded in the recorded data at the current time.
In an application mode, please refer to fig. 2 again, the detection library includes a replacement module and a recording module, when the detection state is started, the application module sends a memory release instruction to the replacement module, the replacement module releases the corresponding memory from the memory management library through a custom function after receiving the memory application instruction, the replacement module obtains a memory address corresponding to the released memory, and when the memory address corresponding to the released memory is recorded in the record data, the replacement module sends a deletion instruction to the recording module, so that the mapping relationship between the memory address corresponding to the released memory and the call stack is deleted from the record data.
S103: and in response to the preset period, the detection library generates a current record file based on the record data at the current moment so that the statistical unit obtains the current record file and determines the position of memory leakage based on the current record file and a historical record file before the current record file.
Specifically, the application module sends a file acquisition instruction to the detection library after running for a preset period, the detection library packages the record data at the current moment based on the file acquisition instruction to generate a current record file, and then the statistical unit analyzes the position of memory leakage by using the current record file and a history file received before the current record file after obtaining the current record file. After the statistical unit obtains the log files corresponding to a plurality of consecutive preset periods in the time sequence, the statistical unit may analyze the location of the memory leak based on the size of the log file and the number of times of occurrence of the call stack in the log file, and refer to the subsequent embodiments for the content of data analysis performed by the statistical unit.
In an application mode, please refer to fig. 2 again, the detection library includes a replacement module and a recording module, when a preset period elapses, the application module sends a file acquisition instruction to the replacement module, the replacement module sends an instruction for collecting a call stack at the current time to the recording module, the recording module generates recording data at the current time into a current recording file and stores the current recording file in the file system, and then the statistical unit acquires the current recording file from an assigned position in the file system at regular time.
According to the scheme, if the detection library receives a memory application instruction of the application module, the detection library obtains a corresponding memory from the memory management library and returns a memory address corresponding to the memory to the application module, and records a mapping relation between the memory address and a call stack corresponding to the application module to obtain recorded data, if the detection library receives a memory release instruction of the application module, the detection library obtains the memory released by the application module, and deletes the mapping relation between the memory address corresponding to the released memory and the call stack corresponding to the application module from the current recorded data, so that the mapping relation between the memory address corresponding to the memory and the call stack which are not released by the application module is kept in the recorded data at the current moment and are always recorded, when a preset period passes, the detection library generates a current recorded file based on the recorded data at the current moment, and then the statistical unit can obtain the current recorded file and then can be based on the current recorded file and the history before the current recorded file And recording the change of the file and finding out the position of memory leakage. Therefore, no matter which type of programming language the application module runs based on, after the mapping relation between the memory address and the call stack corresponding to the application module is recorded and the recording file is output at regular time, the position of memory leakage can be efficiently analyzed by the statistical unit, and the adaptability and the efficiency of detecting the memory leakage are improved.
Referring to fig. 3, fig. 3 is a schematic flowchart illustrating a process of another embodiment of the memory leak detection method according to the present application, in which a detection library disposed between an application module and a memory management library is used as an execution end, including:
s301: and in response to the received memory application instruction of the application module, setting the detection library to be a memory management library which needs to be directly called, applying for a corresponding memory from the memory management library based on the memory application instruction, and returning a memory address corresponding to the applied memory to the application module.
Specifically, the detection library corresponds to a detection state, and when the application module is initially executed between threads, the application module sends a load instruction to the detection library, where the detection state is not started.
Further, before the steps of setting the detection library to be required to directly call the memory management library, applying for the corresponding memory from the memory management library based on the memory application instruction, and returning the memory address corresponding to the applied memory to the application module, the method further comprises the following steps: the detection library replaces the management interface of the memory management library with a custom interface, so that the detection library applies for the memory from the memory management library or releases the memory by using the custom interface.
Further, referring to fig. 2 again, the application module loads the detection library to call the replacement module, the replacement module replaces the memory allocation and release function of the memory management library with the custom function, so that when the application module applies for the memory, the replacement module is called to call the interface defined by the custom function, the detection library applies for the memory or releases the memory from the memory management library by using the custom interface, the detection library is used as a transfer station between the application module and the memory detection library, and the detection library is convenient to record the mapping relationship between the call stack corresponding to the application module and the memory. The memory management library includes, but is not limited to, ptiloc 2 for glibc, tcmalloc for google.
Further, the detection state is not opened by default at an initial stage, when the application module sends an instruction for applying for the memory, the replacement module first determines whether the application module needs to directly call the memory management library, the call initial value corresponding to the application module is not needed, if not, the detection state is opened, the step S301 is entered, if yes, the memory management library is directly called, and a memory application result is returned.
Specifically, when the detection state is opened and a memory application instruction of the application module is received, the detection library sets the corresponding thread local variable to be required to be directly called to the memory management library so as to apply for storing the memory from the memory management library when data is required to be recorded, and after the memory application instruction is obtained, the detection library obtains the corresponding memory from the memory management library through a custom function and returns the memory address corresponding to the memory to the application module.
S302: and judging whether the size of the memory applied by the application module exceeds a preset threshold value.
Specifically, the detection library determines whether the size of the memory applied by the application module exceeds a preset threshold, if so, step S303 is performed, otherwise, step S304 is performed.
S303: and applying for a memory from a memory management library in response to the size of the memory applied by the application module exceeding a preset threshold value, so as to record a mapping relation between a memory address corresponding to the memory applied by the application module and a call stack corresponding to the application module in the memory to obtain recorded data.
Specifically, when the size of the memory applied by the application module exceeds a preset threshold, the detection library applies for a storage memory from the memory management library, and then a mapping relationship between a memory address corresponding to the memory applied by the application module and a call stack corresponding to the application module is recorded in the storage memory, so as to obtain recorded data.
In an application scenario, please refer to fig. 2 again, the detection library includes a replacement module and a recording module, when the detection state is opened and a memory application instruction of the application module is received, the replacement module sets a thread local variable corresponding to the detection library as a memory management library to be directly called, the replacement module obtains a corresponding memory from the memory management library through a custom function and returns the memory to the application module, meanwhile, the replacement module determines whether the size of the memory applied by the application module exceeds a preset threshold, when the size of the memory applied by the application module exceeds the preset threshold, the replacement module applies for a storage memory from the memory management library, and the recording module stores a mapping relationship between a memory address corresponding to the memory applied by the application module and a call stack corresponding to the application module in the storage memory.
S304: the detection library is set to be a memory management library which does not need to be directly called.
Specifically, after the application module obtains the corresponding memory, the detection library is set to be the memory management library which does not need to be directly called, so that the memory management library is directly applied when the memory is required to be applied for storing when the mapping relation is recorded by setting the mode whether the detection library needs to be directly called or not, and no matter whether the mapping relation is recorded or not, the dead cycle does not occur.
S305: and when a memory release instruction of the application module is received, setting the detection library as a memory management library which needs to be directly called, determining a memory address specified by the application module based on the memory release instruction, and releasing a memory corresponding to the memory address specified by the application module in the memory management library.
Specifically, when a memory release instruction of the application module is received, the detection library sets a corresponding thread local variable as a memory management library to be called directly so as to delete data to obtain a storage memory from the memory management library, determines a memory address, which is specified by the application module and needs to be released, through a custom function after the memory release instruction is obtained, and releases a memory corresponding to the memory address from the memory management library.
S306: and judging whether the released memory is recorded in the storage memory.
Specifically, the detection library determines whether the memory released by the application module is recorded in the storage memory, if so, step S307 is performed, otherwise, step S308 is performed.
S307: and in response to the fact that the memory address corresponding to the released memory is recorded in the storage memory, deleting the mapping relation between the memory address corresponding to the released memory and the call stack corresponding to the application module so as to obtain updated recorded data.
Specifically, when the memory address corresponding to the released memory is recorded in the storage memory, the mapping relationship between the memory address corresponding to the released memory and the call stack corresponding to the application module is deleted from the storage memory, and the corresponding storage memory is released and updated record data is obtained.
In one application scenario, referring again to fig. 2, the detection library includes a replacement module and a recording module, when the detection state is opened and a memory release instruction of the application module is received, the replacement module sets the thread local variable corresponding to the detection library as a memory management library to be directly called, releases the corresponding memory from the memory management library through the custom function, and meanwhile, the replacement module judges whether the memory released by the application module is recorded in the storage memory, when the memory address corresponding to the released memory is recorded in the storage memory, the replacement module sends the memory address corresponding to the released memory to the recording module, the mapping relation between the memory address corresponding to the memory released by the recording module and the call stack corresponding to the application module is deleted from the storage memory, and the corresponding storage memory is released to obtain updated recorded data.
S308: the detection library is set to be a memory management library which does not need to be directly called.
Specifically, after the application module releases the corresponding memory, the detection library is set without directly calling the memory management library, so that by setting a mode that whether the detection library needs to directly call the memory management library or not, when the mapping relation is deleted, the memory address corresponding to the released memory is directly obtained from the memory management library, and no matter whether the mapping relation is deleted or not is not trapped in a dead cycle.
S309: and in response to the preset period, the detection library generates a current record file based on the record data at the current moment so that the statistical unit obtains the current record file and determines the position of memory leakage based on the current record file and a historical record file before the current record file.
Specifically, the detection library is provided with compiling options so as to automatically trigger the file acquisition instruction at regular time according to a preset period when the detection library runs, and then the application module receives the record file generated at intervals of the preset period without modifying a code.
In an application mode, a file acquisition instruction is triggered in response to the detection library through a preset period, and a current record file corresponding to the current time is generated based on the mapping relation between the call stacks and the memory addresses corresponding to all application modules collected by the detection library at the current time.
Specifically, the detection library triggers a file acquisition instruction after a preset period, and generates current record files corresponding to the current time from record data which are recorded in the detection library at the current time and are not deleted, so that a statistical unit can acquire the record files with the preset period as an interval after each preset period, after the statistical unit acquires the record files, whether memory leakage occurs or not can be judged according to the size relationship of the plurality of record files in the time sequence, and the position of the memory leakage is acquired by analyzing and calling a stack.
In this embodiment, by setting a manner of whether the detection library needs to directly call the memory management library, the detection library does not fall into a dead loop when recording a mapping relationship or deleting the mapping relationship, a mapping relationship between a memory address corresponding to a memory not released by the application module and a call stack is kept to be recorded in the recorded data at the current time, and when a preset period passes, the detection library generates a current record file based on the recorded data at the current time, so that the statistical unit analyzes the position of the memory leak.
Referring to fig. 4, fig. 4 is a schematic flowchart illustrating a memory leak detection method according to another embodiment of the present application, in which an application module is used as an execution end, including:
s401: and responding to the memory application instruction sent by the application module to the detection library, so that the detection library obtains a corresponding memory from the memory management library based on the memory application instruction and returns a memory address corresponding to the memory to the application module, and recording a mapping relation between the memory address and a call stack corresponding to the application module to obtain recorded data.
Specifically, when the application module sends a memory application instruction to the detection library, the detection library obtains a corresponding memory after applying from the memory management library based on the memory application instruction and returns a memory address corresponding to the memory to the application module, and a mapping relationship between the memory address and a call stack corresponding to the application module is recorded to obtain recorded data.
Optionally, whether the size of the memory applied by the application module exceeds a preset threshold is judged, and if the size of the memory applied by the application module exceeds the preset threshold, a mapping relationship between a memory address corresponding to the memory and a call stack corresponding to the application module is obtained and recorded to obtain recorded data.
S402: and sending a memory release instruction to the detection library in response to the application module, so that the detection library acquires a memory address specified by the application module based on the memory release instruction, determines a memory released by the application module based on the memory address, and deletes a mapping relation between the memory address corresponding to the released memory and a call stack corresponding to the application module from the current record data.
Specifically, when the application module sends a memory release instruction to the detection library, so that the detection library determines a memory address specified by the application model based on the memory release instruction, releases a memory corresponding to the memory address from the memory management library, if the memory address corresponding to the released memory is recorded in the record data, deletes a mapping relationship between the memory address corresponding to the released memory and a call stack corresponding to the application module from the current record data, and if the memory address corresponding to the released memory is not recorded in the record data, indicates that the size of the memory corresponding to the released memory is smaller than a preset threshold value, and does not process the record data, thereby keeping the memory address corresponding to the memory which is not released and is larger than the preset threshold value recorded in the record data at the current time.
S403: and in response to the preset period, the application module sends a file acquisition instruction to the detection library so that the detection library generates a current record file based on the record data at the current moment, and then the statistical unit acquires the current record file and determines the position of memory leakage based on the current record file and a history record file before the current record file.
Specifically, the application module sends a file acquisition instruction to the detection library after running a preset period, so that the detection library packs the recording data at the current moment based on the file acquisition instruction to generate a current recording file, and the statistical unit analyzes the position of memory leakage by using the current recording file and a history file received before the current recording file after obtaining the current recording file. After the statistical unit obtains the record files corresponding to a plurality of successive preset periods in the time sequence, the statistical unit can analyze the position of memory leakage based on the size of the record files and the times of occurrence of call stacks in the record files.
Further, the detection library corresponds to a detection state, the detection state is not opened by default, and the detection state is kept in an opened state after being opened, and the step of opening the detection state comprises the following steps: the application module sends a loading instruction to the detection library so that the detection library can replace a management interface of the memory management library with a custom interface after obtaining the loading instruction; and in response to the detection state not being started and the application module not needing to directly call the memory management library, starting the detection state so that the detection library stores the detection state.
Specifically, the loading detection library may be an implicit link detection library during compilation, or a detection library specified by an environment variable with the highest priority may be used, both of which may achieve the purpose of automatically loading the detection library when the application module runs, the detection library replaces a management interface of the memory management library with a custom interface after obtaining a loading instruction, so that when the application module applies for the memory, an interface defined by a custom function of the replacement module is called, and then the detection library applies for the memory from the memory management library or releases the memory by using the custom interface, and the detection library is used as a transfer station between the application module and the memory detection library, so that the detection library records a mapping relationship between a call stack corresponding to the application module and the memory.
Further, the detection state is not opened by default at an initial stage, when the application module sends an instruction for applying for the memory, the replacement module first judges whether the application module needs to directly call the memory management library, the call initial value corresponding to the application module is not needed, if the detection state is not opened and the application module does not need to directly call the memory management library, the application module opens the detection state to enable the detection library to store the detection state, and if the application module needs to directly call the memory management library, the application module directly calls the memory in the memory management library and returns a memory application result, so that the application module can apply for the memory in two types of memory application modes.
In a specific application scenario, the process occupies memory and increases in the running process of the C/C + + program, so that the detection library is loaded in an implicit link mode, detection is started before the position of memory leakage possibly exists in an application program code, a recording file is output every 30 minutes, and a call stack of the memory leakage is analyzed through a statistical unit.
In another specific application scenario, when the JAVA program finds that the JAVA process occupies an increased memory during the running process, the detection library is loaded in an LD _ load manner, detection is started before a position where memory leakage may exist in the code, a recording file is output every 30 minutes, and a call stack of the memory leakage is analyzed by a statistical tool.
It can be understood that when programs corresponding to other types of programming languages run, statistics and analysis on the record file can be realized in a mode of loading the detection library, so as to obtain the location of memory leakage.
In the above scheme, when the detection state is opened and a memory is required to be applied, the application module sends a memory application instruction to the detection library to enable the detection library to obtain a corresponding memory from the memory management library and return a memory address corresponding to the memory to the application module, and records a mapping relationship between the memory address and a call stack corresponding to the application module when the size of the memory applied by the application module exceeds a preset threshold value to obtain recorded data, when the memory is required to be released, the application module sends a memory release instruction to the detection library to enable the detection library to obtain a memory address corresponding to the memory released by the application module, and deletes the mapping relationship between the memory address corresponding to the released memory and the call stack corresponding to the application module from the current recorded data, thereby keeping the mapping relationship between the memory address corresponding to the memory and the call stack which are not released by the application module in the recorded data at the current moment, and when a preset period passes, the application module sends a file acquisition instruction to the detection library so that the detection library generates a current record file based on the record data at the current moment, and then the statistical unit can find the position of memory leakage based on the current record file and the change of the history record file before the current record file after obtaining the current record file. Therefore, no matter which type of programming language the application module runs based on, after the mapping relation between the memory address and the call stack corresponding to the application module is recorded and the recording file is output at regular time, the position of memory leakage can be efficiently analyzed by the statistical unit, and the adaptability and the efficiency of detecting the memory leakage are improved.
Referring to fig. 5, fig. 5 is a schematic flow chart diagram illustrating a memory leak detection method according to another embodiment of the present application, in which a statistical unit is used as an execution end, including:
s501: and acquiring a current recording file sent by the application module, and extracting a historical recording file before the current recording file.
Specifically, the current record file is obtained based on the method described in any of the above embodiments, and the statistical unit maintains the record files in the total period after receiving the record files at every preset period until the time duration of all the stored record files exceeds the total period, deletes the history record file with the earliest time sequence and receives the current record file sent by the application module, thereby reducing the load of the statistical unit.
S502: and when the current recording file is larger than the last adjacent history recording file, extracting a call stack set formed by call stacks in at least one recording file, and determining the occurrence frequency of the call stacks in the call stack set in all the recording files.
Specifically, if the current record file is larger than the previous adjacent history record file, a call stack set composed of call stacks in at least one record file is extracted, wherein the call stacks in the call stack set are different and respectively correspond to different application modules, and the times of occurrence of the call stacks in the call stack set are counted in all the record files.
In an application mode, all different call stacks in the current record file form a call stack set, and the times of the call stacks in the call stack set appearing in the respective corresponding record files are determined in all the record files.
Specifically, if the size of the current record file is larger than that of the previous adjacent history record file, it indicates that memory leakage may occur in the application module corresponding to the call stack in the current record file, and all the call stacks in the current record file are extracted and deduplicated, so that call stack sets composed of different call stacks are obtained, and the number of times each call stack in the call stack sets appears is counted in all the record files.
In another application mode, all different call stacks in the largest record file form a call stack set, and the times of occurrence of the call stacks in the call stack set in the respective corresponding record files are determined in all the record files.
Specifically, the maximum record file record in all record files in the total period has the most mapping relationship, so that the call stacks recorded in the maximum record file have higher probability of being more comprehensive, all the call stacks in the maximum record file are extracted and deduplicated, so that call stack sets composed of different call stacks are obtained, and the occurrence frequency of each call stack in the call stack sets is counted in all the record files.
In another application mode, all different call stacks in the current record file and the maximum record file form a call stack set, and the times of occurrence of the call stacks in the call stack set in the respective corresponding record files are determined in all the record files.
It can be understood that a more comprehensive call stack set is obtained by extracting and de-duplicating the call stacks in the current record file and the maximum record file, so that call stack sets composed of different call stacks are obtained, and the occurrence frequency of each call stack in the call stack sets is counted in all the record files.
S503: and arranging all the record files according to a time sequence, acquiring a call stack with the times increased in the record files arranged according to the time sequence as a target call stack, and analyzing a memory leakage position corresponding to the application module from the target call stack.
Specifically, all the record files are arranged according to a time sequence, and then a call stack with increasing occurrence frequency is searched in the record files arranged according to the time sequence, namely, the call stack with the unreduced occurrence frequency is used as a target call stack, and a function name and a code line number are analyzed from the target call stack by utilizing an analysis rule, so that a memory leakage position of an application module corresponding to the call stack is positioned.
In an application mode, all the record files are arranged according to a time sequence, and the duplication removal is carried out on the occurrence times corresponding to the same call stack in the adjacent record files; and acquiring a call stack with continuously increasing times as a target call stack, analyzing a function name and a code line number corresponding to the memory application instruction sent by an application module corresponding to the target call stack, and acquiring a memory leakage position corresponding to the application module.
Specifically, the number of times of occurrence corresponding to the same call stack in the adjacent record files is de-duplicated, so that the trend of the number of times of occurrence of the call stack is judged more accurately, the call stack with continuously increasing number of times of occurrence is used as a target call stack, and a function name and a code line number corresponding to the target call stack are analyzed by using an ELF (element-based language) viewing tool, so that the memory leakage position of an application module corresponding to the call stack is accurately positioned.
In an application scenario, the statistical unit includes a search module, a statistical module and an analysis module, the statistical module judges whether the size of the current record file is larger than the size of the previous adjacent history record file, if so, the search module searches the largest record file in all the record files, and if not, the search module waits for the record file after the next preset period.
Furthermore, the address query module sequences call stacks in the largest recording file and removes repeated call stacks to obtain a call stack set, the call stack set is sent to the statistical module, the statistical module sequences all the recording files in time sequence, determines the occurrence frequency of the call stacks in the call stack set in all the recording files, removes the occurrence frequency corresponding to the same call stack in adjacent recording files, screens out the call stacks with increasing occurrence frequency as target call stacks and sends the target call stacks to the analysis module, and the analysis module analyzes the function names and the code line number corresponding to the target call stacks by using an ELF viewing tool.
In a specific application scenario, the memory of the program from which the symbol is removed continuously increases, the detection library can still be used to collect the record file, and the statistical tool is used to analyze the call stack corresponding to the increased memory, at this time, the library file before the symbol is removed needs to be found, or the same source code is compiled again to preserve the symbol library, and the ELF checking tool is used to restore the function name and the code line number based on the call stack analyzed by statistics and the library with the symbol compiled from the same source code.
According to the scheme, after the statistical unit obtains the current recording file, the statistical unit can search the memory leakage position in the recording file based on the change of the current recording file and the historical recording file. Therefore, no matter which type of programming language the application module runs based on, after the mapping relation between the memory address and the call stack corresponding to the application module is recorded and the recording file is output at regular time, the position of memory leakage can be efficiently analyzed by the statistical unit, and the adaptability and the efficiency of detecting the memory leakage are improved.
Referring to fig. 6, fig. 6 is a schematic structural diagram of an embodiment of an electronic device 60 of the present application, where the electronic device 60 includes a memory 601 and a processor 602 coupled to each other, where the memory 601 stores program data (not shown), and the processor 602 invokes the program data to implement the memory leak detection method in any of the embodiments described above, and the description of relevant contents refers to the detailed description of the embodiments of the method described above, which is not repeated herein.
Referring to fig. 7, fig. 7 is a schematic structural diagram of an embodiment of a computer storage medium 70 of the present application, the computer storage medium 70 stores program data 700, and the program data 700 is executed by a processor to implement the memory leak detection method in any of the above embodiments, and a description of relevant contents refers to a detailed description of the above method embodiments, which is not repeated herein.
The above description is only for the purpose of illustrating embodiments of the present application and is not intended to limit the scope of the present application, and all modifications of equivalent structures and equivalent processes, which are made by the contents of the specification and the drawings of the present application or are directly or indirectly applied to other related technical fields, are also included in the scope of the present application.
Claims (12)
1. A method for memory leak detection, the method comprising:
in response to receiving a memory application instruction of an application module, a detection library acquires a corresponding memory from a memory management library based on the memory application instruction, returns a memory address corresponding to the memory to the application module, and records a mapping relation between the memory address and a call stack corresponding to the application module to acquire recorded data;
in response to receiving a memory release instruction of an application module, the detection library acquires a memory address specified by the application module based on the memory release instruction, determines a memory released by the application module based on the memory address, and deletes a mapping relation between the memory address corresponding to the released memory and a call stack corresponding to the application module from current recorded data;
and responding to the fact that a preset period passes, the detection library generates a current record file based on the record data at the current moment, so that a statistical unit obtains the current record file and determines the position of memory leakage based on the current record file and a historical record file before the current record file.
2. The method according to claim 1, wherein the step of obtaining a corresponding memory from a memory management library based on the memory application instruction, returning a memory address corresponding to the memory to the application module, and recording a mapping relationship between the memory address and a call stack corresponding to the application module to obtain recorded data includes:
setting the detection library to be required to directly call the memory management library, applying for a corresponding memory from the memory management library based on the memory application instruction, and returning a memory address corresponding to the applied memory to the application module, thereby setting the detection library to be free from directly calling the memory management library;
before the step of setting the detection library to be free from directly calling the memory management library, the method further comprises the following steps:
and responding to the situation that the size of the memory applied by the application module exceeds a preset threshold, applying for a memory from the memory management library to store the memory so as to record the mapping relation between the memory address corresponding to the memory applied by the application module and the call stack corresponding to the application module in the memory to obtain the recorded data.
3. The memory leak detection method according to claim 2, wherein the step of obtaining the memory address specified by the application module based on the memory release instruction, determining the memory released by the application module based on the memory address, and deleting the mapping relationship between the memory address corresponding to the released memory and the call stack corresponding to the application module from the current record data includes:
setting the detection library to be required to directly call the memory management library, determining a memory address specified by the application module based on a memory release instruction, releasing a memory corresponding to the memory address specified by the application module in the memory management library, and further setting the detection library to be not required to directly call the memory management library;
before the step of setting the detection library to be free from directly calling the memory management library, the method further comprises the following steps:
and in response to the fact that the memory address corresponding to the released memory is recorded in the storage memory, deleting the mapping relation between the memory address corresponding to the released memory and the call stack corresponding to the application module to obtain the updated recorded data.
4. The method according to claim 2, wherein before the step of setting the detection library to require direct call of the memory management library, applying for a corresponding memory from the memory management library based on the memory application instruction, and returning a memory address corresponding to the memory applied to the application module, the method further comprises:
and the detection library replaces a management interface of the memory management library with a custom interface so that the detection library applies for a memory from the memory management library or releases the memory by using the custom interface.
5. The memory leak detection method according to claim 1, wherein the step of generating, by the detection library, a current log file based on the log data at the current time in response to the elapse of a preset period includes:
and in response to the detection library triggering a file acquisition instruction through the preset period, generating the current record file corresponding to the current time based on the mapping relation between the call stacks corresponding to all the application modules and the memory address, which is collected by the detection library at the current time.
6. A memory leak detection method, the method comprising:
responding to a memory application instruction sent by an application module to a detection library, enabling the detection library to obtain a corresponding memory from a memory management library based on the memory application instruction, returning a memory address corresponding to the memory to the application module, and recording a mapping relation between the memory address and a call stack corresponding to the application module to obtain recorded data;
responding to a memory release instruction sent by the application module to a detection library, so that the detection library obtains a memory address specified by the application module based on the memory release instruction, determines a memory released by the application module based on the memory address, and deletes a mapping relation between the memory address corresponding to the released memory and a call stack corresponding to the application module from current recorded data;
and in response to the preset period, the application module sends a file acquisition instruction to the detection library so that the detection library generates a current record file based on the current record data, and then the statistical unit acquires the current record file and determines the position of memory leakage based on the current record file and a historical record file before the current record file.
7. The method according to claim 6, wherein the detection library corresponds to a detection state, the detection state is not opened by default, and the detection state remains opened after being opened, and the step of opening the detection state includes:
the application module sends a loading instruction to the detection library so that the detection library can replace a management interface of the memory management library with a custom interface after obtaining the loading instruction;
and in response to the detection state not being started and the application module not needing to directly call the memory management library, starting the detection state so that the detection library stores the detection state.
8. A method for memory leak detection, the method comprising:
acquiring a current recording file sent by an application module, and extracting a historical recording file before the current recording file; wherein the current log file is obtained based on the method of any of the preceding claims 1-7;
when the current recording file is larger than the last adjacent historical recording file, extracting a call stack set formed by call stacks in at least one recording file, and determining the occurrence frequency of the call stacks in the call stack set in all the recording files;
and arranging all the record files according to a time sequence, acquiring a call stack with the times increased in the record files arranged according to the time sequence as a target call stack, and analyzing a memory leakage position corresponding to the application module from the target call stack.
9. The method according to claim 8, wherein the step of extracting a call stack set composed of call stacks in at least one log file and determining the number of times of occurrence of the call stacks in the call stack set in all the log files comprises:
all different call stacks in the current recording file and/or the maximum recording file form the call stack set;
and determining the times of occurrence of the call stacks in the call stack set in the respective corresponding record files in all the record files.
10. The memory leak detection method according to claim 9, wherein the step of arranging all the record files in a time sequence, acquiring a call stack with an increased number of times of occurrence in the record files arranged in the time sequence as a target call stack, and analyzing a memory leak position corresponding to the application module from the target call stack includes:
arranging all the recording files according to a time sequence, and removing the duplication of the occurrence times corresponding to the same call stack in the adjacent recording files;
and acquiring a call stack with continuously increasing times as the target call stack, analyzing a function name and a code line number corresponding to the target call stack when the application module sends the memory application instruction, and acquiring a memory leakage position corresponding to the application module.
11. An electronic device, comprising: a memory and a processor coupled to each other, wherein the memory stores program data that the processor calls to perform the method of any of claims 1-10.
12. A computer-readable storage medium, on which program data are stored, which program data, when being executed by a processor, carry out the method according to any one of claims 1-10.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202111614917.7A CN114546795A (en) | 2021-12-27 | 2021-12-27 | Memory leak detection method, electronic device, and computer-readable storage medium |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202111614917.7A CN114546795A (en) | 2021-12-27 | 2021-12-27 | Memory leak detection method, electronic device, and computer-readable storage medium |
Publications (1)
Publication Number | Publication Date |
---|---|
CN114546795A true CN114546795A (en) | 2022-05-27 |
Family
ID=81669625
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202111614917.7A Pending CN114546795A (en) | 2021-12-27 | 2021-12-27 | Memory leak detection method, electronic device, and computer-readable storage medium |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN114546795A (en) |
Cited By (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN115878405A (en) * | 2023-03-08 | 2023-03-31 | 杭州拓数派科技发展有限公司 | PostgreSQL database memory detection method and system |
CN116661987A (en) * | 2022-12-29 | 2023-08-29 | 荣耀终端有限公司 | Memory application method and electronic equipment |
CN116860639A (en) * | 2023-07-11 | 2023-10-10 | 深圳依时货拉拉科技有限公司 | Memory leakage detection method, detection device, computer equipment and storage medium |
-
2021
- 2021-12-27 CN CN202111614917.7A patent/CN114546795A/en active Pending
Cited By (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN116661987A (en) * | 2022-12-29 | 2023-08-29 | 荣耀终端有限公司 | Memory application method and electronic equipment |
CN115878405A (en) * | 2023-03-08 | 2023-03-31 | 杭州拓数派科技发展有限公司 | PostgreSQL database memory detection method and system |
CN116860639A (en) * | 2023-07-11 | 2023-10-10 | 深圳依时货拉拉科技有限公司 | Memory leakage detection method, detection device, computer equipment and storage medium |
CN116860639B (en) * | 2023-07-11 | 2024-03-29 | 深圳依时货拉拉科技有限公司 | Memory leakage detection method, detection device, computer equipment and storage medium |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN114546795A (en) | Memory leak detection method, electronic device, and computer-readable storage medium | |
US8429620B2 (en) | Memory leak diagnosis | |
CN104965754A (en) | Task scheduling method and task scheduling apparatus | |
US9189393B2 (en) | Computer, control method of computer, and recording medium | |
CN111966603B (en) | Memory leakage detection method and device, readable storage medium and electronic equipment | |
CN111367760A (en) | Log collection method and device, computer equipment and storage medium | |
CN111737203A (en) | Database history log backtracking method, device, system, equipment and storage medium | |
CN114385551B (en) | Log time-sharing management method, device, equipment and storage medium | |
CN117033058A (en) | Analysis method, device, equipment and medium for software crash data | |
CN110781166B (en) | Data management method and device in storage system | |
CN117112522A (en) | Concurrent process log management method, device, equipment and storage medium | |
CN110727565B (en) | Network equipment platform information collection method and system | |
CN109032940B (en) | Test scene input method, device, equipment and storage medium | |
CN115705294B (en) | Method, device, electronic equipment and medium for acquiring function call information | |
CN107122247B (en) | Method and device for detecting static occupied picture | |
CN112231279B (en) | Log management system | |
CN113886342A (en) | File format conversion method and device, storage medium and processor | |
WO2020177495A1 (en) | Database connection management method and apparatus, and device | |
CN112860469A (en) | Method, device, equipment and storage medium for collecting information of katon log | |
CN114201353A (en) | Processing method, device and storage medium for program time-consuming data | |
CN113821193A (en) | Information generation method and device and storage medium | |
CN112685334A (en) | Method, device and storage medium for block caching of data | |
CN111177097B (en) | Log collection method, device and equipment and computer readable storage medium | |
CN106959888B (en) | Task processing method and device in cloud storage system | |
CN116860639B (en) | Memory leakage detection method, detection device, computer equipment and storage medium |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination |