CN111143100A - Memory leak detection method, device, equipment and storage medium - Google Patents
Memory leak detection method, device, equipment and storage medium Download PDFInfo
- Publication number
- CN111143100A CN111143100A CN201911253495.8A CN201911253495A CN111143100A CN 111143100 A CN111143100 A CN 111143100A CN 201911253495 A CN201911253495 A CN 201911253495A CN 111143100 A CN111143100 A CN 111143100A
- Authority
- CN
- China
- Prior art keywords
- detected
- objects
- memory
- garbage
- total set
- 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/07—Responding to the occurrence of a fault, e.g. fault tolerance
- G06F11/0703—Error or fault processing not based on redundancy, i.e. by taking additional measures to deal with the error or fault not making use of redundancy in operation, in hardware, or in data representation
- G06F11/0706—Error or fault processing not based on redundancy, i.e. by taking additional measures to deal with the error or fault not making use of redundancy in operation, in hardware, or in data representation the processing taking place on a specific hardware platform or in a specific software environment
- G06F11/073—Error or fault processing not based on redundancy, i.e. by taking additional measures to deal with the error or fault not making use of redundancy in operation, in hardware, or in data representation the processing taking place on a specific hardware platform or in a specific software environment in a memory management context, e.g. virtual memory or cache management
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Quality & Reliability (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Storage Device Security (AREA)
Abstract
The embodiment of the disclosure judges whether an object to be detected which is not subjected to garbage collection exists in a total set of objects to be detected according to a preset total set of objects to be detected and a subset of objects to be detected which are subjected to garbage collection when a preset memory leakage detection condition is met by monitoring the life cycle of the objects to be detected in an application program and determining the total set of the objects to be detected and the subset of the objects to be detected which are subjected to garbage collection, and then obtains a primary judgment result of whether the memory leakage occurs according to the judgment result. In the embodiment, the risk that the APP rushes out due to memory overflow can be reduced, memory leakage is automatically detected, the learning cost is low, and the analysis efficiency is high.
Description
Technical Field
The present application relates to the field of intelligent terminal technologies, and in particular, to a method, an apparatus, a device, and a storage medium for detecting memory leakage.
Background
In computer science, Memory Leak (Memory Leak) refers to that a Memory is allocated to a process for use, and the process does not release the Memory after using the Memory, so that the allocated Memory cannot be recycled. As the process runs, less and less memory is available in the system, causing the system to crash because there is not enough memory to run the process. The reason for the memory leak may be caused by hidden danger of code written by a developer or system exception.
At present, in order to avoid system paralysis caused by memory leakage, terminal equipment is usually connected with a computer in the running process of an application program, and experienced detection personnel perform memory analysis by using a memory analysis tool, so that the conditions of low analysis efficiency, possibility of missing check in manual check and the like are obviously met.
Disclosure of Invention
To overcome the problems in the related art, the present disclosure provides a memory leak detection method, apparatus, device, and storage medium.
According to a first aspect of the embodiments of the present disclosure, a method for detecting memory leak is provided, where the method includes:
monitoring the life cycle of an object to be detected in an application program, and determining a total set of the object to be detected and a garbage-recovered subset of the object to be detected;
when a preset memory leakage detection condition is met, judging whether the objects to be detected which are not subjected to garbage collection exist in the total set of the objects to be detected or not according to the determined total set of the objects to be detected and the subsets of the objects to be detected which are subjected to garbage collection;
and obtaining a primary judgment result of whether the memory leakage occurs according to the judgment result.
In an optional embodiment, the meeting of the preset memory leak detection condition includes: after the current Activity exits for a specified time; the total set of the objects to be detected is a set of the objects to be detected in the current Activity;
or, the preset memory leak detection condition is met, including: after the application program exits for a specified time; the total set of the objects to be detected is a set of the objects to be detected in the Activity occupying the memory in the application program.
In an optional embodiment, the method further comprises:
when memory leakage is preliminarily judged, calling a garbage collection method to collect garbage, and updating the garbage-collected to-be-detected object subset;
after the recovery is finished, judging whether the objects to be detected which are not subjected to garbage recovery exist in the total set of the objects to be detected according to the updated subsets of the objects to be detected which are subjected to garbage recovery;
and if the object to be detected which is not recovered still exists, obtaining the final judgment result of the memory leakage.
In an optional embodiment, the method further comprises:
after an ultimate judgment result of memory leakage is obtained, a leakage log of an unrecycled object to be detected is exported;
analyzing the leakage log to obtain a path of the object to be detected which is not recovered;
an alert notification including the path is output.
In an alternative embodiment, the output comprises an alert notification for the path, including one or more of:
outputting an alarm notice containing the path in the intelligent terminal;
and sending the alarm notice containing the path to a server associated with the intelligent terminal.
In an optional embodiment, the object to be detected is stored in a weak reference relationship; if the object to be detected referred by the weak reference is garbage-collected, the weak reference is added into a reference queue associated with the weak reference;
the method for judging whether the objects to be detected which are not subjected to garbage collection exist in the total set of the objects to be detected according to the determined total set of the objects to be detected and the subsets of the objects to be detected which are subjected to garbage collection includes the following steps:
and removing the objects to be detected corresponding to the weak references in the reference queue from the total set of the objects to be detected, and judging whether the objects to be detected which are not subjected to garbage recovery exist in the total set of the objects to be detected according to whether the objects to be detected exist in the removed set.
According to a second aspect of the embodiments of the present disclosure, there is provided a memory leak detection apparatus, including:
a periodic listening module configured to: monitoring the life cycle of an object to be detected in an application program, and determining a total set of the object to be detected and a garbage-recovered subset of the object to be detected;
a reclamation determination module configured to: when a preset memory leakage detection condition is met, judging whether the objects to be detected which are not subjected to garbage collection exist in the total set of the objects to be detected or not according to the determined total set of the objects to be detected and the subsets of the objects to be detected which are subjected to garbage collection;
a result obtaining module configured to; and obtaining a primary judgment result of whether the memory leakage occurs according to the judgment result.
In an optional embodiment, the meeting of the preset memory leak detection condition includes: after the current Activity exits for a specified time; the total set of the objects to be detected is a set of the objects to be detected in the current Activity.
In an optional embodiment, the meeting of the preset memory leak detection condition includes: after the application program exits for a specified time; the total set of the objects to be detected is a set of the objects to be detected in the Activity occupying the memory in the application program.
In an optional embodiment, the apparatus further comprises:
a garbage collection module configured to: when memory leakage is preliminarily judged, calling a garbage collection method to collect garbage, and updating the garbage-collected to-be-detected object subset;
the recovery determination module is further configured to: after the recovery is finished, judging whether the objects to be detected which are not subjected to garbage recovery exist in the total set of the objects to be detected according to the updated subsets of the objects to be detected which are subjected to garbage recovery;
the result obtaining module is further configured to: and if the object to be detected which is not recovered still exists, obtaining the final judgment result of the memory leakage.
In an optional embodiment, the apparatus further comprises:
a log export module configured to: after an ultimate judgment result of memory leakage is obtained, a leakage log of an unrecycled object to be detected is exported;
a log analysis module configured to: analyzing the leakage log to obtain a path of the object to be detected which is not recovered;
a notification output module configured to: an alert notification including the path is output.
In an alternative embodiment, the notification output module is configured to:
outputting an alarm notice containing the path in the intelligent terminal; and/or the presence of a gas in the gas,
and sending the alarm notice containing the path to a server associated with the intelligent terminal.
In an optional embodiment, the object to be detected is stored in a weak reference relationship; if the object to be detected referred by the weak reference is garbage-collected, the weak reference is added into a reference queue associated with the weak reference;
the recovery determination module is specifically configured to:
and removing the objects to be detected corresponding to the weak references in the reference queue from the total set of the objects to be detected, and judging whether the objects to be detected which are not subjected to garbage recovery exist in the total set of the objects to be detected according to whether the objects to be detected exist in the removed set.
According to a third aspect of embodiments of the present disclosure, there is provided a computer device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, wherein the processor implements the method as any one of the above when executing the program.
According to a fourth aspect of embodiments of the present disclosure, there is provided a computer readable storage medium, on which a computer program is stored, which program, when executed by a processor, performs the steps of the method as set forth in any one of the above.
The technical scheme provided by the embodiment of the disclosure can have the following beneficial effects:
the embodiment of the disclosure monitors the life cycle of the objects to be detected in the application program, determines the total set of the objects to be detected and the garbage-recovered subset of the objects to be detected, and determines whether the objects to be detected which are not garbage-recovered exist in the total set of the objects to be detected according to the determined garbage-recovered subset of the objects to be detected when a preset memory leakage detection condition is met, and further obtains a primary determination result of whether the memory leakage occurs according to the determination result. In the embodiment, the problem of memory overflow caused by a code style and the like can be solved, the risk of APP rushing out caused by memory overflow is reduced, memory leakage is automatically detected, the learning cost is low, and the analysis efficiency is high.
It is to be understood that both the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the disclosure.
Drawings
The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present disclosure and together with the description, serve to explain the principles of the disclosure.
Fig. 1 is a flow chart illustrating a memory leak detection method according to an exemplary embodiment of the present disclosure.
FIG. 2 is a flow chart illustrating another memory leak detection method according to an example embodiment of the present disclosure.
Fig. 3A-3D are schematic diagrams of a notification interface illustrated by the present disclosure, according to an example embodiment.
Fig. 4 is a block diagram illustrating a memory leak detection apparatus according to an exemplary embodiment of the present disclosure.
Fig. 5 is a schematic structural diagram of a memory leak detection apparatus according to an exemplary embodiment of the present disclosure.
Detailed Description
Reference will now be made in detail to the exemplary embodiments, examples of which are illustrated in the accompanying drawings. When the following description refers to the accompanying drawings, like numbers in different drawings represent the same or similar elements unless otherwise indicated. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present disclosure. Rather, they are merely examples of apparatus and methods consistent with certain aspects of the present disclosure, as detailed in the appended claims.
The terminology used in the present disclosure is for the purpose of describing particular embodiments only and is not intended to be limiting of the disclosure. As used in this disclosure and the appended claims, the singular forms "a," "an," and "the" are intended to include the plural forms as well, unless the context clearly indicates otherwise. It should also be understood that the term "and/or" as used herein refers to and encompasses any and all possible combinations of one or more of the associated listed items.
It is to be understood that although the terms first, second, third, etc. may be used herein to describe various information, such information should not be limited to these terms. These terms are only used to distinguish one type of information from another. For example, first information may also be referred to as second information, and similarly, second information may also be referred to as first information, without departing from the scope of the present disclosure. The word "if" as used herein may be interpreted as "at … …" or "when … …" or "in response to a determination", depending on the context.
In computer science, memory leak refers to that memory is allocated to an object for use, and the memory is not released after the object uses the memory, so that the allocated memory cannot be recycled. As the object runs, less and less memory is available in the system, causing the system to crash because there is not enough memory to run the object. For example, when Activity.ondestroy is called, Activity, as well as the views it refers to and the associated bitmaps should be reclaimed. However, if a background thread holds the reference of the Activity, the memory corresponding to the Activity cannot be recycled. This eventually leads to memory exhaustion and then Crash due to OOM.
In the related art, a memory analysis tool (e.g., mat (memory analyzer tool)) may be used to analyze the memory problem. The use of MAT to analyze the memory problem has some difficulties and low efficiency, and a memory leak problem may be examined and compared many times. Analyzing the memory problem by using MAT cannot automatically locate a key memory leakage place, and developers need to click and check in a multi-item detection item, so that the manual checking may have the possibility of omission, and the developers need to have related knowledge.
In view of this, the embodiment of the present disclosure provides an automatic memory leak detection method, which includes monitoring a life cycle of an object to be detected in an application, determining a total set of the object to be detected and a subset of the object to be detected that has been subjected to garbage collection, determining whether an object to be detected that has not been subjected to garbage collection exists according to the determined total set of the object to be detected and the subset of the object to be detected that has been subjected to garbage collection when a preset memory leak detection condition is satisfied, and further obtaining a primary determination result of whether a memory leak occurs according to the determination result. In the embodiment, the problem of memory overflow of the program caused by the code style and the like can be solved, the risk of APP rushing due to the memory overflow is reduced, memory leakage is automatically detected, the learning cost is low, and the analysis efficiency is high.
The embodiments of the present disclosure will be described below with reference to the accompanying drawings.
As shown in fig. 1, fig. 1 is a flowchart illustrating a memory leak detection method according to an exemplary embodiment of the present disclosure, including the following steps:
in step 102, monitoring the life cycle of the objects to be detected in the application program, and determining a total set of the objects to be detected and a garbage-recovered subset of the objects to be detected;
in step 104, when a preset memory leak detection condition is met, judging whether an object to be detected which is not garbage-recovered exists in the total set of objects to be detected according to the determined total set of objects to be detected and the garbage-recovered subset of objects to be detected;
in step 106, a primary determination result of whether the memory leak occurs is obtained according to the determination result.
The memory leak method provided by this embodiment may be implemented by software, or by a combination of software and hardware, or by hardware, where the related hardware may be composed of two or more physical entities, or may be composed of one physical entity. For example, the method of the present embodiment may be applied to a terminal device with processing capability, where the terminal device may be a device such as a PC, a tablet computer, a notebook computer, and a desktop computer, or a portable device such as a smart phone, a PDA (personal digital Assistant), an e-book reader, or a wearable device such as a smart band and a smart watch.
The object to be detected may be an object in an application. For example, it may be an object in Activity that is in an active state, a paused state, a stopped state, or the like. Taking the current Activity (Activity in an active state) as an example, the object to be detected may be an object in the Activity. Specifically, the object to be detected may be an attribute in Activity, such as a view object and a Context object in a page. An Activity may have many views, such as a button, a text, an input box, etc. Activity is an interface for user interaction that provides a window for a user to complete a related operation. After an Activity is created in development, a layout interface is assigned to the Activity by calling the setContentView (View) method, and the interface is the interface provided for user interaction. The Android system can manage Activity in an Activity stack mode, and the Activity is self-created and destroyed through a life cycle method. Activity is in an active state, at which time Activity is at the top of the stack, which is a visible state, and can interact with a user. When an Activity loses focus, it is either placed on the top of the stack by a new non-full-screen Activity or by a transparent Activity, which transitions to a used state. At this time, Activity only loses the ability of interacting with the user, all state information and member variables thereof still exist, and the Activity can be recycled by the system only under the condition that the memory of the system is in shortage. When one Activity is completely covered by another, the covered Activity enters a Stopped state, where it is no longer visible, but maintains all of its state information and its member variables as in the used state. When Activity is reclaimed by the system, Activity is in the Killed state.
The life cycle of the object to be detected is a process, for example, a typical life cycle is a normal life cycle process in which Activity experiences are created, run, stopped, destroyed and the like under the condition that a user participates. In one embodiment, the lifecycle of the object to be detected may be monitored by invoking a specified monitoring activity method. For example, one may first start with the install of LeakCanary; for the first time, ActityRefWatcher is created and watchActivities (watch Activities method) is called to listen for the Activity's lifecycle.
In the process of monitoring the life cycle of the objects to be detected in the application program, the total set of the objects to be detected and the garbage-recovered subset of the objects to be detected can be determined. The total set of objects to be detected comprises all the objects to be detected in the application program. The garbage-recovered subset of the objects to be detected comprises the garbage-recovered objects to be detected in the application program. The subset of objects to be detected that has been garbage collected is a subset of the total set of objects to be detected. The total set of objects to be detected may be determined by the target desired to be detected.
In one example, the objects to be detected are stored in a weak reference relationship. The objects to be detected that are referenced by the weak references, which may be added to the reference queue associated therewith, are garbage collected. To this end, the reference queue may be utilized to characterize a subset of objects to be detected that have been garbage collected.
When the weak reference and the reference queue ReferenceQueue are used jointly, if an object held by the weak reference is garbage-collected, the Java virtual machine can add the weak reference to the reference queue associated with the weak reference. That is, an Activity object held by keyedweakference, if garbage collected, is added to the reference queue.
In the embodiment of the disclosure, the weak reference relationship is used for storing the object to be detected, so that strong dependence caused by strong reference can be avoided.
The preset memory leak detection condition may be a preset condition for triggering detection of a memory leak. In one example, the meeting of the preset memory leak detection condition includes: after the current Activity exits for a specified time; the total set of the objects to be detected is a set of the objects to be detected in the current Activity. The current Activity may be an Activity that is currently in an active state. For example, any object in the current Activity can be the object to be detected.
The embodiment can detect that the current Activity will not cause memory leak when the current Activity exits.
In another example, the meeting of the preset memory leak detection condition includes: after the application program exits for a specified time; the total set of the objects to be detected is a set of the objects to be detected in the Activity occupying the memory in the application program. The Activity occupying the memory in the application program may include an Activity currently in an active state, and may also include an Activity in a suspended state, a stopped state, and the like.
The embodiment can detect that the application program can not cause memory leak when the application program exits.
The specified time may be a preset time for garbage collection, and may be 5s, for example. It should be understood that the preset memory leak detection condition is only an example, and other existing or future conditions for triggering the memory leak determination may be applicable to the present disclosure and all should be included in the protection scope of the present disclosure.
When the preset memory leak detection condition is met, whether the object to be detected which is not subjected to garbage collection exists in the object to be detected collection can be judged according to the determined object to be detected collection and the garbage-collected object to be detected subset. And further obtaining a primary judgment result of whether the memory leakage occurs according to the judgment result. For example, if an object to be detected which is not subjected to garbage collection exists, a primary judgment result of memory leakage is obtained; and if the object to be detected which is not subjected to garbage collection does not exist, obtaining a primary judgment result that the memory leakage does not occur. Because the total set of the objects to be detected represents the total set of the objects to be detected, and the subset of the objects to be detected represents the set of the objects to be detected which are garbage-recovered, for this reason, the two sets can be used to determine whether the objects to be detected which are not garbage-recovered exist. For example, when Activity is destroyed, an inactivydestroyed method of lifecycallbacks may be called, and this time, the inactivydestroyed method may be called to analyze whether there is a memory leak, and the inactivydestroyed method may call back refwatch.
In an example, the determining, according to the determined total set of the objects to be detected and the subset of the objects to be detected that have been garbage-collected, whether there are objects to be detected that have not been garbage-collected in the total set of the objects to be detected may include: and removing the objects to be detected corresponding to the weak references in the reference queue from the total set of the objects to be detected, and judging whether the objects to be detected which are not subjected to garbage recovery exist in the total set of the objects to be detected according to whether the objects to be detected exist in the removed set.
This embodiment determines whether there are objects to be detected that are not garbage collected by removing a subset from the total set.
For example, an ensureGone method may be adopted to remove a reference that has been recovered first, determine a weak reference, that is, whether the current Activity is recovered, and directly return a memory leak result if there is an object to be detected that is not recovered.
In an optional embodiment, to improve the leak determination accuracy, the method further includes:
when memory leakage is preliminarily judged, calling a garbage collection method to collect garbage, and updating the garbage-collected to-be-detected object subset;
after the recovery is finished, judging whether the objects to be detected which are not subjected to garbage recovery exist in the total set of the objects to be detected according to the updated subsets of the objects to be detected which are subjected to garbage recovery;
and if the object to be detected which is not recovered still exists, obtaining the final judgment result of the memory leakage.
In this embodiment, the problem of memory leakage is primarily solved by calling a garbage collection method, and if there are still objects to be detected that are not collected, a final determination result of memory leakage is obtained, which further proves that memory leakage occurs.
For example, after Activity ontorsony Ts (T may be 5), whether the weak reference of the current Activity is reclaimed or not is detected, if the weak reference is reclaimed, it is proved that no memory leak occurs, and if the weak reference is not reclaimed, a gctrigger. rungc (garbage reclamation method) is called to invoke a garbage reclamation machine to reclaim the weak reference. And detecting whether the weak reference of Activity is recycled or not, if so, proving that the memory leak does not occur, and if not, determining that the memory leak occurs.
Further, when memory leakage occurs, an alarm notification can be output. If so, outputting an alarm notification after obtaining the final judgment result of the memory leak. Illustratively, the method further comprises: after an ultimate judgment result of memory leakage is obtained, a leakage log of an unrecycled object to be detected is exported; analyzing the leakage log to obtain a path of the object to be detected which is not recovered; an alert notification including the path is output.
The leak log may be a log related to the leak. For example, hprof. For example, a dump heap may be used to generate hprof, a path of an object to be detected that is not recovered, which is referred to as a leakage path for short, may be found by analyzing a hprof file, and then an alarm notification including the path is output. E.g., sent to a notification bar, and/or output log information of the leak path, etc.
Illustratively, the output comprises an alert notification for the path, including one or more of:
outputting an alarm notice containing the path in the intelligent terminal;
and sending the alarm notice containing the path to a server associated with the intelligent terminal.
And the server side associated with the intelligent terminal can analyze according to the alarm notification.
The various technical features in the above embodiments can be arbitrarily combined, so long as there is no conflict or contradiction between the combinations of the features, but the combination is limited by the space and is not described one by one, and therefore, any combination of the various technical features in the above embodiments also belongs to the scope disclosed in the present specification.
One combination is exemplified.
As shown in fig. 2, fig. 2 is a flowchart illustrating another memory leak detection method according to an exemplary embodiment of the present disclosure. The life cycle of Activity is monitored. At the time of Activity ontestory, a corresponding Refrence (Weak reference) and RefreneQueue (reference queue) are created and a background process is started to detect. And after a period of time, reading from the RefreneQueue, and if the Refrence of the corresponding Activity is not read, preliminarily judging that memory leakage possibly occurs. At this time, a garbage collection method can be called to collect garbage, and after a period of time, the garbage is read again to judge whether the object is collected or not. If the reference of the corresponding Activity is not read from the referencequeue, it can be determined that the memory leak occurs. After a memory leak occurs, the dump tread memory generates hprof. For example, a memory file is analyzed by a HeapAnalyzer, and the shortest strong reference path to the GC root is calculated to obtain a leakage path (e.g., using haha library analysis). And detecting the position of code memory leakage, transmitting the reference chain so as to display the alarm result in a chain mode, and alarming in the modes of message notification, log output and the like.
A specific example is illustrated. The MainActivity has memory leakage because a non-static internal class LeakThread holds the reference of the external class MainActivity, and time-consuming operation is performed in the LeakThread, so that the MainActivity cannot be released. After refwatch is obtained and its watch method is called, the parameter of watch method is the object to be monitored. Of course, the onDestroy method is redundant in this example, because leak cancer, when calling the install method, launches an activitrerwatcher class that automatically monitors whether a memory leak occurs after the Activity executes the onDestroy method. When the program is run, an application icon named leak is generated on the interface. And then continuously switching the horizontal and vertical screens, and flashing a prompt box at the moment, wherein the prompt contents are as follows: "Dumping memoryapp wire freeze. Brrrr. For a few moments, the memory leak information is revealed by the Notification. As shown in fig. 3A. The Notification indicates that the MainActivity has a memory leak, and the leaked memory is 787B. Clicking on the Notification can enter the memory leak detail page, and in addition, can enter the memory leak detail page through a list interface of a leak application, which is shown in fig. 3B. The memory leak detail page is shown in FIG. 3C. Clicking on the plus sign (+) can look at the package name where the particular class is located. The entire detail is a reference chain: the inside class, leak thread, of MainActivity refers to this $0 of leak thread, the meaning of this $0 is a reference to the outside class where the inside class automatically retains, and this outside class is an example of MainActivity given in the last row of detail, which will result in MainActivity not being able to be garbage collected by GC, thus resulting in memory leakage. In addition, the heap dump (hprof file) and info information can be shared out, as shown in fig. 3D.
The embodiment aims at the fully-automatic memory leak check of the Android Activity component. Some behaviors can be customized (number of dump files and leak objects, custom exceptions, custom processing of analysis results, etc.). The cost of integration and use to the Android project is low, development and testing personnel do not need to use tools to analyze the hprof file, and friendly interface display and notification are realized.
While, for purposes of simplicity of explanation, the foregoing method embodiments have been described as a series of acts or combination of acts, it will be appreciated by those skilled in the art that the present disclosure is not limited by the order of acts, as some steps may, in accordance with the present disclosure, occur in other orders and concurrently.
Further, those skilled in the art should also appreciate that the embodiments described in the specification are exemplary embodiments and that acts and modules referred to are not necessarily required by the disclosure.
Corresponding to the embodiment of the memory leak detection method, the disclosure also provides embodiments of a memory leak detection device, equipment applied by the device, and a storage medium.
As shown in fig. 4, fig. 4 is a block diagram of a memory leak detection apparatus according to an exemplary embodiment of the present disclosure, the apparatus including:
a periodic listening module 42 configured to: monitoring the life cycle of an object to be detected in an application program, and determining a total set of the object to be detected and a garbage-recovered subset of the object to be detected;
a reclamation determination module 44 configured to: when a preset memory leakage detection condition is met, judging whether the objects to be detected which are not subjected to garbage collection exist in the total set of the objects to be detected or not according to the determined total set of the objects to be detected and the subsets of the objects to be detected which are subjected to garbage collection;
a result obtaining module 46 configured to; and obtaining a primary judgment result of whether the memory leakage occurs according to the judgment result.
In an optional embodiment, the meeting of the preset memory leak detection condition includes: after the current Activity exits for a specified time; the total set of the objects to be detected is a set of the objects to be detected in the current Activity.
In an optional embodiment, the meeting of the preset memory leak detection condition includes: after the application program exits for a specified time; the total set of the objects to be detected is a set of the objects to be detected in the Activity occupying the memory in the application program.
In an alternative embodiment, the device further comprises (not shown in fig. 4):
a garbage collection module configured to: when memory leakage is preliminarily judged, calling a garbage collection method to collect garbage, and updating the garbage-collected to-be-detected object subset;
the recycling determination module 44 is further configured to: after the recovery is finished, judging whether the objects to be detected which are not subjected to garbage recovery exist in the total set of the objects to be detected according to the updated subsets of the objects to be detected which are subjected to garbage recovery;
the result obtaining module 46 is further configured to: and if the object to be detected which is not recovered still exists, obtaining the final judgment result of the memory leakage.
In an alternative embodiment, the device further comprises (not shown in fig. 4):
a log export module configured to: after an ultimate judgment result of memory leakage is obtained, a leakage log of an unrecycled object to be detected is exported;
a log analysis module configured to: analyzing the leakage log to obtain a path of the object to be detected which is not recovered;
a notification output module configured to: an alert notification including the path is output.
In an alternative embodiment, the notification output module is configured to:
outputting an alarm notice containing the path in the intelligent terminal; and/or the presence of a gas in the gas,
and sending the alarm notice containing the path to a server associated with the intelligent terminal.
In an optional embodiment, the object to be detected is stored in a weak reference relationship; if the object to be detected referred by the weak reference is garbage-collected, the weak reference is added into a reference queue associated with the weak reference;
the recycling determination module 44 is specifically configured to:
and removing the objects to be detected corresponding to the weak references in the reference queue from the total set of the objects to be detected, and judging whether the objects to be detected which are not subjected to garbage recovery exist in the total set of the objects to be detected according to whether the objects to be detected exist in the removed set.
The specific details of the implementation process of the functions and actions of each module in the device are referred to the implementation process of the corresponding step in the method, and are not described herein again.
For the device embodiments, since they substantially correspond to the method embodiments, reference may be made to the partial description of the method embodiments for relevant points. The above-described embodiments of the apparatus are merely illustrative, wherein the modules described as separate parts may or may not be physically separate, and the parts displayed as modules may or may not be physical modules, may be located in one place, or may be distributed on a plurality of network modules. Some or all of the modules can be selected according to actual needs to achieve the purpose of the disclosed solution. One of ordinary skill in the art can understand and implement it without inventive effort.
Accordingly, the present disclosure also provides a computer-readable storage medium having stored thereon a computer program which, when executed by a processor, performs the steps of any of the methods described above.
The present disclosure may take the form of a computer program product embodied on one or more storage media including, but not limited to, disk storage, CD-ROM, optical storage, and the like, having program code embodied therein. Computer-usable storage media include permanent and non-permanent, removable and non-removable media, and information storage may be implemented by any method or technology. The information may be computer readable instructions, data structures, modules of a program, or other data. Examples of the storage medium of the computer include, but are not limited to: phase change memory (PRAM), Static Random Access Memory (SRAM), Dynamic Random Access Memory (DRAM), other types of Random Access Memory (RAM), Read Only Memory (ROM), Electrically Erasable Programmable Read Only Memory (EEPROM), flash memory or other memory technologies, compact disc read only memory (CD-ROM), Digital Versatile Discs (DVD) or other optical storage, magnetic tape storage or other magnetic storage devices, or any other non-transmission medium, may be used to store information that may be accessed by a computing device.
Accordingly, an embodiment of the present disclosure provides a computer device, where the device includes a memory, a processor, and a computer program stored in the memory and executable on the processor, where the processor implements any of the memory leak detection methods when executing the program.
Fig. 5 is a schematic structural diagram illustrating a memory leak detection apparatus 500 according to an exemplary embodiment. For example, the apparatus 500 may be a user device, which may be embodied as a mobile phone, a computer, a digital broadcast terminal, a messaging device, a game console, a tablet device, a medical device, a fitness device, a personal digital assistant, a wearable device such as a smart watch, smart glasses, a smart bracelet, a smart running shoe, and the like.
Referring to fig. 5, the apparatus 500 may include one or more of the following components: processing component 502, memory 504, power component 506, multimedia component 508, audio component 510, input/output (I/O) interface 512, sensor component 514, and communication component 516.
The processing component 502 generally controls overall operation of the device 500, such as operations associated with display, telephone calls, data communications, camera operations, and recording operations. The processing components 502 may include one or more processors 520 to execute instructions to perform all or a portion of the steps of the methods described above. Further, the processing component 502 can include one or more modules that facilitate interaction between the processing component 502 and other components. For example, the processing component 502 can include a multimedia module to facilitate interaction between the multimedia component 508 and the processing component 502.
The memory 504 is configured to store various types of data to support operation at the device 500. Examples of such data include instructions for any application or method operating on device 500, contact data, phonebook data, messages, pictures, videos, and so forth. The memory 504 may be implemented by any type or combination of volatile or non-volatile memory devices such as Static Random Access Memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic or optical disks.
The power supply component 506 provides power to the various components of the device 500. The power components 506 may include a power management system, one or more power supplies, and other components associated with generating, managing, and distributing power for the apparatus 500.
The multimedia component 508 includes a screen that provides an output interface between the device 500 and a user. In some embodiments, the screen may include a Liquid Crystal Display (LCD) and a Touch Panel (TP). If the screen includes a touch panel, the screen may be implemented as a touch screen to receive an input signal from a user. The touch panel includes one or more touch sensors to sense touch, slide, and gestures on the touch panel. The touch sensor may not only sense the boundary of the touch or slide action but also detect the duration and pressure associated with the touch or slide operation. In some embodiments, the multimedia component 508 includes a front facing camera and/or a rear facing camera. The front-facing camera and/or the rear-facing camera may receive external multimedia data when the device 500 is in an operating mode, such as a shooting mode or a video mode. Each front camera and rear camera may be a fixed optical lens system or have a focal length and optical zoom capability.
The audio component 510 is configured to output and/or input audio signals. For example, audio component 510 includes a Microphone (MIC) configured to receive external audio signals when apparatus 500 is in an operating mode, such as a call mode, a recording mode, and a voice recognition mode. The received audio signals may further be stored in the memory 504 or transmitted via the communication component 516. In some embodiments, audio component 510 further includes a speaker for outputting audio signals.
The I/O interface 512 provides an interface between the processing component 502 and peripheral interface modules, which may be keyboards, click wheels, buttons, etc. These buttons may include, but are not limited to: a home button, a volume button, a start button, and a lock button.
The sensor assembly 514 includes one or more sensors for providing various aspects of status assessment for the device 500. For example, the sensor assembly 514 may detect an open/closed state of the device 500, the relative positioning of the components, such as a display and keypad of the apparatus 500, the sensor assembly 514 may also detect a change in position of the apparatus 500 or a component of the apparatus 500, the presence or absence of user contact with the apparatus 500, orientation or acceleration/deceleration of the apparatus 500, and a change in temperature of the apparatus 500. The sensor assembly 514 may include a proximity sensor configured to detect the presence of a nearby object without any physical contact. The sensor assembly 514 may also include a light sensor, such as a CMOS or CCD image sensor, for use in imaging applications. In some embodiments, the sensor assembly 514 may also include an acceleration sensor, a gyroscope sensor, a magnetic sensor, a pressure sensor, or a temperature sensor.
The communication component 516 is configured to facilitate communication between the apparatus 500 and other devices in a wired or wireless manner. The apparatus 500 may access a wireless network based on a communication standard, such as WiFi, 2G or 3G, 4G LTE, 5G NR, or a combination thereof. In an exemplary embodiment, the communication component 516 receives a broadcast signal or broadcast related information from an external broadcast management system via a broadcast channel. In an exemplary embodiment, the aforementioned communication component 516 further includes a Near Field Communication (NFC) module to facilitate short-range communications. For example, the NFC module may be implemented based on Radio Frequency Identification (RFID) technology, infrared data association (IrDA) technology, Ultra Wideband (UWB) technology, Bluetooth (BT) technology, and other technologies.
In an exemplary embodiment, the apparatus 500 may be implemented by one or more Application Specific Integrated Circuits (ASICs), Digital Signal Processors (DSPs), Digital Signal Processing Devices (DSPDs), Programmable Logic Devices (PLDs), Field Programmable Gate Arrays (FPGAs), controllers, micro-controllers, microprocessors or other electronic components for performing the above-described memory leak detection methods.
In an exemplary embodiment, there is also provided a non-transitory computer readable storage medium, such as the memory 504 including instructions that, when executed by the processor 520 of the apparatus 500, enable the apparatus 500 to perform a memory leak detection method, the method including:
monitoring the life cycle of an object to be detected in an application program, and determining a total set of the object to be detected and a garbage-recovered subset of the object to be detected;
when a preset memory leakage detection condition is met, judging whether the objects to be detected which are not subjected to garbage collection exist in the total set of the objects to be detected or not according to the determined total set of the objects to be detected and the subsets of the objects to be detected which are subjected to garbage collection;
and obtaining a primary judgment result of whether the memory leakage occurs according to the judgment result.
The non-transitory computer readable storage medium may be a ROM, a Random Access Memory (RAM), a CD-ROM, a magnetic tape, a floppy disk, an optical data storage device, and the like.
Other embodiments of the disclosure will be apparent to those skilled in the art from consideration of the specification and practice of the disclosure disclosed herein. This disclosure is intended to cover any variations, uses, or adaptations of the disclosure following, in general, the principles of the disclosure and including such departures from the present disclosure as come within known or customary practice within the art to which the disclosure pertains. It is intended that the specification and examples be considered as exemplary only, with a true scope and spirit of the disclosure being indicated by the following claims.
It will be understood that the present disclosure is not limited to the precise arrangements described above and shown in the drawings and that various modifications and changes may be made without departing from the scope thereof. The scope of the present disclosure is limited only by the appended claims.
Claims (14)
1. A method for memory leak detection, the method comprising:
monitoring the life cycle of an object to be detected in an application program, and determining a total set of the object to be detected and a garbage-recovered subset of the object to be detected;
when a preset memory leakage detection condition is met, judging whether the objects to be detected which are not subjected to garbage collection exist in the total set of the objects to be detected or not according to the determined total set of the objects to be detected and the subsets of the objects to be detected which are subjected to garbage collection;
and obtaining a primary judgment result of whether the memory leakage occurs according to the judgment result.
2. The method according to claim 1, wherein the predetermined memory leak detection condition is satisfied, and comprises: after the current Activity exits for a specified time; the total set of the objects to be detected is a set of the objects to be detected in the current Activity;
or, the preset memory leak detection condition is met, including: after the application program exits for a specified time; the total set of the objects to be detected is a set of the objects to be detected in the Activity occupying the memory in the application program.
3. The method of claim 1, further comprising:
when memory leakage is preliminarily judged, calling a garbage collection method to collect garbage, and updating the garbage-collected to-be-detected object subset;
after the recovery is finished, judging whether the objects to be detected which are not subjected to garbage recovery exist in the total set of the objects to be detected according to the updated subsets of the objects to be detected which are subjected to garbage recovery;
and if the object to be detected which is not recovered still exists, obtaining the final judgment result of the memory leakage.
4. The method of claim 3, further comprising:
after an ultimate judgment result of memory leakage is obtained, a leakage log of an unrecycled object to be detected is exported;
analyzing the leakage log to obtain a path of the object to be detected which is not recovered;
an alert notification including the path is output.
5. The method of claim 4, wherein the output includes an alert notification for the path, including one or more of:
outputting an alarm notice containing the path in the intelligent terminal;
and sending the alarm notice containing the path to a server associated with the intelligent terminal.
6. The method according to any one of claims 1 to 5, characterized in that the objects to be detected are stored in a weak reference relationship; if the object to be detected referred by the weak reference is garbage-collected, the weak reference is added into a reference queue associated with the weak reference;
the method for judging whether the objects to be detected which are not subjected to garbage collection exist in the total set of the objects to be detected according to the determined total set of the objects to be detected and the subsets of the objects to be detected which are subjected to garbage collection includes the following steps:
and removing the objects to be detected corresponding to the weak references in the reference queue from the total set of the objects to be detected, and judging whether the objects to be detected which are not subjected to garbage recovery exist in the total set of the objects to be detected according to whether the objects to be detected exist in the removed set.
7. A memory leak detection apparatus, the apparatus comprising:
a periodic listening module configured to: monitoring the life cycle of an object to be detected in an application program, and determining a total set of the object to be detected and a garbage-recovered subset of the object to be detected;
a reclamation determination module configured to: when a preset memory leakage detection condition is met, judging whether the objects to be detected which are not subjected to garbage collection exist in the total set of the objects to be detected or not according to the determined total set of the objects to be detected and the subsets of the objects to be detected which are subjected to garbage collection;
a result obtaining module configured to; and obtaining a primary judgment result of whether the memory leakage occurs according to the judgment result.
8. The apparatus of claim 7, wherein the predetermined memory leak detection condition is satisfied and comprises: after the current Activity exits for a specified time; the total set of the objects to be detected is a set of the objects to be detected in the current Activity;
or, the preset memory leak detection condition is met, including: after the application program exits for a specified time; the total set of the objects to be detected is a set of the objects to be detected in the Activity occupying the memory in the application program.
9. The apparatus of claim 7, further comprising:
a garbage collection module configured to: when memory leakage is preliminarily judged, calling a garbage collection method to collect garbage, and updating the garbage-collected to-be-detected object subset;
the recovery determination module is further configured to: after the recovery is finished, judging whether the objects to be detected which are not subjected to garbage recovery exist in the total set of the objects to be detected according to the updated subsets of the objects to be detected which are subjected to garbage recovery;
the result obtaining module is further configured to: and if the object to be detected which is not recovered still exists, obtaining the final judgment result of the memory leakage.
10. The apparatus of claim 9, further comprising:
a log export module configured to: after an ultimate judgment result of memory leakage is obtained, a leakage log of an unrecycled object to be detected is exported;
a log analysis module configured to: analyzing the leakage log to obtain a path of the object to be detected which is not recovered;
a notification output module configured to: an alert notification including the path is output.
11. The apparatus of claim 10, wherein the notification output module is configured to:
outputting an alarm notice containing the path in the intelligent terminal; and/or the presence of a gas in the gas,
and sending the alarm notice containing the path to a server associated with the intelligent terminal.
12. The apparatus according to any one of claims 7 to 11, wherein the objects to be detected are stored in a weak reference relationship; if the object to be detected referred by the weak reference is garbage-collected, the weak reference is added into a reference queue associated with the weak reference;
the recovery determination module is specifically configured to:
and removing the objects to be detected corresponding to the weak references in the reference queue from the total set of the objects to be detected, and judging whether the objects to be detected which are not subjected to garbage recovery exist in the total set of the objects to be detected according to whether the objects to be detected exist in the removed set.
13. A computer device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, wherein the processor implements the method of any one of claims 1 to 6 when executing the program.
14. A computer-readable storage medium, on which a computer program is stored which, when being executed by a processor, carries out the steps of the method according to any one of claims 1 to 6.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201911253495.8A CN111143100A (en) | 2019-12-09 | 2019-12-09 | Memory leak detection method, device, equipment and storage medium |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201911253495.8A CN111143100A (en) | 2019-12-09 | 2019-12-09 | Memory leak detection method, device, equipment and storage medium |
Publications (1)
Publication Number | Publication Date |
---|---|
CN111143100A true CN111143100A (en) | 2020-05-12 |
Family
ID=70518027
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201911253495.8A Pending CN111143100A (en) | 2019-12-09 | 2019-12-09 | Memory leak detection method, device, equipment and storage medium |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN111143100A (en) |
Cited By (6)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN111984541A (en) * | 2020-08-31 | 2020-11-24 | 北京达佳互联信息技术有限公司 | Memory leak detection method and device and terminal |
CN112463626A (en) * | 2020-12-10 | 2021-03-09 | 网易(杭州)网络有限公司 | Memory leak positioning method and device, computer equipment and storage medium |
CN112817844A (en) * | 2021-01-25 | 2021-05-18 | 北京小米移动软件有限公司 | Background process residence test method, device, equipment and storage medium |
CN112925526A (en) * | 2021-03-30 | 2021-06-08 | 平安科技(深圳)有限公司 | Leakcanar-based compiling method, device, equipment and medium |
CN113791947A (en) * | 2021-09-02 | 2021-12-14 | 北京字节跳动网络技术有限公司 | Information monitoring method, information monitoring device, electronic equipment and readable storage medium |
WO2023284793A1 (en) * | 2021-07-16 | 2023-01-19 | 维沃移动通信有限公司 | Memory leak processing method and apparatus, and electronic device |
Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
WO2018103216A1 (en) * | 2016-12-08 | 2018-06-14 | 武汉斗鱼网络科技有限公司 | Method and apparatus for detecting memory leakages |
CN108664390A (en) * | 2017-03-31 | 2018-10-16 | 华为技术有限公司 | A kind of method for detecting memory leakage and equipment |
CN109558308A (en) * | 2018-09-29 | 2019-04-02 | 中国平安人寿保险股份有限公司 | Application program method for detecting memory leakage, device, electronic equipment and storage medium |
-
2019
- 2019-12-09 CN CN201911253495.8A patent/CN111143100A/en active Pending
Patent Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
WO2018103216A1 (en) * | 2016-12-08 | 2018-06-14 | 武汉斗鱼网络科技有限公司 | Method and apparatus for detecting memory leakages |
CN108664390A (en) * | 2017-03-31 | 2018-10-16 | 华为技术有限公司 | A kind of method for detecting memory leakage and equipment |
CN109558308A (en) * | 2018-09-29 | 2019-04-02 | 中国平安人寿保险股份有限公司 | Application program method for detecting memory leakage, device, electronic equipment and storage medium |
Cited By (8)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN111984541A (en) * | 2020-08-31 | 2020-11-24 | 北京达佳互联信息技术有限公司 | Memory leak detection method and device and terminal |
CN112463626A (en) * | 2020-12-10 | 2021-03-09 | 网易(杭州)网络有限公司 | Memory leak positioning method and device, computer equipment and storage medium |
CN112463626B (en) * | 2020-12-10 | 2023-07-11 | 网易(杭州)网络有限公司 | Memory leakage positioning method and device, computer equipment and storage medium |
CN112817844A (en) * | 2021-01-25 | 2021-05-18 | 北京小米移动软件有限公司 | Background process residence test method, device, equipment and storage medium |
CN112925526A (en) * | 2021-03-30 | 2021-06-08 | 平安科技(深圳)有限公司 | Leakcanar-based compiling method, device, equipment and medium |
CN112925526B (en) * | 2021-03-30 | 2023-09-19 | 平安科技(深圳)有限公司 | Compiling method, device, equipment and medium based on Leakcanary |
WO2023284793A1 (en) * | 2021-07-16 | 2023-01-19 | 维沃移动通信有限公司 | Memory leak processing method and apparatus, and electronic device |
CN113791947A (en) * | 2021-09-02 | 2021-12-14 | 北京字节跳动网络技术有限公司 | Information monitoring method, information monitoring device, electronic equipment and readable storage medium |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN111143100A (en) | Memory leak detection method, device, equipment and storage medium | |
CN107992248B (en) | Message display method and device | |
US20170139944A1 (en) | Method, device and computer-readable medium for monitoring a file in system partition | |
CN107659717B (en) | State detection method, device and storage medium | |
CN104598128A (en) | Method and system for recommending solutions on basis of operation behaviors of users | |
CN115905137A (en) | File descriptor leakage monitoring method, device, equipment and storage medium | |
CN110889691A (en) | Information display method, device and equipment | |
CN111966491B (en) | Method for counting occupied memory and terminal equipment | |
CN104932970A (en) | Monitoring method and device of memory leakage | |
CN109634762B (en) | Data recovery method and device, electronic equipment and storage medium | |
CN108965606B (en) | Method and device for determining ambient temperature | |
CN116055617B (en) | Message notification method, electronic device, computer storage medium and chip system | |
CN105824513A (en) | Message processing method and apparatus | |
CN117149476A (en) | Fault reporting method and related device | |
CN105553772B (en) | Flow monitoring method and device | |
CN114780334A (en) | Power consumption monitoring method, power consumption monitoring device and storage medium | |
CN114218118A (en) | Code testing method and device, electronic equipment and storage medium | |
CN113961427A (en) | System memory analysis method and electronic equipment | |
US20220342739A1 (en) | Method for determining faulty computing core in multi-core processor and electronic device | |
CN114461360A (en) | Thread control method, thread control device, terminal and storage medium | |
CN113407879A (en) | Data reporting method, device and readable medium | |
CN115729754A (en) | Equipment testing method and device, electronic equipment and storage medium | |
CN116701320B (en) | Log generation method and related device | |
CN106293629A (en) | Storehouse acquisition methods and device | |
CN106598811B (en) | Anomalous event processing method, device and terminal |
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 |