A memory leak detection method and an electronic device

By detecting the memory information of processes, the rate of memory increase and total usage are determined, solving the problem of inaccurate detection of memory leaks in existing technologies. This enables accurate detection and handling of memory leaks, improving the performance of electronic devices.

CN115687035BActive Publication Date: 2026-04-10HONOR DEVICE CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
HONOR DEVICE CO LTD
Filing Date
2021-07-23
Publication Date
2026-04-10

AI Technical Summary

Technical Problem

Existing technologies cannot accurately detect memory leaks in electronic devices, leading to insufficient system memory and affecting device performance.

Method used

By obtaining process memory information, we can determine the process's memory increase rate and total usage to identify whether there is a memory leak. Suspicious processes are added to a gray list and periodically scanned. The gray list is then updated to accurately detect memory leaks.

Benefits of technology

It accurately detects processes with memory leaks, improving the performance of electronic devices and avoiding memory shortages and lag issues.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115687035B_ABST
    Figure CN115687035B_ABST
Patent Text Reader

Abstract

The embodiment of the application provides a memory leakage detection method and electronic equipment, relates to the terminal technical field, and can accurately detect the memory leakage problem of the application in the electronic equipment. The electronic equipment comprises a first process and a second process, and the method comprises the following steps: acquiring first memory information corresponding to the first process and second memory information corresponding to the second process; determining that the first process meets a preset condition and the second process does not meet the preset condition according to the first memory information corresponding to the first process and the second memory information corresponding to the second process; acquiring third memory information corresponding to the first process; and judging whether the first process has memory leakage or not according to the third memory information corresponding to the first process.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of electronic equipment technology, and in particular to a method for detecting memory leaks and an electronic device. Background Technology

[0002] With the development of electronic devices, various applications (APPs) have emerged in large numbers, allowing users to handle many tasks on their devices and providing convenience. However, if an application allocates memory but does not use it for an extended period without releasing it, it will lead to a continuous shortage of system memory, affecting system operation and resulting in a serious memory leak problem. Currently, many applications suffer from memory leaks, which directly causes performance issues such as lag during application use, thus affecting the overall usability of the electronic device. However, current electronic device systems cannot accurately detect memory leaks in applications. Summary of the Invention

[0003] This application provides a method and electronic device for detecting memory leaks, which can accurately detect memory leaks in applications.

[0004] To achieve the above objectives, this application adopts the following technical solution:

[0005] On one hand, this application provides a method for detecting memory leaks, applied to an electronic device. The electronic device includes a first process and a second process. The method includes: acquiring first memory information corresponding to the first process and second memory information corresponding to the second process; determining, based on the first memory information and the second memory information, whether the first process meets preset conditions and whether the second process does not meet preset conditions; acquiring third memory information corresponding to the first process; and determining, based on the third memory information, whether the first process has a memory leak. A process that meets the preset conditions can also be called a suspicious process, such as the first process in this case.

[0006] Therefore, we can first identify the first process in the electronic device that may have a memory leak, and then judge whether the first process has a memory leak based on the memory information corresponding to the first process. This can accurately detect processes with memory leaks, so that appropriate actions can be taken to deal with the processes with memory leaks, thereby improving the performance of the electronic device.

[0007] In one possible implementation, the first memory information includes a first memory acquisition time and the corresponding first memory usage. Determining that the first process meets the preset conditions includes: determining that the first process meets the preset conditions based on the first memory increase rate and / or the first total memory usage of the first process; wherein the first memory increase rate is determined based on at least two first memory acquisition times and the corresponding first memory usage of the first process; and the first total memory usage is determined based on the first memory usage.

[0008] Therefore, by using the first memory increase rate and / or the first total memory usage of the first process, it can be determined whether the first process may have a memory leak, and the process that may have a memory leak in the electronic device can be accurately detected.

[0009] In another possible implementation, the first process is determined to meet the preset conditions based on the first memory increase rate and / or the first total memory usage, including: if the first memory increase rate of the first process is greater than the first rate threshold and / or the first total memory usage is greater than the usage threshold, then the first process is determined to meet the preset conditions.

[0010] Therefore, by identifying processes with excessively high memory growth rates and / or high total memory usage as potentially memory-leaking processes, it is possible to accurately detect processes in electronic devices that may have memory leaks.

[0011] In another possible implementation, the third memory information includes the second memory acquisition time and the corresponding second memory usage. Based on the third memory information corresponding to the first process, it is determined whether the first process has a memory leak, including: determining whether the first process has a memory leak based on at least one of the first process's second memory increase rate, average memory increase rate, and total second memory usage; wherein, the second memory increase rate is determined based on at least two second memory acquisition times and the corresponding second memory usage of the first process; the average memory increase rate is determined based on at least one second memory increase rate; and the total second memory usage is determined based on the second memory usage.

[0012] Therefore, by measuring the memory increase rate and / or total memory usage of the first process, it is possible to accurately determine whether the first process has a memory leak.

[0013] In another possible implementation, the presence of a memory leak in the first process is determined based on at least one of the second memory increase rate, the average memory increase rate, and the second total memory usage, including: if the average memory increase rate of the first process is greater than or equal to the second rate threshold, then the first process is determined to have a memory leak.

[0014] Therefore, the first process, which continuously requests a large amount of memory in a short period of time, is identified as having a memory leak, and it is possible to accurately determine whether the first process has a memory leak.

[0015] In another possible implementation, determining whether the first process has a memory leak based on at least one of the second memory increase rate, the average memory increase rate, and the second total memory usage of the first process further includes: if the average memory increase rate of the first process is less than the second rate threshold, and the number of second memory increase rates is greater than the first number threshold, and a consecutive preset number of second memory increase rates are all greater than the third rate threshold, then it is determined that the first process has a memory leak; wherein, the third rate threshold is less than the second rate threshold.

[0016] Therefore, by determining that although the first process did not request a large amount of memory in a short period of time, it continuously requested memory from the system, and the amount of memory requested was continuously increasing, it was possible to accurately determine whether the first process had a memory leak.

[0017] In another possible implementation, determining whether the first process has a memory leak based on at least one of the second memory increase rate, the average memory increase rate, and the second total memory usage of the first process further includes: if the average memory increase rate of the first process is less than the second rate threshold, and the number of second memory increase rates is greater than the first number threshold, and at least one of the consecutive preset number of second memory increase rates is less than the third rate threshold, and the second total memory usage is greater than the memory threshold, then it is determined that the first process has a memory leak; wherein, the third rate threshold is less than the second rate threshold.

[0018] Therefore, by determining that although the first process did not request a large amount of memory in a short period of time, it continuously requested memory from the system and the total amount of memory requested was relatively large, it was possible to accurately determine whether the first process had a memory leak.

[0019] In another possible implementation, determining whether the first process has a memory leak based on at least one of the second memory increase rate, the average memory increase rate, and the second total memory usage of the first process further includes: if the average memory increase rate of the first process is less than the second rate threshold, and the number of second memory increase rates is greater than the first number threshold, and at least one of the consecutive preset number of second memory increase rates is less than the third rate threshold, and the second total memory usage is less than or equal to the memory threshold, then it is determined that the first process does not have a memory leak.

[0020] Thus, it is possible to determine whether the first process has a memory leak based on the memory increase rate and total memory usage of the first process.

[0021] In another possible implementation, before determining whether the first process has a memory leak based on the third memory information corresponding to the first process, the method further includes: counting the number of cycles for obtaining the third memory information of the first process and the number of second memory occupancy values; determining that the number of cycles is greater than the second threshold, or that the number of second memory occupancy values ​​matches the number of cycles.

[0022] Therefore, by determining the number of memory acquisition cycles of the first process and the number of second memory usages acquired, it is determined that the acquired third memory information corresponding to the first process is sufficient, thereby improving the accuracy of judging whether the first process has a memory leak.

[0023] In another possible implementation, the method further includes: acquiring first memory information corresponding to the first process and second memory information corresponding to the second process in a first cycle; and acquiring third memory information corresponding to the first process in a second cycle.

[0024] Therefore, by periodically acquiring the memory information of processes, it is possible to periodically determine whether there is a memory leak in the processes of electronic devices, thereby improving the performance of electronic devices.

[0025] In another possible implementation, the second cycle is shorter than the first cycle.

[0026] Therefore, by using a finer-grained cycle to obtain the memory information of the first process, it is possible to determine in a timely manner whether the first process has a memory leak, thus improving the accuracy of determining whether the first process has a memory leak.

[0027] In another possible implementation, after determining that the first process meets the preset conditions, the method also includes adding the first process to the gray list.

[0028] Therefore, by adding the first process to the gray list, the first process that may have a memory leak can be recorded.

[0029] In another possible implementation, after adding the first process to the gray list, the method further includes: if the first memory increase rate of the first process is less than or equal to a first rate threshold and / or the total first memory usage is less than or equal to a usage threshold, then the first process is removed from the gray list.

[0030] This enables the determination of whether processes in an electronic device have memory leaks based on the memory information of the first process, so as to update the gray list.

[0031] In another possible implementation, after determining that the first process has a memory leak, the method further includes: displaying a prompt message; wherein the prompt message is used to indicate that the application corresponding to the first process has a memory leak.

[0032] Therefore, by prompting users on the electronic device's display interface that a process with a memory leak exists, users can take appropriate action based on the prompts, thus avoiding performance issues such as insufficient memory or lag in the electronic device.

[0033] On the other hand, this application provides an electronic device, including: one or more processors; a memory leak detection module; a memory leak processing module; a first process and a second process; and a memory; wherein the memory leak detection module is used to acquire first memory information corresponding to the first process and second memory information corresponding to the second process sent by the memory leak processing module; the memory leak detection module is also used to determine, based on the first memory information corresponding to the first process and the second memory information corresponding to the second process, whether the first process meets preset conditions and whether the second process does not meet preset conditions; the memory leak detection module is also used to acquire third memory information corresponding to the first process sent by the memory leak processing module; and the memory leak detection module is also used to determine, based on the third memory information corresponding to the first process, whether the first process has a memory leak.

[0034] Understandably, the memory leak detection module of the electronic device provided above is also used to execute the memory leak detection method provided above. The beneficial effects it can achieve can be referred to in the beneficial effects of the corresponding method provided above, and will not be repeated here.

[0035] In another aspect, this application provides an electronic device, including: a memory, a display screen, and one or more processors; the memory, the display screen, and the processors are coupled. The memory stores computer program code, which includes computer instructions; when the electronic device is running, the processor executes one or more computer instructions stored in the memory to cause the electronic device to perform the memory leak detection method as described in any one of the preceding aspects.

[0036] In another aspect, this application provides a computer storage medium including computer instructions that, when executed on an electronic device, cause the electronic device to perform a memory leak detection method as described in any one aspect.

[0037] In another aspect, this application provides a computer program product that, when run on an electronic device, causes the electronic device to perform the memory leak detection method as described in any one aspect.

[0038] Understandably, the electronic devices, computer storage media, and computer program products provided above are all used to execute the memory leak detection method provided above. Therefore, the beneficial effects they can achieve can be referred to the beneficial effects of the corresponding methods provided above, and will not be repeated here. Attached Figure Description

[0039] Figure 1 Example diagrams illustrating application scenarios of memory leaks provided in embodiments of this application;

[0040] Figure 2 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application;

[0041] Figure 3 A software structure diagram of an electronic device provided in an embodiment of this application;

[0042] Figure 4 Example of an application scenario for a memory leak detection method provided in this application embodiment. Figure 1 ;

[0043] Figure 5 A flowchart illustrating a memory leak method provided in an embodiment of this application;

[0044] Figure 6 An example diagram of an interface call provided in an embodiment of this application;

[0045] Figure 7 Example diagram of another interface call provided in the embodiments of this application;

[0046] Figure 8 Example of an application scenario for a memory leak detection method provided in this application embodiment. Figure 2 ;

[0047] Figure 9 Example of an application scenario for a memory leak detection method provided in this application embodiment. Figure 3 ;

[0048] Figure 10 Example of an application scenario for a memory leak detection method provided in this application embodiment. Figure 4 ;

[0049] Figure 11A Example of an application scenario for a memory leak detection method provided in this application embodiment. Figure 5 ;

[0050] Figure 11B Example of an application scenario for a memory leak detection method provided in this application embodiment. Figure 6 ;

[0051] Figure 12 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation

[0052] The technical solutions of the embodiments of this application will be described below with reference to the accompanying drawings. In the description of the embodiments of this application, unless otherwise stated, " / " means "or," for example, A / B can mean A or B; the term "and / or" in this document is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, and B existing alone.

[0053] Hereinafter, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of indicated technical features. Therefore, a feature defined as "first" or "second" may explicitly or implicitly include one or more of that feature. In the description of embodiments of this application, unless otherwise stated, "a plurality of" means two or more.

[0054] In the embodiments of this application, the terms "exemplary" or "for example" are used to indicate that something is an example, illustration, or description. Any embodiment or design that is described as "exemplary" or "for example" in the embodiments of this application should not be construed as being more preferred or advantageous than other embodiments or design. Specifically, the use of the terms "exemplary" or "for example" is intended to present the relevant concepts in a specific manner.

[0055] Currently, when applications on electronic devices allocate a large amount of memory in a short period of time, and then stop using that memory after use or when the application goes into the background, but fail to release the allocated memory, continuing to occupy it, other applications on the electronic device may experience performance issues such as insufficient available memory or lag when starting or running.

[0056] For example, such as Figure 1 The image shown is an example of an application scenario involving memory leaks. The maximum available memory on an electronic device is 8GB. When launching TikTok... At one point, Douyin requested up to 5GB of memory from the system within a short period (e.g., 3 minutes), while the electronic device only had 3GB of available memory remaining. After use or when running in the background, up to 5GB of memory is no longer needed. Figure 1 In China, Douyin Only 3GB of memory is needed. However, TikTok... It continues to occupy 5GB of memory for extended periods without being used or released. Once other applications are launched on the electronic device, the device may experience insufficient available memory. For example... Figure 1 In the middle, the user launched Douyin. Then, WeChat was launched. and Taobao On WeChat It requested 1GB of memory from the system upon startup, which is available on Taobao. Upon startup, the device requests 2GB of memory from the system. At this point, the device has 0GB of available memory, resulting in insufficient memory for WeChat. and Taobao Other newly launched applications may experience performance issues such as lag during operation.

[0057] Traditional methods for detecting memory leaks mostly involve determining whether an application's memory usage time or size exceeds a certain threshold. If it does, it's assumed that memory is not being released or is being released slowly. However, if the application's prolonged or large memory usage is a normal business requirement, this could lead to a false positive.

[0058] In another scenario, traditional detection methods can also determine if an application has a memory leak based on its abnormal responses. However, there are situations where an application has a memory leak but does not exhibit any abnormal responses, preventing the electronic device's system from promptly recognizing the memory leak.

[0059] For example, Android's memory mechanism is controlled by the system, which allocates memory for applications and periodically reviews and reclaims it. However, there are situations where the corresponding memory is still occupied by a process, which can cause memory reclamation to fail, resulting in an error message and an abnormal response from the system. However, this situation is sporadic and cannot handle most memory leaks; that is, most applications do not provide an abnormal response when memory leaks occur.

[0060] For example, such as Figure 1 As shown, Douyin Within a short period of time (e.g., 3 minutes), it requested up to 5GB of memory from the system, in Douyin. After use or when running in the background, Douyin no longer needs up to 5GB of memory. Only 3GB of memory is needed. However, TikTok... It continues to occupy 5GB of memory for an extended period, without being used or released. At this point, only 3GB of available memory remains on the electronic device. (TikTok) A memory leak has occurred. However, TikTok... The system failed to detect the abnormality, thus preventing the electronic device's system from promptly notifying it of Douyin (TikTok). There is a memory leak.

[0061] This application provides a method for detecting memory leaks. This method can identify processes with excessively high memory requests or high total memory usage within a short period, identifying these processes as suspicious. Furthermore, it determines whether a suspicious process has a memory leak by capturing its memory information. The process can be a system application or a third-party application (e.g., an application downloaded from an app store or app market). This method can accurately detect processes with memory leaks, allowing for appropriate handling and improving the performance of electronic devices.

[0062] For example, the memory leak detection method provided in this application embodiment can be applied to electronic devices with displays such as mobile phones, tablets, personal computers (PCs), personal digital assistants (PDAs), smartwatches, netbooks, wearable electronic devices, augmented reality (AR) devices, virtual reality (VR) devices, in-vehicle devices, smart cars, and smart speakers. This application embodiment does not impose any limitations on this.

[0063] For example, Figure 2 A schematic diagram of one structure of an electronic device 100 is shown. For example... Figure 2 As shown, the electronic device may include: a processor 110, an external memory interface 120, an internal memory 121, a universal serial bus (USB) interface 130, a charging management module 140, a power management module 141, a battery 142, an antenna 1, an antenna 2, a mobile communication module 150, a wireless communication module 160, an audio module 170, a speaker 170A, a receiver 170B, a microphone 170C, a headphone jack 170D, a sensor module 180, buttons 190, a motor 191, an indicator 192, a camera 193, a display screen 194, and a subscriber identification module (SIM) card interface 195, etc.

[0064] It is understood that the structure illustrated in this embodiment does not constitute a specific limitation on the electronic device. In other embodiments, the electronic device may include more or fewer components than illustrated, or combine some components, or split some components, or have different component arrangements. The illustrated components may be implemented in hardware, software, or a combination of software and hardware.

[0065] Processor 110 may include one or more processing units, such as: application processor (AP), modem processor, graphics processing unit (GPU), image signal processor (ISP), controller, memory, video codec, digital signal processor (DSP), baseband processor, and / or neural network processing unit (NPU), etc. Different processing units may be independent devices or integrated into one or more processors.

[0066] A controller can be the nerve center and command center of an electronic device. Based on the instruction opcode and timing signals, the controller generates operation control signals to control the fetching and execution of instructions.

[0067] The processor 110 may also include a memory for storing instructions and data. In some embodiments, the memory in the processor 110 is a cache memory. This memory can store instructions or data that the processor 110 has just used or that are used repeatedly. If the processor 110 needs to use the instruction or data again, it can retrieve it directly from the memory. This avoids repeated accesses, reduces the waiting time of the processor 110, and thus improves the efficiency of the system.

[0068] In some embodiments, the processor 110 may include one or more interfaces. Interfaces may include an inter-integrated circuit (I2C) interface, an inter-integrated circuit sound (I2S) interface, a pulse code modulation (PCM) interface, a universal asynchronous receiver / transmitter (UART) interface, a mobile industry processor interface (MIPI), a general-purpose input / output (GPIO) interface, a subscriber identity module (SIM) interface, and / or a universal serial bus (USB) interface, etc.

[0069] It is understood that the interface connection relationships between the modules illustrated in this embodiment are merely illustrative and do not constitute a structural limitation on the electronic device. In other embodiments, the electronic device may also employ different interface connection methods or combinations of multiple interface connection methods as described in the above embodiments.

[0070] The charging management module 140 receives charging input from a charger. The charger can be a wireless charger or a wired charger. In some wired charging embodiments, the charging management module 140 receives charging input from the wired charger via a USB interface 130. In some wireless charging embodiments, the charging management module 140 receives wireless charging input via the wireless charging coil of the electronic device. While charging the battery 142, the charging management module 140 can also supply power to the electronic device via the power management module 141.

[0071] The power management module 141 connects the battery 142, the charging management module 140, and the processor 110. The power management module 141 receives input from the battery 142 and / or the charging management module 140, providing power to the processor 110, internal memory 121, external memory, display screen 194, camera 193, and wireless communication module 160, etc. The power management module 141 can also monitor parameters such as battery capacity, battery cycle count, and battery health status (leakage current, impedance). In some other embodiments, the power management module 141 may also be located within the processor 110. In other embodiments, the power management module 141 and the charging management module 140 may be located in the same device.

[0072] The wireless communication function of electronic devices can be realized through antenna 1, antenna 2, mobile communication module 150, wireless communication module 160, modem processor and baseband processor, etc.

[0073] Electronic devices implement display functions through a GPU, a display screen 194, and an application processor. The GPU is a microprocessor for image processing, connecting the display screen 194 and the application processor. The GPU is used to perform mathematical and geometric calculations and for graphics rendering. The processor 110 may include one or more GPUs, which execute program instructions to generate or modify display information.

[0074] Display screen 194 is used to display images, videos, etc. Display screen 194 includes a display panel. The display panel can be a liquid crystal display (LCD), an organic light-emitting diode (OLED), an active-matrix organic light-emitting diode (AMOLED), a flexible light-emitting diode (FLED), a Mini LED, a MicroLED, a Micro-OLED, a quantum dot light-emitting diode (QLED), etc.

[0075] Electronic devices can achieve shooting functions through ISP, camera 193, video codec, GPU, display 194 and application processor.

[0076] The ISP (Image Signal Processor) is used to process data fed back from the camera 193. For example, when taking a picture, the shutter is opened, and light is transmitted through the lens to the camera's photosensitive element. The light signal is converted into an electrical signal, and the camera's photosensitive element transmits the electrical signal to the ISP for processing, transforming it into an image visible to the naked eye. The ISP can also perform algorithmic optimization of image noise, brightness, and skin tone. The ISP can also optimize parameters such as exposure and color temperature of the shooting scene. In some embodiments, the ISP can be set in the camera 193.

[0077] Camera 193 is used to capture still images or videos. An object is projected onto a photosensitive element by generating an optical image through the lens. The photosensitive element can be a charge-coupled device (CCD) or a complementary metal-oxide-semiconductor (CMOS) phototransistor. The photosensitive element converts the light signal into an electrical signal, which is then passed to an ISP for conversion into a digital image signal. The ISP outputs the digital image signal to a DSP for processing. The DSP converts the digital image signal into image signals in standard RGB, YUV, or other formats. In some embodiments, the electronic device may include one or N cameras 193, where N is a positive integer greater than 1.

[0078] Digital signal processors (DSPs) are used to process digital signals. Besides digital image signals, they can also process other digital signals. For example, when an electronic device is selecting a frequency, a DSP can perform a Fourier transform on the frequency energy.

[0079] Video codecs are used to compress or decompress digital video. Electronic devices can support one or more video codecs. This allows the electronic device to play or record video in various encoded formats, such as Moving Picture Experts Group (MPEG) 1, MPEG2, MPEG3, MPEG4, etc.

[0080] An NPU (Neural Processing Unit) is a computational processor for neural networks (NNs). By borrowing the structure of biological neural networks, such as the transmission patterns between neurons in the human brain, it can rapidly process input information and continuously learn on its own. NPUs enable intelligent cognitive applications in electronic devices, such as image recognition, facial recognition, speech recognition, and text understanding.

[0081] Electronic devices can implement audio functions such as music playback and recording through audio modules 170, speakers 170A, receivers 170B, microphones 170C, headphone jacks 170D, and application processors.

[0082] Audio module 170 is used to convert digital audio information into analog audio signal output, and also to convert analog audio input into digital audio signal. Audio module 170 can also be used for encoding and decoding audio signals. In some embodiments, audio module 170 may be located in processor 110, or some functional modules of audio module 170 may be located in processor 110. Speaker 170A, also called a "loudspeaker," is used to convert audio electrical signals into sound signals. Receiver 170B, also called a "handset," is used to convert audio electrical signals into sound signals. Microphone 170C, also called a "microphone" or "microphone," is used to convert sound signals into electrical signals.

[0083] The 170D headphone jack is used to connect wired headphones. The 170D headphone jack can be a USB 130 interface or a 3.5mm Open Mobile Terminal Platform (OMTP) standard interface, a CTIA (Cellular Telecommunications Industry Association of the USA) standard interface.

[0084] The external storage interface 120 can be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the electronic device. The external memory card communicates with the processor 110 through the external storage interface 120 to perform data storage functions. For example, audio and video files can be stored on the external memory card.

[0085] Internal memory 121 can be used to store computer executable program code, which includes instructions. Processor 110 executes various functional applications and data processing of the electronic device by running the instructions stored in internal memory 121. For example, in this embodiment, processor 110 can execute instructions stored in internal memory 121, which may include a program storage area and a data storage area.

[0086] The program storage area can store the operating system, at least one application program required for a function (such as sound playback, image playback, etc.). The data storage area can store data created during the use of the electronic device (such as audio data, phonebook, etc.). Furthermore, the internal memory 121 can include high-speed random access memory, and may also include non-volatile memory, such as at least one disk storage device, flash memory device, universal flash storage (UFS), etc.

[0087] Internal memory 121, also known as RAM, is used to store information during system operation. In this embodiment, when a user launches an application on an electronic device, the application requests corresponding memory from the system. For example, when a user launches WeChat... At that time, WeChat It will request 2GB of memory from the system; when the user launches Meituan... At that time, Meituan It will request 1.5G of memory from the system, and so on.

[0088] It should be noted that the amount of memory requested by the system when the application starts is only an example. The amount of memory requested by the application when starting depends on the actual situation and is not limited here.

[0089] In this embodiment of the application, the processor 110 can detect suspicious applications corresponding to suspicious processes by running instructions stored in the internal memory 121, and accurately detect applications that have memory leaks based on the memory usage of the suspicious applications, so as to process the applications that have been found to have memory leaks.

[0090] The software system of electronic device 100 can adopt a layered architecture, event-driven architecture, microkernel architecture, microservice architecture, or cloud architecture. This application embodiment uses the layered architecture Android system as an example to exemplify the software structure of electronic device 100.

[0091] Figure 3 This is a software structure diagram of an electronic device provided in an embodiment of this application.

[0092] Understandably, a layered architecture divides software into several layers, each with a clear role and function. Layers communicate with each other through software interfaces. In some embodiments, the Android system may include an application layer (referred to as the application layer), an application framework layer (referred to as the framework layer), the Android runtime, system libraries, and the Linux kernel layer. The aforementioned application layer may include a series of application packages.

[0093] like Figure 3 As shown, the application package may include system applications. System applications refer to applications installed on the electronic device before it leaves the factory. For example, system applications may include applications such as Settings, Device Manager, Camera, Gallery, Calendar, Music, SMS, and Calls.

[0094] Application packages can also include third-party applications, which are applications that users install after downloading and installing them from an app store (or app market). For example, map applications (such as Baidu Maps). Gaode Map (etc.), food delivery apps (such as Meituan) Eleme Reading applications (such as e-books) ), social applications (such as WeChat) ) and ride-hailing apps (such as Didi Chuxing) )wait.

[0095] The application framework layer described above provides an application programming interface (API) and programming framework for applications in the application layer. The application framework layer includes some predefined functions.

[0096] like Figure 3 As shown, the application framework layer may include a window manager, content provider, view system, phone manager, resource manager, notification manager, etc.

[0097] The window manager is used to manage windowed applications. It can retrieve screen size, determine the presence of a status bar, lock the screen, and capture screenshots, among other things.

[0098] Content providers store and retrieve data, making that data accessible to applications. This data can include videos, images, audio, phone calls made and received, browsing history and bookmarks, phone books, and more.

[0099] A view system includes visual controls, such as controls for displaying text and controls for displaying images. View systems can be used to build applications. A display interface can consist of one or more views. For example, a display interface including a text notification icon could include views for displaying text and views for displaying images.

[0100] The phone manager is used to provide communication functions for electronic device 100. For example, it manages call status (including connection and disconnection).

[0101] The file explorer provides applications with various resources, such as localized strings, icons, images, layout files, video files, and more.

[0102] The notification manager allows applications to display notifications in the status bar. These notifications can be used to deliver informational messages and can disappear automatically after a short pause, requiring no user interaction. For example, the notification manager can be used to notify users of download completion or message alerts. The notification manager can also display notifications as icons or scrolling text in the top status bar, such as notifications from background applications, or as dialog boxes on the screen. Examples include displaying text messages in the status bar, emitting sounds, vibrating electronic devices, and flashing indicator lights.

[0103] The Android Runtime consists of core libraries and a virtual machine. The Android runtime is responsible for the scheduling and management of the Android system.

[0104] The core library consists of two parts: one part is the functionalities that need to be called by the Java language, and the other part is the Android core library.

[0105] The application layer and application framework layer run in a virtual machine. The virtual machine executes the Java files of the application layer and application framework layer as binary files. The virtual machine is used to perform functions such as object lifecycle management, stack management, thread management, security and exception management, and garbage collection.

[0106] System libraries can include multiple functional modules. For example: surface manager, media libraries, 3D graphics processing libraries (e.g., OpenGL ES), 2D graphics engines (e.g., SGL), daemon modules, etc.

[0107] The kernel layer is the layer between hardware and software. The kernel layer includes at least the display driver, camera driver, audio driver, and sensor driver. For example... Figure 3 As shown, the kernel layer also includes a memory viewing module and a log information capture module.

[0108] The daemon module is a key module for maintenance and testing. It exists independently of the native Android system as a daemon process, starting with the entire system upon boot and running until shutdown. The daemon module's architecture resides above the Linux kernel layer and below the application layer in the native framework layer.

[0109] Among them, a daemon process is a process that is detached from the control electronic device and periodically performs a certain task or waits to process certain events in the background. It is detached from the terminal to prevent the information of the process during execution from being displayed on any terminal and to prevent the process from being terminated by any terminal interrupt information.

[0110] In another scenario, if the system layer does not establish a daemon module, a mechanism similar to a daemon will be rebuilt and placed in the local framework layer above the Linux kernel layer and below the application layer, without conflicting with Android's native code and capabilities.

[0111] It can be understood that the memory leak detection method in this application is applicable not only to Android systems with a daemon process module, but also to Android systems without a daemon process module, and is not limited thereto. This application uses an Android system with a daemon process module as an example to illustrate the software structure of an electronic device.

[0112] like Figure 3 As shown, the daemon module includes a runtime management module, which in turn includes a memory leak detection module and a memory leak handling module.

[0113] In one possible implementation, when a user turns on an electronic device, the daemon process module starts along with the system startup, and the memory leak detection module starts along with the daemon process module. When a user launches an application on the electronic device, the application allocates memory within a short period. Some applications may not release this memory after it has been used up or after entering the background, leading to memory leaks. Therefore, the memory leak detection module needs to periodically scan the memory usage of applications launched on the electronic device to determine whether any applications with suspected memory leaks exist. For example, the scanning period for memory leak detection can be 500 seconds, 600 seconds, or 800 seconds, etc. This embodiment does not limit the specific duration of the scanning period.

[0114] In actual operation, applications in electronic devices may correspond to one process or multiple processes. When an application corresponds to one process, the memory usage of that process can be used to determine whether the corresponding application is suspicious. When an application corresponds to multiple processes, the memory usage of those processes can be used to determine whether the processes belong to suspicious applications. If an electronic device determines that any one of the multiple processes corresponding to an application has a memory leak, that application can be identified as suspicious.

[0115] When the memory leak detection module starts, the memory viewing module located in the kernel layer captures process memory information and sends it to the memory leak detection module. Upon receiving the process memory information captured by the memory viewing module, the memory leak detection module records suspicious processes that may have memory leaks. For example, the memory leak detection module adds the process identifiers (e.g., process ID and process name) of suspicious processes to a gray list. This gray list stores the process identifiers of suspicious processes that may have memory leaks. The memory leak detection module performs periodic cyclic scans to update the gray list.

[0116] As an example, suppose that when the memory leak detection module starts, the memory viewing module located in the kernel layer captures the memory information of running processes A, B, C, and D. After the memory viewing module sends the memory information of the above processes to the memory leak detection module, the memory leak detection module determines that processes A and D may have memory leaks based on the memory information of the above processes, and adds the process IDs of processes A and D to a gray list. As shown in Table 1 below, the gray list can include the process IDs of processes A and D (e.g., 111, 114) and process names (e.g., A and D).

[0117] Table 1

[0118] Process ID Process Name 111 A 114 D

[0119] It should be noted that the process IDs and process names of processes A and D mentioned above are only for illustrative purposes. In the actual memory leak detection process, the process identifiers in the gray list shall be based on the actual process identifiers of the processes, and no restrictions are imposed here.

[0120] In this embodiment of the application, after determining that there are suspicious processes in the gray list, it is necessary to continue to determine whether there is a memory leak in the suspicious processes in the gray list, so as to determine whether the application corresponding to the suspicious process has a memory leak.

[0121] In one possible scenario, after the memory leak detection module completes a cycle of scanning, there may be suspicious processes with memory leaks in the graylist. In this case, the memory leak handling module is triggered, and it periodically collects memory information corresponding to the suspicious processes in the graylist.

[0122] Here, the memory leak handling module collects memory information about suspicious processes at a shorter interval than the memory leak detection module. For example, while the memory leak detection module updates the graylist every 500 seconds, the memory leak handling module can collect memory information about suspicious processes every 50 seconds. Therefore, by using a finer-grained interval to obtain memory information from suspicious processes, the memory leak handling module improves the accuracy of determining whether a suspicious process has a memory leak.

[0123] When the memory leak handling module starts, the memory viewing module located at the kernel layer captures the memory information of suspicious processes in the graylist. Further, the memory viewing module sends the captured process memory information to the memory leak handling module. Upon receiving the memory information of the suspicious processes captured by the memory viewing module, the memory leak handling module sends the memory information of the suspicious processes to the memory leak detection module. After receiving the memory information of the suspicious processes sent by the memory leak handling module, the memory leak detection module determines whether the suspicious process has a memory leak based on the memory information. Once the memory leak detection module determines that a suspicious process has a memory leak, it can send the relevant information of the process to other modules of the electronic device. Simultaneously, the memory leak handling module starts the log information capture module. Then, after the log information capture module starts, it captures the relevant stack information of the process used to analyze the memory leak, and the entire memory leak detection process ends. The relevant stack information of the process includes the system's underlying memory information.

[0124] In this embodiment, when the system allocates memory to an application, the log information capture module can use a system-provided interface (such as the backtrace interface) to obtain the application's stack information and record the corresponding stack information and memory address. When the system reclaims application memory, it clears the stack information of the reclaimed memory from the record. When the log information capture module captures the memory's stack information, it can retrieve the corresponding stack information from the record based on the memory address.

[0125] All the technical solutions involved in the following embodiments can be implemented in the electronic device 100 having the above-described hardware structure and software architecture. The following uses a mobile phone as an example to illustrate this solution.

[0126] When the phone boots up, the daemon process module starts along with the phone's operating system, and the memory leak detection module starts along with the daemon process module. After the phone boots up, if the user launches an application, the application will request memory from the system, and the application's corresponding process will also occupy memory. For example... Figure 4 As shown, the user launched TikTok on their phone. After that, Douyin Memory was requested from the system. For example... Figure 5 As shown, after the memory leak detection module starts, it triggers the startup of the memory viewing module in the kernel layer. The memory viewing module provides the memory information of the processes corresponding to already running applications to the memory leak detection module. The memory leak detection module periodically checks the process memory information to identify suspicious processes that may have memory leaks and records these suspicious processes. For example, the memory leak detection module can add suspicious processes to a graylist.

[0127] Process memory information can be used to measure a process's memory usage. Memory usage can be measured by the process's memory increment rate and / or total memory usage. The memory increment rate refers to the rate at which the memory used by a process increases. Total memory usage can be the process's total memory usage or the ratio of the process's memory usage to the system's memory. A higher memory increment rate indicates that the process uses more memory in a short period of time, and a higher total memory usage indicates that the process uses more system memory. In both of these cases, the likelihood of a memory leak in the process is relatively high.

[0128] In this embodiment, when the memory leak detection module determines the memory increase rate and / or total memory usage of a process, it can first obtain the overall memory information of the system or the overall memory information of the process at runtime by calling native Android interfaces (such as / proc / meminfo interface, / proc / pid / smaps). Further, the memory leak detection module determines the memory increase rate and / or total memory usage of the process based on the overall memory information of the process and the overall memory information of the system. The overall memory information of the system can include unused memory information, available memory information, kernel-level memory, etc. Kernel-level memory refers to the memory usage method allocated in the system kernel, which is more purpose-oriented. For example, Slub, vmalloc, and skb are parameters of the memory management methods built into the Linux system. The overall memory information of the process can include virtual memory size, memory requested after the process starts, anonymously mapped physical memory, etc.

[0129] For example, the ` / proc / meminfo` interface is the primary interface for understanding the memory usage of a Linux system. By calling the ` / proc / meminfo` interface, you can obtain the ` / proc / meminfo` file in the corresponding directory of the phone, determine the overall memory information of the system from the ` / proc / meminfo` file, and return the current total memory and its usage. Figure 6 As an example, the / proc / meminfo file may include Figure 6 The system's overall memory information. For example... Figure 6 As shown, MemTotal refers to the memory available to the kernel from power-on to boot completion. The value of MemTotal is generally fixed during system operation. MemFree represents unused memory. MemAvailable is a statistical value of the amount of currently available memory. Since MemFree does not represent all available memory, some memory in the system, although used, can be reclaimed. Therefore, this reclaimable memory plus MemFree equals the total available memory, i.e., MemAvailable.

[0130] It needs to be explained that, Figure 6 The text only shows a portion of the contents of the / proc / meminfo file. The full contents are based on the actual system memory usage obtained by calling the / proc / meminfo interface, and are not limited here.

[0131] For example, ` / proc / pid / smaps` is an interface for understanding the memory usage of a running process. By calling ` / proc / pid / smaps`, you can view the overall memory information of the process corresponding to its process ID (PID), including the resident set size (RSS) and / or ION memory. RSS refers to the memory space allocated to the corresponding process after requesting it, including shared libraries; it can be considered as all the memory space actually occupied by the corresponding process. ION memory refers to shared memory located in the swap partition, mainly used for fast data exchange between applications, and is a memory management mode unique to Android. Figure 7 As an example, Figure 7 The image shows the memory usage of the process with PID 1. For example... Figure 7 As shown, Size refers to the virtual memory space size, which is 432KB. Rss refers to the memory actually allocated by the system for process PID 1. It should be explained that... Figure 7The image only shows a portion of the contents of the / proc / smaps file. The complete contents are based on the overall memory information of the process obtained by actually calling the / proc / smaps interface, and are not limited here.

[0132] After obtaining the overall memory information of the system and the overall memory information of the process at runtime through the above interface, the memory leak detection module can determine the memory increase rate of the process based on the overall memory information of the process at runtime obtained in different scanning cycles.

[0133] For example, assuming the memory leak detection module obtains through the above interface that process A occupies 300MB of memory (e.g., RSS memory) in the 500th second and 400MB of memory in the 1000th second, then the memory increase rate of process A can be determined to be (400-300) / (1000-500) = 0.2MB / s.

[0134] In addition, after obtaining the overall memory information of the system and the overall memory information of the process at runtime through the above interface, the memory leak detection module can also determine the total memory usage of the process based on the memory information of the process at runtime and the overall memory information of the system.

[0135] For example, such as Figure 6 The memory leak detection module determined the system's memory to be 5535788KB by calling an interface. Figure 7 The memory leak detection module determines, through an interface call, that the actual memory allocated to process PID 1 (i.e., RSS memory) is 112KB. The module can determine that process PID 1 occupies a total of 112KB of system memory, or it can calculate the memory usage ratio of process PID 1 as 112 / 5535788.

[0136] In this embodiment, after obtaining the process memory information provided by the memory viewing module, the memory leak detection module determines the process's memory increase rate and / or total memory usage based on the process's memory information. Furthermore, the memory leak detection module determines whether the process is a suspicious process based on the process's memory increase rate and / or total memory usage, including but not limited to the following three methods:

[0137] In one possible approach, the memory leak detection module determines, based on the process's memory information, that the process's memory increase rate exceeds a first threshold. In this case, it indicates that the process has requested a large amount of memory in a short period of time, suggesting a potential memory leak, and therefore the process is added to a graylist.

[0138] In another possible approach, the memory leak detection module determines, based on the process's memory information, that the process's total memory usage exceeds a threshold (e.g., the process's memory usage exceeds a preset threshold, or the process's memory usage as a percentage of system memory exceeds a preset percentage). In this case, it indicates that the process is consuming too much system memory, suggesting a potential memory leak, and therefore the process is added to a graylist.

[0139] For example, assuming the memory usage threshold is 300MB, if the memory leak detection module determines that a process's RSS is 350KB by calling an interface, then the memory leak detection module determines that the process's RSS is greater than the memory usage threshold, and can therefore identify the process as suspicious. Figure 7 The memory leak detection module determines, through an interface call, that the RSS of process PID 1 is 112KB. Since the RSS of process PID 1 is less than the memory usage threshold, the module can conclude that this process does not have a potential memory leak.

[0140] In another possible approach, the memory leak detection module determines both the process's memory increase rate and total memory usage based on the process's memory information. If the process's memory increase rate exceeds a first rate threshold and its total memory usage exceeds a usage threshold, it indicates that the process has requested a large amount of memory in a short period, and its total system memory usage is excessive. Therefore, the process is considered to have a potential memory leak and is added to the graylist.

[0141] It should be explained that the aforementioned first rate threshold and occupancy threshold can be set according to the process's memory usage, or they can be built-in system values, which are not limited here.

[0142] In this embodiment, when the memory leak detection module determines whether a process is a suspicious process based on its memory information, it can determine whether the process is a suspicious process based on at least one of the following: the process's memory increase rate, total memory usage, and whether the process's memory has not been accessed for a long time. For example, if the memory leak detection module determines based on the process's memory information that the process has not been accessed within a preset time period, then the process can be determined to be a suspicious process.

[0143] In this embodiment of the application, after one cycle of the memory leak detection module is completed, there may be no suspicious processes in the gray list.

[0144] For example, in one possible scenario, the user doesn't launch any applications after the phone boots up. In this case, when the memory leak detection module's scan cycle ends, it hasn't detected any processes that might have memory leaks, the graylist is empty, and the module continues with subsequent loops. Figure 8 As shown, in response to the user's action of starting the phone, the daemon process module located in the phone's native layer starts when the phone boots up, and consequently, the memory leak detection module starts along with the daemon process module. Since the user has not launched any applications on the phone, the graylist is empty after the memory leak detection module's loop ends.

[0145] For example, in one possible scenario, after the phone boots up, the user launches an application on the phone. At the end of the memory leak detection module's scan cycle, if it determines that the process corresponding to the launched application is not a suspicious process and the graylist is empty, the memory leak detection module continues with subsequent cyclic scanning.

[0146] In other embodiments, after the memory leak detection module starts, the user launches an application. After the current scanning cycle executed by the memory leak detection module ends, the memory leak detection module determines, based on the memory information of the process corresponding to the application provided by the memory viewing module, that the process may have a memory leak, and then adds the identifier of the process that may have a memory leak to the gray list. Figure 9 As an example, after the phone is powered on, the memory leak detection module starts a 500-second loop. Then, the user launches TikTok. After the 500-second loop ends, the memory leak detection module uses the memory information provided by the memory viewing module to determine the location of TikTok. The process may have a memory leak. After the current loop of the memory leak detection module finishes executing, Douyin will be... The process ID and process name are added to the graylist. For example, Figure 9 Douyin The process ID is 1111, and the process name is Douyin. .

[0147] It should be explained that the above example assumes Douyin (TikTok). For multiple processes, such as Douyin If at least one of the corresponding processes may have a memory leak, then it can be determined that Douyin... Memory leaks may exist. The process IDs and process names used in this embodiment are merely illustrative; the actual process IDs and process names should be determined based on the actual operation of the application and are not limited here.

[0148] In this embodiment, after the memory leak detection module determines that there are suspicious processes in the gray list, it further determines whether the suspicious processes have memory leaks. It is understood that the application corresponding to a suspicious process may also have memory leaks and can be called a suspicious application; if a suspicious process has a memory leak, it indicates that the application corresponding to that suspicious process has a memory leak.

[0149] Still with Figure 5 To illustrate, after a scan cycle of the memory leak detection module is completed, suspicious processes are added to a graylist. This means that if a suspicious process with a memory leak is found in the graylist, the memory leak handling module is triggered. The memory leak handling module periodically obtains the memory information of suspicious processes from the graylist through the memory viewing module. Then, the memory leak handling module provides this memory information to the memory leak detection module, which determines whether the suspicious process has a memory leak based on this information.

[0150] like Figure 9 Douyin is on the gray list The process ID and process name corresponding to the suspicious process triggered the memory leak handling module to start. For example... Figure 10 As shown, the memory leak handling module starts up and periodically (e.g., every 50 seconds) retrieves data from Douyin (TikTok). The memory information corresponding to suspicious processes is collected to determine whether these processes are experiencing memory leaks. Simultaneously, the memory leak detection module also acquires memory information from other applications on the phone provided by the memory viewing module at a 500-second scan cycle.

[0151] After the memory leak handling module starts, it acquires data from Douyin (TikTok) provided by the memory viewing module at a 50-second collection cycle. The memory information corresponding to the suspicious process in Douyin. The memory information corresponding to the suspicious process is provided to the memory leak detection module so that the module can determine whether the suspicious process has a memory leak. Once the memory leak detection module determines that the suspicious process has a memory leak, it reports the corresponding information to other modules of the phone and starts the log information capture module. The log information capture module captures the relevant stack information used to analyze the memory leak, and the detection process ends.

[0152] It needs to be explained that the memory leak detection module is based on Douyin (TikTok). Once the memory information of a suspicious process is used to determine that the process has a memory leak, the TikTok app can be identified. There is a memory leak.

[0153] In this embodiment, the memory leak handling module can collect memory information of suspicious processes more frequently. For example, the memory leak handling module can periodically collect memory information of suspicious processes, and the collection cycle is shorter than the scanning cycle of the memory leak detection module. Therefore, the memory leak handling module uses a finer-grained cycle to acquire memory information of suspicious processes in real time, thereby promptly determining whether a suspicious process has a memory leak and improving the accuracy of determining whether a suspicious process has a memory leak. For example, the memory leak detection module scans every 500 seconds to identify suspicious processes with memory leaks. After a 500-second scanning cycle, if a suspicious process is found in the gray list, the memory leak handling module acquires the memory information of the suspicious process every 50 seconds. That is, after the memory leak detection module starts, after identifying a suspicious process with a memory leak at 500 seconds, the memory leak handling module collects the memory information of the suspicious process every 50 seconds at 550 seconds, 600 seconds, etc.

[0154] It is understandable that when the memory leak handling module periodically collects memory information of suspicious processes, there may be instances where no memory information of suspicious processes is collected in a particular collection cycle. For example, the memory leak handling module collects memory information of suspicious processes every 50 seconds at memory collection times (such as the 550th second, 600th second, 650th second, etc.), and no data is collected in the 600th second. In other words, after the memory leak handling module periodically collects memory information of suspicious processes, there may be instances where the amount of memory collected from suspicious processes is less than the number of collection cycles. Here, the memory collection time refers to the time it takes to obtain memory information of suspicious processes; it can also be called the memory acquisition time, and is not specifically defined here.

[0155] In this embodiment of the application, when the memory leak detection module determines whether a suspicious process has a memory leak based on the memory information of the suspicious process, it can first calculate the average memory increase rate based on the memory collection time and memory usage of the suspicious process, so as to determine whether the suspicious process in the gray list has a memory leak based on the average memory increase rate.

[0156] As an example, as shown in Table 2 below, assume the memory leak detection module obtains eight memory sampling times and corresponding eight memory usage values ​​for a suspicious process. Based on these eight sampling times and corresponding memory usage values, the memory increase rate of the suspicious process is determined to be 10KB / s in the time interval from 550 to 600 seconds, 14KB / s in the time interval from 600 to 650 seconds, 16KB / s in the time interval from 650 to 700 seconds, 16KB / s in the time interval from 700 to 750 seconds, 20KB / s in the time interval from 750 to 800 seconds, and 24KB / s in the time interval from 800 to 850 seconds. Then, based on these seven memory increase rates and the number of memory increase rates, the memory leak detection module determines that the average memory increase rate of the suspicious process is approximately 14.3KB / s.

[0157] Table 2

[0158] Memory sampling time (seconds) Memory usage (KB) 550 5000 600 5500 650 6200 700 7000 750 7800 800 8800 850 10000

[0159] It should be noted that the memory acquisition time and memory usage in Table 2 above are only illustrative examples and are not limited in this embodiment.

[0160] In this embodiment of the application, when the memory leak detection module determines whether a suspicious process has a memory leak based on the collected memory usage information of the suspicious process, it includes, but is not limited to, the following six situations.

[0161] In the first scenario, if the average memory increase rate of the suspicious process is greater than or equal to the second slope threshold, it indicates that the suspicious process has been continuously requesting a large amount of memory in a short period of time, confirming that the suspicious process has a memory leak. In this case, the system can return the first flag (e.g., return 1).

[0162] As an example, if the memory leak detection module calculates that the average memory increase rate of a suspicious process is greater than 500KB / s, then the suspicious process is determined to have a memory leak, and the system returns 1.

[0163] In this embodiment, after the memory leak detection module determines whether a suspicious process has a memory leak, it returns a corresponding identifier. The electronic device's system can then determine whether an application currently has a memory leak based on the identifier returned by the memory leak detection module. For example, if the memory leak detection module determines that a suspicious process has a memory leak and returns a first identifier, the system can determine whether the application of the electronic device currently has a memory leak based on the first identifier.

[0164] In the second scenario, if the average memory increase rate of the suspicious process is less than the second rate threshold, and the number of calculated memory increase rates is greater than the first rate threshold, and the last calculated consecutive preset number of memory increase rates are all greater than the third rate threshold (where the third rate threshold is less than the second rate threshold), this indicates that although the suspicious process has not requested a large amount of memory in a short period, it is continuously requesting memory from the system, and the amount of memory requested is continuously increasing. This also confirms that the suspicious process has a memory leak. In this case, the system also returns the first flag (e.g., returns 1). For example, the second rate threshold is set to 500KB / s, and the third rate threshold is set to 30KB / s.

[0165] As an example, suppose the memory leak detection module calculates the average memory increase rate of the suspicious process to be less than 500KB / s, and the rate calculation is performed 10 times or more, and the memory increase rate of the last 4 consecutive calculations is greater than 30KB / s. Then, it is determined that the suspicious process has a memory leak, and the system returns 1.

[0166] In the third scenario, if the average memory increase rate of the suspicious process is less than the second rate threshold, and the number of calculated memory increase rates is greater than the first rate threshold, and one of the last calculated consecutive preset number of memory increase rates is less than the third rate threshold, and the total memory usage is greater than the memory threshold, then this indicates that although the suspicious process did not request a large amount of memory in a short period of time, it continuously requested memory from the system, and the total amount of memory requested was large. This also confirms that the suspicious process has a memory leak. In this case, the system can return the first flag (e.g., return 1).

[0167] As an example, suppose the memory leak detection module calculates that the average memory increase rate of the suspicious process is less than 500KB / s, and the rate calculation is performed 10 times or more. In the last four consecutive calculations, there is a memory increase rate of less than 30KB / s, and the total memory usage exceeds 200MB. Then, it is determined that the suspicious process has a memory leak, and the system returns 1.

[0168] In the fourth scenario, if the average memory increase rate of the suspicious process is less than the second rate threshold, and the number of calculated memory increase rates is greater than the first rate threshold, and one of the last calculated consecutive preset number of memory increase rates is less than the third rate threshold, and the total memory usage is less than or equal to the memory threshold, then this indicates that the suspicious process did not request a large amount of memory in a short period of time. Although the suspicious process continuously requests memory from the system, the memory requested by the suspicious process occupies a small amount of the total system memory, confirming that the suspicious process does not have a memory leak. In this case, the system can return the second flag (e.g., return 2).

[0169] As an example, suppose the average memory increase rate of the suspected process currently calculated by the memory leak detection module is less than 500KB / s, and the rate calculation has been performed 10 times or more, and one of the last four consecutive memory increase rate calculations is less than 30KB / s, and the total memory usage does not exceed 200MB, then it is determined that the suspected process does not have a memory leak, and the system returns 2.

[0170] In the fifth scenario, if the number of cycles during which the memory leak handling module collects memory information from suspicious processes is less than the second threshold, then the memory leak detection module has not obtained sufficient memory information from suspicious processes. This indicates insufficient data, making it impossible to determine if a suspicious process has a memory leak. In this case, the system can return a third flag (e.g., 0). For example, if the memory leak handling module collects memory information from suspicious processes less than 5 times, it considers the data insufficient.

[0171] In the sixth scenario, if the memory leak detection module determines that the amount of memory information collected from suspicious processes is less than the number of collection cycles, then the memory leak handling module has not obtained sufficient memory information from the suspicious processes. Therefore, it can be determined that the data is insufficient and it is impossible to determine whether a suspicious process has a memory leak. In this case, the system can return a third flag (e.g., return 0).

[0172] It should be noted that the values ​​of the second rate threshold, third rate threshold, first number threshold, second number threshold, and memory threshold in the above example are merely illustrative descriptions and can be set to different values ​​depending on the suspected process. That is, the second rate threshold, third rate threshold, first number threshold, second number threshold, and memory threshold may be the same or different for different processes, and this is not limited here. For example, if the suspected process is TikTok... For processes, you can set a second rate threshold of 500KB / s, a third rate threshold of 30KB / s, a first number threshold of 10 times, a second number threshold of 4 times, and a memory threshold of 200MB; if the suspicious process is WeChat... The process can set a second rate threshold of 400KB / s, a third rate threshold of 25KB / s, a first number threshold of 8 times, a second number threshold of 4 times, and a memory threshold of 180MB.

[0173] A mobile phone may have only one process running or multiple processes running simultaneously, meaning that only one process may be using system memory or multiple processes may be using system memory at the same time. For example, a user may only launch TikTok after starting their phone. Or, TikTok might be running on your phone at the same time. ,WeChat Taobao Multiple processes, etc.

[0174] In some embodiments, when a process starts on a mobile phone, the memory leak detection module can periodically detect the memory usage of multiple started processes to identify processes that may have memory leaks. In the next scan cycle, if the memory leak detection module determines that a suspicious process already in the gray list may not have a memory leak, it can remove that suspicious process from the gray list at the end of the scan cycle. Alternatively, if the memory leak detection module determines that other suspicious processes with memory leaks exist in the next scan cycle, it can update the gray list based on these suspicious processes at the end of the scan cycle. Thus, the memory leak detection module can periodically determine whether processes in an electronic device have memory leaks based on the memory information of running processes collected by the memory monitoring module, and update the gray list accordingly.

[0175] For example, suppose TikTok is launched on the phone at the same time. ,WeChat and Taobao The memory leak detection module performs a 500-second cycle on the launched Douyin app. ,WeChat and Taobao The system detects memory usage and identifies Douyin within a given period. If a memory leak is suspected, Douyin will be removed after the scan cycle ends. The corresponding process ID and process name are added to the graylist. During the next 500-second scan cycle of the memory leak detection module, the memory leak detection module determines whether Douyin (TikTok) is involved. If there is no memory leak, then TikTok can be removed from the gray list after the scanning cycle ends. The corresponding process ID and process name. Alternatively, the memory leak detection module can determine Taobao's process ID during the next 500-second scan cycle. If a memory leak exists, Taobao can be removed after the scan cycle ends. The corresponding process ID and process name are added to the gray list.

[0176] In some embodiments of this application, after the memory leak detection module determines that certain applications on the phone have memory leaks, the phone can notify the user of the memory leak, allowing the user to take appropriate action. Thus, by having the user address the memory leak in a timely manner based on the notification, the phone's performance is improved. For example, the phone's display screen shows a notification indicating that certain applications have memory leaks, and the user can actively instruct the phone to release the memory occupied by that application.

[0177] like Figure 11A As shown, the memory leak detection module identifies Douyin. When a memory leak is detected, the phone's display will show a message, such as... Figure 11A The message "Warning: Douyin has a memory leak" is displayed to alert users. A memory leak has been detected, so users can take appropriate action based on the prompts.

[0178] In some embodiments, see Figure 11B The phone can prompt the user with "TikTok has a memory leak, do you want to release the memory used by TikTok?" If the phone detects that the user clicks the "Yes" button, it will close the TikTok app and release the memory used by TikTok to avoid performance issues such as insufficient memory or lag.

[0179] It needs to be explained that, Figure 11A and Figure 11B The method of displaying prompts in the display is merely an example. Prompts can be displayed anywhere on the phone's screen, such as the top, bottom, left, or right side; there are no limitations on this. Furthermore, the method of displaying prompts in the phone's display is only an example description; prompts can also be made to the user through sounds, etc., and there are no limitations on this either.

[0180] In summary, in this embodiment of the application, after the memory leak detection module determines that there is a suspicious process with a memory leak among the processes started in the electronic device, it collects the memory information of the suspicious process to further determine whether the suspicious process has a memory leak. It can accurately detect processes with memory leaks so that appropriate processing can be carried out on processes with memory leaks, thereby improving the performance of the electronic device.

[0181] Compared to existing memory leak detection methods, which may have false positives or false negatives when identifying processes with memory leaks, the memory leak detection method of this application can accurately detect processes with memory leaks.

[0182] like Figure 12 As shown in the illustration, this application discloses an electronic device, which can be the aforementioned mobile phone. Specifically, the electronic device may include: a touchscreen 1201, which includes a touch sensor 1206 and a display screen 1207; one or more processors 1202; a memory 1203; one or more application programs (not shown); and one or more computer programs 1204. These components can be connected via one or more communication buses 1205. The one or more computer programs 1204 are stored in the memory 1203 and configured to be executed by the one or more processors 1202. The one or more computer programs 1204 include instructions that can be used to perform the relevant steps in the above embodiments.

[0183] It is understood that, in order to achieve the aforementioned functions, the aforementioned terminals and other devices include hardware structures and / or software modules corresponding to the execution of each function. Those skilled in the art should readily recognize that, based on the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein, the embodiments of this application can be implemented in hardware or a combination of hardware and computer software. Whether a function is executed in hardware or by computer software driving hardware depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of the embodiments of this invention.

[0184] This application embodiment can divide the aforementioned terminal, etc., into functional modules according to the above method examples. For example, each function can be divided into its own functional module, or two or more functions can be integrated into one processing module. The integrated module can be implemented in hardware or as a software functional module. It should be noted that the module division in this embodiment is illustrative and only represents one logical functional division; in actual implementation, there may be other division methods.

[0185] Through the above description of the embodiments, those skilled in the art will clearly understand that, for the sake of convenience and brevity, only the division of the above functional modules is used as an example. In practical applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above. The specific working process of the system, device, and unit described above can be referred to the corresponding process in the foregoing method embodiments, and will not be repeated here.

[0186] In the embodiments of this application, the functional units can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.

[0187] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solutions of the embodiments of this application, essentially, or the parts that contribute to the prior art, or all or part of the technical solutions, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) or processor to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as flash memory, portable hard disk, read-only memory, random access memory, magnetic disk, or optical disk.

[0188] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any changes or substitutions within the technical scope disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.

Claims

1. A method for detecting memory leaks, applied to an electronic device, the electronic device comprising a first process and a second process, characterized in that, The method comprises: obtaining first memory information corresponding to the first process and second memory information corresponding to the second process; determining that the first process meets a preset condition and the second process does not meet the preset condition according to the first memory information corresponding to the first process and the second memory information corresponding to the second process; obtaining third memory information corresponding to the first process; the third memory information is used to determine a second memory increase rate, an average memory increase rate and a second memory total occupation amount of the first process; determining whether the first process has a memory leak according to at least one of the second memory increase rate, the average memory increase rate and the second memory total occupation amount of the first process; in a case where the average memory increase rate of the first process is greater than or equal to a second rate threshold, determining that the first process has a memory leak; in a case where the average memory increase rate of the first process is less than the second rate threshold, the number of the second memory increase rates is greater than a first number threshold, and the continuous preset number of second memory increase rates are all greater than a third rate threshold, determining that the first process has a memory leak; wherein the third rate threshold is less than the second rate threshold; in a case where at least one of the continuous preset number of second memory increase rates is less than the third rate threshold and the second memory total occupation amount is greater than a memory threshold, determining that the first process has a memory leak.

2. The method of claim 1, wherein, The first memory information comprises a first memory acquisition time and corresponding first memory occupation amount, and the determination that the first process meets a preset condition comprises: determining that the first process meets the preset condition according to a first memory increase rate and / or a first memory total occupation amount of the first process; wherein the first memory increase rate is determined according to at least two first memory acquisition times and corresponding first memory occupation amounts of the first process; and the first memory total occupation amount is determined according to the first memory occupation amount.

3. The method of claim 2, wherein, The determination that the first process meets the preset condition according to the first memory increase rate and / or the first memory total occupation amount of the first process comprises: if the first memory increase rate of the first process is greater than a first rate threshold and / or the first memory total occupation amount is greater than an occupation amount threshold, it is determined that the first process meets the preset condition.

4. The method of claim 1, wherein, The third memory information comprises a second memory acquisition time and corresponding second memory occupation amount, the second memory increase rate is determined according to at least two second memory acquisition times and corresponding second memory occupation amounts of the first process, and the average memory increase rate is determined according to the at least one second memory increase rate; the second memory total occupation amount is determined according to the second memory occupation amount.

5. The method of claim 1, wherein, The determination whether the first process has a memory leak according to at least one of the second memory increase rate, the average memory increase rate and the second memory total occupation amount of the first process further comprises: If the average memory increase rate of the first process is less than the second rate threshold, the number of the second memory increase rates is greater than the first number threshold, at least one of the continuous preset number of second memory increase rates is less than the third rate threshold, and the second total memory usage is less than or equal to the memory threshold, it is determined that the first process does not exist memory leakage.

6. The method according to any one of claims 1 to 5, characterized in that, Before judging whether the first process exists memory leakage according to at least one of the second memory increase rate, the average memory increase rate and the second total memory usage of the first process, the method further comprises: Statistically obtaining the number of periods of the third memory information corresponding to the first process and the number of second memory usages; Determining that the number of periods is greater than the second number threshold, or the number of second memory usages matches the number of periods.

7. The method according to any one of claims 1 to 5, characterized in that, The method further comprises: Obtaining the first memory information corresponding to the first process and the second memory information corresponding to the second process in a first period; Obtaining the third memory information corresponding to the first process in a second period.

8. The method of claim 7, wherein: wherein The second period is less than the first period.

9. The method according to any one of claims 1 to 5, characterized in that, After determining that the first process meets the preset condition, the method further comprises: Adding the first process to a gray list.

10. The method of claim 9, wherein, After adding the first process to the gray list, the method further comprises: If the first memory increase rate of the first process is less than or equal to the first rate threshold and / or the first total memory usage is less than or equal to the usage threshold, the first process is removed from the gray list.

11. The method of claim 1, wherein, After determining that the first process exists memory leakage, the method further comprises: Displaying prompt information; wherein the prompt information is used to prompt that the application corresponding to the first process exists memory leakage.

12. An electronic device, comprising: Comprises: One or more processors; Memory; Wherein the memory stores one or more computer programs, the one or more computer programs comprise instructions, when the instructions are executed by the electronic device, the electronic device executes the memory leakage detection method of any one of claims 1-11.

13. A computer-readable storage medium having stored therein instructions, the computer-readable storage medium being characterized by, When the instructions run on the electronic device, the electronic device executes the memory leakage detection method of any one of claims 1-11.

14. A computer program product, characterised in that, The computer program product comprises computer instructions, when the computer instructions run on the electronic device, the electronic device executes the memory leakage detection method of any one of claims 1-11.

Citation Information

Patent Citations

  • Memory leak detection

    US9760464B1