Memory leak detection method and device

By using Hook technology in the iOS operating system to replace the initialization methods of view controllers and view base classes, and utilizing weak reference collections to store feature information of view controllers and views, the problem of rapid location of memory leak detection in iOS development is solved, and the view or view controller with memory leaks can be quickly and accurately located, thereby improving detection efficiency and accuracy.

CN113760711BActive Publication Date: 2025-09-16BEIJING WODONG TIANJUN INFORMATION TECH CO LTD +1
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202011061176.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-09-30
Publication Date
2025-09-16
Estimated Expiration
2040-09-30

AI Technical Summary

Technical Problem

Existing technologies cannot quickly locate views or view controllers with memory leaks in iOS development, and the Leaks tool cannot effectively detect memory leaks caused by view hierarchy relationships.

Method used

By using Hook technology in the iOS operating system to replace the initialization methods of view controllers and view base classes, a weak reference collection is used to store feature information of view controllers and views. After the page is exited, the weak reference collection is analyzed to determine whether there is unreleased feature information to determine whether a memory leak occurs.

Benefits of technology

It can quickly determine whether a memory leak occurs in the page to be detected without affecting the normal operation of the application, and accurately locate the view or view controller that causes the memory leak, thereby improving the efficiency and accuracy of memory leak detection.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113760711B_ABST
    Figure CN113760711B_ABST
Patent Text Reader

Abstract

The present invention discloses a memory leak detection method and device, relating to the field of computer technology. A specific implementation of the method includes: upon entering a page to be detected in an application, initializing a view controller and at least one view of the page to be detected, and storing characteristic information of the view controller and the view in a pre-established weak reference set; after exiting the page to be detected, determining whether a memory leak has occurred in the page to be detected based on the information currently stored in the weak reference set. This implementation can quickly determine whether a memory leak has occurred in the page to be detected in an application.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a memory leak detection method and device. Background Art

[0002] iOS is a mobile operating system. During iOS development, multiple objects are initialized and occupy memory. If these objects don't release memory promptly, memory leaks can occur. Memory leaks waste system memory, slowing application execution and even causing system crashes. Leaks, a performance debugging tool, is commonly used to detect memory leaks.

[0003] In implementing the present invention, the inventors discovered that the prior art suffers from at least the following issues: Using Leaks to detect memory leaks is cumbersome and cannot quickly pinpoint the view or view controller causing the leak. Furthermore, Leaks does not perform view-level processing on the detection results, making it ineffective at pinpointing the problem point causing the memory leak. For example, if view a contains view b, and view b is not released unless view a releases it, then Leaks can only detect that both view a and view b have leaked, but cannot determine that view a is the cause of the leak. Summary of the Invention

[0004] In view of this, an embodiment of the present invention provides a memory leak detection method and apparatus, which can quickly determine whether a memory leak occurs in a page to be detected of an application program.

[0005] To achieve the above object, according to one aspect of the present invention, a memory leak detection method is provided.

[0006] The memory leak detection method of an embodiment of the present invention is applied to the iOS operating system, including: when entering the page to be detected of an application, initializing the view controller and at least one view of the page to be detected, storing the characteristic information of the view controller and the characteristic information of the view in a pre-established weak reference set; after exiting the page to be detected, determining whether a memory leak occurs in the page to be detected based on the information currently stored in the weak reference set.

[0007] Optionally, the initializing the view controller and at least one view of the page to be detected, and storing the characteristic information of the view controller and the characteristic information of the view to a pre-established weak reference set, includes: executing a preset first method to initialize the view controller of the page to be detected, and storing the characteristic information of the view controller to the weak reference set according to the first method; executing a preset second method to initialize at least one view of the page to be detected, and storing the characteristic information of the view to the weak reference set according to the second method.

[0008] Optionally, the memory leak detection method further includes: before entering the page to be detected of the application, replacing the initialization method of the view controller base class of the application with the first method, and replacing the initialization method of the view base class of the application with the second method.

[0009] Optionally, the memory leak detection method further includes: clearing information in the weak reference set before entering the page to be detected of the application.

[0010] Optionally, the memory leak detection method further includes: after exiting the page to be detected, removing feature information containing preset keywords in the weak reference set; wherein the keywords are related to a predetermined view controller or view that is not used for memory leak detection.

[0011] Optionally, determining whether a memory leak occurs in the page to be detected based on information currently stored in the weak reference set includes: determining whether the weak reference set currently stores characteristic information of the view controller or characteristic information of the view of the page to be detected; if so, determining that a memory leak occurs in the page to be detected; otherwise, determining that no memory leak occurs in the page to be detected.

[0012] Optionally, the memory leak detection method further includes: after exiting the page to be detected, if it is determined that the weak reference collection currently stores characteristic information of the view controller of the page to be detected, then determining that the view controller is the problem point that causes the memory leak in the page to be detected; if it is determined that the weak reference collection currently does not store the characteristic information of the view controller of the page to be detected, and stores characteristic information of one or more views of the page to be detected, then determining that the root view among the one or more views is the problem point that causes the memory leak in the page to be detected.

[0013] Optionally, the memory leak detection method further includes: after determining the problem point, storing characteristic information of the problem point in a preset log file.

[0014] Optionally, the characteristic information of the view controller includes at least one of the following: class name, memory address, attribute information, and inheritance information; the characteristic information of the view includes at least one of the following: class name, memory address, attribute information, and inheritance information; a weak reference relationship exists between the weak reference set and the information stored therein, and the weak reference set includes NSPointerArray.

[0015] To achieve the above object, according to another aspect of the present invention, a memory leak detection device is provided.

[0016] The memory leak detection device of an embodiment of the present invention is applied to the iOS operating system and may include: a feature information storage unit, used to: when entering the page to be detected of an application, initialize the view controller and at least one view of the page to be detected, and store the feature information of the view controller and the feature information of the view in a pre-established weak reference set; a detection unit, used to: after exiting the page to be detected, determine whether a memory leak occurs in the page to be detected based on the information currently stored in the weak reference set.

[0017] Optionally, the device may further include a method replacement unit, which is used to: before entering the page to be detected of the application, replace the initialization method of the view controller base class of the application with a preset first method, and replace the initialization method of the view base class of the application with a preset second method; the feature information storage unit is further used to: execute the first method to initialize the view controller of the page to be detected, and store the feature information of the view controller to the weak reference set according to the first method; execute the second method to initialize at least one view of the page to be detected, and store the feature information of the view to the weak reference set according to the second method.

[0018] To achieve the above objective, according to another aspect of the present invention, an electronic device is provided.

[0019] An electronic device of the present invention includes: one or more processors; a storage device for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors implement the memory leak detection method provided by the present invention.

[0020] To achieve the above objective, according to another aspect of the present invention, a computer-readable storage medium is provided.

[0021] A computer-readable storage medium of the present invention stores a computer program, which implements the memory leak detection method provided by the present invention when the program is executed by a processor.

[0022] According to the technical solution of the present invention, the embodiments of the above invention have the following advantages or beneficial effects: Before performing memory leak detection on the application's page to be detected, the initialization method of the application's view controller base class is pre-installed with a preset first method, and the initialization method of the application's view base class is pre-installed with a preset second method using iOS's Hook technology, so that the view controller executes the first method during initialization, and the view executes the second method during initialization. Then, when the page to be detected is entered, when the view controller of the page to be detected is initialized, the view controller's feature information can be stored in a preset weak reference set according to the first method; when the view of the page to be detected is initialized, the view's feature information can be stored in the weak reference set according to the second method. Because the weak reference set does not retain the information stored therein and does not affect the release of the view controller or view, when the page to be detected is exited, the feature information of the normally released view controller or view is cleared from the weak reference set, while the feature information of the view controller or view experiencing a memory leak continues to exist in the weak reference set. Therefore, the information currently stored in the weak reference set can be used to determine whether the page to be detected has a memory leak. Furthermore, the embodiment of the present invention can perform an in-depth analysis of the information in the weak reference set to locate the problem point that causes the memory leak. Specifically, after performing keyword filtering on the information in the weak reference set, if the feature information of the view controller of the page to be detected exists in the weak reference set, it means that the view controller is the problem point; if the feature information of the view controller of the page to be detected does not exist in the weak reference set but the feature information of at least one view of the page to be detected exists, then the root view in the above views (i.e., the view with the smallest index value index) is determined as the problem point, and then the application code can be accurately located based on the feature information of the problem point. Through the above settings, it is possible to quickly determine whether the page to be detected has a memory leak without affecting the normal operation of the application, and then locate the view or view controller that causes the memory leak, which is conducive to finally locking the problem code for rapid repair and avoiding wasting time on views that are not problem points.

[0023] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with specific embodiments. BRIEF DESCRIPTION OF THE DRAWINGS

[0024] The accompanying drawings are provided for a better understanding of the present invention and are not intended to limit the present invention.

[0025] Figure 1 Schematic diagram of the main steps of the memory leak detection method according to an embodiment of the present invention;

[0026] Figure 2 Schematic diagram of a specific implementation of a memory leak detection method according to an embodiment of the present invention;

[0027] Figure 3 Schematic diagram of specific execution steps of the memory leak detection method according to an embodiment of the present invention;

[0028] Figure 4 It is a schematic diagram of components of a memory leak detection device according to an embodiment of the present invention;

[0029] Figure 5 2 is a schematic diagram of the structure of an electronic device used to implement the memory leak detection method in an embodiment of the present invention. DETAILED DESCRIPTION

[0030] The following description of exemplary embodiments of the present invention is made in conjunction with the accompanying drawings, in which various details of the embodiments of the present invention are included to facilitate understanding. These details should be considered as merely exemplary. Therefore, it should be appreciated by those skilled in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for the sake of clarity and conciseness, descriptions of well-known functions and structures are omitted in the following description.

[0031] It should be pointed out that, in the absence of conflict, the embodiments of the present invention and the technical features therein may be combined with each other.

[0032] Figure 1 FIG. 4 is a schematic diagram of the main steps of a memory leak detection method according to an embodiment of the present invention.

[0033] like Figure 1 As shown, the memory leak detection method according to the embodiment of the present invention can be specifically performed according to the following steps:

[0034] Step S101: when entering a page to be detected of an application, initialize a view controller and at least one view of the page to be detected, and store feature information of the view controller and feature information of the view into a pre-established weak reference set.

[0035] The memory leak detection method according to the embodiment of the present invention may be applied in the iOS operating system. The following first describes some concepts related to the present invention in the iOS operating system.

[0036] View Controller and View are concepts in the UI (User Interface) section of iOS development. A view controller is an object that inherits from UIViewController. Each view controller specifically manages an application page and is used to manage the view hierarchy of that page, add the entire view hierarchy to the application window, and handle related user events. The above-mentioned UIViewController is the base class for view controllers. A view is an object that inherits from UIView and is responsible for rendering a rectangular area on the page and responding to user events that occur in that area. An application page can have dozens or even hundreds of views, which are managed by the view controller of that page. Views can be stacked. In iOS, an index value is used to distinguish the hierarchy of each view. The index starts at zero and increases in steps of 1. An index of zero represents the bottom layer of the view hierarchy. View types can include buttons, text, images, etc. The above-mentioned UIView is the base class for views.

[0037] In iOS, data containers are called collections. iOS provides different types of collections, such as NSArray, NSMutableArray, NSPointerArray, NSMapTable, and NSHashTable. These collections can be divided into strong reference collections and weak reference collections based on the reference method. When an object is placed in a strong reference collection, the object's reference count (iOS implements memory management through reference counting; an object can only be released when its reference count reaches zero) increases by 1. The strong reference collection now holds the object and will not release it unless the strong reference collection releases it. When an object is placed in a weak reference collection, the object's reference count does not increase. The weak reference collection does not hold the object and will release it directly when needed, regardless of whether the weak reference collection releases it. In other words, a weak reference relationship exists between the weak reference collection and the objects it stores, and the weak reference collection has no impact on the object's lifecycle. NSArray and NSMutableArray are strong reference collections, while NSPointerArray, NSMapTable, and NSHashTable are weak reference collections. NSPointerArray is a weak reference array that stores ordered data, and the data is stored in the same order as it enters the array.

[0038] The memory leak detection method of an embodiment of the present invention can be executed in an electronic device (e.g., a smart mobile terminal) that has the iOS operating system installed. In step S101, when entering the page to be detected of an application, the view controller and at least one view of the page to be detected are first initialized. After initialization, the characteristic information of the view controller and the characteristic information of the view are stored in a pre-established weak reference set. Generally, the above-mentioned weak reference set can be an NSPointerArray, which can be created when the application is launched. The characteristic information of the view controller is used to identify the view controller and / or describe the properties of the view controller, and the characteristic information of the view is used to identify the view and / or describe the properties of the view. In some embodiments, the characteristic information of the view controller includes at least one of the following: class name, memory address, attribute information, and inheritance information. Among them, the class name of the view controller helps to locate the corresponding application code, the memory address can be used as an identifier of the view controller, the attribute information can include various attributes such as foreground color and background color, and the inheritance information indicates the inheritance relationship of the view controller, such as the parent class and child class of the view controller. The characteristic information of the view includes at least one of the following: class name, memory address, attribute information, and inheritance information. The view class name helps locate the corresponding application code, the memory address can be used as a view identifier, attribute information can include view type, coordinates, dimensions, and other properties, and inheritance information indicates the inheritance relationship of views, such as the view's parent class and child class. In practical applications, weak reference collections can store view controller pointers to reflect view controller characteristics, and weak reference collections can store view pointers to reflect view characteristics.

[0039] As a preferred solution, step S101 can be performed in the following manner: before entering the page to be detected of the application, the initialization method (i.e., init method) of the view controller base class of the application is replaced with a preset first method, and the initialization method (i.e., init method) of the view base class of the application is replaced with a preset second method using the Hook mechanism of iOS. The above replacement can be achieved through the Method Swizzle (method replacement) function provided by iOS. Both the first method and the second method are custom methods. The first method may include the initialization method of the view controller base class and the logic of storing its feature information in a weak reference set after the view controller is initialized. The second method may include the initialization method of the view base class and the logic of storing its feature information in a weak reference set after the view is initialized. In some embodiments, the first method and the second method may also include the following logic: determine whether a weak reference set currently exists: if so, end the process; if not, create a weak reference set.

[0040] It can be understood that since the view controllers of an application are all derived from the view controller base class, implementing the first method in the view controller base class in the above manner enables each view controller to execute the first method upon initialization, thereby achieving the purpose of storing the characteristic information of the view controller of the page to be detected in the weak reference set after initialization in step S101. Similarly, since the views of an application are all derived from the view base class, implementing the second method in the view base class in the above manner enables each view to execute the second method upon initialization, thereby achieving the purpose of storing the characteristic information of the view of the page to be detected in the weak reference set after initialization in step S101. That is, when entering the page to be detected of the application, the first method is executed to initialize the view controller of the page to be detected, and the characteristic information of the view controller is stored in the above-mentioned weak reference set according to the first method; the second method is executed to initialize at least one view of the page to be detected, and the characteristic information of the view is stored in the above-mentioned weak reference set according to the second method.

[0041] Preferably, before executing step S101, the information in the weak reference set can be cleared to avoid bringing redundant information that may have previously existed in the weak reference set into the subsequent analysis process. In a specific application, multiple control buttons, such as a start button and an end button, can be displayed in the outermost view of the application. When the user clicks the start button, the page to be detected is ready to be detected, and the weak reference set can be cleared at this time; when the user clicks the end button, it indicates that the relevant operation (such as entering the page to be detected, exiting the page to be detected, etc.) has been completed, and memory leak analysis can be performed at this time.

[0042] It should be noted that implementing step S101 through Method Swizzle is only an optional method. In actual applications, step S101 can also be implemented by modifying the view controller base class initialization method and the view base class initialization method, or by direct coding.

[0043] Step S102: After exiting the page to be detected, determine whether a memory leak occurs in the page to be detected based on the information currently stored in the weak reference set.

[0044] After step S101, the view controller and feature information of the view of the page to be detected are stored in the weak reference set. Since the weak reference set does not hold the feature information therein, when the page to be detected exits, the feature information of the view controller or view that is normally released will be cleared from the weak reference set. The feature information of the view controller or view that cannot be released for various reasons will remain in the weak reference set. Therefore, in step S102, the memory leak situation can be analyzed based on the information currently stored in the weak reference set. Generally, the way to exit the page to be detected can be to return to the parent page of the page to be detected or to close the page to be detected. It can be understood that step S102 is a memory leak analysis process, which can be executed after the user clicks the above-mentioned end button.

[0045] In some embodiments, the weak reference set may contain information related to specific view controllers or views. These view controllers or views may be managed by the iOS system and are generally not used for memory leak analysis. In other words, the presence of information related to these view controllers or views in the weak reference set does not indicate that a memory leak has occurred in the page to be detected. For example, when entering a certain page, the weak reference set may contain information related to the UITabbarcontroller (a type of view controller). This view controller is managed by the iOS system, and the presence of this information in the weak reference set is not related to a memory leak. In this case, after exiting the page to be detected, the relevant information in the weak reference set can be first queried using a preset keyword (e.g., UITabbarcontroller). If feature information containing the keyword is found, it is cleared to prevent redundant information from affecting subsequent analysis. It is understood that the above keywords are related to predetermined view controllers or views (i.e., the above specific view controllers or views) that are not used for memory leak detection. It should be noted that the above keyword-based filtering step is only a preferred option. In actual applications, even if this step is not performed, the required information can still be extracted from the weak reference set for memory leak analysis.

[0046] In specific applications, it can be determined whether the weak reference collection currently stores the feature information of the view controller of the page to be detected or the feature information of the view: if so, it is determined that a memory leak occurs in the page to be detected; otherwise, it is determined that no memory leak occurs in the page to be detected. After determining that a memory leak occurs in the page to be detected, further, if it is determined that the weak reference collection currently stores the feature information of the view controller of the page to be detected, since the view controller will not release any view unless it releases it, it is determined that the view controller is the problem point that causes the memory leak in the page to be detected; if it is determined that the weak reference collection currently does not store the feature information of the view controller of the page to be detected, and stores the feature information of one or more views of the page to be detected, since the root view (the view with the smallest index) is not released, the other views will not be released, so it is determined that the above-mentioned root view is the problem point that causes the memory leak in the page to be detected. At this time, the index of each view can be compared to locate the root view.

[0047] In particular, if the weak reference set is NSPointerArray, since NSPointerArray stores ordered data, the order in which the data is stored is consistent with the time order in which the data enters the set. Therefore, when determining the root view among multiple views, the view at the front of the NSPointerArray can be directly determined as the root view. For example, the view controller of the page to be detected is A, and the managed views are B, C, and D (the hierarchy increases sequentially). When entering the page to be detected, initialization is performed in the order of A, B, C, and D, and the feature information of A, B, C, and D is stored in NSPointerArray in order from front to back. After exiting the page to be detected, if only the feature information of B, C, and D exists in NSPointerArray, B, which is arranged in the front, can be directly used as the root view without comparing the index of each view. If the weak reference set is NSMapTable or NSHashTable, the root view needs to be determined by comparing the index of the view.

[0048] In an embodiment of the present invention, after identifying a problem point, its characteristic information can be stored in a pre-set log file for subsequent analysis. For example, the problematic code can be directly targeted based on the class name of the view controller or view that is the problem point, thereby quickly resolving a memory leak. In practical applications, after identifying a problem point, all information in the weak reference collection except for the characteristic information of the problem point can be cleared, and the current information in the weak reference collection can be stored in the aforementioned log file.

[0049] Figure 2 FIG. 1 is a schematic diagram showing a specific implementation of a memory leak detection method according to an embodiment of the present invention. Figure 2As shown, in step S201, the application is started. In step S202, a weak reference set is created. In step S203, the weak reference set is first cleared when preparing to perform memory leak detection. In step S204, the page to be detected of the application is entered. In step S205, the view controller and view are initialized, and the feature information of the view controller and the feature information of the view are placed in the weak reference set. In step S206, the page to be detected is exited. In step S207, keyword filtering is performed on the information in the weak reference set. In step S208, it is determined whether the feature information of the view controller or view of the page to be detected currently exists in the weak reference set: if not, it is determined that no memory leak occurs in the page to be detected (step S209); if so, it is determined that a memory leak occurs in the page to be detected (step S210). In step S211, after determining that a memory leak has occurred in the page to be detected, the system determines whether the characteristic information of the view controller of the page to be detected is currently in the weak reference collection. If so, the view controller is identified as the problem point (step S212); otherwise, the root view in the view is identified as the problem point (step S213). In step S214, the characteristic information of the problem point is stored in a log file for subsequent analysis.

[0050] Figure 3 FIG. 1 is a schematic diagram of specific execution steps of the memory leak detection method according to an embodiment of the present invention. Figure 3 As shown, in step S301, the APP (mobile application) is started. In step S302, the initialization methods of the view controller base class and the view base class are hooked (i.e., replaced). In step S303, control buttons such as the start button and the end button are implemented in the outermost view of the APP. In step S304, the user clicks the start button, and the weak reference collection is cleared at this time. In step S305, the page to be tested is jumped to. In step S306, when entering the page to be tested, the view controller and view of the page to be tested are initialized, and the feature information of the view controller and view is stored in the weak reference collection. In step S307, the page to be tested is exited. In step S308, the user clicks the end button to start memory leak analysis. In step S309, a hierarchical analysis is performed on the feature information in the weak reference collection, that is, the view controller or root view is determined to be the problem point. In step S310, keyword filtering is performed on the information in the weak reference collection. In step S311, the feature information of the problem point is stored in a log file. In step S312 , the log is displayed to the user to show the memory leak detection result.

[0051] According to the technical solution of an embodiment of the present invention, before performing memory leak detection on an application's page to be tested, iOS's Hook technology is preliminarily used to replace the initialization method of the application's view controller base class with a preset first method and the initialization method of the application's view base class with a preset second method. This causes the view controller to execute the first method upon initialization, and the view to execute the second method upon initialization. Subsequently, when the page to be tested is entered, when the view controller of the page to be tested is initialized, the view controller's characteristic information is stored in a preset weak reference set according to the first method. When the view of the page to be tested is initialized, the view's characteristic information is stored in the weak reference set according to the second method. Because the weak reference set does not retain the information stored therein, it has no impact on the release of the view controller or view. Therefore, when the page to be tested is exited, the characteristic information of the view controller or view that was released normally is cleared from the weak reference set, while the characteristic information of the view controller or view that experienced a memory leak will continue to exist in the weak reference set. Therefore, the information currently stored in the weak reference set can be used to determine whether the page to be tested has experienced a memory leak. Furthermore, embodiments of the present invention can perform in-depth analysis of the information in the weak reference set to locate the problem causing the memory leak. Specifically, after keyword filtering is performed on the information in the weak reference set, if the feature information of the view controller of the page to be detected exists in the weak reference set, it is indicated that the view controller is the problem point; if the feature information of the view controller of the page to be detected does not exist in the weak reference set but the feature information of at least one view of the page to be detected exists, the root view in the above view is determined as the problem point, and then the application code can be accurately located based on the feature information of the problem point. Through the above settings, it is possible to quickly determine whether a memory leak occurs in the page to be detected without affecting the normal operation of the application, and then locate the view or view controller that causes the memory leak, which is conducive to finally locking the problem code for rapid repair and avoiding wasting time on views that are not problem points.

[0052] It should be noted that for the aforementioned method embodiments, for ease of description, they are expressed as a series of action combinations. However, those skilled in the art should be aware that the present invention is not limited to the order of the actions described, and certain steps can actually be performed in other orders or simultaneously. In addition, those skilled in the art should also be aware that the embodiments described in this specification are all preferred embodiments, and the actions and modules involved are not necessarily required to implement the present invention.

[0053] In order to better implement the above solutions of the embodiments of the present invention, relevant devices for implementing the above solutions are also provided below.

[0054] See also Figure 4As shown, the memory leak detection device 400 provided by the embodiment of the present invention may include a feature information storage unit 401 and a detection unit 402 .

[0055] Among them, the feature information storage unit 401 can be used to: when entering the page to be detected of the application, initialize the view controller and at least one view of the page to be detected, and store the feature information of the view controller and the feature information of the view in a pre-established weak reference set; the detection unit 402 can be used to: after exiting the page to be detected, determine whether a memory leak occurs in the page to be detected based on the information currently stored in the weak reference set.

[0056] In an embodiment of the present invention, the device 400 may further include a method replacement unit, which is used to: before entering the page to be detected of the application, replace the initialization method of the view controller base class of the application with a preset first method, and replace the initialization method of the view base class of the application with a preset second method; the feature information storage unit 401 may be further used to: execute the first method to initialize the view controller of the page to be detected, and store the feature information of the view controller to the weak reference set according to the first method; execute the second method to initialize at least one view of the page to be detected, and store the feature information of the view to the weak reference set according to the second method.

[0057] In a specific application, the apparatus 400 may further include a clearing unit, which is used to clear the information in the weak reference set before entering the page to be detected of the application.

[0058] In actual applications, the device 400 may further include a filtering unit, which is used to: after exiting the page to be detected, remove the feature information containing preset keywords in the weak reference set; wherein, the keywords are related to a predetermined view controller or view that is not used for memory leak detection.

[0059] As a preferred solution, the detection unit 402 can be further used to: determine whether the weak reference collection currently stores characteristic information of the view controller or characteristic information of the view of the page to be detected; if so, determine that a memory leak occurs in the page to be detected; otherwise, determine that no memory leak occurs in the page to be detected.

[0060] Preferably, in an embodiment of the present invention, the detection unit 402 can be further used to: after exiting the page to be detected, if it is determined that the weak reference collection currently stores the characteristic information of the view controller of the page to be detected, then determine that the view controller is the problem point that causes the memory leak in the page to be detected; if it is determined that the weak reference collection currently does not store the characteristic information of the view controller of the page to be detected, and stores the characteristic information of one or more views of the page to be detected, then determine that the root view among the one or more views is the problem point that causes the memory leak in the page to be detected.

[0061] In an optional implementation, the apparatus 400 may further include an output unit, which is configured to: after determining the problem point, store the characteristic information of the problem point in a preset log file.

[0062] In addition, in an embodiment of the present invention, the characteristic information of the view controller includes at least one of the following: class name, memory address, attribute information, and inheritance information; the characteristic information of the view includes at least one of the following: class name, memory address, attribute information, and inheritance information; a weak reference relationship exists between the weak reference set and the information stored therein, and the weak reference set includes NSPointerArray.

[0063] According to the technical solution of an embodiment of the present invention, before performing memory leak detection on an application's page to be tested, iOS's Hook technology is preliminarily used to replace the initialization method of the application's view controller base class with a preset first method and the initialization method of the application's view base class with a preset second method. This causes the view controller to execute the first method upon initialization, and the view to execute the second method upon initialization. Subsequently, when the page to be tested is entered, when the view controller of the page to be tested is initialized, the view controller's characteristic information is stored in a preset weak reference set according to the first method. When the view of the page to be tested is initialized, the view's characteristic information is stored in the weak reference set according to the second method. Because the weak reference set does not retain the information stored therein, it has no impact on the release of the view controller or view. Therefore, when the page to be tested is exited, the characteristic information of the view controller or view that was released normally is cleared from the weak reference set, while the characteristic information of the view controller or view that experienced a memory leak will continue to exist in the weak reference set. Therefore, the information currently stored in the weak reference set can be used to determine whether the page to be tested has experienced a memory leak. Furthermore, embodiments of the present invention can perform in-depth analysis of the information in the weak reference set to locate the problem causing the memory leak. Specifically, after keyword filtering is performed on the information in the weak reference set, if the feature information of the view controller of the page to be detected exists in the weak reference set, it is indicated that the view controller is the problem point; if the feature information of the view controller of the page to be detected does not exist in the weak reference set but the feature information of at least one view of the page to be detected exists, the root view in the above view is determined as the problem point, and then the application code can be accurately located based on the feature information of the problem point. Through the above settings, it is possible to quickly determine whether a memory leak occurs in the page to be detected without affecting the normal operation of the application, and then locate the view or view controller that causes the memory leak, which is conducive to finally locking the problem code for rapid repair and avoiding wasting time on views that are not problem points.

[0064] The present invention also provides an electronic device. The electronic device according to an embodiment of the present invention includes: one or more processors; and a storage device configured to store one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement the memory leak detection method provided by the present invention.

[0065] Reference below Figure 5 , which shows a schematic structural diagram of a computer system 500 of an electronic device suitable for implementing an embodiment of the present invention. Figure 5 The electronic device shown is only an example and should not limit the functions and scope of use of the embodiments of the present invention.

[0066] like Figure 5As shown, the computer system 500 includes a central processing unit (CPU) 501, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 502 or a program loaded from a storage unit 508 into a random access memory (RAM) 503. Various programs and data required for the operation of the computer system 500 are also stored in the RAM 503. The CPU 501, the ROM 502, and the RAM 503 are connected to each other via a bus 504. An input / output (I / O) interface 505 is also connected to the bus 504.

[0067] The following components are connected to the I / O interface 505: an input section 506 including a keyboard, a mouse, and the like; an output section 507 including devices such as a cathode ray tube (CRT), a liquid crystal display (LCD), and a speaker; a storage section 508 including a hard disk; and a communication section 509 including a network interface card such as a LAN card or a modem. The communication section 509 performs communication processing via a network such as the Internet. A drive 510 is also connected to the I / O interface 505 as needed. A removable medium 511, such as a magnetic disk, an optical disk, a magneto-optical disk, or a semiconductor memory, is installed in the drive 510 as needed, so that computer programs read therefrom can be installed in the storage section 508 as needed.

[0068] In particular, according to embodiments disclosed herein, the processes described in the main step diagrams above can be implemented as computer software programs. For example, embodiments of the present invention include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for executing the methods shown in the main step diagrams. In the above embodiments, the computer program can be downloaded and installed from a network via the communication section 509 and / or installed from removable media 511. When the computer program is executed by the central processing unit 501, the above-described functions defined in the system of the present invention are performed.

[0069] It should be noted that the computer-readable medium described in the present invention may be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. A computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or component, or any combination thereof. More specific examples of computer-readable storage media may include, but are not limited to, an electrical connection having one or more conductors, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, device, or component. In the present invention, a computer-readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A computer-readable signal medium may also be any computer-readable medium other than a computer-readable storage medium that can transmit, propagate, or transport a program for use by or in conjunction with an instruction execution system, apparatus, or device. Program code embodied on a computer-readable medium may be transmitted using any suitable medium, including but not limited to wireless, wireline, optical cable, RF, or any suitable combination thereof.

[0070] The flowcharts and block diagrams in the accompanying drawings illustrate the possible implementation architecture, functions and operations of the systems, methods and computer program products according to various embodiments of the present invention. In this regard, each box in the flowchart or block diagram can represent a module, program segment, or a part of code, and the above-mentioned module, program segment, or a part of code contains one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in an order different from that marked in the accompanying drawings. For example, two 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 box in the block diagram or flowchart, and the combination of boxes in the block diagram or flowchart, can be implemented with a dedicated hardware-based system that performs the specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0071] The units involved in the embodiments of the present invention may be implemented in software or hardware. The units described may also be provided in a processor. For example, they may be described as: a processor including a feature information storage unit and a detection unit. The names of these units do not, in some cases, limit the units themselves. For example, the feature information storage unit may also be described as "a unit that provides information in a weak reference set to a detection unit."

[0072] As another aspect, the present invention further provides a computer-readable medium, which may be included in the device described in the above embodiment; or may exist independently and not be assembled into the device. The above computer-readable medium carries one or more programs, and when the above one or more programs are executed by the device, the device performs the following steps: when entering the page to be detected of the application, initializing the view controller and at least one view of the page to be detected, storing the feature information of the view controller and the feature information of the view in a pre-established weak reference set; after exiting the page to be detected, determining whether a memory leak occurs in the page to be detected based on the information currently stored in the weak reference set.

[0073] According to the technical solution of an embodiment of the present invention, before performing memory leak detection on an application's page to be tested, iOS's Hook technology is preliminarily used to replace the initialization method of the application's view controller base class with a preset first method and the initialization method of the application's view base class with a preset second method. This causes the view controller to execute the first method upon initialization, and the view to execute the second method upon initialization. Subsequently, when the page to be tested is entered, when the view controller of the page to be tested is initialized, the view controller's characteristic information is stored in a preset weak reference set according to the first method. When the view of the page to be tested is initialized, the view's characteristic information is stored in the weak reference set according to the second method. Because the weak reference set does not retain the information stored therein, it has no impact on the release of the view controller or view. Therefore, when the page to be tested is exited, the characteristic information of the view controller or view that was released normally is cleared from the weak reference set, while the characteristic information of the view controller or view that experienced a memory leak will continue to exist in the weak reference set. Therefore, the information currently stored in the weak reference set can be used to determine whether the page to be tested has experienced a memory leak. Furthermore, embodiments of the present invention can perform in-depth analysis of the information in the weak reference set to locate the problem causing the memory leak. Specifically, after keyword filtering is performed on the information in the weak reference set, if the feature information of the view controller of the page to be detected exists in the weak reference set, it is indicated that the view controller is the problem point; if the feature information of the view controller of the page to be detected does not exist in the weak reference set but the feature information of at least one view of the page to be detected exists, the root view in the above view is determined as the problem point, and then the application code can be accurately located based on the feature information of the problem point. Through the above settings, it is possible to quickly determine whether a memory leak occurs in the page to be detected without affecting the normal operation of the application, and then locate the view or view controller that causes the memory leak, which is conducive to finally locking the problem code for rapid repair and avoiding wasting time on views that are not problem points.

[0074] The above specific embodiments do not limit the scope of protection of the present invention. Those skilled in the art will appreciate that various modifications, combinations, sub-combinations, and substitutions may occur depending on design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention are intended to be included within the scope of protection of the present invention.

Claims

1. A memory leak detection method, applied to the iOS operating system; characterized in that: include: When entering a page to be detected of an application, initialize a view controller of the page to be detected and at least one view managed by the view controller, store characteristic information of the view controller and characteristic information of the view in a pre-established weak reference set, and a weak reference relationship exists between the weak reference set and the information stored therein; After exiting the page to be detected, determining whether the weak reference set currently stores feature information of the view controller or feature information of the view of the page to be detected; If it is determined that the weak reference set currently stores characteristic information of the view controller of the page to be detected, then it is determined that the view controller is the problem point that causes the memory leak in the page to be detected; If it is determined that the weak reference collection currently does not store the characteristic information of the view controller of the page to be detected, and stores the characteristic information of one or more views of the page to be detected, it is determined that the root view of the one or more views is the problem point that causes the memory leak of the page to be detected.

2. The memory leak detection method according to claim 1, wherein: Initializing the view controller and at least one view of the page to be detected, and storing the characteristic information of the view controller and the characteristic information of the view in a pre-established weak reference set, includes: Execute a preset first method to initialize the view controller of the page to be detected, and store the characteristic information of the view controller into the weak reference set according to the first method; A preset second method is executed to initialize at least one view of the page to be detected, and characteristic information of the view is stored in the weak reference set according to the second method.

3. The memory leak detection method according to claim 2, wherein: The memory leak detection method further comprises: Before entering the page to be detected of the application, the initialization method of the view controller base class of the application is replaced with the first method, and the initialization method of the view base class of the application is replaced with the second method.

4. The memory leak detection method according to claim 1, wherein: The memory leak detection method further comprises: Before entering the page to be detected of the application, the information in the weak reference collection is cleared.

5. The memory leak detection method according to claim 4, characterized in that: The memory leak detection method further comprises: After exiting the page to be detected, the feature information containing preset keywords in the weak reference set is removed; wherein the keywords are related to a predetermined view controller or view that is not used for memory leak detection.

6. The memory leak detection method according to claim 5, characterized in that: If it is determined that the weak reference set does not currently store the feature information of the view controller or the feature information of the view of the page to be detected, it is determined that no memory leak occurs in the page to be detected.

7. The memory leak detection method according to claim 1, wherein: The memory leak detection method further comprises: After the problem point is determined, the characteristic information of the problem point is stored in a preset log file.

8. The memory leak detection method according to any one of claims 1 to 7, characterized in that: The characteristic information of the view controller includes at least one of the following: class name, memory address, attribute information, and inheritance information; The characteristic information of the view includes at least one of the following: class name, memory address, attribute information, and inheritance information.

9. The memory leak detection method according to any one of claims 1 to 7, characterized in that: The weak reference collection includes NSPointerArray, If the weak reference collection is NSPointerArray, when determining the root view among multiple views, directly determine the view at the front of the NSPointerArray as the root view.

10. A memory leak detection device, applied to the iOS operating system; characterized in that: include: a feature information storage unit configured to: upon entering a page to be detected of an application, initialize a view controller of the page to be detected and at least one view managed by the view controller, and store feature information of the view controller and the view in a pre-established weak reference set, wherein a weak reference relationship exists between the weak reference set and the information stored therein; The detection unit is configured to: after exiting the page to be detected, determine whether the weak reference set currently stores characteristic information of the view controller or characteristic information of the view of the page to be detected; If it is determined that the weak reference set currently stores characteristic information of the view controller of the page to be detected, then it is determined that the view controller is the problem point that causes the memory leak in the page to be detected; If it is determined that the weak reference collection currently does not store the characteristic information of the view controller of the page to be detected, and stores the characteristic information of one or more views of the page to be detected, it is determined that the root view of the one or more views is the problem point that causes the memory leak of the page to be detected.

11. The memory leak detection device according to claim 10, wherein: The apparatus further includes a method replacement unit configured to: before entering a page to be detected of an application, replace an initialization method of a view controller base class of the application with a preset first method, and replace an initialization method of a view base class of the application with a preset second method; The feature information storage unit is further used to: execute the first method to initialize the view controller of the page to be detected, and store the feature information of the view controller to the weak reference set according to the first method; execute the second method to initialize at least one view of the page to be detected, and store the feature information of the view to the weak reference set according to the second method.

12. An electronic device, characterized in that: include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 9.

13. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 9 is implemented.

Citation Information

Patent Citations

  • Detection method and device for application memory leaking

    CN106095689A

  • Method and device for analyzing memory objects of mobile application

    CN106776342A