Memory leakage processing method and device, storage medium and electronic equipment

By collecting and filtering objects when the view controller disappears, identifying and uploading memory leak targets, the problem of MLeaksFinder's inability to detect comprehensively is solved, enabling broader memory leak detection and interference-free processing during the testing phase.

CN114595087BActive Publication Date: 2026-03-27SHENZHEN YISHIHUOLALA TECH CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-03-02
Publication Date
2026-03-27

AI Technical Summary

Technical Problem

The existing memory leak detection tool MLeaksFinder cannot comprehensively detect objects other than controllers and their view objects, and it can easily affect program testers during the testing phase.

Method used

When the view controller disappears, collect all the objects it holds, filter and encapsulate them to identify the target objects of the memory leak, and send an indication message to the server to expand the detection scope and avoid the impact of the testing phase.

Benefits of technology

Comprehensive detection of controllers, their view objects, and other non-system objects improves detection coverage and comprehensiveness, avoids interference during the testing phase, and enhances the accuracy and efficiency of memory leak detection.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114595087B_ABST
    Figure CN114595087B_ABST
Patent Text Reader

Abstract

Embodiments of the present application disclose a memory leakage processing method and device, a storage medium and an electronic device. The method comprises: collecting all objects held by a view controller when the view controller disappears, determining target objects with memory leakage from all the objects, and sending a target message indicating that the target objects have memory leakage to a server based on the target objects. The scheme expands the detection range of memory leakage and improves the detection coverage, can more comprehensively detect objects with memory leakage, and uploads the collected memory leakage information to the server and distributes it to relevant personnel for processing, avoiding the impact of memory leakage detection in the testing stage.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of electronic devices, and in particular to a memory leakage processing method and device, a storage medium and an electronic device. BACKGROUND

[0002] MLeaksFinder is a mature memory leakage detection open source tool in the industry, which can detect memory leakage problems of controllers and views in the development process. However, MLeaksFinder only processes controller and view objects, and other objects with memory leakage are prone to be missed. In addition, MLeaksFinder uses a pop-up window to prompt the memory leakage problem, which is likely to affect the program testers in the test stage. SUMMARY

[0003] The embodiments of the present application provide a memory leakage processing method and device, a storage medium and an electronic device, which improve the coverage rate of memory leakage detection.

[0004] In a first aspect, the embodiments of the present application provide a memory leakage processing method, comprising:

[0005] collecting all objects held by the view controller when the view controller disappears;

[0006] determining a target object with memory leakage from all objects;

[0007] sending a target message based on the target object to a server, the target message indicating that the target object has memory leakage.

[0008] In a second aspect, the embodiments of the present application provide a memory leakage processing device, comprising:

[0009] a collection unit configured to collect all objects held by the view controller when the view controller disappears;

[0010] a determination unit configured to determine a target object with memory leakage from all objects;

[0011] a sending unit configured to send a target message based on the target object to a server, the target message indicating that the target object has memory leakage.

[0012] In an embodiment, the determination unit comprises:

[0013] a screening subunit configured to screen objects meeting a preset detection condition from all objects as to-be-detected objects and add the to-be-detected objects to a first set;

[0014] a determination subunit configured to determine a target object with memory leakage from the to-be-detected objects in the first set.

[0015] In an embodiment, the determining sub-unit is configured to:

[0016] encapsulate the to-be-detected object and a host object holding the to-be-detected object to obtain a proxy object;

[0017] perform memory leak detection on the proxy object to determine a target object with memory leak.

[0018] In an embodiment, the determining sub-unit is further configured to:

[0019] send a test message to the proxy object;

[0020] if the proxy object can respond to the test message, determine whether the host object in the proxy object has been released;

[0021] if the host object in the proxy object has been released, determine the to-be-detected object in the proxy object as the target object with memory leak.

[0022] In an embodiment, the screening sub-unit is configured to:

[0023] obtain attribute information of each object in all objects;

[0024] determine whether the current object meets a preset detection condition according to the attribute information.

[0025] In an embodiment, the apparatus further includes:

[0026] an obtaining unit configured to, before determining the target object with memory leak from the to-be-detected objects in the first set, obtain a holding attribute of each object and a class inheritance chain of the object;

[0027] a first processing unit configured to determine a strongly-referenced holding object on each object and the class inheritance chain based on the holding attribute, and add the strongly-referenced holding object to a second set;

[0028] a second processing unit configured to screen the strongly-referenced holding object meeting the preset detection condition from the second set, and add the strongly-referenced holding object to the first set.

[0029] In a third aspect, an embodiment of the present application further provides a computer readable storage medium, wherein the storage medium stores a plurality of instructions, and the instructions are suitable for being loaded by a processor to execute the memory leak processing method.

[0030] In a fourth aspect, an embodiment of the present application further provides an electronic device, including a processor and a memory, wherein the processor is electrically connected with the memory, the memory is configured to store instructions and data, and the processor is configured to execute the memory leak processing method.

[0031] The embodiment of the present application can comprehensively detect the object with memory leakage, not only processing the controller and its view object, but also processing other non-system class objects held by it, expanding the detection range of memory leakage, improving the detection coverage and comprehensiveness; in addition, the collected memory leakage information can be uploaded to the server and distributed to the relevant personnel for processing, avoiding the influence of memory leakage detection in the testing stage. BRIEF DESCRIPTION OF DRAWINGS

[0032] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed in the embodiment description will be briefly introduced. Obviously, the drawings in the following description are only some embodiments of the present application, and other drawings can be obtained by those skilled in the art without creative labor.

[0033] Figure 1 is a flowchart of the memory leakage processing method provided by the embodiment of the present application.

[0034] Figure 2 is another flowchart of the memory leakage processing method provided by the embodiment of the present application.

[0035] Figure 3 is a structure diagram of the memory leakage processing device provided by the embodiment of the present application.

[0036] Figure 4 is a structure diagram of the electronic device provided by the embodiment of the present application.

[0037] Figure 5 is another structure diagram of the electronic device provided by the embodiment of the present application. DETAILED DESCRIPTION

[0038] The technical solutions in the embodiments of the present application will be described clearly and completely below with reference to the drawings in the embodiments of the present application. Obviously, the described embodiments are only some embodiments of the present application, not all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative labor are within the scope of protection of the present application.

[0039] The embodiment of the present application provides a memory leakage processing method, device, storage medium and electronic device. The following will be described in detail.

[0040] In an embodiment, a memory leakage processing method is provided, which is applied to electronic devices such as smart phones, tablet computers, notebook computers, etc. Referring to Figure 1 The specific process of the memory leakage processing method can be as follows:

[0041] 101. When the view controller disappears, collect all objects held by the view controller.

[0042] The view controller (UIViewController) is the basis of the internal structure of an application. Each application contains at least one view controller, and most applications contain multiple view controllers. The UIViewController class defines methods and properties to manage views, handle events, switch from one view controller to another, and coordinate other parts of the application. Custom code needed to implement application behavior can be added by inheriting from UIViewController (or one of its subclasses).

[0043] In the embodiments of the present application, the logic for determining whether the view controller disappears is in the system callback method, which only needs to satisfy that the view controller is being removed from the parent controller or that the view controller is disappearing. Unlike MLeaksFinder, it does not need to combine associated objects in the view controller and navigation controller (UINavigationController) to determine.

[0044] In the embodiments, the holding of objects can be roughly divided into strong reference holding and weak reference holding. Strong reference holds objects, and weak reference does not hold objects. Strong reference can release objects, but weak reference cannot, because weak reference does not hold objects. When a weak reference points to an object held by a strong reference, when the strong reference releases the object, the weak reference will be automatically assigned to nil, i.e., the weak reference will automatically point to nil.

[0045] In practical applications, the objects held by the collected view controller include system objects such as sub-controllers and views, and other non-system class objects. Among them, system objects can be obtained through existing properties exposed by the system, for example, by calling the system method class_copyPropertyList. Other non-system class objects are obtained by traversing the runtime.

[0046] 102. Determine the target object of memory leak from all objects.

[0047] Specifically, in order to improve detection efficiency and reduce unnecessary detection, the collected objects can be preliminarily screened to exclude objects that are unlikely to have memory leaks. For example, detection conditions can be set in advance according to actual needs to include objects that meet the conditions in the detection range. That is, the step of "determining the target object of memory leak from all objects" can include the following processes:

[0048] Filtering objects meeting preset detection conditions from all objects as to-be-detected objects and adding the to-be-detected objects to the first set;

[0049] Determining target objects with memory leaks from the to-be-detected objects in the first set.

[0050] In the implementation process, when filtering to-be-detected objects meeting preset detection conditions from all objects, attribute information of each object in all objects can be acquired, and then it is determined whether the current object meets the preset detection conditions according to the acquired attribute information. For example, the preset detection conditions can be set as "non-class object", "non-system class to which the object belongs", and the like, and whether the object meets the preset detection conditions is determined through the attribute information exposed by the system.

[0051] In actual application, if the order of magnitude of the to-be-detected objects is too large, and each object is scattered, a large amount of resources needs to be consumed to perform the detection operation. Based on this, the related multiple objects can be encapsulated into one object, so as to collect the detection logic. That is, in the embodiment, the step of "determining target objects with memory leaks from the to-be-detected objects in the first set" can include the following logic:

[0052] Encapsulating the to-be-detected objects and the host objects holding the to-be-detected objects to obtain proxy objects;

[0053] Performing memory leak detection on the proxy objects to determine target objects with memory leaks.

[0054] In the embodiment, the related objects (i.e., to-be-detected objects and host objects holding the to-be-detected objects) that need to be detected can be encapsulated in one proxy object, and then the logic of determining whether there is a memory leak can be completed in the proxy object, without being scattered in the to-be-detected objects and the host objects for detection. If not encapsulated, one object needs to be acquired and then detected, and another object needs to be acquired and then detected, so the logic is very scattered. If the objects are first uniformly encapsulated into proxy objects, and then the proxy objects are uniformly traversed and it is determined whether there is a memory leak, the logic is more collected and convenient for extension.

[0055] In an embodiment, when performing memory leak detection on the proxy objects, a test message can be sent to the proxy objects, and whether the detection objects can respond to the message is used as a basis for determining whether the detection objects have memory leaks. That is, the step of "performing memory leak detection on the proxy objects to determine target objects with memory leaks" can include the following process:

[0056] Sending a test message to the proxy objects;

[0057] If the proxy objects can respond to the test message, it is determined whether the host objects in the proxy objects have been released;

[0058] If the host object in the proxy object has been released, the to-be-detected object in the proxy object is determined as the target object of the memory leak.

[0059] If the host object in the proxy object has not been released, the to-be-detected object is not detected.

[0060] In the implementation, after exiting a page, the related resources of the page can not be immediately destroyed and released. In order to ensure the accuracy of the detection, a certain time (for example, 2 seconds) can be delayed before sending the test message for detection.

[0061] In an embodiment, in addition to the self class of the to-be-detected object, the non-system object held by the parent class in the current inheritance chain also needs to be concerned. In order to ensure the accuracy, the non-system object held by the object class and the inheritance chain thereof needs to be recursively traversed and collected. That is, before the target object of the memory leak is determined from the to-be-detected object in the first set, the following process can also be included:

[0062] Obtain the holding attribute of each object and the class inheritance chain thereof in all objects;

[0063] Determine the strong reference holding object of each object and the class inheritance chain thereof based on the holding attribute, and add the strong reference holding object to the second set;

[0064] Filter the strong reference holding object meeting the preset detection condition from the second set, and add the strong reference holding object to the first set.

[0065] Specifically, as long as an object A is strongly referenced and held by an object B, the object A will not be destroyed and released, that is, the potential memory leak object. In the present scheme, the system method class_copyPropertyList can be called to obtain the attribute array of the object, the elements in the attribute array are traversed, and the strong reference holding object is selected and filtered according to the string returned by the system method property_getAttributes. When the strongly referenced and held object is obtained, the object_getIvar method can be used to avoid triggering the calling logic of KVO and causing other problems.

[0066] 103、sending a target message to the server based on the target object, wherein the target message indicates that the target object has a memory leak.

[0067] Specifically, after the target object of the memory leak is determined, the related information of the target object can be uploaded to the server, and then the related developers are assigned to solve the problem according to the uploaded target information. In this way, the pop-up window interference in the test stage is avoided, and the test efficiency is improved.

[0068] As can be seen from the above, the object with the memory leak can be comprehensively detected, not only the view controller and the view object thereof, but also other non-system class objects held thereby, the detection range of the memory leak is expanded, and the detection coverage and comprehensiveness are improved; in addition, the collected memory leak information can be uploaded to the server and distributed to the relevant personnel for processing, so as to avoid the influence of the memory leak detection on the testing stage.

[0069] In yet another embodiment of the present application, a memory leak detection method is also provided, which is optimized on the basis of MLeaksFinder, and the detection range of the memory leak is expanded to cover different stages of software development. Most of the classes in the iOS system are inherited from the NSObject class, so the logic of collecting objects can be processed in the category of the NSObject class. Referring to Figure 2 The memory leak detection logic is as follows:

[0070] (1) Monitoring whether the view controller disappears

[0071] The logic of judging whether the view controller has disappeared is in the system callback method, but it is not necessary to judge in the view controller and the navigation controller in combination with the associated object as in MLeaksFinder, but only one of the following conditions needs to be met:

[0072] a. The view controller is being removed from the parent controller

[0073] b. The view controller is disappearing

[0074] (2) Obtaining existing system objects of the view controller

[0075] In the embodiment, the existing system objects of the view controller can include the controller array (childViewControllers), the modal controller (presentedViewController), and the view object (view).

[0076] (3) Obtaining other non-system objects

[0077] Specifically, it is necessary to judge whether the object belongs to the category of ignored objects. In addition to the class of the object itself, attention should also be paid to the non-system objects held by the parent class in the current inheritance chain. After obtaining the attribute array of a class, the elements in the array need to be traversed, and relevant judgments are made to determine whether the involved object can be collected (see point 8 below).

[0078] (4) Judging whether the object belongs to the ignored category

[0079] In the embodiment, the condition for the object to be ignored can meet one of the following conditions:

[0080] a, the object is a class object

[0081] b, the class of the object is a system class

[0082] Since there is only one copy of the class object at runtime, it will not cause memory leaks, so it can be ignored.

[0083] (5) Determine whether the object has been collected

[0084] Specifically, in order to prevent a dead loop from occurring during collection, it is necessary to determine whether the current object has been collected, which can be handled by using a system collection class.

[0085] (6) Wrap the proxy object and add it to the candidate set

[0086] In this embodiment, the wrapped proxy object includes the object to be detected, the host object (i.e., the object holding the object to be detected), and the holding path (i.e., the holding relationship chain of the object to be detected, which facilitates subsequent problem finding).

[0087] (7) Get the holding properties on the current class and its class inheritance chain

[0088] In order to ensure accuracy, it is necessary to recursively traverse the non-system objects held by the object class and its inheritance chain, and the termination condition satisfies one of the following:

[0089] a, the class of the object does not exist

[0090] b, the class of the object is a system class

[0091] Specifically, before using an object, it can be determined whether it exists to avoid the risk of crashing. In iOS, classes have inheritance relationships, and the final ancestor class of most classes is the NSObject class, which has no parent class.

[0092] (8) Get the properties of the object

[0093] Specifically, the class_copyPropertyList system method is called to obtain the property array of the object, and then the string returned by the system method property_getAttributes is used to determine whether the object is strongly held. When obtaining the strongly held object, the object_getIvar method can be used to obtain it.

[0094] Since the obtained strongly held object may also hold other object properties, it is necessary to re-run the above-mentioned point 3 logic on the obtained object.

[0095] (9) After collecting the proxy object, delay 2s to determine whether the target object of the proxy object has a memory leak, and the judgment logic is as follows:

[0096] If the host object is not released, there is no need to check the target object;

[0097] If the host object is released but the target object is not, it indicates a memory leak in the target object.

[0098] If the current environment is in the testing phase, the collected memory leak objects will be uploaded to the server. If the current environment is in the development phase, a pop-up window will alert developers to the existence of a memory leak.

[0099] This solution handles not only controllers and their view objects, but also other non-system class objects they hold. During the development phase, a pop-up notification appears to alert developers; during the testing phase, no notification appears, and the collected data is uploaded to the server and distributed to relevant developers for resolution. This solution expands the scope of memory leak detection, covering both the software development and testing phases.

[0100] In another embodiment of this application, a memory leak handling device is also provided. This memory leak handling device can be integrated into an electronic device in the form of software or hardware, specifically including devices such as mobile phones, tablets, and laptops. Figure 3 As shown, the memory leak handling device 300 may include: a collection unit 301, a determination unit 302, and a sending unit 304, wherein:

[0101] Collection unit 301 is used to collect all objects held by the view controller when the view controller disappears;

[0102] The determination unit 302 is used to determine the target object of the memory leak from all objects;

[0103] The sending unit 303 is used to send a target message to the server based on the target object, the target message indicating that the target object has a memory leak.

[0104] In one embodiment, the determining unit 302 includes:

[0105] The filtering sub-unit is used to filter out objects that meet the preset detection conditions from all objects as objects to be detected and add them to the first set;

[0106] The determination sub-unit is used to identify the target object with a memory leak from the objects to be detected in the first set.

[0107] In one embodiment, the determining subunit is used for:

[0108] The object to be detected and the host object that strongly references the object to be detected are encapsulated to obtain a proxy object;

[0109] The memory leakage detection is performed on the proxy object, and the target object with the memory leakage is determined.

[0110] In an embodiment, the determining subunit is further configured to:

[0111] sending a test message to the proxy object;

[0112] if the proxy object can respond to the test message, determining whether the host object in the proxy object has been released;

[0113] if the host object in the proxy object has been released, determining the to-be-detected object in the proxy object as the target object with the memory leakage.

[0114] In an embodiment, the screening subunit is configured to:

[0115] acquiring attribute information of each object in all objects;

[0116] judging whether the current object meets the preset detection condition according to the attribute information.

[0117] In an embodiment, the apparatus 300 can further include:

[0118] an acquiring unit configured to acquire a holding attribute of each object and a class inheritance chain of the object before determining the target object with the memory leakage from the to-be-detected object in the first set;

[0119] a first processing unit configured to determine a strong reference holding object on the class inheritance chain of each object based on the holding attribute, and add the strong reference holding object to a second set;

[0120] a second processing unit configured to screen the strong reference holding object meeting the preset detection condition from the second set, and add the strong reference holding object to the first set.

[0121] As can be seen, the memory leakage processing apparatus provided by the embodiments of the present application can comprehensively detect the object with the memory leakage, not only processing the controller and the view object, but also processing other non-system class objects held by it, expanding the detection range of the memory leakage and improving the detection coverage and comprehensiveness; in addition, the collected memory leakage information can be uploaded to a server and distributed to relevant personnel for processing, avoiding the influence of the memory leakage detection on the testing stage.

[0122] In another embodiment of the present application, an electronic device is provided, which can be a smart phone, a tablet computer or the like smart terminal. As shown in Figure 4 the electronic device 400 includes a processor 401 and a memory 402. The processor 401 is electrically connected to the memory 402.

[0123] The processor 401 is the control center of the electronic device 400, and connects various parts of the electronic device through various interfaces and lines, and performs various functions and processes data of the electronic device by running or loading applications stored in the memory 402 and calling data stored in the memory 402, thereby performing overall monitoring of the electronic device.

[0124] In the embodiment, the processor 401 in the electronic device 400 loads instructions corresponding to processes of one or more applications into the memory 402, and runs the applications stored in the memory 402 by the processor 401, thereby implementing various functions according to the following steps:

[0125] When the view controller disappears, all objects held by the view controller are collected;

[0126] The target object of the memory leak is determined from all objects;

[0127] A target message indicating that the target object has a memory leak is sent to the server based on the target object.

[0128] In an embodiment, when the target object of the memory leak is determined from all objects, the processor 401 specifically performs the following operations:

[0129] The objects that meet the preset detection condition are filtered from all objects as to-be-detected objects and added to a first set;

[0130] The target object of the memory leak is determined from the to-be-detected objects in the first set.

[0131] In an embodiment, when the target object of the memory leak is determined from the to-be-detected objects in the first set, the processor 401 can perform the following operations:

[0132] The to-be-detected object and the host object holding the to-be-detected object are encapsulated to obtain a proxy object;

[0133] The proxy object is detected for the memory leak to determine the target object of the memory leak.

[0134] In an embodiment, when the target object of the memory leak is determined from the to-be-detected objects in the first set, the processor 401 can perform the following operations:

[0135] A test message is sent to the proxy object;

[0136] If the proxy object can respond to the test message, it is determined whether the host object in the proxy object has been released;

[0137] If the host object in the proxy object has been released, the to-be-detected object in the proxy object is determined as the target object of the memory leak.

[0138] In an embodiment, when the to-be-detected objects meeting the preset detection condition are screened from all the objects, the processor 401 can perform the following operations:

[0139] Obtain attribute information of each object in all the objects;

[0140] Determine whether the current object meets the preset detection condition according to the attribute information.

[0141] In some embodiments, before the target object of the memory leak is determined from the to-be-detected objects in the first set, the method further includes:

[0142] Obtain the holding attribute of each object and its class inheritance chain in all the objects;

[0143] Determine the strong reference holding object of each object and the class inheritance chain based on the holding attribute, and add it to the second set;

[0144] Screen the strong reference holding object meeting the preset detection condition from the second set, and add it to the first set.

[0145] The memory 402 can be used to store applications and data. The applications stored in the memory 402 include instructions executable in the processor. The applications can constitute various functional modules. The processor 401 executes various functional applications by running the applications stored in the memory 402.

[0146] In some embodiments, as shown in Figure 5 The electronic device 400 further includes a display screen 403, a control circuit 404, a radio frequency circuit 405, an input unit 406, a sensor 408, and a power supply 409. The processor 401 is electrically connected with the display screen 403, the control circuit 404, the radio frequency circuit 405, the input unit 406, the camera 407, the sensor 408, and the power supply 409, respectively.

[0147] The display screen 403 can be used to display information input by a user or provided to the user, and various graphical user interfaces of the electronic device, which can be composed of images, texts, icons, videos, and any combination thereof.

[0148] The control circuit 404 is electrically connected with the display screen 403, and is used to control the display screen 403 to display information.

[0149] The radio frequency circuit 405 is used to transceive radio frequency signals, so as to build wireless communication with the electronic device or other electronic devices, and transceive signals between the server or other electronic devices.

[0150] The input unit 406 can be configured to receive inputted digital, character information or user feature information (e.g., fingerprint), and generate keyboard, mouse, joystick, optical or trackball signal input related to user settings and function control. The input unit 406 can include a fingerprint identification module.

[0151] The camera 407 can be configured to collect image information. The camera can be a single camera with one lens, or can have two or more lenses.

[0152] The sensor 408 can be configured to collect external environment information. The sensor 408 can include an ambient brightness sensor, an acceleration sensor, a light sensor, a motion sensor, and other sensors.

[0153] The power supply 409 can be configured to supply power to various components of the electronic device 400. In some embodiments, the power supply 409 can be logically connected to the processor 401 through a power management system, so that the power management system can be used to manage charging, discharging, and power consumption management.

[0154] Although Figure 5 The electronic device 400 can further include a speaker, a Bluetooth module, and the like, which are not shown in the figure.

[0155] As described above, the electronic device provided by the embodiments of the present application can comprehensively detect objects with memory leaks, not only processing the controller and its view objects, but also processing other non-system objects held by it, thereby expanding the detection range of memory leaks and improving the detection coverage and comprehensiveness. In addition, the collected memory leak information can be uploaded to a server and distributed to relevant personnel for processing, thereby avoiding the impact of memory leak detection on the testing stage.

[0156] In some embodiments, a computer readable storage medium is also provided, which stores a plurality of instructions adapted to be loaded by a processor to execute any of the above memory leak processing methods.

[0157] Those skilled in the art can understand that all or part of the steps in the above embodiments can be completed by programs instructing related hardware, and the programs can be stored in a computer readable storage medium, which can include a read only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, etc.

[0158] The memory leakage processing method, device, storage medium and electronic equipment provided by the embodiments of the present application are described in detail above, and the principles and implementation manners of the present application are described by applying specific examples. The above embodiment description is only used to help understand the method of the present application and its core idea; at the same time, for those skilled in the art, according to the idea of the present application, the specific implementation manner and application range will be changed, and the above description should not be understood as a limitation of the present application.

Claims

1. A method for handling memory leaks, characterized in that, include: When the view controller disappears, collect all objects held by the view controller; Identify the target object of the memory leak from all objects; A target message is sent to the server based on the target object, and the target message indicates that the target object has a memory leak; The process of identifying the target object of the memory leak from all objects includes: Select objects that meet the preset detection conditions from all objects as objects to be detected and add them to the first set; Get the held properties of each object in the entire object set and its class inheritance chain; Based on the holding attributes, each object and the strongly referenced holding objects on the class inheritance chain are determined and added to the second set; Select strongly referenced objects that meet the preset detection conditions from the second set and add them to the first set; Identify the target objects with memory leaks from the objects to be detected in the first set.

2. The memory leak handling method according to claim 1, characterized in that, The step of identifying the target object with a memory leak from the objects to be detected in the first set includes: The object to be detected and the host object that strongly references the object to be detected are encapsulated to obtain a proxy object; Perform memory leak detection on the proxy object to identify the target object with a memory leak.

3. The memory leak handling method according to claim 2, characterized in that, The process of detecting memory leaks in proxy objects and identifying target objects with memory leaks includes: Send a test message to the proxy object; If the proxy object can respond to the test message, then determine whether the host object in the proxy object has been released; If the host object in the proxy object has been released, then the object to be detected in the proxy object is identified as the target object with a memory leak.

4. The memory leak handling method according to claim 1, characterized in that, The step of filtering out objects that meet preset detection conditions from all objects includes: Retrieve the attribute information of each object in all objects; Based on the attribute information, determine whether the current object meets the preset detection conditions.

5. A memory leak handling device, characterized in that, include: A collection unit is used to collect all objects held by the view controller when the view controller disappears. The determining unit is used to determine the target object of the memory leak from all objects, including: filtering objects that meet preset detection conditions from all objects as objects to be detected and adding them to a first set; obtaining the holding attributes of each object and its class inheritance chain in all objects; determining the strong reference holding objects of each object and its class inheritance chain based on the holding attributes and adding them to a second set; filtering the strong reference holding objects that meet the preset detection conditions from the second set and adding them to the first set; and determining the target object with the memory leak from the objects to be detected in the first set. A sending unit is used to send a target message to the server based on the target object, the target message indicating that the target object has a memory leak.

6. The memory leak handling apparatus according to claim 5, characterized in that, The determining unit is used for: Select objects that meet the preset detection conditions from all objects as objects to be detected and add them to the first set; Identify the target objects with memory leaks from the objects to be detected in the first set.

7. A computer-readable storage medium, characterized in that, The storage medium stores a plurality of instructions, which are adapted to be loaded by a processor to execute the memory leak handling method according to any one of claims 1-4.

8. An electronic device, characterized in that, The device includes a processor and a memory, wherein the processor is electrically connected to the memory, and the memory is used to store instructions and data; the processor is used to execute the memory leak handling method according to any one of claims 1-4.

Citation Information

Patent Citations

  • Memory leak detection method and device, computer equipment and storage medium

    CN113407419A