WO2018103216A1 - Method and apparatus for detecting memory leakages - Google Patents

Method and apparatus for detecting memory leakages Download PDF

Info

Publication number
WO2018103216A1
WO2018103216A1 PCT/CN2017/075737 CN2017075737W WO2018103216A1 WO 2018103216 A1 WO2018103216 A1 WO 2018103216A1 CN 2017075737 W CN2017075737 W CN 2017075737W WO 2018103216 A1 WO2018103216 A1 WO 2018103216A1
Authority
WO
WIPO (PCT)
Prior art keywords
hprof
memory
references
target application
file
Prior art date
Application number
PCT/CN2017/075737
Other languages
French (fr)
Chinese (zh)
Inventor
丁萍
Original Assignee
武汉斗鱼网络科技有限公司
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 武汉斗鱼网络科技有限公司 filed Critical 武汉斗鱼网络科技有限公司
Publication of WO2018103216A1 publication Critical patent/WO2018103216A1/en

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/3636Software debugging by tracing the execution of the program

Definitions

  • the present invention relates to the field of computer technologies, and in particular, to a method and apparatus for detecting a memory leak.
  • Memory leak refers to the function module continuously applying for memory space. When this function is finished, the memory space requested by the function cannot be recovered. The appearance of a memory leak will cause the memory to rise, eventually causing the system to force the application to end and free up memory. Forcibly ending the application causes the application to exit abnormally, reducing the user experience.
  • Embodiments of the present invention provide a method and apparatus for detecting a memory leak for implementing a technical effect of automatically detecting a memory leak.
  • the present invention provides a method for detecting a memory leak, comprising:
  • the memory detection module monitors each job activity of the target application
  • a reference chain that causes a memory leak is determined from one or more of the reference chains.
  • determining, according to each of the hprof files, a reference relationship of the reference stored in the hprof file, determining a reference chain of each of the references including:
  • the shortest path is the reference chain.
  • determining a reference chain that causes a memory leak from one or more of the reference chains including:
  • the reference chain is determined to be the reference chain that causes a memory leak.
  • the method before dumping the one or more references that are not cleared to one or more binary dump file hprof files of the target application, the method further includes:
  • the method further includes:
  • the report file is sent to a server associated with the target application.
  • the code corresponding to the memory detection module is inserted in the code of the target application, and the code of the memory detection module is in the same level as the code of the target application.
  • the method further includes:
  • the target application is executed in a main thread, and the memory detection module executes in a memory detection thread different from the main thread.
  • the present invention provides a memory leak detecting apparatus, including:
  • a monitoring unit configured to monitor each job activity of the target application
  • a first determining unit configured to determine that the target application has a memory leak when monitoring that any of the activities exits and one or more references of the activity are not cleared;
  • a dumping unit configured to dump the one or more references that are not cleared one by one into one or more binary dump file hprof files of the target application, the hprof file capable of indicating a reference Citation relationship
  • a second determining unit configured to determine, according to each of the hprof files, a reference relationship of the reference stored by the hprof file, and determine a reference chain of each of the references;
  • a third determining unit configured to determine, from one or more of the reference chains, a reference chain that causes a memory leak.
  • the second determining unit is configured to parse each of the hprof files to obtain a reference key of each of the hprof files, where the hprof file represents the reference relationship by using the reference key; a reference key to determine a direct reference function for each of the references; calculate each of the direct references The shortest path to the garbage collection function used to reclaim memory, the shortest path being the reference chain.
  • the third determining unit is configured to determine whether a length of each of the reference chains exceeds 1; when the length of the reference chain exceeds 1, determining that the reference chain is the reference chain that causes a memory leak .
  • the device further includes:
  • a filtering unit configured to call the garbage collection function to clear one or more pieces before dumping the one or more references that are not cleared to one or more binary dump file hprof files of the target application The reference; when there is one or more of the references that are not cleared by the garbage collection function, the notification dumping unit dumps the one or more references that are not cleared to the target application one by one Steps in one or more binary dump files in the hprof file.
  • the device further includes:
  • a recording unit configured to record the reference chain that causes a memory leak in the report file after determining a reference chain that causes a memory leak from one or more of the reference chains;
  • a sending unit configured to send the report file to a server associated with the target application.
  • the memory detection module monitors each job activity of the target application, and determines that the target application is determined when any Activity is exited and one or more references of the Activity are not cleared. There is a memory leak, and then one or more references that have not been cleaned are dumped into one or more binary dump file hprof files of the target application.
  • the hprof file can represent a reference relationship of the reference, and then based on each The hprof file represents the reference relationship of the reference stored in the hprof file, determines the reference chain for each reference, and determines the chain of references that caused the memory leak from one or more reference chains.
  • the memory detection module automatically detects the memory leak, and determines the reference chain that causes the memory leak according to the reference relationship represented by the hprof file, and realizes the location of the location memory leak. Therefore, the prior art technical problem of detecting memory leakage dependent on manual is solved, and the technical effect of automatically detecting and locating memory leakage is realized.
  • FIG. 1 is a flowchart of a method for detecting a memory leak in an embodiment of the present invention
  • FIG. 2 is a schematic diagram of an exemplary function topology in an embodiment of the present invention.
  • FIG. 3 is a schematic structural diagram of a device for detecting a memory leak according to an embodiment of the present invention.
  • Embodiments of the present invention provide a method and apparatus for detecting a memory leak for implementing a technical effect of automatically detecting a memory leak.
  • the memory detection module monitors each job activity of the target application, and determines that the target application is determined when any Activity is exited and one or more references of the Activity are not cleared. There is a memory leak, and then one or more references that have not been cleaned are dumped into one or more binary dump file hprof files of the target application.
  • the hprof file can represent a reference relationship of the reference, and then based on each The hprof file represents the reference relationship of the reference stored in the hprof file, determines the reference chain for each reference, and determines the chain of references that caused the memory leak from one or more reference chains.
  • the memory detection module automatically detects the memory leak, and determines the reference chain that causes the memory leak according to the reference relationship represented by the hprof file, and realizes the location of the location memory leak. Therefore, the prior art technical problem of detecting memory leakage dependent on manual is solved, and the technical effect of automatically detecting and locating memory leakage is realized.
  • a first aspect of the present invention provides a method for detecting a memory leak.
  • FIG. 1 a flowchart of a method for detecting a memory leak in an embodiment of the present invention is provided. The method includes:
  • the memory detection module monitors each job activity of the target application
  • S103 dump the one or more references that are not cleared to one or more binary dump file hprof files of the target application, and the hprof file can represent a reference relationship of the reference;
  • S104 Determine, according to each of the hprof files, a reference relationship of the reference stored in the hprof file, and determine a reference chain of each of the references;
  • S105 Determine a reference chain that causes a memory leak from one or more of the reference chains.
  • the memory detection module in the embodiment of the present invention runs in the target application.
  • the code of the memory detection module is written in the Python language.
  • Python is an object-oriented, interpreted computer programming language. Python syntax is simple and clear, with a rich and powerful class library. It is often nicknamed the glue language, which makes it easy to link modules made in other languages. Write the good memory detection module code and then compile it with gradle or ANT, and finally insert it into the code of the target application.
  • the canary.jar package is compiled from Java JAVA language collection ANR (Application Not Responding) information log tool.
  • ANR Application Not Responding
  • you compile with the gradle compilation method you do not need to package, directly plug the code of the memory detection module into the code of the target application.
  • a person skilled in the art to which the present invention pertains may select gradle, ANT or other compilation manner according to actual conditions, and the present invention does not specifically limit it.
  • the code of the memory detection module and the code of the target application are placed in the same level directory.
  • the target application runs in the main thread, providing the user with the various functions of the target application.
  • the memory detection module runs in a memory detection thread different from the main thread, so that the operations performed in the memory detection thread do not affect the main thread, and thus do not affect the normal operation of the target application.
  • the server associated with the target application sends the improved target application to each application download platform or UE (User Equipment). Then the UE downloads the target application from the application download platform or the server, and installs the target application into the UE. Since the memory detection module is inserted in the target application, the target application can automatically detect and locate the memory leak of the target application when the target application is running in the UE.
  • UE User Equipment
  • the memory detection module also runs with the running of the target application, and then monitors each job activity of the target application.
  • the memory detection module in the embodiment of the present invention has a reference chain monitoring method RefWatcher.watch() method. Use the RefWatcher.watch() method to create a KeyedWeakReference object that monitors the Activity's weak reference key value in each Activity. When the Activity is initialized in the Initialization Oncreat function, the KeyedWeakReference object is also initialized.
  • one or more references that have not been cleared are dumped into the hprof file in the system file of the target application. Specifically dump a dump into a hprof file.
  • the number of hprof files for an Activity is the number of references that the Activity has not been cleared.
  • the binary dump file hprof file is a snapshot storage type file, which contains all the data representation, as well as the basic data types inside the JAVA object and the thread. The values of the fields in the record object and the methods in the hprof file generation are carried out. It is because of the nature of the hprof file that the hprof file can represent the reference relationship of the reference.
  • S104 based on each hprof file, a reference chain for each reference is determined.
  • S104 is implemented by the following process:
  • a shortest path is calculated for each of the direct reference functions to a garbage collection function for reclaiming memory, the shortest path being the reference chain.
  • the hprof file is parsed, and the only reference key reference key in the hprof file is parsed.
  • the hprof file uses the reference key to indicate the reference relationship, and the reference key can indicate the direct reference function of the reference.
  • the direct reference function refers to a function that directly references the Activity.
  • function A refers to function C by referring to function B
  • function C refers to Activity
  • function C is the direct reference function of Activity. Therefore, a direct reference function for each reference can be determined based on each reference key.
  • the GC function is a function for retrieving the various references of the Activity after the Activity exits.
  • the reference cannot be reclaimed by the GC function, indicating that the reference chain of the reference is abnormal, causing a memory leak.
  • the shortest path between the direct reference function and the GC function in the embodiment of the present invention is a reference chain.
  • Figure 2 shows a functional topology diagram.
  • the direct reference function is an A function
  • the A function to the GC function can have two paths. The first one is "A function - B function - C function - GC function”
  • the second is "A function - D" Function - GC function”. Since the first path references the GC function three times, and the second path only references the GC function twice, the second path is the shortest, so the second path is the reference chain of the A function. In a similar manner, a reference chain for each reference is obtained, resulting in a total of one or more reference chains.
  • S105 is executed to determine a reference chain with a memory leak from one or more reference chains.
  • S105 in the embodiment of the present invention is implemented by the following process:
  • the reference chain is determined to be the reference chain that causes a memory leak.
  • the length of the reference chain refers to the number of times the direct reference function to the GC function total reference function.
  • the length of the first path in FIG. 2 is 3, and the length of the second path is 2.
  • a reference chain having a length exceeding 1 is used as a reference chain for referring to a memory leak.
  • the method may further include:
  • the report file is sent to a server associated with the target application.
  • the reference chain that causes the memory leak is saved in the report file in the UE, and the report file is sent to the server associated with the target application. This allows server administrators to fix memory leaks by viewing one or more reference chains in the report file to restore memory leaks.
  • prompt information indicating a memory leak may also be output.
  • the reference chain that causes the memory leak can be passed to the display leak service DisplayLeakService function, and then the text prompt information is outputted in the form of a notification in the notification bar.
  • the person skilled in the art to which the present invention pertains may be selected according to actual conditions, and the present invention is not specifically limited.
  • the method may further include:
  • the reference that cannot be cleared may not be cleared due to the memory leak. Therefore, in order to filter out due to memory leak The reference cannot be cleared, and the calculation amount of subsequent positioning memory leak is reduced.
  • the GC function is called to try to clear the reference that cannot be cleared again. .
  • the second aspect of the present invention further provides a memory leak detecting device, as shown in FIG. 3, comprising:
  • the monitoring unit 101 is configured to monitor each job activity of the target application
  • the first determining unit 102 is configured to: when monitoring any of the activities to exit, and one or more references of the activity are not cleared, determining that the target application has a memory leak;
  • the dumping unit 103 is configured to dump the one or more references that are not cleared to one or more binary dump file hprof files of the target application, the hprof file can represent a reference Reference relationship
  • a second determining unit 104 configured to determine, according to each of the hprof files, a reference relationship of the reference of the hprof file storage, and determine a reference chain of each of the references;
  • the third determining unit 105 is configured to determine, from one or more of the reference chains, a reference chain that causes a memory leak.
  • the second determining unit 104 is configured to parse each of the hprof files to obtain a reference key of each of the hprof files, and the hprof file represents the reference relationship by using the reference key;
  • a reference key is used to determine a direct reference function for each of the references;
  • a shortest path for each of the direct reference functions to a garbage collection function for reclaiming memory is calculated, the shortest path being the reference chain.
  • the third determining unit 105 is configured to determine whether the length of each of the reference chains exceeds 1; when the length of the reference chain exceeds 1, determining that the reference chain is the reference chain that causes a memory leak.
  • the device for detecting a memory leak in the embodiment of the present invention may further include:
  • a filtering unit configured to call the garbage collection function to clear one or more pieces before dumping the one or more references that are not cleared to one or more binary dump file hprof files of the target application The reference; when there is one or more of the references that are not cleared by the garbage collection function, the notification dumping unit dumps the one or more references that are not cleared to the target application one by one Steps in one or more binary dump files in the hprof file.
  • the device for detecting a memory leak in the embodiment of the present invention may further include:
  • a recording unit configured to determine a reference chain that causes a memory leak from one or more of the reference chains After that, the reference chain that causes the memory leak is recorded in the report file;
  • a sending unit configured to send the report file to a server associated with the target application.
  • the memory detection module monitors each job activity of the target application, and determines that the target application is determined when any Activity is exited and one or more references of the Activity are not cleared. There is a memory leak, and then one or more references that have not been cleaned are dumped into one or more binary dump file hprof files of the target application.
  • the hprof file can represent a reference relationship of the reference, and then based on each The hprof file represents the reference relationship of the reference stored in the hprof file, determines the reference chain for each reference, and determines the chain of references that caused the memory leak from one or more reference chains.
  • the memory detection module automatically detects the memory leak, and determines the reference chain that causes the memory leak according to the reference relationship represented by the hprof file, and realizes the location of the location memory leak. Therefore, the prior art technical problem of detecting memory leakage dependent on manual is solved, and the technical effect of automatically detecting and locating memory leakage is realized.
  • embodiments of the present invention can be provided as a method, system, or computer program product. Accordingly, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment, or a combination of software and hardware. Moreover, the invention can take the form of a computer program product embodied on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) including computer usable program code.
  • computer-usable storage media including but not limited to disk storage, CD-ROM, optical storage, etc.
  • the computer program instructions can also be stored in a computer readable memory that can direct a computer or other programmable data processing device to operate in a particular manner, such that the instructions stored in the computer readable memory produce an article of manufacture comprising the instruction device.
  • the device is implemented in a flow or a flow chart The functions specified in a block or blocks of a flow and/or block diagram.
  • These computer program instructions can also be loaded onto a computer or other programmable data processing device such that a series of operational steps are performed on a computer or other programmable device to produce computer-implemented processing for execution on a computer or other programmable device.
  • the instructions provide steps for implementing the functions specified in one or more of the flow or in a block or blocks of a flow diagram.

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Computer Hardware Design (AREA)
  • Quality & Reliability (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Debugging And Monitoring (AREA)

Abstract

A method and apparatus for detecting memory leakages, used in the automatic detection of memory leakages. Said method comprises: a memory detection module monitors each job activity of a target application (S101); when any of said activities are monitored as having exited, and one or more references of said activity are not cleared, it is determined that a memory leakage exists in said target application (S102); said one or more references which are not cleared are dumped one-by-one into one or more binary dump files (hprof files) of said target application, wherein said hprof file is capable of representing the referential relationship of a reference (S103); on the basis of each of said hprof files, the referential relationship of a reference saved in said hprof file is represented, thus the reference chain for each reference may be determined (S104); and a reference chain causing memory leakages may be determined from one or more of said reference chains (S105).

Description

一种内存泄漏的检测方法和装置Method and device for detecting memory leak 技术领域Technical field
本发明涉及计算机技术领域,尤其涉及一种内存泄漏的检测方法和装置。The present invention relates to the field of computer technologies, and in particular, to a method and apparatus for detecting a memory leak.
背景技术Background technique
内存泄漏是指功能模块持续申请内存空间,当结束该功能后,该功能申请的内存空间未能回收。内存泄漏的出现会导致内存不断上涨,最终引发***强行结束应用,释放内存。强行结束应用导致应用异常退出,降低用户体验。Memory leak refers to the function module continuously applying for memory space. When this function is finished, the memory space requested by the function cannot be recovered. The appearance of a memory leak will cause the memory to rise, eventually causing the system to force the application to end and free up memory. Forcibly ending the application causes the application to exit abnormally, reducing the user experience.
在现有技术中,通过人工测试来检测内存泄漏。但是,人工测试偶然性,很难测试出所有内存泄漏问题。并且,人工测试环境与实际应用环境存在差异,且人工测试在投入市场前须完成,故人工测试很难还原内存泄漏的实际情况。In the prior art, memory leaks are detected by manual testing. However, manual testing is contingent and it is difficult to test out all memory leaks. Moreover, there is a difference between the manual test environment and the actual application environment, and the manual test must be completed before being put into the market, so the manual test is difficult to restore the actual situation of the memory leak.
所以,由于现有技术依赖人工检测内存泄漏,所以导致检测内存泄漏和解决内存泄漏周期长。Therefore, since the prior art relies on manual detection of memory leaks, it leads to detection of memory leaks and long memory leaks.
发明内容Summary of the invention
发明实施例提供了一种内存泄漏的检测方法和装置,用于实现自动检测内存泄漏的技术效果。Embodiments of the present invention provide a method and apparatus for detecting a memory leak for implementing a technical effect of automatically detecting a memory leak.
第一方面,本发明提供了一种内存泄漏的检测方法,包括:In a first aspect, the present invention provides a method for detecting a memory leak, comprising:
内存检测模块对目标应用的每个作业Activity进行监控;The memory detection module monitors each job activity of the target application;
当监控到任一所述Activity退出,且所述Activity的一条或多条引用未被清除时,确定所述目标应用存在内存泄漏;Determining that the target application has a memory leak when monitoring that any of the activities exits and one or more references of the Activity are not cleared;
将未被清除的所述一条或多条引用一一倾卸到所述目标应用的一个或多个二进制转储文件hprof文件中,所述hprof文件能够表示出一条引用的引用关系;And dumping the one or more references that are not cleared to one or more binary dump file hprof files of the target application, the hprof file being capable of indicating a reference relationship of a reference;
基于每个所述hprof文件表示所述hprof文件存储的引用的引用关系,确定出每条所述引用的引用链;Determining a reference chain of each of the references based on a reference relationship of each of the hprof files representing a reference to the hprof file storage;
从一条或多条所述引用链中确定出导致内存泄漏的引用链。A reference chain that causes a memory leak is determined from one or more of the reference chains.
可选的,基于每个所述hprof文件表示所述hprof文件存储的引用的引用关系,确定出每条所述引用的引用链,包括:Optionally, determining, according to each of the hprof files, a reference relationship of the reference stored in the hprof file, determining a reference chain of each of the references, including:
解析每个所述hprof文件,获得每个所述hprof文件的引用键,所述hprof文件通过所述引用键表示所述引用关系;Parsing each of the hprof files to obtain a reference key for each of the hprof files, the hprof file representing the reference relationship by the reference key;
基于每个所述引用键,确定每个所述引用的直接引用函数;Determining a direct reference function for each of the references based on each of the reference keys;
计算每个所述直接引用函数到用于回收内存的垃圾回收函数的最短路径, 所述最短路径为所述引用链。Calculate the shortest path of each of the direct reference functions to the garbage collection function used to reclaim memory, The shortest path is the reference chain.
可选的,从一条或多条所述引用链中确定出导致内存泄漏的引用链,包括:Optionally, determining a reference chain that causes a memory leak from one or more of the reference chains, including:
判断每个所述引用链的长度是否超过1;Determining whether the length of each of the reference chains exceeds 1;
当所述引用链的长度超过1时,确定所述引用链为所述导致内存泄漏的引用链。When the length of the reference chain exceeds 1, the reference chain is determined to be the reference chain that causes a memory leak.
可选的,在将未被清除的所述一条或多条引用一一倾卸到所述目标应用的一个或多个二进制转储文件hprof文件中之前,还包括:Optionally, before dumping the one or more references that are not cleared to one or more binary dump file hprof files of the target application, the method further includes:
调用垃圾回收函数清除一个或多个所述引用;Calling the garbage collection function to clear one or more of the references;
当存在未能被所述垃圾回收函数清除的一个或多个所述引用时,执行所述将未被清除的所述一条或多条引用一一倾卸到所述目标应用的一个或多个二进制转储文件hprof文件中的步骤。Performing one or more of the one or more references that are not to be purged to one or more of the target applications when there is one or more of the references that are not cleared by the garbage collection function The steps in the binary dump file hprof file.
可选的,从一个或多个所述引用链中确定出导致内存泄漏的引用链之后,还包括:Optionally, after determining the reference chain that causes the memory leak from one or more of the reference chains, the method further includes:
将所述导致内存泄漏的引用链记录在报告文件中;Recording the reference chain that causes the memory leak in the report file;
向与所述目标应用关联的服务器发送所述报告文件。The report file is sent to a server associated with the target application.
可选的,所述内存检测模块对应的代码插桩在所述目标应用的代码中,且所述内存检测模块的代码与所述目标应用的代码在同一级目录下。Optionally, the code corresponding to the memory detection module is inserted in the code of the target application, and the code of the memory detection module is in the same level as the code of the target application.
可选的,所述方法还包括:Optionally, the method further includes:
所述目标应用在主线程中执行,所述内存检测模块在不同于主线程的内存检测线程中执行。The target application is executed in a main thread, and the memory detection module executes in a memory detection thread different from the main thread.
第二方面,本发明提供了一种内存泄漏的检测装置,包括:In a second aspect, the present invention provides a memory leak detecting apparatus, including:
监控单元,用于对目标应用的每个作业Activity进行监控;a monitoring unit, configured to monitor each job activity of the target application;
第一确定单元,用于当监控到任一所述Activity退出,且所述Activity的一条或多条引用未被清除时,确定所述目标应用存在内存泄漏;a first determining unit, configured to determine that the target application has a memory leak when monitoring that any of the activities exits and one or more references of the activity are not cleared;
倾卸单元,用于将未被清除的所述一条或多条引用一一倾卸到所述目标应用的一个或多个二进制转储文件hprof文件中,所述hprof文件能够表示出一条引用的引用关系;a dumping unit, configured to dump the one or more references that are not cleared one by one into one or more binary dump file hprof files of the target application, the hprof file capable of indicating a reference Citation relationship
第二确定单元,用于基于每个所述hprof文件表示所述hprof文件存储的引用的引用关系,确定出每条所述引用的引用链;a second determining unit, configured to determine, according to each of the hprof files, a reference relationship of the reference stored by the hprof file, and determine a reference chain of each of the references;
第三确定单元,用于从一条或多条所述引用链中确定出导致内存泄漏的引用链。And a third determining unit, configured to determine, from one or more of the reference chains, a reference chain that causes a memory leak.
可选的,所述第二确定单元用于解析每个所述hprof文件,获得每个所述hprof文件的引用键,所述hprof文件通过所述引用键表示所述引用关系;基于每个所述引用键,确定每个所述引用的直接引用函数;计算每个所述直接引用 函数到用于回收内存的垃圾回收函数的最短路径,所述最短路径为所述引用链。Optionally, the second determining unit is configured to parse each of the hprof files to obtain a reference key of each of the hprof files, where the hprof file represents the reference relationship by using the reference key; a reference key to determine a direct reference function for each of the references; calculate each of the direct references The shortest path to the garbage collection function used to reclaim memory, the shortest path being the reference chain.
可选的,所述第三确定单元用于判断每个所述引用链的长度是否超过1;当所述引用链的长度超过1时,确定所述引用链为所述导致内存泄漏的引用链。Optionally, the third determining unit is configured to determine whether a length of each of the reference chains exceeds 1; when the length of the reference chain exceeds 1, determining that the reference chain is the reference chain that causes a memory leak .
可选的,所述装置还包括:Optionally, the device further includes:
筛选单元,用于在将未被清除的所述一条或多条引用一一倾卸到所述目标应用的一个或多个二进制转储文件hprof文件中之前,调用垃圾回收函数清除一个或多个所述引用;当存在未能被所述垃圾回收函数清除的一个或多个所述引用时,通知倾卸单元将未被清除的所述一条或多条引用一一倾卸到所述目标应用的一个或多个二进制转储文件hprof文件中的步骤。a filtering unit, configured to call the garbage collection function to clear one or more pieces before dumping the one or more references that are not cleared to one or more binary dump file hprof files of the target application The reference; when there is one or more of the references that are not cleared by the garbage collection function, the notification dumping unit dumps the one or more references that are not cleared to the target application one by one Steps in one or more binary dump files in the hprof file.
可选的,所述装置还包括:Optionally, the device further includes:
记录单元,用于从一个或多个所述引用链中确定出导致内存泄漏的引用链之后,将所述导致内存泄漏的引用链记录在报告文件中;a recording unit, configured to record the reference chain that causes a memory leak in the report file after determining a reference chain that causes a memory leak from one or more of the reference chains;
发送单元,用于向与所述目标应用关联的服务器发送所述报告文件。And a sending unit, configured to send the report file to a server associated with the target application.
本申请实施例中的上述一个或多个技术方案,至少具有如下一种或多种技术效果:The above one or more technical solutions in the embodiments of the present application have at least one or more of the following technical effects:
在本发明实施例的技术方案中,内存检测模块对目标应用的每个作业Activity进行监控,当监控到任一Activity退出,且所述Activity的一条或多条引用未被清除时,确定目标应用存在内存泄漏,然后将未被清除的一条或多条引用一一倾卸到目标应用的一个或多个二进制转储文件hprof文件中,hprof文件能够表示出一条引用的引用关系,进而基于每个hprof文件表示hprof文件存储的引用的引用关系,确定出每条引用的引用链,并从一条或多条引用链中确定出导致内存泄漏的引用链。由上述描述看出,本发明实施例通过内存检测模块实现了自动检测内存泄漏,并且根据hprof文件表示的引用关系确定出确定出导致内存泄漏的引用链,实现了定位内存泄漏的所在位置。由此,解决了现有技术检测内存泄漏依赖人工的技术问题,实现了自动检测并定位内存泄漏的技术效果。In the technical solution of the embodiment of the present invention, the memory detection module monitors each job activity of the target application, and determines that the target application is determined when any Activity is exited and one or more references of the Activity are not cleared. There is a memory leak, and then one or more references that have not been cleaned are dumped into one or more binary dump file hprof files of the target application. The hprof file can represent a reference relationship of the reference, and then based on each The hprof file represents the reference relationship of the reference stored in the hprof file, determines the reference chain for each reference, and determines the chain of references that caused the memory leak from one or more reference chains. It is seen from the above description that the memory detection module automatically detects the memory leak, and determines the reference chain that causes the memory leak according to the reference relationship represented by the hprof file, and realizes the location of the location memory leak. Therefore, the prior art technical problem of detecting memory leakage dependent on manual is solved, and the technical effect of automatically detecting and locating memory leakage is realized.
附图说明DRAWINGS
图1为本发明实施例中内存泄漏的检测方法流程图;1 is a flowchart of a method for detecting a memory leak in an embodiment of the present invention;
图2为本发明实施例中一示例性函数拓扑示意图;2 is a schematic diagram of an exemplary function topology in an embodiment of the present invention;
图3为本发明实施例中内存泄漏的检测装置结构示意图。FIG. 3 is a schematic structural diagram of a device for detecting a memory leak according to an embodiment of the present invention.
具体实施方式 detailed description
发明实施例提供了一种内存泄漏的检测方法和装置,用于实现自动检测内存泄漏的技术效果。Embodiments of the present invention provide a method and apparatus for detecting a memory leak for implementing a technical effect of automatically detecting a memory leak.
为了解决上述技术问题,本发明提供的技术方案总体思路如下:In order to solve the above technical problem, the general idea of the technical solution provided by the present invention is as follows:
在本发明实施例的技术方案中,内存检测模块对目标应用的每个作业Activity进行监控,当监控到任一Activity退出,且所述Activity的一条或多条引用未被清除时,确定目标应用存在内存泄漏,然后将未被清除的一条或多条引用一一倾卸到目标应用的一个或多个二进制转储文件hprof文件中,hprof文件能够表示出一条引用的引用关系,进而基于每个hprof文件表示hprof文件存储的引用的引用关系,确定出每条引用的引用链,并从一条或多条引用链中确定出导致内存泄漏的引用链。由上述描述看出,本发明实施例通过内存检测模块实现了自动检测内存泄漏,并且根据hprof文件表示的引用关系确定出确定出导致内存泄漏的引用链,实现了定位内存泄漏的所在位置。由此,解决了现有技术检测内存泄漏依赖人工的技术问题,实现了自动检测并定位内存泄漏的技术效果。In the technical solution of the embodiment of the present invention, the memory detection module monitors each job activity of the target application, and determines that the target application is determined when any Activity is exited and one or more references of the Activity are not cleared. There is a memory leak, and then one or more references that have not been cleaned are dumped into one or more binary dump file hprof files of the target application. The hprof file can represent a reference relationship of the reference, and then based on each The hprof file represents the reference relationship of the reference stored in the hprof file, determines the reference chain for each reference, and determines the chain of references that caused the memory leak from one or more reference chains. It is seen from the above description that the memory detection module automatically detects the memory leak, and determines the reference chain that causes the memory leak according to the reference relationship represented by the hprof file, and realizes the location of the location memory leak. Therefore, the prior art technical problem of detecting memory leakage dependent on manual is solved, and the technical effect of automatically detecting and locating memory leakage is realized.
下面通过附图以及具体实施例对本发明技术方案做详细的说明,应当理解本申请实施例以及实施例中的具体特征是对本申请技术方案的详细的说明,而不是对本申请技术方案的限定,在不冲突的情况下,本申请实施例以及实施例中的技术特征可以相互组合。The technical solutions of the present invention are described in detail below with reference to the accompanying drawings and specific embodiments. It is understood that the specific features of the embodiments and the embodiments of the present application are the detailed description of the technical solutions of the present application, and are not limited to the technical solutions of the present application. In the case of no conflict, the technical features in the embodiments of the present application and the embodiments may be combined with each other.
本文中术语“和/或”,仅仅是一种描述关联对象的关联关系,表示可以存在三种关系,例如,A和/或B,可以表示:单独存在A,同时存在A和B,单独存在B这三种情况。另外,本文中字符“/”,一般表示前后关联对象是一种“或”的关系。The term "and/or" in this context is merely an association describing the associated object, indicating that there may be three relationships, for example, A and / or B, which may indicate that A exists separately, and both A and B exist, respectively. B these three situations. In addition, the character "/" in this article generally indicates that the contextual object is an "or" relationship.
本发明第一方面提供了一种内存泄漏的检测方法,请参考图1,为本发明实施例中内存泄漏的检测方法流程图。该方法包括:A first aspect of the present invention provides a method for detecting a memory leak. Referring to FIG. 1, a flowchart of a method for detecting a memory leak in an embodiment of the present invention is provided. The method includes:
S101:内存检测模块对目标应用的每个作业Activity进行监控;S101: The memory detection module monitors each job activity of the target application;
S102:当监控到任一所述Activity退出,且所述Activity的一条或多条引用未被清除时,确定所述目标应用存在内存泄漏;S102: When it is monitored that any of the activities exits, and one or more references of the activity are not cleared, determining that the target application has a memory leak;
S103:将未被清除的所述一条或多条引用一一倾卸到所述目标应用的一个或多个二进制转储文件hprof文件中,所述hprof文件能够表示出一条引用的引用关系;S103: dump the one or more references that are not cleared to one or more binary dump file hprof files of the target application, and the hprof file can represent a reference relationship of the reference;
S104:基于每个所述hprof文件表示所述hprof文件存储的引用的引用关系,确定出每条所述引用的引用链;S104: Determine, according to each of the hprof files, a reference relationship of the reference stored in the hprof file, and determine a reference chain of each of the references;
S105:从一条或多条所述引用链中确定出导致内存泄漏的引用链。S105: Determine a reference chain that causes a memory leak from one or more of the reference chains.
具体来讲,为了便于在用户使用目标应用过程中对目标应用内存泄漏进行 检测,本发明实施例中的内存检测模块运行在目标应用中。具体来讲,在本发明实施例中,使用Python语言编写内存检测模块的代码。Python是一种面向对象、解释型计算机程序设计语言。Python语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,能够把用其他语言制作的各种模块很轻松地联结在一起。编写好的内存检测模块代码再利用gradle或ANT进行编译,最后插桩到目标应用的代码中。Specifically, in order to facilitate the application application memory leak during the user's use of the target application The memory detection module in the embodiment of the present invention runs in the target application. Specifically, in the embodiment of the present invention, the code of the memory detection module is written in the Python language. Python is an object-oriented, interpreted computer programming language. Python syntax is simple and clear, with a rich and powerful class library. It is often nicknamed the glue language, which makes it easy to link modules made in other languages. Write the good memory detection module code and then compile it with gradle or ANT, and finally insert it into the code of the target application.
其中,如果利用ANT的编译方式进行编译,则具体为将内存检测模块的代码编译打包为canary.jar包,然后将canary.jar包插桩到目标应用的代码中。其中,canary.jar包是爪哇JAVA语言搜集ANR(应用程序无响应,Application Not Responding)信息日志的工具编译而成的。或者,如果利用gradle的编译方式进行编译,则无需打包,直接将内存检测模块的代码插桩到目标应用的代码中即可。在具体实现过程中,本发明所属领域的普通技术人员可以根据实际进行选择gradle、ANT或者其他编译方式,本发明不做具体限制。Among them, if you use the ANT compilation method to compile, specifically compile the code of the memory detection module into the canary.jar package, and then insert the canary.jar package into the code of the target application. Among them, the canary.jar package is compiled from Java JAVA language collection ANR (Application Not Responding) information log tool. Or, if you compile with the gradle compilation method, you do not need to package, directly plug the code of the memory detection module into the code of the target application. In a specific implementation process, a person skilled in the art to which the present invention pertains may select gradle, ANT or other compilation manner according to actual conditions, and the present invention does not specifically limit it.
进一步,为了避免内存检测模块对目标应用程序的正常运行产生影响,在本发明实施例中,将内存检测模块的代码和目标应用程序的代码放置在同一级目录中。在运行目标应用时,目标应用运行在主线程中,为用户提供目标应用的各个功能。而内存检测模块则运行在不同于主线程的内存检测线程中,进而使得内存检测线程中执行的各个操作不会影响主线程,进而不影响目标应用的正常运行。Further, in order to prevent the memory detection module from affecting the normal operation of the target application, in the embodiment of the present invention, the code of the memory detection module and the code of the target application are placed in the same level directory. When the target application is run, the target application runs in the main thread, providing the user with the various functions of the target application. The memory detection module runs in a memory detection thread different from the main thread, so that the operations performed in the memory detection thread do not affect the main thread, and thus do not affect the normal operation of the target application.
上述对目标应用的改进完成后,与目标应用关联的服务器将改进后的目标应用发送到各个应用下载平台或UE(用户设备,User Equipment)。进而UE从应用下载平台或者服务器中下载目标应用,并将目标应用安装到UE中。由于目标应用中插桩了内存检测模块,因此目标应用在UE中运行时,目标应用就可以自动对目标应用的内存泄漏进行检测和定位。After the improvement of the target application is completed, the server associated with the target application sends the improved target application to each application download platform or UE (User Equipment). Then the UE downloads the target application from the application download platform or the server, and installs the target application into the UE. Since the memory detection module is inserted in the target application, the target application can automatically detect and locate the memory leak of the target application when the target application is running in the UE.
下面就对如何对内存泄漏检测和定位进行具体介绍。Here is a detailed description of how to detect and locate memory leaks.
首先,S101中,在目标应用运行过程中,内存检测模块也会随目标应用的运行而运行,进而对目标应用的每个作业Activity进行监控。具体来讲,本发明实施例中的内存检测模块具有引用链监控方法RefWatcher.watch()方法。利用RefWatcher.watch()方法在每个Activity中创建能够监控Activity的弱引用键值KeyedWeakReference对象,进而Activity在初始化Oncreat函数中初始化时,KeyedWeakReference对象也同时被初始化。First, in S101, during the running of the target application, the memory detection module also runs with the running of the target application, and then monitors each job activity of the target application. Specifically, the memory detection module in the embodiment of the present invention has a reference chain monitoring method RefWatcher.watch() method. Use the RefWatcher.watch() method to create a KeyedWeakReference object that monitors the Activity's weak reference key value in each Activity. When the Activity is initialized in the Initialization Oncreat function, the KeyedWeakReference object is also initialized.
在正常情况下,Activity退出后,该Activity的全部引用将会被回收清除,进而释放该Activity申请的内存。因此,当KeyedWeakReference监测到Activity退出,然而该Activity的一条或多条引用却未被全部清除时,则S102 中确定目标应用出现了内存泄漏。Under normal circumstances, after the Activity exits, all references to the Activity will be reclaimed, and the memory requested by the Activity will be released. Therefore, when the KeyedWeakReference detects that the Activity exits, but one or more references to the Activity are not all cleared, then S102 A memory leak has been identified in the target application.
为了定位导致内存泄漏的原因,进一步,在S103中,将未被清除的一条或多个引用倾卸到目标应用的***文件中的hprof文件中。具体为一条引用倾卸到一个hprof文件中。换言之,一个Activity的hprof文件的数量,为该Activity未被清除的引用数量。二进制转储文件hprof文件是一种快照存储类型的文件,它包含了所有数据表现方式,以及JAVA对象和线程内部的基本数据类型,记录对象中域的值和hprof文件生成时有哪些方法在被执行。正是由于hprof文件的特性,使得hprof文件能够表示引用的引用关系。In order to locate the cause of the memory leak, further, in S103, one or more references that have not been cleared are dumped into the hprof file in the system file of the target application. Specifically dump a dump into a hprof file. In other words, the number of hprof files for an Activity is the number of references that the Activity has not been cleared. The binary dump file hprof file is a snapshot storage type file, which contains all the data representation, as well as the basic data types inside the JAVA object and the thread. The values of the fields in the record object and the methods in the hprof file generation are carried out. It is because of the nature of the hprof file that the hprof file can represent the reference relationship of the reference.
所以,S104中,基于每个hprof文件,确定出每条引用的引用链。在本发明实施例中,S104通过如下过程实现:Therefore, in S104, based on each hprof file, a reference chain for each reference is determined. In the embodiment of the present invention, S104 is implemented by the following process:
解析每个所述hprof文件,获得每个所述hprof文件的引用键,所述hprof文件通过所述引用键表示所述引用关系;Parsing each of the hprof files to obtain a reference key for each of the hprof files, the hprof file representing the reference relationship by the reference key;
基于每个所述引用键,确定每个所述引用的直接引用函数;Determining a direct reference function for each of the references based on each of the reference keys;
计算每个所述直接引用函数到用于回收内存的垃圾回收函数的最短路径,所述最短路径为所述引用链。A shortest path is calculated for each of the direct reference functions to a garbage collection function for reclaiming memory, the shortest path being the reference chain.
首先对hprof文件进行解析,解析出hprof文件中唯一的引用键reference key。其中,hprof文件正是通过reference key来表示引用关系,通过reference key可以指示出这条引用的直接引用函数。在本发明实施例中,直接引用函数指的是直接引用Activity的函数。举例来说,函数A通过引用函数B进而引用函数C,而函数C引用了Activity,则函数C就是Activity的直接引用函数。因此,基于每个引用键可以确定出每条引用的直接引用函数。First, the hprof file is parsed, and the only reference key reference key in the hprof file is parsed. Among them, the hprof file uses the reference key to indicate the reference relationship, and the reference key can indicate the direct reference function of the reference. In the embodiment of the present invention, the direct reference function refers to a function that directly references the Activity. For example, function A refers to function C by referring to function B, and function C refers to Activity, then function C is the direct reference function of Activity. Therefore, a direct reference function for each reference can be determined based on each reference key.
接下来,计算直接引用函数到GC(垃圾回收,Gabage,collection)函数的最短路径。GC函数是用于在Activity退出后回收该Activity的各条引用的函数。引用不能被GC函数回收,表示引用所在的引用链异常,引发内存泄漏。其中,本发明实施例中直接引用函数到GC函数之间的最短路径就是引用链。Next, calculate the shortest path to the direct reference function to the GC (Garbage, Collection) function. The GC function is a function for retrieving the various references of the Activity after the Activity exits. The reference cannot be reclaimed by the GC function, indicating that the reference chain of the reference is abnormal, causing a memory leak. The shortest path between the direct reference function and the GC function in the embodiment of the present invention is a reference chain.
具体来讲,由于目标应用中存在大量的函数,且大部分函数可以引用其他函数,以及被其他函数引用,所以,直接引用函数到GC函数的路径就有多条。举例来说,图2示出一函数拓扑示意图。在图2中,直接引用函数为A函数,A函数到GC函数可以有两条路径,第一条为“A函数-B函数-C函数-GC函数”,第二条为“A函数-D函数-GC函数”。由于第一条路径引用了三次才到达GC函数,而第二条路径仅引用两次就到达GC函数,所以第二条路径最短,所以第二条路径是A函数的引用链。通过类似的方式,获得每条引用的引用链,进而总共获得一条或多条引用链。 Specifically, since there are a large number of functions in the target application, and most of the functions can refer to other functions and are referenced by other functions, there are multiple paths to directly refer to the function of the GC function. For example, Figure 2 shows a functional topology diagram. In Figure 2, the direct reference function is an A function, and the A function to the GC function can have two paths. The first one is "A function - B function - C function - GC function", and the second is "A function - D" Function - GC function". Since the first path references the GC function three times, and the second path only references the GC function twice, the second path is the shortest, so the second path is the reference chain of the A function. In a similar manner, a reference chain for each reference is obtained, resulting in a total of one or more reference chains.
确定出引用链之后,执行S105,从一条或多条引用链中确定出存在内存泄漏的引用链。具体来讲,本发明实施例中的S105通过如下过程实现:After the reference chain is determined, S105 is executed to determine a reference chain with a memory leak from one or more reference chains. Specifically, S105 in the embodiment of the present invention is implemented by the following process:
判断每个所述引用链的长度是否超过1;Determining whether the length of each of the reference chains exceeds 1;
当所述引用链的长度超过1时,确定所述引用链为所述导致内存泄漏的引用链。When the length of the reference chain exceeds 1, the reference chain is determined to be the reference chain that causes a memory leak.
具体来讲,判断引用链的长度是否超过1,进而判断引用链是否导致了内存泄漏。在本发明实施例中,引用链的长度指的是直接引用函数到GC函数总共引用函数的次数。举例来说,图2中的第一条路径的长度为3,第二条路径的长度为2。Specifically, it is determined whether the length of the reference chain exceeds 1, and it is determined whether the reference chain causes a memory leak. In the embodiment of the present invention, the length of the reference chain refers to the number of times the direct reference function to the GC function total reference function. For example, the length of the first path in FIG. 2 is 3, and the length of the second path is 2.
当引用链长度超过1时,表示直接引用函数不能被GC函数所直接引用,进而表示直接引用函数无法被GC函数直接清除,因此该引用链导致了内存泄漏。所以,在本发明实施例中,将长度超过1的引用链作为引用内存泄漏的引用链。When the length of the reference chain exceeds 1, it means that the direct reference function cannot be directly referenced by the GC function, which means that the direct reference function cannot be directly cleared by the GC function, so the reference chain causes a memory leak. Therefore, in the embodiment of the present invention, a reference chain having a length exceeding 1 is used as a reference chain for referring to a memory leak.
进一步,为了能够更好地维护目标应用,作为一种可选的实施例,在S105之后,还可以包括:Further, in order to be able to better maintain the target application, as an optional embodiment, after S105, the method may further include:
将所述导致内存泄漏的引用链记录在报告文件中;Recording the reference chain that causes the memory leak in the report file;
向与所述目标应用关联的服务器发送所述报告文件。The report file is sent to a server associated with the target application.
具体来讲,将导致内存泄漏的引用链保存在UE中的报告文件中,并且将报告文件发送给目标应用关联的服务器。由此使得服务器的管理人员通过查看报告文件中的一条或多个引用链,进而还原出内存泄漏的情况,从而修复内存泄漏的问题。Specifically, the reference chain that causes the memory leak is saved in the report file in the UE, and the report file is sent to the server associated with the target application. This allows server administrators to fix memory leaks by viewing one or more reference chains in the report file to restore memory leaks.
或者,为了提示用户目标应用出现内存泄漏,作为一种可选的实施例,在S105之后,还可以输出表示内存泄漏的提示信息。具体来讲,提示信息以及输出提示信息的方式有多种。例如可以将导致内存泄漏的引用链传入为显示泄漏服务DisplayLeakService函数,进而以通知栏显示通知的形式输出文字提示信息。或者,也可以调用音频输出函数输出异常提示音。本发明所属领域的普通技术人员可以根据实际选择,本发明不做具体限制。Alternatively, in order to prompt the user that the target application has a memory leak, as an optional embodiment, after S105, prompt information indicating a memory leak may also be output. Specifically, there are various ways of prompting information and outputting prompt information. For example, the reference chain that causes the memory leak can be passed to the display leak service DisplayLeakService function, and then the text prompt information is outputted in the form of a notification in the notification bar. Alternatively, you can call the audio output function to output an abnormal sound. The person skilled in the art to which the present invention pertains may be selected according to actual conditions, and the present invention is not specifically limited.
较佳地,作为一种可选的实施例,在将未被清除的一条或多条引用一一倾卸到hprof文件中之前,还可以包括:Preferably, as an optional embodiment, before the one or more references that are not cleared are dumped into the hprof file, the method may further include:
调用垃圾回收函数清除一个或多个所述引用;Calling the garbage collection function to clear one or more of the references;
当存在未能被所述垃圾回收函数清除的一个或多个所述引用时,执行所述将未被清除的所述一条或多条引用一一倾卸到所述目标应用的一个或多个二进制转储文件hprof文件中的步骤。Performing one or more of the one or more references that are not to be purged to one or more of the target applications when there is one or more of the references that are not cleared by the garbage collection function The steps in the binary dump file hprof file.
具体来讲,在具体实现过程中,Activity退出后,不能被清除的引用也可以并不是由于内存泄漏而导致不能被清除,因此,为了筛选出是由于内存泄漏 而不能清除的引用,同时降低后续定位内存泄漏的计算量,在Activity退出后,内存检测线程如果检测到Activity的引用没有被全部清除,则调用GC函数对这些未能被清除的引用再次尝试清除。Specifically, in the specific implementation process, after the Activity exits, the reference that cannot be cleared may not be cleared due to the memory leak. Therefore, in order to filter out due to memory leak The reference cannot be cleared, and the calculation amount of subsequent positioning memory leak is reduced. After the activity exits, if the memory detection thread detects that the reference of the Activity has not been completely cleared, the GC function is called to try to clear the reference that cannot be cleared again. .
此时,对于并非内存泄漏导致不能被清除的引用将顺利被内存检测线程创建的GC函数清除,而此时依然未能被GC函数清除的一条或多条引用,则就是由于内存泄漏而导致不能清除的引用了。所以,通过上述过程就筛选出了真正定位内存泄漏需要的引用,同时也降低了后续处理的数据量。At this point, for a reference that cannot be cleared by a memory leak, the GC function created by the memory detection thread will be successfully cleared, and one or more references that have not been cleared by the GC function at this time may be caused by a memory leak. Cleared references. Therefore, through the above process, the reference that really needs to locate the memory leak is filtered out, and the amount of data processed later is also reduced.
基于与前述实施例中内存泄漏的检测方法同样的发明构思,本发明第二方面还提供一种内存泄漏的检测装置,如图3所示,包括:Based on the same inventive concept as the method for detecting a memory leak in the foregoing embodiment, the second aspect of the present invention further provides a memory leak detecting device, as shown in FIG. 3, comprising:
监控单元101,用于对目标应用的每个作业Activity进行监控;The monitoring unit 101 is configured to monitor each job activity of the target application;
第一确定单元102,用于当监控到任一所述Activity退出,且所述Activity的一条或多条引用未被清除时,确定所述目标应用存在内存泄漏;The first determining unit 102 is configured to: when monitoring any of the activities to exit, and one or more references of the activity are not cleared, determining that the target application has a memory leak;
倾卸单元103,用于将未被清除的所述一条或多条引用一一倾卸到所述目标应用的一个或多个二进制转储文件hprof文件中,所述hprof文件能够表示出一条引用的引用关系;The dumping unit 103 is configured to dump the one or more references that are not cleared to one or more binary dump file hprof files of the target application, the hprof file can represent a reference Reference relationship
第二确定单元104,用于基于每个所述hprof文件表示所述hprof文件存储的引用的引用关系,确定出每条所述引用的引用链;a second determining unit 104, configured to determine, according to each of the hprof files, a reference relationship of the reference of the hprof file storage, and determine a reference chain of each of the references;
第三确定单元105,用于从一条或多条所述引用链中确定出导致内存泄漏的引用链。The third determining unit 105 is configured to determine, from one or more of the reference chains, a reference chain that causes a memory leak.
具体来讲,第二确定单元104用于解析每个所述hprof文件,获得每个所述hprof文件的引用键,所述hprof文件通过所述引用键表示所述引用关系;基于每个所述引用键,确定每个所述引用的直接引用函数;计算每个所述直接引用函数到用于回收内存的垃圾回收函数的最短路径,所述最短路径为所述引用链。Specifically, the second determining unit 104 is configured to parse each of the hprof files to obtain a reference key of each of the hprof files, and the hprof file represents the reference relationship by using the reference key; A reference key is used to determine a direct reference function for each of the references; a shortest path for each of the direct reference functions to a garbage collection function for reclaiming memory is calculated, the shortest path being the reference chain.
具体来讲,第三确定单元105用于判断每个所述引用链的长度是否超过1;当所述引用链的长度超过1时,确定所述引用链为所述导致内存泄漏的引用链。Specifically, the third determining unit 105 is configured to determine whether the length of each of the reference chains exceeds 1; when the length of the reference chain exceeds 1, determining that the reference chain is the reference chain that causes a memory leak.
进一步,本发明实施例中的内存泄漏的检测装置还可以包括:Further, the device for detecting a memory leak in the embodiment of the present invention may further include:
筛选单元,用于在将未被清除的所述一条或多条引用一一倾卸到所述目标应用的一个或多个二进制转储文件hprof文件中之前,调用垃圾回收函数清除一个或多个所述引用;当存在未能被所述垃圾回收函数清除的一个或多个所述引用时,通知倾卸单元将未被清除的所述一条或多条引用一一倾卸到所述目标应用的一个或多个二进制转储文件hprof文件中的步骤。a filtering unit, configured to call the garbage collection function to clear one or more pieces before dumping the one or more references that are not cleared to one or more binary dump file hprof files of the target application The reference; when there is one or more of the references that are not cleared by the garbage collection function, the notification dumping unit dumps the one or more references that are not cleared to the target application one by one Steps in one or more binary dump files in the hprof file.
更进一步,本发明实施例中的内存泄漏的检测装置还可以包括:Further, the device for detecting a memory leak in the embodiment of the present invention may further include:
记录单元,用于从一个或多个所述引用链中确定出导致内存泄漏的引用链 之后,将所述导致内存泄漏的引用链记录在报告文件中;a recording unit, configured to determine a reference chain that causes a memory leak from one or more of the reference chains After that, the reference chain that causes the memory leak is recorded in the report file;
发送单元,用于向与所述目标应用关联的服务器发送所述报告文件。And a sending unit, configured to send the report file to a server associated with the target application.
前述图1-图2实施例中的内存泄漏的检测方法的各种变化方式和具体实例同样适用于本实施例的内存泄漏的检测装置,通过前述对内存泄漏的检测方法的详细描述,本领域技术人员可以清楚的知道本实施例中内存泄漏的检测装置的实施方法,所以为了说明书的简洁,在此不再详述。Various changes and specific examples of the method for detecting a memory leak in the foregoing embodiments of FIG. 1 to FIG. 2 are also applicable to the memory leak detecting apparatus of the present embodiment, and the foregoing detailed description of the method for detecting a memory leak is known in the art. The method of implementing the memory leak detecting device in this embodiment can be clearly understood by the skilled person, and therefore, for the sake of brevity of the description, it will not be described in detail herein.
本申请实施例中的上述一个或多个技术方案,至少具有如下一种或多种技术效果:The above one or more technical solutions in the embodiments of the present application have at least one or more of the following technical effects:
在本发明实施例的技术方案中,内存检测模块对目标应用的每个作业Activity进行监控,当监控到任一Activity退出,且所述Activity的一条或多条引用未被清除时,确定目标应用存在内存泄漏,然后将未被清除的一条或多条引用一一倾卸到目标应用的一个或多个二进制转储文件hprof文件中,hprof文件能够表示出一条引用的引用关系,进而基于每个hprof文件表示hprof文件存储的引用的引用关系,确定出每条引用的引用链,并从一条或多条引用链中确定出导致内存泄漏的引用链。由上述描述看出,本发明实施例通过内存检测模块实现了自动检测内存泄漏,并且根据hprof文件表示的引用关系确定出确定出导致内存泄漏的引用链,实现了定位内存泄漏的所在位置。由此,解决了现有技术检测内存泄漏依赖人工的技术问题,实现了自动检测并定位内存泄漏的技术效果。In the technical solution of the embodiment of the present invention, the memory detection module monitors each job activity of the target application, and determines that the target application is determined when any Activity is exited and one or more references of the Activity are not cleared. There is a memory leak, and then one or more references that have not been cleaned are dumped into one or more binary dump file hprof files of the target application. The hprof file can represent a reference relationship of the reference, and then based on each The hprof file represents the reference relationship of the reference stored in the hprof file, determines the reference chain for each reference, and determines the chain of references that caused the memory leak from one or more reference chains. It is seen from the above description that the memory detection module automatically detects the memory leak, and determines the reference chain that causes the memory leak according to the reference relationship represented by the hprof file, and realizes the location of the location memory leak. Therefore, the prior art technical problem of detecting memory leakage dependent on manual is solved, and the technical effect of automatically detecting and locating memory leakage is realized.
本领域内的技术人员应明白,本发明的实施例可提供为方法、***、或计算机程序产品。因此,本发明可采用完全硬件实施例、完全软件实施例、或结合软件和硬件方面的实施例的形式。而且,本发明可采用在一个或多个其中包含有计算机可用程序代码的计算机可用存储介质(包括但不限于磁盘存储器、CD-ROM、光学存储器等)上实施的计算机程序产品的形式。Those skilled in the art will appreciate that embodiments of the present invention can be provided as a method, system, or computer program product. Accordingly, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment, or a combination of software and hardware. Moreover, the invention can take the form of a computer program product embodied on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) including computer usable program code.
本发明是参照根据本发明实施例的方法、设备(***)、和计算机程序产品的流程图和/或方框图来描述的。应理解可由计算机程序指令实现流程图和/或方框图中的每一流程和/或方框、以及流程图和/或方框图中的流程和/或方框的结合。可提供这些计算机程序指令到通用计算机、专用计算机、嵌入式处理机或其他可编程数据处理设备的处理器以产生一个机器,使得通过计算机或其他可编程数据处理设备的处理器执行的指令产生用于实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能的装置。The present invention has been described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (system), and computer program products according to embodiments of the invention. It will be understood that each flow and/or block of the flowchart illustrations and/or FIG. These computer program instructions can be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing device to produce a machine for the execution of instructions for execution by a processor of a computer or other programmable data processing device. Means for implementing the functions specified in one or more of the flow or in a block or blocks of the flow chart.
这些计算机程序指令也可存储在能引导计算机或其他可编程数据处理设备以特定方式工作的计算机可读存储器中,使得存储在该计算机可读存储器中的指令产生包括指令装置的制造品,该指令装置实现在流程图一个流程或多个 流程和/或方框图一个方框或多个方框中指定的功能。The computer program instructions can also be stored in a computer readable memory that can direct a computer or other programmable data processing device to operate in a particular manner, such that the instructions stored in the computer readable memory produce an article of manufacture comprising the instruction device. The device is implemented in a flow or a flow chart The functions specified in a block or blocks of a flow and/or block diagram.
这些计算机程序指令也可装载到计算机或其他可编程数据处理设备上,使得在计算机或其他可编程设备上执行一系列操作步骤以产生计算机实现的处理,从而在计算机或其他可编程设备上执行的指令提供用于实现在流程图一个流程或多个流程和/或方框图一个方框或多个方框中指定的功能的步骤。These computer program instructions can also be loaded onto a computer or other programmable data processing device such that a series of operational steps are performed on a computer or other programmable device to produce computer-implemented processing for execution on a computer or other programmable device. The instructions provide steps for implementing the functions specified in one or more of the flow or in a block or blocks of a flow diagram.
显然,本领域的技术人员可以对本发明进行各种改动和变型而不脱离本发明的精神和范围。这样,倘若本发明的这些修改和变型属于本发明权利要求及其等同技术的范围之内,则本发明也意图包含这些改动和变型在内。 It is apparent that those skilled in the art can make various modifications and variations to the invention without departing from the spirit and scope of the invention. Thus, it is intended that the present invention cover the modifications and modifications of the invention

Claims (10)

  1. 一种内存泄漏的检测方法,其特征在于,包括:A method for detecting a memory leak, comprising:
    内存检测模块对目标应用的每个作业Activity进行监控;The memory detection module monitors each job activity of the target application;
    当监控到任一所述Activity退出,且所述Activity的一条或多条引用未被清除时,确定所述目标应用存在内存泄漏;Determining that the target application has a memory leak when monitoring that any of the activities exits and one or more references of the Activity are not cleared;
    将未被清除的所述一条或多条引用一一倾卸到所述目标应用的一个或多个二进制转储文件hprof文件中,所述hprof文件能够表示出一条引用的引用关系;And dumping the one or more references that are not cleared to one or more binary dump file hprof files of the target application, the hprof file being capable of indicating a reference relationship of a reference;
    基于每个所述hprof文件表示所述hprof文件存储的引用的引用关系,确定出每条所述引用的引用链;Determining a reference chain of each of the references based on a reference relationship of each of the hprof files representing a reference to the hprof file storage;
    从一条或多条所述引用链中确定出导致内存泄漏的引用链。A reference chain that causes a memory leak is determined from one or more of the reference chains.
  2. 如权利要求1所述的方法,其特征在于,基于每个所述hprof文件表示所述hprof文件存储的引用的引用关系,确定出每条所述引用的引用链,包括:The method of claim 1, wherein the reference chain of each of the references is determined based on a reference relationship of each of the hprof files representing a reference to the hprof file storage, including:
    解析每个所述hprof文件,获得每个所述hprof文件的引用键,所述hprof文件通过所述引用键表示所述引用关系;Parsing each of the hprof files to obtain a reference key for each of the hprof files, the hprof file representing the reference relationship by the reference key;
    基于每个所述引用键,确定每个所述引用的直接引用函数;Determining a direct reference function for each of the references based on each of the reference keys;
    计算每个所述直接引用函数到用于回收内存的垃圾回收函数的最短路径,所述最短路径为所述引用链。A shortest path is calculated for each of the direct reference functions to a garbage collection function for reclaiming memory, the shortest path being the reference chain.
  3. 如权利要求2所述的方法,其特征在于,从一条或多条所述引用链中确定出导致内存泄漏的引用链,包括:The method of claim 2 wherein the reference chain resulting in a memory leak is determined from one or more of said reference chains, including:
    判断每个所述引用链的长度是否超过1;Determining whether the length of each of the reference chains exceeds 1;
    当所述引用链的长度超过1时,确定所述引用链为所述导致内存泄漏的引用链。When the length of the reference chain exceeds 1, the reference chain is determined to be the reference chain that causes a memory leak.
  4. 如权利要求3所述的方法,其特征在于,在将未被清除的所述一条或多条引用一一倾卸到所述目标应用的一个或多个二进制转储文件hprof文件中之前,还包括:The method of claim 3, wherein before dumping the one or more references that are not cleared one by one into one or more binary dump file hprof files of the target application, include:
    调用垃圾回收函数清除一个或多个所述引用;Calling the garbage collection function to clear one or more of the references;
    当存在未能被所述垃圾回收函数清除的一个或多个所述引用时,执行所述将未被清除的所述一条或多条引用一一倾卸到所述目标应用的一个或多个二进制转储文件hprof文件中的步骤。Performing one or more of the one or more references that are not to be purged to one or more of the target applications when there is one or more of the references that are not cleared by the garbage collection function The steps in the binary dump file hprof file.
  5. 如权利要求4项所述的方法,其特征在于,从一个或多个所述引用链中确定出导致内存泄漏的引用链之后,还包括:The method of claim 4, after determining a reference chain that causes a memory leak from one or more of the reference chains, further comprising:
    将所述导致内存泄漏的引用链记录在报告文件中;Recording the reference chain that causes the memory leak in the report file;
    向与所述目标应用关联的服务器发送所述报告文件。 The report file is sent to a server associated with the target application.
  6. 如权利要求1-5任一项所述的方法,其特征在于,所述内存检测模块对应的代码插桩在所述目标应用的代码中,且所述内存检测模块的代码与所述目标应用的代码在同一级目录下。The method according to any one of claims 1-5, wherein the code corresponding to the memory detection module is inserted in the code of the target application, and the code of the memory detection module and the target application The code is in the same directory.
  7. 如权利要求1-5任一项所述的方法,其特征在于,所述方法还包括:The method of any of claims 1-5, wherein the method further comprises:
    所述目标应用在主线程中执行,所述内存检测模块在不同于主线程的内存检测线程中执行。The target application is executed in a main thread, and the memory detection module executes in a memory detection thread different from the main thread.
  8. 一种内存泄漏的检测装置,其特征在于,包括:A device for detecting a memory leak, comprising:
    监控单元,用于对目标应用的每个作业Activity进行监控;a monitoring unit, configured to monitor each job activity of the target application;
    第一确定单元,用于当监控到任一所述Activity退出,且所述Activity的一条或多条引用未被清除时,确定所述目标应用存在内存泄漏;a first determining unit, configured to determine that the target application has a memory leak when monitoring that any of the activities exits and one or more references of the activity are not cleared;
    倾卸单元,用于将未被清除的所述一条或多条引用一一倾卸到所述目标应用的一个或多个二进制转储文件hprof文件中,所述hprof文件能够表示出一条引用的引用关系;a dumping unit, configured to dump the one or more references that are not cleared one by one into one or more binary dump file hprof files of the target application, the hprof file capable of indicating a reference Citation relationship
    第二确定单元,用于基于每个所述hprof文件表示所述hprof文件存储的引用的引用关系,确定出每条所述引用的引用链;a second determining unit, configured to determine, according to each of the hprof files, a reference relationship of the reference stored by the hprof file, and determine a reference chain of each of the references;
    第三确定单元,用于从一条或多条所述引用链中确定出导致内存泄漏的引用链。And a third determining unit, configured to determine, from one or more of the reference chains, a reference chain that causes a memory leak.
  9. 如权利要求7所述的装置,其特征在于,所述第二确定单元用于解析每个所述hprof文件,获得每个所述hprof文件的引用键,所述hprof文件通过所述引用键表示所述引用关系;基于每个所述引用键,确定每个所述引用的直接引用函数;计算每个所述直接引用函数到用于回收内存的垃圾回收函数的最短路径,所述最短路径为所述引用链。The apparatus according to claim 7, wherein said second determining unit is configured to parse each of said hprof files to obtain a reference key of each of said hprof files, said hprof file being represented by said reference key a reference relationship; determining a direct reference function for each of the references based on each of the reference keys; calculating a shortest path of each of the direct reference functions to a garbage collection function for reclaiming memory, the shortest path being The reference chain.
  10. 如权利要求9所述的装置,其特征在于,所述第三确定单元用于判断每个所述引用链的长度是否超过1;当所述引用链的长度超过1时,确定所述引用链为所述导致内存泄漏的引用链。 The apparatus according to claim 9, wherein said third determining unit is configured to determine whether a length of each of said reference chains exceeds 1; and when said reference chain has a length exceeding 1, determine said reference chain For the reference chain that caused the memory leak.
PCT/CN2017/075737 2016-12-08 2017-03-06 Method and apparatus for detecting memory leakages WO2018103216A1 (en)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN201611119526.7 2016-12-08
CN201611119526.7A CN106649105B (en) 2016-12-08 2016-12-08 A kind of detection method and device of memory overflow

Publications (1)

Publication Number Publication Date
WO2018103216A1 true WO2018103216A1 (en) 2018-06-14

Family

ID=58819122

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2017/075737 WO2018103216A1 (en) 2016-12-08 2017-03-06 Method and apparatus for detecting memory leakages

Country Status (2)

Country Link
CN (1) CN106649105B (en)
WO (1) WO2018103216A1 (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111143100A (en) * 2019-12-09 2020-05-12 北京小米移动软件有限公司 Memory leak detection method, device, equipment and storage medium

Families Citing this family (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107168875B (en) * 2017-05-15 2020-07-03 南京大学 Activity component leakage detection method based on android application multi-entry characteristics
CN107908539B (en) * 2017-07-24 2020-07-17 平安科技(深圳)有限公司 Method, terminal and computer readable storage medium for detecting memory leakage of application program
CN107291598B (en) * 2017-08-07 2018-07-06 腾讯科技(深圳)有限公司 Monitoring method, monitoring device, monitoring device and storage medium
CN107291480B (en) * 2017-08-15 2020-12-15 中国农业银行股份有限公司 Function calling method and device
CN107590013B (en) * 2017-09-05 2020-12-04 南京大学 Efficient method for detecting leakage of Android application Service component
CN108334779B (en) * 2018-01-30 2023-11-21 上海连尚网络科技有限公司 Application processing method, device and computing storage medium
CN109445913B (en) * 2018-10-22 2020-07-03 北京宝兰德软件股份有限公司 Resource object leakage detection method and device
CN115794651B (en) * 2022-12-21 2023-08-18 北京水木羽林科技有限公司 Conflict-free pile inserting method and system supporting multiple binaries

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20050071460A1 (en) * 2003-09-29 2005-03-31 International Business Machines Corporation Automated scalable and adaptive system for memory analysis via identification of leak root candidates
CN104932970A (en) * 2015-05-29 2015-09-23 小米科技有限责任公司 Monitoring method and device of memory leakage
CN105302714A (en) * 2014-07-24 2016-02-03 腾讯科技(深圳)有限公司 Method and apparatus for monitoring memory leak in test process
CN106095689A (en) * 2016-06-24 2016-11-09 北京奇虎科技有限公司 The detection method of a kind of application memory leakage and device

Family Cites Families (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101639804A (en) * 2008-07-29 2010-02-03 国际商业机器公司 Method for confirming memory leakage position in program and device
CN105204996A (en) * 2015-09-24 2015-12-30 努比亚技术有限公司 Memory leak detection device and method and terminal

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20050071460A1 (en) * 2003-09-29 2005-03-31 International Business Machines Corporation Automated scalable and adaptive system for memory analysis via identification of leak root candidates
CN105302714A (en) * 2014-07-24 2016-02-03 腾讯科技(深圳)有限公司 Method and apparatus for monitoring memory leak in test process
CN104932970A (en) * 2015-05-29 2015-09-23 小米科技有限责任公司 Monitoring method and device of memory leakage
CN106095689A (en) * 2016-06-24 2016-11-09 北京奇虎科技有限公司 The detection method of a kind of application memory leakage and device

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111143100A (en) * 2019-12-09 2020-05-12 北京小米移动软件有限公司 Memory leak detection method, device, equipment and storage medium

Also Published As

Publication number Publication date
CN106649105B (en) 2019-06-04
CN106649105A (en) 2017-05-10

Similar Documents

Publication Publication Date Title
WO2018103216A1 (en) Method and apparatus for detecting memory leakages
US10671381B2 (en) Continuous integration with reusable context aware jobs
US10586053B2 (en) Method for automatically detecting security vulnerability based on hybrid fuzzing, and apparatus thereof
US9740594B2 (en) Automated debug trace specification
CN110941528B (en) Log buried point setting method, device and system based on fault
US20080005281A1 (en) Error capture and reporting in a distributed computing environment
US8671397B2 (en) Selective data flow analysis of bounded regions of computer software applications
CN109255240B (en) Vulnerability processing method and device
US20150006961A1 (en) Capturing trace information using annotated trace output
CN105446868A (en) System compatibility testing method, test case management method and related devices
CN110737892B (en) Detection method aiming at APC injection and related device
CN113971031A (en) Software package dependency relationship checking method and device
CN103971056A (en) Method and device for preventing application program in operating system from being uninstalled
US20240160748A1 (en) Method And System For Data Flow Monitoring To Identify Application Security Vulnerabilities And To Detect And Prevent Attacks
CN111125697B (en) Intelligent contract defect triggerability detection method and system based on defect abstract
US8261122B1 (en) Estimation of recovery time, validation of recoverability, and decision support using recovery metrics, targets, and objectives
JP2011145824A (en) Information processing apparatus, fault analysis method, and fault analysis program
KR101500512B1 (en) Device and method for securing computer
WO2015067170A1 (en) Method and system for analyzing android application program
CN113101643A (en) Resource file management method, device, equipment and storage medium
CN112905199A (en) Code update detection method and device
US20140040866A1 (en) Managing code instrumentation in a production computer program
US8291389B2 (en) Automatically detecting non-modifying transforms when profiling source code
WO2020177495A1 (en) Database connection management method and apparatus, and device
CN112528331A (en) Privacy disclosure risk detection method, device and system

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 17878152

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 17878152

Country of ref document: EP

Kind code of ref document: A1