A memory leak detection method, device, equipment and medium
By obtaining object information when the target page exits, determining the strong reference relationship and building a strong reference tree, the problem of limited detection scope in the existing technology is solved, and the detection of memory leaks that are not in circular references is realized, which improves detection accuracy sex.
Patent Information
- Application Number
- CN202111589323.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-23
- Publication Date
- 2024-11-26
- Estimated Expiration
- 2041-12-23
AI Technical Summary
The prior art is limited by the tool's detection object and scope when detecting memory leaks, and cannot detect memory leaks in which objects are not in circular references.
By obtaining object information when the target page exits, determining the strong reference relationship between different objects, building a strong reference tree, finding unreleased nodes as candidate leaked objects, and using circular reference detection tools to determine the target leaked object.
It extends the scope of memory leak detection, improves detection accuracy, and can detect memory leaked objects that are not in circular references.
Smart Images

Figure CN114296977B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of memory processing technology, and in particular to a memory leak detection method, device, equipment and medium. Background Art
[0002] The Objective-C language usually uses reference counting to manage memory. Therefore, when multiple Objective-C objects strongly reference each other and form a circular reference, these objects cannot be released, causing memory leaks.
[0003] In the related art, memory leak detection can be achieved by combining leak detection tools and circular reference detection tools. However, this method is limited by the objects detected by the tools. When the object is not within the detection range of the above tools or is not in a circular reference, memory leaks cannot be detected. Summary of the invention
[0004] In order to solve the above technical problems, the present disclosure provides a memory leak detection method, device, equipment and medium.
[0005] The present disclosure provides a memory leak detection method, the method comprising:
[0006] When the target page is exited, object information of the target page is obtained, where the target page includes a plurality of objects;
[0007] Determine a strong reference relationship between different objects according to the object information;
[0008] Determine a candidate leakage object according to the strong reference relationship between the different objects;
[0009] The target leakage object is determined according to the circular reference relationship of the candidate leakage objects.
[0010] The present disclosure also provides a memory leak detection device, the device comprising:
[0011] An object module, used for obtaining object information of a target page when the target page is exited, wherein the target page includes a plurality of objects;
[0012] A relationship module, used for determining a strong reference relationship between different objects according to the object information;
[0013] A candidate module, used to determine a candidate leakage object according to the strong reference relationship between the different objects;
[0014] A determination module is used to determine a target leakage object according to a circular reference relationship of the candidate leakage objects.
[0015] An embodiment of the present disclosure also provides an electronic device, which includes: a processor; a memory for storing executable instructions of the processor; the processor is used to read the executable instructions from the memory and execute the instructions to implement the memory leak detection method provided in the embodiment of the present disclosure.
[0016] The embodiment of the present disclosure further provides a computer-readable storage medium, wherein the storage medium stores a computer program, and the computer program is used to execute the memory leak detection method provided by the embodiment of the present disclosure.
[0017] The technical solution provided by the embodiment of the present disclosure has the following advantages over the prior art: the memory leak detection solution provided by the embodiment of the present disclosure obtains the object information of the target page when the target page exits, and the target page includes multiple objects; determines the strong reference relationship between different objects based on the object information; determines the candidate leak object based on the strong reference relationship between different objects; and determines the target leak object based on the circular reference relationship of the candidate leak object. With the above technical solution, the candidate leak object can be determined by determining the strong reference relationship between different objects of the page, and then the leak object can be determined based on the circular reference relationship of the candidate leak object. Compared with the detection object limited by the tool in the related technology, the detection scope of memory leaks is expanded and the accuracy of memory leak detection is improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] The above and other features, advantages and aspects of the embodiments of the present disclosure will become more apparent with reference to the following detailed description in conjunction with the accompanying drawings. Throughout the accompanying drawings, the same or similar reference numerals represent the same or similar elements. It should be understood that the drawings are schematic and the originals and elements are not necessarily drawn to scale.
[0019] Figure 1 A flowchart of a memory leak detection method provided by an embodiment of the present disclosure;
[0020] Figure 2 A schematic diagram of an object in a page provided by an embodiment of the present disclosure;
[0021] Figure 3 A flowchart of another memory leak detection method provided by an embodiment of the present disclosure;
[0022] Figure 4 A schematic diagram of a strong reference tree provided in an embodiment of the present disclosure;
[0023] Figure 5 A schematic diagram of the structure of a memory leak detection device provided by an embodiment of the present disclosure;
[0024] Figure 6A schematic diagram of the structure of an electronic device provided in an embodiment of the present disclosure. DETAILED DESCRIPTION
[0025] Embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although certain embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be construed as being limited to the embodiments described herein, which are instead provided for a more thorough and complete understanding of the present disclosure. It should be understood that the drawings and embodiments of the present disclosure are only for exemplary purposes and are not intended to limit the scope of protection of the present disclosure.
[0026] It should be understood that the various steps described in the method embodiments of the present disclosure may be performed in different orders and / or in parallel. In addition, the method embodiments may include additional steps and / or omit the steps shown. The scope of the present disclosure is not limited in this respect.
[0027] The term "including" and its variations used herein are open inclusions, i.e., "including but not limited to". The term "based on" means "based at least in part on". The term "one embodiment" means "at least one embodiment"; the term "another embodiment" means "at least one additional embodiment"; the term "some embodiments" means "at least some embodiments". The relevant definitions of other terms will be given in the following description.
[0028] It should be noted that the concepts such as "first" and "second" mentioned in the present disclosure are only used to distinguish different devices, modules or units, and are not used to limit the order or interdependence of the functions performed by these devices, modules or units.
[0029] It should be noted that the modifications of "one" and "plurality" mentioned in the present disclosure are illustrative rather than restrictive, and those skilled in the art should understand that unless otherwise clearly indicated in the context, it should be understood as "one or more".
[0030] The names of the messages or information exchanged between multiple devices in the embodiments of the present disclosure are only used for illustrative purposes and are not used to limit the scope of these messages or information.
[0031] In the related art, memory leak detection can be achieved by combining a leak detection tool with a circular reference detection tool. For example, the leak detection tool can be MLeaksFinder, and the circular reference detection tool can be FBRetainCycleDetector. MLeaksFinder will traverse the current objects before the page exits, including views and view controllers. For objects that are still not released after 2 seconds of page exit, FBRetainCycleDetector is used to find circular references. However, this method is limited to the objects detected by the tools. For example, the memory leak detection scheme combined with MLeaksFinder and FBRetainCycleDetector has an obvious limitation. MLeaksFinder will only carry out circular reference detection for views and view controllers. When an object is not in a circular reference, it can be released normally, or an object is not a view or view controller and is not within the detection range, then the circular reference will not be detected for the object. Therefore, when the object is not within the detection range of the above-mentioned tool or is not in a circular reference, memory leaks cannot be detected. In order to solve the above problems, the embodiments of the present disclosure provide a memory leak detection method, which is introduced below in conjunction with specific embodiments.
[0032] Figure 1 The present invention provides a flowchart of a memory leak detection method provided by an embodiment of the present invention. The method can be executed by a memory leak detection device, wherein the device can be implemented by software and / or hardware and can generally be integrated in an electronic device. Figure 1 As shown, the method includes:
[0033] Step 101: When a target page is exited, object information of the target page is obtained, where the target page includes multiple objects.
[0034] Memory leak can be understood as the heap memory dynamically allocated in the application is not released or cannot be released by the program for some reason, resulting in a waste of system memory, slowing down the program running speed or even causing serious consequences such as system crash. Applications implemented in Objective-C language usually use reference counting to manage memory and release unused objects. When multiple Objective-C objects strongly reference each other and form a circular reference, these objects cannot be released, causing memory leak. The memory leak detection method of the disclosed embodiment is applied to terminal devices of the IOS operating system.
[0035] The target page can be any page in the application, without specific limitation. The target page can include multiple objects, such as views, view controllers, page classes, and Objective-C classes, without specific limitation. Object information can be understood as a general term for the layout information of the corresponding objects in the target page, and the layout information of each object includes its instance variables, reference relationships, and the value of specific instance variables.
[0036] In an embodiment of the present disclosure, obtaining object information of a target page may include: obtaining instance variables of each object included in the target page through a first variable acquisition interface; obtaining target instance variables referenced by strong references in the instance variables of each object through a variable memory layout acquisition interface, and then obtaining the value of each target instance variable through a second variable acquisition interface.
[0037] Among them, strong reference is also called strong holding. The "__strong" attribute can be used to indicate that any object in memory can hold (retain) other objects. As long as the previous object needs it, the object will remain in the memory. Strong reference exists relative to weak reference. Strong reference prevents the object from being released, while weak reference does not prevent the object from being released. The first variable acquisition interface can be the class_copyIvarList interface, the variable memory layout acquisition interface can be the class_getIvarLayout interface, and the second variable acquisition interface can be the object_getIvar interface. The target instance variable can be an instance variable referenced using a strong reference.
[0038] For example, Figure 2 A schematic diagram of an object in a page provided by an embodiment of the present disclosure, such as Figure 2 As shown, the figure shows three objects of the target page, including object 201, object 202 and object 203. Object 201 can be MYDetailViewController, which can be a class of a view controller. The target page can be a view controller. Object 202 can be MYObjectA, and object 203 can be MYObjectB. Both MYObjectA and MYObjectB are any Objective-C classes.
[0039] right Figure 2The above method can be used to obtain the object information of the three objects in MYDetailViewController. For example, there is a target instance variable named object in MYDetailViewController, and the value of the instance variable is object 202, that is, MYObjectA; MYObjectA has a target instance variable named innerObject, and the value of the instance variable is object 203, that is, MYObjectB; MYObjectB has a target instance variable named delegate, and the value of the instance variable is object 202, that is, MYObjectA object.
[0040] Step 102: Determine the strong reference relationship between different objects according to the object information.
[0041] Among them, a strong reference relationship can be understood as a relationship in which one object strongly references or strongly holds another object.
[0042] In the disclosed embodiment, determining the strong reference relationship between different objects based on object information may include: determining the strong reference relationship between different objects based on the target instance variable of each object included in the target page and the corresponding value. Optionally, determining the strong reference relationship between different objects based on the target instance variable of each object included in the target page and the corresponding value includes: if the value of the target instance variable of the first object is the second object, determining that the first object strongly references the second object.
[0043] Specifically, after obtaining the object information of multiple objects of the target page, you can search in the object information to determine the target instance variable of each object and the corresponding value. If the value of the target instance variable of the first object is the second object, it is determined that there is a strong reference relationship between the first object and the second object. The first object and the second object can be any two objects of the target page.
[0044] See also Figure 2 , object 201 strongly references object 202, and there is a strong reference relationship between object 201 and object 202; object 202 strongly references object 203, and object 203 strongly references object 202, and there is a strong reference relationship between object 202 and object 203.
[0045] Step 103: Determine candidate leakage objects according to the strong reference relationship between different objects.
[0046] The candidate leakage object may be an object that is preliminarily determined to be likely to fail to be released and thus cause memory leakage.
[0047] For example, Figure 3 A flowchart of another memory leak detection method provided by an embodiment of the present disclosure is shown in FIG. Figure 3As shown, in a feasible implementation manner, determining a candidate leakage object according to a strong reference relationship between different objects may include the following steps:
[0048] Step 301: construct a strong reference tree according to the strong reference relationships between different objects.
[0049] Among them, the strong reference tree can be understood as a data structure in the computer field, which uses a tree form to represent the strong reference relationship between different objects, with the objects being nodes and the strong reference relationship between different objects being branches of the tree.
[0050] In an embodiment of the present disclosure, constructing a strong reference tree based on the strong reference relationship between different objects may include: constructing a strong reference tree based on the strong reference relationship between different objects using a directed graph depth traversal algorithm of a stack, wherein the root node of the strong reference tree is the view controller of the target page.
[0051] The depth first search (DFS) algorithm can be a traversal algorithm for graphs and trees. The directed graph depth traversal algorithm using a stack can be understood as starting from a vertex in the graph or tree to visit all vertices in the graph, and each vertex is visited only once. The specific process may include: first visit the specified starting vertex, if there are any adjacent vertices of the currently visited vertex that have not been visited, then select any one to visit; otherwise, return to the most recently visited vertex; until all vertices connected to the starting vertex are visited; if there are still vertices in the graph or tree that have not been visited at this time, then select one of the vertices as the starting vertex and visit it, otherwise, the traversal ends.
[0052] Specifically, the target page can be a view controller, which is used as the root node of the strong reference tree. The depth of the stack is set, and the strong reference tree is constructed using the directed graph depth traversal algorithm of the stack according to the strong reference relationship between different objects. The depth of the stack represents the traversal stop condition, which can be set to 30 layers, for example.
[0053] For example, Figure 4 A schematic diagram of a strong reference tree provided by an embodiment of the present disclosure, such as Figure 4 As shown in the figure, an exemplary strong reference tree is shown, which can be specifically for Figure 3 The strong reference tree constructed by the three objects of the target page in Figure 3 The three objects in are connected, so Figure 3 and Figure 4 The structure of the strong reference tree is the same as Figure 3 If there are disconnected objects in Figure 3 and Figure 4 The structure is different.
[0054] Step 302: By traversing the strong reference tree, objects corresponding to nodes that have not been released are determined as candidate leakage objects.
[0055] Specifically, after constructing the strong reference tree, the memory leak detection device can traverse the strong reference tree to find the unreleased node, and determine the object of the node as a candidate leak object. Figure 4 , in the figure, MYObjectA and MYObjectB can be candidate leakage objects.
[0056] Step 104: Determine the target leakage object according to the circular reference relationship of the candidate leakage objects.
[0057] Among them, a circular reference relationship, also known as a reference loop, can be a relationship under the above-mentioned strong reference relationship. When two objects hold each other, that is, when the two objects strongly reference each other, it is determined that the two objects have a circular reference relationship. For example, if object A strongly holds object B, and object B also strongly holds object A, it is determined that there is a circular reference relationship between objects A and B. The target leak object can be understood as an object that is finally determined to be unable to be released and has a memory leak.
[0058] In an embodiment of the present disclosure, determining a target leakage object according to a circular reference relationship of a candidate leakage object may include: detecting whether the candidate leakage object has a circular reference relationship by a circular reference detection tool; and determining the candidate leakage object having the circular reference relationship as the target leakage object.
[0059] The circular reference detection tool can be a tool for detecting circular reference relationships, and is not limited to specific ones. For example, the above-mentioned FBRetainCycleDetector can be used. Since when two objects have a circular reference relationship, the memory release depends on each other, resulting in that both objects cannot release memory, causing a memory leak. After determining the candidate leak object, the memory leak detection device can use the circular reference detection tool to detect whether the candidate leak object has a circular reference relationship. If a candidate leak object is detected to have a circular reference relationship, the candidate leak object is determined to be a target leak object, and the information of the target leak object can then be reported.
[0060] For example, see Figure 4In the figure, MYObjectA and MYObjectB can be candidate leak objects, and MYObjectA and MYObjectB form a circular reference, that is, they have a circular reference relationship, then MYObjectA and MYObjectB are the final leak objects. Optionally, when MYObjectA and MYObjectB do not have a circular reference relationship, but have a circular reference relationship with another object MYObjectC (not shown in the figure), then only MYObjectA is the final leak object.
[0061] The memory leak detection scheme provided by the embodiment of the present disclosure obtains the object information of the target page when the target page is exited, and the target page includes multiple objects; determines the strong reference relationship between different objects based on the object information; determines the candidate leak object based on the strong reference relationship between different objects; and determines the target leak object based on the circular reference relationship of the candidate leak object. With the above technical scheme, the candidate leak object can be determined by determining the strong reference relationship between different objects of the page, and then the leak object can be determined based on the circular reference relationship of the candidate leak object. Compared with the detection object limited by the tool in the related technology, the detection scope of memory leaks is expanded and the accuracy of memory leak detection is improved.
[0062] In the related art, the method of finding candidate leakage objects through leakage detection tools is: for the exited page (the exited page is a view controller), find the subviews and subview controllers of the view controller through the page hierarchy relationship; then list the subviews and subview controllers that are still not released 2s after the page is exited as candidate leakage objects. Therefore, the candidate leakage objects are all views or view controllers, and cannot be other objects, and there is a possibility of missed detection.
[0063] In this solution, the objects provided to the circular reference detection tool for detection are called candidate leak objects. This solution determines candidate leak objects in the following way: for the exited page, through the strong reference relationship (that is, the strong holding relationship) between objects, a strong reference tree with the exited page as the root node is found, and then the nodes that have not been released are found by traversing the strong reference tree, which are candidate leak objects. The circular reference detection tool searches for reference loops (that is, circular reference relationships) on the candidate leak tool, and deduplicates to determine the final leak object. Since the objects in the strong reference tree are not limited to views and view controllers, the candidate leak objects are not limited to views and view controllers, so that the detection scope is extended from the circular references of views or view controllers to other objects. This solution uses the strong reference relationship in memory to replace the page hierarchical relationship of views and view controllers, and determines the candidate leak objects by constructing a strong reference tree. Then, the leak object can be determined based on the circular reference relationship of the candidate leak object, reducing the possibility of missed detection and improving the accuracy of memory leak detection.
[0064] Figure 5 This is a schematic diagram of the structure of a memory leak detection device provided by an embodiment of the present disclosure. The device can be implemented by software and / or hardware and can generally be integrated into an electronic device. Figure 5 As shown, the device comprises:
[0065] The object module 501 is used to obtain the object information of the target page when the target page is exited, and the target page includes multiple objects;
[0066] A relationship module 502, configured to determine a strong reference relationship between different objects according to the object information;
[0067] A candidate module 503 is used to determine a candidate leakage object according to the strong reference relationship between the different objects;
[0068] The determination module 504 is configured to determine a target leakage object according to the circular reference relationship of the candidate leakage objects.
[0069] Optionally, the object module 501 is specifically used for:
[0070] Acquire the instance variable of each object included in the target page through a first variable acquisition interface;
[0071] The target instance variable referenced by strong reference in the instance variable of each object is obtained through the variable memory layout acquisition interface, and then the value of each target instance variable is obtained through the second variable acquisition interface.
[0072] Optionally, the relationship module 502 is specifically used for:
[0073] According to the target instance variable of each object included in the target page and the corresponding value, a strong reference relationship between different objects is determined.
[0074] Optionally, the relationship module 502 is specifically used for:
[0075] If the value of the target instance variable of the first object is the second object, it is determined that the first object strongly references the second object.
[0076] Optionally, the candidate module 503 is used to:
[0077] Constructing a strong reference tree according to the strong reference relationships between the different objects;
[0078] By traversing the strong reference tree, objects corresponding to nodes that have not been released are determined as candidate leakage objects.
[0079] Optionally, the candidate module 503 is used to:
[0080] A strong reference tree is constructed according to the strong reference relationship between the different objects using a directed graph depth traversal algorithm of a stack, wherein the root node of the strong reference tree is the view controller of the target page.
[0081] Optionally, the determining module 504 is used to:
[0082] Detecting whether the candidate leakage object has a circular reference relationship by using a circular reference detection tool;
[0083] The candidate leakage object having the circular reference relationship is determined as the target leakage object.
[0084] The memory leak detection device provided in the embodiments of the present disclosure can execute the memory leak detection method provided in any embodiment of the present disclosure, and has the corresponding functional modules and beneficial effects of the execution method.
[0085] The embodiments of the present disclosure also provide a computer program product, including a computer program / instruction, which implements the memory leak detection method provided by any embodiment of the present disclosure when executed by a processor.
[0086] Figure 6 This is a schematic diagram of the structure of an electronic device provided by an embodiment of the present disclosure. Figure 6 , which shows a schematic diagram of the structure of an electronic device 600 suitable for implementing the embodiment of the present disclosure. The electronic device 600 in the embodiment of the present disclosure may include, but is not limited to, mobile terminals such as mobile phones, notebook computers, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), vehicle-mounted terminals (such as vehicle-mounted navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Figure 6 The electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.
[0087] like Figure 6 As shown, the electronic device 600 may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 601, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage device 608 into a random access memory (RAM) 603. In the RAM 603, various programs and data required for the operation of the electronic device 600 are also stored. The processing device 601, the ROM 602, and the RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.
[0088] Typically, the following devices may be connected to the I / O interface 605: an input device 606 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 607 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 608 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 609. The communication device 609 may allow the electronic device 600 to communicate with other devices wirelessly or by wire to exchange data. Although Figure 6 The electronic device 600 is shown with various devices, but it should be understood that it is not required to implement or possess all the devices shown. More or fewer devices may be implemented or possessed instead.
[0089] In particular, according to an embodiment of the present disclosure, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program carried on a non-transitory computer-readable medium, and the computer program contains program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device 609, or installed from a storage device 608, or installed from a ROM 602. When the computer program is executed by the processing device 601, the above-mentioned functions defined in the memory leak detection method of the embodiment of the present disclosure are executed.
[0090] It should be noted that the computer-readable medium disclosed above may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present disclosure, a computer-readable storage medium may be any tangible medium containing or storing a program that may be used by or in combination with an instruction execution system, device or device. In the present disclosure, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, in which a computer-readable program code is carried. This propagated data signal may take a variety of forms, including but not limited to an electromagnetic signal, an optical signal, or any suitable combination of the above. The computer readable signal medium may also be any computer readable medium other than a computer readable storage medium, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer readable medium may be transmitted using any suitable medium, including but not limited to: wires, optical cables, RF (radio frequency), etc., or any suitable combination of the above.
[0091] In some embodiments, the client and the server may communicate using any currently known or future developed network protocol such as HTTP (HyperText Transfer Protocol), and may be interconnected with any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include a local area network ("LAN"), a wide area network ("WAN"), an internet (e.g., the Internet), and a peer-to-peer network (e.g., an ad hoc peer-to-peer network), as well as any currently known or future developed network.
[0092] The computer-readable medium may be included in the electronic device, or may exist independently without being incorporated into the electronic device.
[0093] The computer-readable medium carries one or more programs. When the one or more programs are executed by the electronic device, the electronic device: when the target page is exited, obtains the object information of the target page, wherein the target page includes multiple objects; determines the strong reference relationship between different objects according to the object information; determines the candidate leakage object according to the strong reference relationship between the different objects; and determines the target leakage object according to the circular reference relationship of the candidate leakage object.
[0094] Computer program code for performing the operations of the present disclosure may be written in one or more programming languages or a combination thereof, including, but not limited to, object-oriented programming languages, such as Java, Smalltalk, C++, and conventional procedural programming languages, such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., through the Internet using an Internet service provider).
[0095] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present disclosure. In this regard, each square box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, the program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some implementations as replacements, the functions marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two square boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0096] The units involved in the embodiments described in the present disclosure may be implemented by software or hardware, wherein the name of a unit does not, in some cases, limit the unit itself.
[0097] The functions described above herein may be performed at least in part by one or more hardware logic components. For example, without limitation, exemplary types of hardware logic components that may be used include: field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), complex programmable logic devices (CPLDs), and the like.
[0098] In the context of the present disclosure, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or equipment. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0099] The above description is only a preferred embodiment of the present disclosure and an explanation of the technical principles used. Those skilled in the art should understand that the scope of disclosure involved in the present disclosure is not limited to the technical solutions formed by a specific combination of the above technical features, but should also cover other technical solutions formed by any combination of the above technical features or their equivalent features without departing from the above disclosed concept. For example, the above features are replaced with the technical features with similar functions disclosed in the present disclosure (but not limited to) by each other to form a technical solution.
[0100] In addition, although each operation is described in a specific order, this should not be understood as requiring these operations to be performed in the specific order shown or in a sequential order. Under certain circumstances, multitasking and parallel processing may be advantageous. Similarly, although some specific implementation details are included in the above discussion, these should not be interpreted as limiting the scope of the present disclosure. Some features described in the context of a separate embodiment can also be implemented in a single embodiment in combination. On the contrary, the various features described in the context of a single embodiment can also be implemented in multiple embodiments individually or in any suitable sub-combination mode.
[0101] Although the subject matter has been described in language specific to structural features and / or methodological logical actions, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or actions described above. On the contrary, the specific features and actions described above are merely example forms of implementing the claims.
Claims
1. A memory leak detection method, characterized in that: include: When the target page is exited, object information of the target page is obtained, where the target page includes a plurality of objects; Determine a strong reference relationship between different objects according to the object information; Determine a candidate leakage object according to the strong reference relationship between the different objects; Determine the target leakage object according to the circular reference relationship of the candidate leakage object Determining a candidate leakage object according to the strong reference relationship between the different objects includes: Building a strong reference tree according to the strong reference relationships between the different objects, wherein the root node of the strong reference tree is the view controller of the target page; By traversing the strong reference tree, objects corresponding to nodes that have not been released are determined as candidate leakage objects.
2. The method according to claim 1, characterized in that Obtaining the object information of the target page, including: Acquire the instance variable of each object included in the target page through a first variable acquisition interface; The target instance variable referenced by strong reference in the instance variable of each object is obtained through the variable memory layout acquisition interface, and then the value of each target instance variable is obtained through the second variable acquisition interface.
3. The method according to claim 2, characterized in that Determining a strong reference relationship between different objects according to the object information includes: According to the target instance variable of each object included in the target page and the corresponding value, a strong reference relationship between different objects is determined.
4. The method according to claim 3, characterized in that Determining a strong reference relationship between different objects according to a target instance variable and a corresponding value of each object included in the target page includes: If the value of the target instance variable of the first object is the second object, it is determined that the first object strongly references the second object.
5. The method according to claim 1, characterized in that Building a strong reference tree according to the strong reference relationships between the different objects includes: A strong reference tree is constructed according to the strong reference relationship between the different objects using a directed graph depth traversal algorithm of a stack.
6. The method according to claim 1, characterized in that Determining a target leakage object according to a circular reference relationship of the candidate leakage objects includes: Detecting whether the candidate leakage object has a circular reference relationship by using a circular reference detection tool; The candidate leakage object having the circular reference relationship is determined as the target leakage object.
7. A memory leak detection device, characterized in that: include: An object module, used for obtaining object information of a target page when the target page is exited, wherein the target page includes a plurality of objects; A relationship module, used for determining a strong reference relationship between different objects according to the object information; A candidate module, used to determine a candidate leakage object according to the strong reference relationship between the different objects; A determination module, configured to determine a target leakage object according to a circular reference relationship of the candidate leakage objects; The candidate module is used to: construct a strong reference tree according to the strong reference relationship between the different objects, the root node of the strong reference tree is the view controller of the target page; by traversing the strong reference tree, determine the objects corresponding to the nodes that have not been released as the candidate leakage objects.
8. An electronic device, characterized in that: The electronic device comprises: processor; a memory for storing instructions executable by the processor; The processor is used to read the executable instructions from the memory and execute the instructions to implement the memory leak detection method described in any one of claims 1-6.
9. A computer-readable storage medium, characterized in that: The storage medium stores a computer program, and the computer program is used to execute the memory leak detection method described in any one of claims 1 to 6.
Citation Information
Patent Citations
Memory leak analysis method and related device
CN109634835A
Method, system and article for detecting memory leaks in Java software
US20050204342A1
Method for Resolving Memory Leak Problems Resulting from Loitering Objects
US20090282206A1
Statistical analysis of heap dynamics for memory leak investigations
US20110276833A1
