CN113849394A - Memory leak detection method and device, electronic equipment and computer storage medium - Google Patents

Memory leak detection method and device, electronic equipment and computer storage medium Download PDF

Info

Publication number
CN113849394A
CN113849394A CN202010600750.8A CN202010600750A CN113849394A CN 113849394 A CN113849394 A CN 113849394A CN 202010600750 A CN202010600750 A CN 202010600750A CN 113849394 A CN113849394 A CN 113849394A
Authority
CN
China
Prior art keywords
thread
application program
memory
information
list
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
Application number
CN202010600750.8A
Other languages
Chinese (zh)
Inventor
韩立
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing ByteDance Network Technology Co Ltd
Original Assignee
Beijing ByteDance Network Technology Co Ltd
Priority date (The priority date 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 date listed.)
Filing date
Publication date
Application filed by Beijing ByteDance Network Technology Co Ltd filed Critical Beijing ByteDance Network Technology Co Ltd
Priority to CN202010600750.8A priority Critical patent/CN113849394A/en
Publication of CN113849394A publication Critical patent/CN113849394A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/362Software debugging
    • G06F11/366Software debugging using diagnostics
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5011Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
    • G06F9/5016Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals the resource being the memory
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5011Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
    • G06F9/5022Mechanisms to release resources

Abstract

The disclosure provides a memory leak detection method and device, electronic equipment and a computer readable storage medium, and relates to the field of computers. The method comprises the following steps: when an application program runs, acquiring a thread list corresponding to the application program; the thread list comprises at least one thread currently being executed by the application program; acquiring thread information of each thread in the thread list based on a preset rule; and when detecting that at least one of the thread information of each thread meets a preset condition, judging that the memory leakage exists in the application program. The method and the device not only reduce a large amount of labor cost and time cost, but also greatly improve the detection efficiency.

Description

Memory leak detection method and device, electronic equipment and computer storage medium
Technical Field
The present disclosure relates to the field of computer technologies, and in particular, to a method and an apparatus for detecting memory leakage, an electronic device, and a computer-readable storage medium.
Background
When developing an application, a more common way to create a thread is to create a thread through a specified function to perform a specified behavior action.
When the designated function creates the thread, the default created thread is in a connection state, and if no other setting is carried out, after the thread executes the action, the thread can not release the thread resource occupied by the thread when exiting.
If the thread needs to release resources when finishing exiting, the created thread needs to call pthread _ j through other threads, or the created new thread sets itself to be in a separation state before exiting, or the state of the thread is designated to be in the separation state when the thread is created. However, during the development process, the software developer may forget to perform the above setting, so that the created thread does not release the thread resource after exiting. As the program runs for a long time, the thread resources which are not released are more and more, memory leakage is caused, and finally, the memory cannot be allocated and crashed when the application program runs in a normal function.
For such memory leaks, software developers can only find and solve problems by checking codes. At present, the complexity of software is increasingly huge, large software may be developed by dozens of people or even hundreds of people together, the number of modules forming the software is large, the service capacity of developers is high and the bottoms of the developers are low, and meanwhile, the problem is solved by solving the problem, which obviously wastes time and labor and is difficult to solve the problem of thread resource leakage.
Disclosure of Invention
This summary is provided to introduce a selection of concepts in a simplified form that are further described below in the detailed description. This summary is not intended to identify key features or essential features of the claimed subject matter, nor is it intended to be used to limit the scope of the claimed subject matter.
The disclosure provides a method and a device for detecting memory leakage, electronic equipment and a computer readable storage medium, which can solve the problem of detection of memory leakage. The technical scheme is as follows:
in a first aspect, a method for detecting memory leak is provided, where the method includes:
when an application program runs, acquiring a thread list corresponding to the application program; the thread list comprises at least one thread currently being executed by the application program;
acquiring thread information of each thread in the thread list based on a preset rule;
and when detecting that at least one of the thread information of each thread meets a preset condition, judging that the memory leakage exists in the application program.
In a second aspect, there is provided a device for detecting memory leak, the device including:
the device comprises a first acquisition module, a second acquisition module and a third acquisition module, wherein the first acquisition module is used for acquiring a thread list corresponding to an application program when the application program runs; the thread list comprises at least one thread currently being executed by the application program;
the second acquisition module is used for acquiring the thread information of each thread in the thread list based on a preset rule;
and the judging module is used for judging that the memory leakage exists in the application program when detecting that at least one of the thread information of each thread meets a preset condition.
In a third aspect, an electronic device is provided, which includes:
a processor, a memory, and a bus;
the bus is used for connecting the processor and the memory;
the memory is used for storing operation instructions;
the processor is configured to call the operation instruction, and the executable instruction enables the processor to execute an operation corresponding to the method for detecting memory leak according to the first aspect of the disclosure.
In a fourth aspect, a computer-readable storage medium is provided, on which a computer program is stored, and the computer program, when executed by a processor, implements the method for detecting memory leak according to the first aspect of the disclosure.
The technical scheme provided by the disclosure has the following beneficial effects:
when an application program runs, acquiring a thread list corresponding to the application program; the thread list comprises at least one thread currently executed by the application program, thread information of each thread in the thread list is obtained based on a preset rule, and when at least one piece of thread information in the thread information of each thread is detected to meet a preset condition, it is determined that memory leakage exists in the application program. Therefore, when the application program runs, a thread list of the application program is obtained by traversing every preset time interval, then the original thread descriptors of all threads in the thread list are replaced by the target thread descriptors, then the target thread descriptors are adopted to obtain the thread information of all the threads, and whether the thread information meets the preset memory leakage condition is detected, so that whether the memory leakage exists in the application program can be determined, and therefore whether the memory leakage occurs in the application program can be detected in real time.
Drawings
The above and other features, advantages and aspects of various embodiments of the present disclosure will become more apparent by referring to the following detailed description when taken in conjunction with the accompanying drawings. Throughout the drawings, the same or similar reference numbers refer to the same or similar elements. It should be understood that the drawings are schematic and that elements and features are not necessarily drawn to scale.
Fig. 1 is a schematic flowchart illustrating a method for detecting memory leakage according to an embodiment of the present disclosure;
fig. 2 is a schematic flowchart illustrating a method for detecting memory leakage according to another embodiment of the present disclosure;
FIG. 3 is another alternative flow diagram of FIG. 2 in accordance with the present disclosure;
FIG. 4 is a schematic flow diagram of an alternative to FIG. 2 of the present disclosure;
fig. 5 is a schematic structural diagram of a device for detecting memory leakage according to yet another embodiment of the present disclosure;
fig. 6 is a schematic structural diagram of an electronic device for detecting memory leak according to yet another embodiment of the present disclosure.
Detailed Description
Embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. While certain embodiments of the present disclosure are shown in the drawings, it is to be understood that the present disclosure may be embodied in various forms and should not be construed as limited to the embodiments set forth herein, but rather are provided for a more thorough and complete understanding of the present disclosure. It should be understood that the drawings and embodiments of the disclosure are for illustration purposes only and are not intended to limit the scope of the disclosure.
It should be understood that the various steps recited in the method embodiments of the present disclosure may be performed in a different order, and/or performed in parallel. Moreover, method embodiments may include additional steps and/or omit performing the illustrated steps. The scope of the present disclosure is not limited in this respect.
The term "include" and variations thereof as used herein are open-ended, i.e., "including but not limited to". The term "based on" is "based, at least in part, on". The term "one embodiment" means "at least one embodiment"; the term "another embodiment" means "at least one additional embodiment"; the term "some embodiments" means "at least some embodiments". Relevant definitions for other terms will be given in the following description.
It should be noted that the terms "first", "second", and the like in the present disclosure are only used for distinguishing different devices, modules or units, and are not used for limiting the devices, modules or units to be determined as different devices, modules or units, and are not used for limiting the sequence or interdependence relationship of the functions executed by the devices, modules or units.
It is noted that references to "a", "an", and "the" modifications in this disclosure are intended to be illustrative rather than limiting, and that those skilled in the art will recognize that "one or more" may be used unless the context clearly dictates otherwise.
The names of messages or information exchanged between devices in the embodiments of the present disclosure are for illustrative purposes only, and are not intended to limit the scope of the messages or information.
To make the objects, technical solutions and advantages of the present disclosure more apparent, embodiments of the present disclosure will be described in detail with reference to the accompanying drawings.
The present disclosure provides a method and an apparatus for detecting memory leak, an electronic device, and a computer-readable storage medium, which are intended to solve the above technical problems in the prior art.
The following describes the technical solutions of the present disclosure and how to solve the above technical problems in specific embodiments. The following several specific embodiments may be combined with each other, and details of the same or similar concepts or processes may not be repeated in some embodiments. Embodiments of the present disclosure will be described below with reference to the accompanying drawings.
In one embodiment, a method for detecting a memory leak is provided, as shown in fig. 1, the method includes:
step S101, when an application program runs, a thread list corresponding to the application program is obtained; the thread list comprises at least one thread currently executed by the application program;
specifically, a plurality of threads may be executed during the running of the application program, so in order to detect which thread does not release the thread resource when exiting, a thread list corresponding to the application program may be acquired during the running of the application program, and the thread list may include all threads currently being executed by the application program during the running.
It should be noted that, in practical applications, the threads executed by the application program at different time periods during runtime may be different, and therefore, the threads in the thread list obtained by each traversal may not be identical.
Step S102, acquiring thread information of each thread in a thread list based on a preset rule;
after the thread list is obtained each time, the thread information of each thread in the thread list can be obtained based on a preset rule; the thread information may include at least one of a signal stack pointer and a thread ID of the thread, and certainly, the thread information may also include other contents, and may be set according to actual requirements in actual applications, which is not limited in this disclosure.
Step S103, when it is detected that at least one of the thread information of the threads satisfies a preset condition, it is determined that there is a memory leak in the application program.
After the thread information of each thread in the thread list is acquired, whether the thread information of each thread meets a preset condition is detected in sequence, and if the thread information of at least one thread does not meet the preset condition, it can be determined that the memory leakage exists in the application program.
In the embodiment of the disclosure, when an application program runs, a thread list corresponding to the application program is obtained; the thread list comprises at least one thread currently executed by the application program, thread information of each thread in the thread list is obtained based on a preset rule, and when at least one piece of thread information in the thread information of each thread is detected to meet a preset condition, memory leakage of the application program is judged. Therefore, when the application program runs, a thread list of the application program is obtained by traversing every preset time interval, then the original thread descriptors of all threads in the thread list are replaced by the target thread descriptors, then the target thread descriptors are adopted to obtain the thread information of all the threads, and whether the thread information meets the preset memory leakage condition is detected, so that whether the memory leakage exists in the application program can be determined, and therefore whether the memory leakage occurs in the application program can be detected in real time.
In another embodiment, a method for detecting a memory leak is provided, as shown in fig. 2, the method includes:
step S201, when an application program runs, acquiring a thread list corresponding to the application program; the thread list comprises at least one thread currently executed by the application program;
specifically, a plurality of threads may be executed during the running of the application program, so in order to detect which thread does not release the thread resource when exiting, a thread list corresponding to the application program may be acquired during the running of the application program, and the thread list may include all threads currently being executed by the application program during the running.
In a preferred embodiment of the present disclosure, acquiring a thread list corresponding to an application program includes:
traversing at least one currently executed thread of the application program by adopting a preset detection thread at intervals of a preset time interval to obtain a thread list; wherein the detection thread is started when detecting that the application program is started.
Specifically, a detection thread may be preset to traverse a thread currently being executed when the application program is running, and the detection thread may be started when the application program is detected to be started. After detecting the thread start, the thread currently executing in the application program runtime may be traversed at preset time intervals, such as 30 seconds, 1 minute, and so on, to obtain a thread list.
It should be noted that, in practical applications, the threads executed by the application program at different time periods during runtime may be different, and therefore, the threads in the thread list obtained by each traversal may not be identical.
Step S202, acquiring thread information of each thread in a thread list based on a preset rule;
after the thread list is obtained each time, the thread information of each thread in the thread list can be obtained based on a preset rule; the thread information may include at least one of a signal stack pointer and a thread ID of the thread, and certainly, the thread information may also include other contents, and may be set according to actual requirements in actual applications, which is not limited in this disclosure.
In a preferred embodiment of the present disclosure, obtaining thread information of each thread in a thread list based on a preset rule includes:
generating a target thread descriptor based on the original thread descriptor of the second dynamic link library; the original thread descriptor and the target thread descriptor have the same data structure;
replacing original thread descriptors corresponding to all threads in the thread list with target thread descriptors;
and acquiring the thread information of each thread through each target thread descriptor.
Where the second dynamically linked library can be libc. In an embodiment of the present disclosure, a target thread descriptor also having the data structure may be generated for obtaining thread information of each thread in the thread list.
Specifically, for each thread in the thread list, the original thread descriptor is replaced with the target thread descriptor, and the target thread descriptor in each thread can obtain the thread information of the corresponding thread through access.
Step S203, when detecting that at least one of the thread information of each thread meets a preset condition, determining that the memory leakage exists in the application program;
after the thread information of each thread in the thread list is acquired, whether the thread information of each thread meets a preset condition is detected in sequence, and if the thread information of at least one thread does not meet the preset condition, it can be determined that the memory leakage exists in the application program.
In a preferred embodiment of the present disclosure, when it is detected that at least one of the thread information of the threads satisfies a preset condition, determining that there is a memory leak in the application program includes:
when detecting that a signal stack pointer of at least one thread in the thread information of each thread is empty, judging that the memory leakage exists in the application program;
and/or the presence of a gas in the gas,
and when the thread ID of at least one thread in the thread information of each thread is detected to be a preset value, judging that the memory leakage exists in the application program.
Specifically, for the thread information of any thread, when it is detected that the signal stack pointer in the thread information is empty, it can be determined that there is a memory leak in the application program.
Alternatively, for the thread information of any thread, when it is detected that the thread ID in the thread information is a predetermined value, for example, when the thread ID is 0, it may be determined that the memory leak exists in the application program.
Or, for the thread information of any thread, when it is detected that the signal stack pointer in the thread information is empty and the thread ID is a predetermined value, it may be determined that the memory leak exists in the application program.
Step S204, acquiring the addresses of the thread execution functions corresponding to the thread information meeting the preset conditions;
specifically, when the memory leak has been detected, the addresses of the thread execution functions may also be obtained according to their respective target thread descriptors for each thread that satisfies the preset condition, that is, each thread whose signal stack pointer is empty and/or whose thread ID is a predetermined value.
Step S205, determining a first dynamic link library file corresponding to each thread execution function based on the address of each thread execution function;
in practical applications, each thread execution function may correspond to a first dynamic link library file, where the first dynamic link library may be release. Specifically, after the address of each thread execution function is determined, the release.so file corresponding to each thread execution function may be determined.
Step S206, determining the function name of each thread execution function with leaked memory based on the address of each thread execution function and each first dynamic link library file;
after the thread execution function address and the first dynamic link library file corresponding to each thread are determined, the function name of each thread execution function without memory leakage caused by thread resource release can be determined based on each thread execution function address and each first dynamic link library file.
In a preferred embodiment of the present disclosure, determining a function name of each thread execution function with memory leak based on an address of each thread execution function and each first dynamic link library file includes:
acquiring a third dynamic link library file corresponding to each first dynamic link library file;
and acquiring the function name of each thread execution function with leaked memory by adopting the address of each thread execution function, each first dynamic link library file and each third dynamic link library file.
Wherein the third dynamic link library file may be a debug. Specifically, when the application program is compiled, a debug.so file corresponding to one another can be generated for a release.so that after the thread execution function address and the first dynamic link library file corresponding to each thread are determined, a third dynamic link library file corresponding to the first dynamic link library file can be further acquired, and then the name of the thread execution function can be acquired by using the thread execution function address, the first dynamic link library file and the third dynamic link library file.
Furthermore, after determining the names of the functions executed by the threads causing the memory leak, developers can position and modify the source codes of the application program, thereby realizing the repair of the application program.
Step S207, determining the memory address of the thread stack corresponding to each piece of thread information meeting the preset condition;
after determining each thread causing memory leakage, the memory address of the thread stack may be further obtained through a preset memory address function, where the preset memory address obtaining function may be pthread _ attr _ getstack.
Step S208, releasing the memory space corresponding to the memory address of each thread stack.
After the memory address of the thread stack of each thread causing memory leakage is acquired, the memory resource occupied by each thread can be released through a preset memory release function, wherein the preset memory translation function can be a munmap function.
It should be noted that, in the embodiment of the present disclosure, after step S201 to step S203, step S204 to step S206 may be executed before step S207 to step S208, as shown in fig. 3; step S204 to step S206 may also be executed simultaneously with step S207 to step S208, as shown in fig. 4, in an actual application, the setting may be performed according to actual requirements, which is not limited in the embodiment of the present disclosure.
In the embodiment of the disclosure, when an application program runs, a thread list corresponding to the application program is obtained; the thread list comprises at least one thread currently executed by the application program, thread information of each thread in the thread list is obtained based on a preset rule, and when at least one piece of thread information in the thread information of each thread is detected to meet a preset condition, memory leakage of the application program is judged. Therefore, when the application program runs, a thread list of the application program is obtained by traversing every preset time interval, then the original thread descriptors of all threads in the thread list are replaced by the target thread descriptors, then the target thread descriptors are adopted to obtain the thread information of all the threads, and whether the thread information meets the preset memory leakage condition is detected, so that whether the memory leakage exists in the application program can be determined, and therefore whether the memory leakage occurs in the application program can be detected in real time.
Furthermore, after the memory leak is detected, the addresses of the thread execution functions of the threads causing the memory leak can be obtained through the target thread descriptor, and the function names of the thread execution functions are further determined based on the addresses of the thread execution functions, so that the source codes of the application programs are positioned, developers can quickly position the source codes with problems and repair the source codes without checking the source codes, and the repair efficiency of the application programs is greatly improved.
Furthermore, after the memory leak is detected, the memory address of the thread stack causing the memory leak can be determined through the related function, so that the memory space corresponding to each memory address is released, the situation that the application program cannot allocate the memory and is crashed due to the fact that the memory is exhausted even if the memory leak occurs during the operation of the application program is avoided, and the operation stability of the application program is ensured.
Fig. 5 is a schematic structural diagram of a device for detecting memory leakage according to another embodiment of the present disclosure, as shown in fig. 5, the device of this embodiment may include:
a first obtaining module 501, configured to obtain a thread list corresponding to an application program when the application program runs; the thread list comprises at least one thread currently executed by the application program;
a second obtaining module 502, configured to obtain thread information of each thread in the thread list based on a preset rule;
the determining module 503 is configured to determine that a memory leak exists in the application program when it is detected that at least one of the thread information of the threads meets a preset condition.
In a preferred embodiment of the present disclosure, the method further includes:
a third obtaining module, configured to obtain addresses of thread execution functions corresponding to respective pieces of thread information that satisfy a preset condition;
the first determining module is used for determining a first dynamic link library file corresponding to each thread execution function based on the address of each thread execution function;
and the second determining module is used for determining the function name of each thread execution function with memory leakage based on the address of each thread execution function and each first dynamic link library file.
In a preferred embodiment of the present disclosure, the method further includes:
the third determining module is used for determining the memory address of the thread stack corresponding to each piece of thread information meeting the preset condition;
and the releasing module is used for releasing the memory space corresponding to the memory address of each thread stack.
In a preferred embodiment of the present disclosure, the first obtaining module is specifically configured to:
traversing at least one currently executed thread of the application program by adopting a preset detection thread at intervals of a preset time interval to obtain a thread list; wherein the detection thread is started when detecting that the application program is started.
In a preferred embodiment of the present disclosure, the second obtaining module includes:
the generation submodule is used for generating a target thread descriptor based on the original thread descriptor of the second dynamic link library; the original thread descriptor and the target thread descriptor have the same data structure;
the replacing submodule is used for replacing the original thread descriptors corresponding to all the threads in the thread list with target thread descriptors;
and the thread information acquisition submodule is used for acquiring the thread information of each thread through each target thread descriptor.
In a preferred embodiment of the present disclosure, the thread information includes at least one of a signal stack pointer and a thread ID of the thread;
the determination module is specifically configured to:
when detecting that a signal stack pointer of at least one thread in the thread information of each thread is empty, judging that the memory leakage exists in the application program;
and when the thread ID of at least one thread in the thread information of each thread is detected to be a preset value, judging that the memory leakage exists in the application program.
In a preferred embodiment of the present disclosure, the second determining module includes:
the file acquisition submodule is used for acquiring a third dynamic link library file corresponding to each first dynamic link library file;
and the function name acquisition submodule is used for acquiring the function name of each thread execution function with leaked memory by adopting the address of each thread execution function, each first dynamic link library file and each third dynamic link library file.
The memory leak detection apparatus of this embodiment can perform the memory leak detection methods according to the first embodiment and the second embodiment of the present disclosure, and the implementation principles thereof are similar, and are not described herein again.
In the embodiment of the disclosure, when an application program runs, a thread list corresponding to the application program is obtained; the thread list comprises at least one thread currently executed by the application program, thread information of each thread in the thread list is obtained based on a preset rule, and when at least one piece of thread information in the thread information of each thread is detected to meet a preset condition, memory leakage of the application program is judged. Therefore, when the application program runs, a thread list of the application program is obtained by traversing every preset time interval, then the original thread descriptors of all threads in the thread list are replaced by the target thread descriptors, then the target thread descriptors are adopted to obtain the thread information of all the threads, and whether the thread information meets the preset memory leakage condition is detected, so that whether the memory leakage exists in the application program can be determined, and therefore whether the memory leakage occurs in the application program can be detected in real time.
Furthermore, after the memory leak is detected, the addresses of the thread execution functions of the threads causing the memory leak can be obtained through the target thread descriptor, and the function names of the thread execution functions are further determined based on the addresses of the thread execution functions, so that the source codes of the application programs are positioned, developers can quickly position the source codes with problems and repair the source codes without checking the source codes, and the repair efficiency of the application programs is greatly improved.
Furthermore, after the memory leak is detected, the memory address of the thread stack causing the memory leak can be determined through the related function, so that the memory space corresponding to each memory address is released, the situation that the application program cannot allocate the memory and is crashed due to the fact that the memory is exhausted even if the memory leak occurs during the operation of the application program is avoided, and the operation stability of the application program is ensured.
Referring now to FIG. 6, a block diagram of an electronic device 600 suitable for use in implementing embodiments of the present disclosure is shown. The electronic devices in the embodiments of the present disclosure may include, but are not limited to, mobile terminals such as mobile phones, notebook computers, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), in-vehicle terminals (e.g., car navigation terminals), and the like, and fixed terminals such as digital TVs, desktop computers, and the like. The electronic device shown in fig. 6 is only an example, and should not bring any limitation to the functions and the scope of use of the embodiments of the present disclosure.
The electronic device includes: a memory and a processor, wherein the processor may be referred to as a processing device 601 described below, and the memory may include at least one of a Read Only Memory (ROM)602, a Random Access Memory (RAM)603, and a storage device 608, which are described below: as shown in fig. 6, electronic device 600 may include a processing means (e.g., central processing unit, graphics processor, etc.) 601 that may perform various appropriate actions and processes in accordance with a program stored in a Read Only Memory (ROM)602 or a program loaded from a storage means 608 into a Random Access Memory (RAM) 603. In the RAM 603, various programs and data necessary for the operation of the electronic apparatus 600 are also stored. The processing device 601, the ROM 602, and the RAM 603 are connected to each other via a bus 604. An input/output (I/O) interface 605 is also connected to bus 604.
Generally, the following devices may be connected to the I/O interface 605: input devices 606 including, for example, a touch screen, touch pad, keyboard, mouse, camera, microphone, accelerometer, gyroscope, etc.; output devices 607 including, for example, a Liquid Crystal Display (LCD), a speaker, a vibrator, and the like; storage 608 including, for example, tape, hard disk, etc.; and a communication device 609. The communication means 609 may allow the electronic device 600 to communicate with other devices wirelessly or by wire to exchange data. While fig. 6 illustrates an electronic device 600 having various means, it is to be understood that not all illustrated means are required to be implemented or provided. More or fewer devices may alternatively be implemented or provided.
In particular, according to an embodiment of the present disclosure, the processes described above with reference to the flowcharts may be implemented as computer software programs. For example, embodiments of the present disclosure include a computer program product comprising a computer program carried on a non-transitory computer readable medium, the computer program containing program code for performing the method illustrated by the flow chart. In such an embodiment, the computer program may be downloaded and installed from a network via the communication means 609, or may be installed from the storage means 608, or may be installed from the ROM 602. The computer program, when executed by the processing device 601, performs the above-described functions defined in the methods of the embodiments of the present disclosure.
It should be noted that the computer readable storage medium of the present disclosure can be a computer readable signal medium or a computer readable storage medium or any combination of the two. A computer readable storage medium may be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the foregoing. More specific examples of the computer readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the present disclosure, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. In contrast, in the present disclosure, a computer readable signal medium may comprise a propagated data signal with computer readable program code embodied therein, either in baseband or as part of a carrier wave. Such a propagated data signal may take many forms, including, but not limited to, electro-magnetic, optical, or any suitable combination thereof. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to: electrical wires, optical cables, RF (radio frequency), etc., or any suitable combination of the foregoing.
In some embodiments, the clients, servers may communicate using any currently known or future developed network Protocol, such as HTTP (hypertext transfer Protocol), and may interconnect with any form or medium of digital data communication (e.g., a communications network). Examples of communication networks include a local area network ("LAN"), a wide area network ("WAN"), the Internet (e.g., the Internet), and peer-to-peer networks (e.g., ad hoc peer-to-peer networks), as well as any currently known or future developed network.
The computer readable medium may be embodied in the electronic device; or may exist separately without being assembled into the electronic device.
The computer readable medium carries one or more programs which, when executed by the electronic device, cause the electronic device to: when an application program runs, acquiring a thread list corresponding to the application program; the thread list comprises at least one thread currently being executed by the application program; acquiring thread information of each thread in the thread list based on a preset rule; and when detecting that at least one of the thread information of each thread meets a preset condition, judging that the memory leakage exists in the application program.
Computer program code for carrying out operations for the present disclosure may be written in any combination of one or more programming languages, including but not limited to an object oriented programming language such as Java, Smalltalk, C + +, and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code may execute entirely on the user's computer, partly on the user's computer, as a stand-alone software package, partly on the user's computer and partly on a remote computer or entirely on the remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any type of network, including a Local Area Network (LAN) or a Wide Area Network (WAN), or the connection may be made to an external computer (for example, through the Internet using an Internet service provider).
The flowchart and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present disclosure. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams and/or flowchart illustration, and combinations of blocks in the block diagrams and/or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
The modules or units described in the embodiments of the present disclosure may be implemented by software or hardware. Wherein the designation of a module or unit does not in some cases constitute a limitation of the unit itself.
The functions described herein above may be performed, at least in part, by one or more hardware logic components. For example, without limitation, exemplary types of hardware logic components that may be used include: field Programmable Gate Arrays (FPGAs), Application Specific Integrated Circuits (ASICs), Application Specific Standard Products (ASSPs), systems on a chip (SOCs), Complex Programmable Logic Devices (CPLDs), and the like.
In the context of this disclosure, a machine-readable medium may be a tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. The machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any suitable combination of the foregoing. More specific examples of a machine-readable storage medium would include an electrical connection based on one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
According to one or more embodiments of the present disclosure, [ example one ] there is provided a memory leak detection method, including:
when an application program runs, acquiring a thread list corresponding to the application program; the thread list comprises at least one thread currently being executed by the application program;
acquiring thread information of each thread in the thread list based on a preset rule;
and when detecting that at least one of the thread information of each thread meets a preset condition, judging that the memory leakage exists in the application program.
In a preferred embodiment of the present disclosure, the method further includes:
acquiring the addresses of the thread execution functions corresponding to the thread information meeting the preset conditions;
determining a first dynamic link library file corresponding to each thread execution function based on the address of each thread execution function;
and determining the function name of each thread execution function with memory leakage based on the address of each thread execution function and each first dynamic link library file.
In a preferred embodiment of the present disclosure, the method further includes:
determining the memory address of the thread stack corresponding to each piece of thread information meeting the preset condition;
and releasing the memory space corresponding to the memory address of each thread stack.
In a preferred embodiment of the present disclosure, the obtaining a thread list corresponding to the application program includes:
traversing at least one thread currently executed by the application program by adopting a preset detection thread at intervals of a preset time interval to obtain the thread list; wherein the detection thread is started when the application program is detected to be started.
In a preferred embodiment of the present disclosure, the obtaining thread information of each thread in the thread list based on a preset rule includes:
generating a target thread descriptor based on the original thread descriptor of the second dynamic link library; the original thread descriptor and the target thread descriptor have the same data structure;
replacing the original thread descriptors corresponding to the threads in the thread list with the target thread descriptors;
and acquiring the thread information of each thread through each target thread descriptor.
In a preferred embodiment of the present disclosure, the thread information includes at least one of a signal stack pointer and a thread ID of the thread;
when it is detected that at least one of the thread information of the threads meets a preset condition, determining that the memory leak exists in the application program, including:
when detecting that a signal stack pointer of at least one thread in the thread information of each thread is empty, judging that the application program has memory leak;
and/or the presence of a gas in the gas,
and when the thread ID of at least one thread in the thread information of each thread is detected to be a preset value, judging that the memory leakage exists in the application program.
In a preferred embodiment of the present disclosure, the determining, based on the address of each thread execution function and each first dynamic link library file, a function name of each thread execution function with a memory leak includes:
acquiring a third dynamic link library file corresponding to each first dynamic link library file;
and acquiring the function name of each thread execution function with leaked memory by adopting the address of each thread execution function, each first dynamic link library file and each third dynamic link library file.
According to one or more embodiments of the present disclosure, [ example two ] there is provided an apparatus of example one, comprising:
the device comprises a first acquisition module, a second acquisition module and a third acquisition module, wherein the first acquisition module is used for acquiring a thread list corresponding to an application program when the application program runs; the thread list comprises at least one thread currently being executed by the application program;
the second acquisition module is used for acquiring the thread information of each thread in the thread list based on a preset rule;
and the judging module is used for judging that the memory leakage exists in the application program when detecting that at least one of the thread information of each thread meets a preset condition.
In a preferred embodiment of the present disclosure, the method further includes:
a third obtaining module, configured to obtain addresses of thread execution functions corresponding to the thread information that meets the preset condition;
the first determining module is used for determining a first dynamic link library file corresponding to each thread execution function based on the address of each thread execution function;
and the second determining module is used for determining the function name of each thread execution function with memory leakage based on the address of each thread execution function and each first dynamic link library file.
In a preferred embodiment of the present disclosure, the method further includes:
a third determining module, configured to determine a memory address of a thread stack corresponding to each piece of thread information that meets the preset condition;
and the releasing module is used for releasing the memory space corresponding to the memory address of each thread stack.
In a preferred embodiment of the present disclosure, the first obtaining module is specifically configured to:
traversing at least one thread currently executed by the application program by adopting a preset detection thread at intervals of a preset time interval to obtain the thread list; wherein the detection thread is started when the application program is detected to be started.
In a preferred embodiment of the present disclosure, the second obtaining module includes:
the generation submodule is used for generating a target thread descriptor based on the original thread descriptor of the second dynamic link library; the original thread descriptor and the target thread descriptor have the same data structure;
a replacing submodule, configured to replace an original thread descriptor corresponding to each thread in the thread list with the target thread descriptor;
and the thread information acquisition submodule is used for acquiring the thread information of each thread through each target thread descriptor.
In a preferred embodiment of the present disclosure, the thread information includes at least one of a signal stack pointer and a thread ID of the thread;
the determination module is specifically configured to:
when detecting that a signal stack pointer of at least one thread in the thread information of each thread is empty, judging that the application program has memory leak;
and when the thread ID of at least one thread in the thread information of each thread is detected to be a preset value, judging that the memory leakage exists in the application program.
In a preferred embodiment of the present disclosure, the second determining module includes:
the file acquisition submodule is used for acquiring a third dynamic link library file corresponding to each first dynamic link library file;
and the function name acquisition submodule is used for acquiring the function name of each thread execution function with leaked memory by adopting the address of each thread execution function, each first dynamic link library file and each third dynamic link library file.
The foregoing description is only exemplary of the preferred embodiments of the disclosure and is illustrative of the principles of the technology employed. It will be appreciated by those skilled in the art that the scope of the disclosure herein is not limited to the particular combination of features described above, but also encompasses other embodiments in which any combination of the features described above or their equivalents does not depart from the spirit of the disclosure. For example, the above features and (but not limited to) the features disclosed in this disclosure having similar functions are replaced with each other to form the technical solution.
Further, while operations are depicted in a particular order, this should not be understood as requiring that such operations be performed in the particular order shown or in sequential order. Under certain circumstances, multitasking and parallel processing may be advantageous. Likewise, while several specific implementation details are included in the above discussion, these should not be construed as limitations on the scope of the disclosure. Certain features that are described in the context of separate embodiments can also be implemented in combination in a single embodiment. Conversely, various features that are described in the context of a single embodiment can also be implemented in multiple embodiments separately or in any suitable subcombination.
Although the subject matter has been described in language specific to structural features and/or methodological acts, it is to be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or acts described above. Rather, the specific features and acts described above are disclosed as example forms of implementing the claims.

Claims (10)

1. A method for detecting a memory leak, comprising:
when an application program runs, acquiring a thread list corresponding to the application program; the thread list comprises at least one thread currently being executed by the application program;
acquiring thread information of each thread in the thread list based on a preset rule;
and when detecting that at least one of the thread information of each thread meets a preset condition, judging that the memory leakage exists in the application program.
2. The method for detecting memory leak according to claim 1, further comprising:
acquiring the addresses of the thread execution functions corresponding to the thread information meeting the preset conditions;
determining a first dynamic link library file corresponding to each thread execution function based on the address of each thread execution function;
and determining the function name of each thread execution function with memory leakage based on the address of each thread execution function and each first dynamic link library file.
3. The method for detecting memory leak according to claim 1 or 2, further comprising:
determining the memory address of the thread stack corresponding to each piece of thread information meeting the preset condition;
and releasing the memory space corresponding to the memory address of each thread stack.
4. The method according to claim 1, wherein the obtaining the thread list corresponding to the application program comprises:
traversing at least one thread currently executed by the application program by adopting a preset detection thread at intervals of a preset time interval to obtain the thread list; wherein the detection thread is started when the application program is detected to be started.
5. The method according to claim 1, wherein the obtaining thread information of each thread in the thread list based on a preset rule includes:
generating a target thread descriptor based on the original thread descriptor of the second dynamic link library; the original thread descriptor and the target thread descriptor have the same data structure;
replacing the original thread descriptors corresponding to the threads in the thread list with the target thread descriptors;
and acquiring the thread information of each thread through each target thread descriptor.
6. The method according to claim 1 or 5, wherein the thread information includes at least one of a signal stack pointer and a thread ID of the thread;
when it is detected that at least one of the thread information of the threads meets a preset condition, determining that the memory leak exists in the application program, including:
when detecting that a signal stack pointer of at least one thread in the thread information of each thread is empty, judging that the application program has memory leak;
and/or the presence of a gas in the gas,
and when the thread ID of at least one thread in the thread information of each thread is detected to be a preset value, judging that the memory leakage exists in the application program.
7. The method according to claim 2, wherein the determining a function name of each thread execution function with the memory leak based on the address of each thread execution function and each first dynamic link library file comprises:
acquiring a third dynamic link library file corresponding to each first dynamic link library file;
and acquiring the function name of each thread execution function with leaked memory by adopting the address of each thread execution function, each first dynamic link library file and each third dynamic link library file.
8. A memory leak detection apparatus, comprising:
the device comprises a first acquisition module, a second acquisition module and a third acquisition module, wherein the first acquisition module is used for acquiring a thread list corresponding to an application program when the application program runs; the thread list comprises at least one thread currently being executed by the application program;
the second acquisition module is used for acquiring the thread information of each thread in the thread list based on a preset rule;
and the judging module is used for judging that the memory leakage exists in the application program when detecting that at least one of the thread information of each thread meets a preset condition.
9. An electronic device, comprising:
a processor, a memory, and a bus;
the bus is used for connecting the processor and the memory;
the memory is used for storing operation instructions;
the processor is configured to execute the method for detecting a memory leak according to any one of claims 1 to 7 by calling the operation instruction.
10. A computer-readable storage medium for storing computer instructions which, when executed on a computer, enable the computer to perform the method for detecting a memory leak of any one of claims 1 to 7.
CN202010600750.8A 2020-06-28 2020-06-28 Memory leak detection method and device, electronic equipment and computer storage medium Pending CN113849394A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010600750.8A CN113849394A (en) 2020-06-28 2020-06-28 Memory leak detection method and device, electronic equipment and computer storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010600750.8A CN113849394A (en) 2020-06-28 2020-06-28 Memory leak detection method and device, electronic equipment and computer storage medium

Publications (1)

Publication Number Publication Date
CN113849394A true CN113849394A (en) 2021-12-28

Family

ID=78972767

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010600750.8A Pending CN113849394A (en) 2020-06-28 2020-06-28 Memory leak detection method and device, electronic equipment and computer storage medium

Country Status (1)

Country Link
CN (1) CN113849394A (en)

Similar Documents

Publication Publication Date Title
CN111090536B (en) Method, device, medium and electronic equipment for acquiring memory leakage information
CN113835992B (en) Memory leakage processing method and device, electronic equipment and computer storage medium
CN110851139B (en) Method and device for checking codes and electronic equipment
CN111625422B (en) Thread monitoring method, thread monitoring device, electronic equipment and computer readable storage medium
CN111309304B (en) Method, device, medium and electronic equipment for generating IDL file
CN111857720B (en) User interface state information generation method and device, electronic equipment and medium
CN112084024B (en) Memory monitoring method, device, medium and electronic equipment
CN113407165B (en) SDK generation and self-upgrade method, device, readable medium and equipment
CN112416303B (en) Software development kit hot repair method and device and electronic equipment
CN112905220B (en) Thermal restoration method, device, equipment and storage medium
CN112306685B (en) Task isolation method, device, electronic equipment and computer readable medium
CN111414308B (en) Application package processing method and application running method and device
CN110888773B (en) Method, device, medium and electronic equipment for acquiring thread identification
CN111538717B (en) Data processing method, device, electronic equipment and computer readable medium
CN113391860B (en) Service request processing method and device, electronic equipment and computer storage medium
CN112379967B (en) Simulator detection method, device, equipment and medium
CN113849394A (en) Memory leak detection method and device, electronic equipment and computer storage medium
CN110764995B (en) Method, device, medium and electronic equipment for detecting file access abnormality
CN111240801A (en) Method, device, medium and electronic equipment for generating heap memory snapshot file
CN111694747B (en) Thread detection method, device, equipment and computer readable medium
CN110489341B (en) Test method and device, storage medium and electronic equipment
CN114296986B (en) Memory leakage positioning method, device, medium and electronic equipment
CN114398233B (en) Load abnormality detection method and device, server and storage medium
CN112650647B (en) Information acquisition method, device, equipment and medium
CN117472425A (en) Resource processing method and device, readable medium and electronic equipment

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
CB02 Change of applicant information
CB02 Change of applicant information

Address after: 100041 B-0035, 2 floor, 3 building, 30 Shixing street, Shijingshan District, Beijing.

Applicant after: Tiktok vision (Beijing) Co.,Ltd.

Address before: 100041 B-0035, 2 floor, 3 building, 30 Shixing street, Shijingshan District, Beijing.

Applicant before: BEIJING BYTEDANCE NETWORK TECHNOLOGY Co.,Ltd.

Address after: 100041 B-0035, 2 floor, 3 building, 30 Shixing street, Shijingshan District, Beijing.

Applicant after: Douyin Vision Co.,Ltd.

Address before: 100041 B-0035, 2 floor, 3 building, 30 Shixing street, Shijingshan District, Beijing.

Applicant before: Tiktok vision (Beijing) Co.,Ltd.