Memory leak detection method and apparatus, electronic device and readable storage medium

By acquiring and analyzing the actual physical memory data of all processes on the Android phones, it is difficult to monitor the memory leakage of all processes in the existing technology at the same time, and achieve high accuracy of memory leak detection.

WO2025020508A1PCT designated stage expired Publication Date: 2025-01-30ZTE CORP

Patent Information

Application Number
PCT/CN2024/077612
Authority / Receiving Office
WO · WO
Patent Type
Applications
Current Assignee / Owner
Priority Date
2023-07-21
Filing Date
2024-02-19
Publication Date
2025-01-30

AI Technical Summary

Technical Problem

The existing technology is difficult to monitor memory leaks in all processes on Android phones at the same time, and there are major misjudgment problems.

Method used

By obtaining the actual physical memory data of the first process of the first process of ROOT permissions, the memory growth trend is determined based on the data obtained multiple times in a row, and the second process has a memory leak when the predetermined conditions are met.

Benefits of technology

The memory leak detection of all processes on the Android phone was implemented, reducing misjudgment and improving the accuracy of the detection.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN2024077612_30012025_PF_FP_ABST
    Figure CN2024077612_30012025_PF_FP_ABST
Patent Text Reader

Abstract

The present application belongs to the technical field of terminal devices. Disclosed are a memory leak detection method and apparatus, an electronic device and a readable storage medium. The method comprises: acquiring actual physical memory data of at least one second process periodically reported by a first process, the first process being a process having a ROOT permission, and the at least one second process being a process currently run by an electronic device; and, when it is determined, on the basis of the actual physical memory data of a second process acquired multiple times in succession, that a memory growth trend of the second process meets a preset condition, determining that the second process has memory leak, the second process being any process of the at least one first process.
Need to check novelty before this filing date? Find Prior Art

Description

Memory leak detection method, device, electronic device and readable storage medium

[0001] Cross-references

[0002] This application claims priority to the Chinese patent application filed with the China Patent Office on July 21, 2023, with application number 202310905095.0 and application name “Memory leak detection method, device, electronic device and readable storage medium”. The entire contents of the application are incorporated by reference into this application. Technical Field

[0003] The embodiments of the present application relate to the field of terminal technology, and in particular to a memory leak detection method, device, electronic device, and readable storage medium. Background Art

[0004] Currently, memory leaks on Android phones are mainly monitored through fixed thresholds and the memory growth rate of two endpoints.

[0005] However, when monitoring using a fixed threshold solution, the large number of programs that can be installed on Android phones and their different memory usage will generate different thresholds, resulting in only being able to monitor the memory of a specific process and not all processes. When judging whether a memory leak has occurred by measuring the memory growth rate of the two endpoints, there is a large problem of misjudgment when monitoring memory leaks due to the growth and release of memory during use.

[0006] Summary of the Invention

[0007] The purpose of the embodiments of the present application is to provide a memory leak detection method, device, electronic device and storage medium, which can solve the problem that when monitoring memory leaks, only the memory leak of a specific process can be monitored, and all processes on the device cannot be monitored at the same time, as well as at least one of the existing misjudged memory leaks.

[0008] In order to solve the above technical problems, this application is implemented as follows:

[0009] In a first aspect, a method for detecting a memory leak is provided, the method comprising: obtaining actual physical memory data of at least one second process periodically reported by a first process, wherein the first process is a process with root (ROOT) permissions, and the at least one second process is a process currently running on an electronic device; and determining that a memory leak exists in the second process when a memory growth trend of the second process satisfies a predetermined condition based on the actual physical memory data of the second process obtained multiple times in succession, wherein the second process is any one of the at least one first process.

[0010] In a second aspect, a memory leak detection device is provided, including: an acquisition module for acquiring actual physical memory data of at least one second process periodically reported by a first process, wherein the first process is a process with root (ROOT) permissions, and the at least one second process is a process currently running on the electronic device; a determination module for determining that there is a memory leak in the second process when, based on the actual physical memory data of the second process acquired multiple times in succession, the memory growth trend of the second process meets a predetermined condition, wherein the second process is any process of the at least one first process.

[0011] In a third aspect, a computer device is provided, comprising a processor and a memory, wherein the memory stores at least one computer program, and the at least one computer program is loaded and executed by the processor to implement the method described in the first aspect.

[0012] In a fourth aspect, a computer-readable storage medium is provided, wherein at least one computer program is stored in the computer-readable storage medium, and the computer program is loaded and executed by a processor to implement the method described in the first aspect.

[0013] In a fifth aspect, a computer program product is provided, which includes at least one computer program, and the computer program is loaded and executed by a processor to implement the method described in the first aspect provided in the above various optional implementations. BRIEF DESCRIPTION OF THE DRAWINGS

[0014] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.

[0015] FIG1 is a flow chart showing a method for detecting a memory leak according to an exemplary embodiment of the present application;

[0016] FIG2 shows a schematic diagram of system data storage provided by an exemplary embodiment of the present application;

[0017] FIG3 shows a schematic diagram of memory relationships provided by an exemplary embodiment of the present application;

[0018] FIG4 is a schematic diagram showing memory usage of an exemplary embodiment of the present application;

[0019] FIG5 is a flow chart showing another method for detecting memory leaks provided by an exemplary embodiment of the present application;

[0020] FIG6 shows a schematic structural diagram of an electronic device provided by an exemplary embodiment of the present application;

[0021] FIG7 is a flow chart showing a method for detecting a memory leak according to an exemplary embodiment of the present application;

[0022] FIG8 is a flow chart showing a method for determining a memory leak according to an exemplary embodiment of the present application;

[0023] FIG9 a shows a schematic diagram of a memory usage scenario in an exemplary embodiment of the present application;

[0024] FIG9 b shows a schematic diagram of another memory usage scenario in an exemplary embodiment of the present application;

[0025] FIG9c is a schematic diagram showing another memory usage scenario in an exemplary embodiment of the present application;

[0026] FIG9 d is a schematic diagram showing another memory usage scenario in an exemplary embodiment of the present application;

[0027] FIG10 shows a schematic structural diagram of a memory leak detection device provided by an exemplary embodiment of the present application;

[0028] FIG11 is a structural block diagram of an electronic device according to an exemplary embodiment of the present application. DETAILED DESCRIPTION

[0029] Exemplary embodiments will be described in detail herein, with examples illustrated in the accompanying drawings. In the following description, when referring to the drawings, identical numerals in different figures represent identical or similar elements, unless otherwise indicated. The embodiments described in the following exemplary embodiments are not intended to represent all embodiments consistent with the present application. Rather, they are merely examples of apparatus and methods consistent with certain aspects of the present application, as detailed in the appended claims.

[0030] The Android system is an open ecosystem with tens of millions of applications running on it. Because system memory is a limited resource, some applications leak memory, leading to a growing shortage of available memory and increasing lag in electronic devices. However, the cause of the lag is often unknown to users.

[0031] In related technologies, monitoring whether a process has a memory leak generally involves using a fixed threshold to determine whether it has a memory leak or by creating a thread in the application for monitoring. However, because the Android system is an open ecosystem, there are tens of millions of programs that can be installed, and the memory required for each program to run is inconsistent, resulting in tens of millions of different thresholds, making it impossible to monitor all processes through the threshold. As for monitoring threads, because the Android system has a sandbox isolation mechanism, process A cannot access the data of process B. Therefore, creating a thread in process A can only monitor memory leaks in process A, but not other processes.

[0032] To address the above issues, an embodiment of the present application provides a memory leak detection method that can determine whether a memory leak exists based on the memory growth trend, thereby adapting to tens of millions of different processes. In addition, in an embodiment of the present application, the actual physical memory data of at least one second process currently running on an electronic device is obtained through a first process with root privileges, thereby monitoring all processes. The technical solution provided by the embodiment of the present application is described below in conjunction with the accompanying drawings.

[0033] FIG1 shows a memory leak detection method provided by an exemplary embodiment of the present application. The method 100 can be executed by an electronic device. In other words, the method can be executed by software or hardware installed on the electronic device. The electronic device can be a terminal device such as a mobile phone. As shown in FIG1 , the method mainly includes the following steps:

[0034] S101: Acquire actual physical memory data of at least one second process periodically reported by a first process.

[0035] The first process is a process with ROOT permission, the at least one second process is a process currently running on the electronic device, and the actual memory data of the second process can be obtained through the first process.

[0036] In one implementation, the at least one first process may include all processes currently running on the electronic device, thereby enabling monitoring of all processes currently running on the electronic device.

[0037] In another implementation, the at least one first process may also include processes whose memory usage is greater than a first threshold among all processes currently running on the electronic device, i.e., only processes whose memory usage is greater than the first threshold are monitored. Since an electronic device may be running a large number of processes, many of which occupy very little memory and therefore do not need to be monitored, in this implementation, only processes whose memory usage is greater than the first threshold among all processes currently running on the electronic device are acquired. This reduces the number of processes to be monitored and ensures that unmonitored processes do not experience memory leaks.

[0038] In a specific application, the first threshold may be determined according to the actual application. For example, the first threshold may be 50M.

[0039] In an embodiment of the present application, each running process of the electronic device corresponds to a subdirectory under the process (proc) directory, and the directory name is the process identifier (PID) of the process. There is a smaps_rollup file under the pid directory, which contains statistical information on the memory usage of the current process, as shown in Figure 2. The smaps_rollup file includes the actual physical memory used (Proportional Set Size, PSS) and the resident memory set (Resident Set Siz, RSS). In an embodiment of the present application, the first process reports the actual physical memory data of the second process, namely PSS. In related technologies, VmSize, namely VSS, in the / proc / pid / status file is also used. Among them:

[0040] VSS = allocated memory + memory occupied by shared libraries;

[0041] RSS = memory in use + memory occupied by shared libraries;

[0042] PSS = memory in use + memory occupied by shared libraries / number of shared library processes.

[0043] In an embodiment of the present application, the actual physical memory data of the second process is used to better analyze memory leaks. For example, in the memory usage diagram shown in FIG3 , the allocated memory of a process may include memory that has been allocated but not yet used. Therefore, using the allocated memory of the process to detect whether the process has a memory leak may not be accurate enough. For example, although the allocated memory of the process is large, the memory it is using may be very small. In addition, as shown in FIG3 , the memory occupied by the shared library may include the memory occupied by multiple processes. If the memory occupied by the shared library is directly used to perform a memory leak analysis on one of the processes, it cannot reflect the actual memory occupied by the process. Therefore, in the present application example, the PSS of the process, that is, the memory being used by the process + the memory occupied by the shared library / the number of shared library processes, is used to analyze whether the process has a memory leak, thereby ensuring the accuracy of the memory leak analysis.

[0044] In the embodiment of the present application, since the first process is a process with root privileges, the first process can read the smaps_rollup file and read the parsing related information from the smaps_rollup file. For example, a first process can be started from the dumpstat process, and the first process will have root privileges. In the embodiment of the present application, the first process can also be called a "memory leak monitoring daemon process."

[0045] S102: When it is determined that a memory growth trend of the second process satisfies a predetermined condition based on actual physical memory data of the second process obtained multiple times in a row, it is determined that a memory leak exists in the second process.

[0046] The second process is any one of the at least one first process. The memory data obtained in the previous step can be used to obtain a memory growth trend of the second process, thereby determining whether there is a memory leak in the second process.

[0047] In the embodiment of the present application, the memory growth trend of each second process is used to determine whether each process has a memory leak, rather than determining whether a memory leak is present according to a fixed threshold, thereby improving the accuracy of the judgment. For example, in the scenario shown in Figure 4 (after the process is started, memory is gradually requested, and a short-term increase occurs), if the memory increment occupied between the starting point and the end point is directly calculated, a memory leak will be determined, but there is no memory leak in essence. In the embodiment of the present application, the memory growth trend of the second process is determined based on the actual physical memory data of the second process obtained multiple times in a row. By judging the memory growth trend of the second process, the misjudgment scenario can be avoided.

[0048] In an embodiment of the present application, based on the periodic reporting of actual physical memory data of at least one second process currently running on an electronic device by a first process with root permissions obtained multiple times in a row, a memory leak is determined to exist in the second process when the memory growth trend of the second process satisfies a predetermined condition. Therefore, multiple second processes currently running on the electronic device can be detected, thereby resolving the problem of only being able to monitor memory leaks of a specific process when monitoring memory leaks, and being unable to monitor all processes on the device simultaneously, as well as the problem of false positives. This allows for the simultaneous monitoring of all processes on the electronic device and reduces false positives.

[0049] FIG5 shows a flowchart of another memory leak detection method according to an exemplary embodiment of the present application. The method 500 can be executed by an electronic device. As shown in FIG5 , the method mainly includes the following steps:

[0050] S501: Acquire actual physical memory data of at least one second process periodically reported by a first process.

[0051] The first process is a process with root ROOT authority, and the at least one second process is a process currently running on the electronic device.

[0052] This step is the same as S101 in FIG1 . For details, please refer to the above related description and will not be repeated here.

[0053] In the embodiment of the present application, optionally, the first process may report according to a fixed period, for example, the first process may scan the PSS periodically.

[0054] Alternatively, reports can be made at irregular intervals. This is because periodic PSS scanning inevitably occupies the CPU, and gaming scenarios are CPU-intensive, requiring as much CPU as possible for gaming. Therefore, by monitoring the electronic device's operating scenario through a memory leak display program, it's essential to adjust the scanning cycle at any time. For example, collecting PSS data every 2 minutes in non-gaming mode and every 10 minutes in gaming mode can improve gaming performance.

[0055] S502: Based on N1 groups of actual physical memory data of the second process obtained for n1 consecutive times, obtain N1 linear regression values ​​corresponding to the second process.

[0056] Wherein, N1 and n1 are integers greater than 1.

[0057] In the embodiment of the present application, the linear regression value can be a univariate linear regression value, and the growth rate of discrete data can be calculated by the univariate linear regression value. Assuming that the data grows linearly and satisfies y=ax+b, where y is the PSS value and x is time, then a represents the PSS growth rate. According to the univariate linear regression equation, it can be seen that:

[0058] In an embodiment of the present application, in two adjacent groups of actual physical memory data, the latter group of actual physical memory data includes the last (n-1) actual physical memory data of the previous group, and the actual physical memory data of the second process obtained for the first time after the last actual physical memory data of the previous group, where n is the number of actual physical memory data included in a group of actual physical memory data.

[0059] For example, the actual physical memory data obtained every 4 times (ie, n1=4) is a group, and one data is right-shifted each time, accumulating 10 groups, and obtaining the linear regression value of each group of actual physical memory data.

[0060] S503: When the N1 linear regression values ​​are all greater than a second threshold, determine that the memory growth trend of the second process meets a predetermined condition.

[0061] For example, the actual physical memory data obtained every four times is grouped together for linear regression calculation, with one data point shifted right each time. If the cumulative values ​​of all ten groups of linear regression are greater than 3, it is considered a memory leak; if any of the values ​​is less than 0 in the middle, it is cleared and counted again.

[0062] Through the above technical solution provided in the embodiment of the present application, it is possible to judge whether the memory growth trend of the second process meets the predetermined conditions through the piecewise linear regression value of the actual physical memory data of the second process, thereby avoiding misjudgment scenarios.

[0063] In one implementation, as shown in FIG5 , the method may further include:

[0064] S504: When the N1 linear regression values ​​are all not greater than the second threshold, obtain N2 linear regression values ​​corresponding to the second process based on N2 groups of actual physical memory data of the second process obtained for n2 consecutive times.

[0065] Where N2 and n2 are integers greater than 1, and N1>N2, n1 <n2。

[0066] In this implementation, to ensure the accuracy of the judgment, when all of the N1 linear regression values are not greater than the second threshold, the actual physical memory data of N2 groups of the second process can also be obtained with a step size larger than n1, and based on the actual physical memory data of each group of the second process, N2 linear regression values corresponding to the second process are obtained.

[0067] S505: When all of the N2 linear regression values are greater than the second threshold, it is determined that the memory growth trend of the second process meets the predetermined condition.

[0068] For example, the actual physical memory data of the second process obtained every 6 times can be used as a group for linear regression calculation. Each time, one data is shifted to the right. If the accumulated 8 groups of linear regression values are all greater than 3, it is considered that there is a memory leak; if there is a value less than 0 in the middle, it is cleared and the count is restarted.

[0069] In one implementation, as shown in FIG. 5, the method may further include:

[0070] S506: When all of the N2 linear regression values are not completely greater than the second threshold, based on the actual physical memory data of the second process obtained continuously n3 times in N3 groups, N3 linear regression values corresponding to the second process are obtained.

[0071] Wherein, N3 and n3 are integers greater than 1, and N2 > N3, n2 < n3.

[0072] In this implementation, to ensure the accuracy of the judgment, when all of the N2 linear regression values are not greater than the second threshold, the actual physical memory data of N3 groups of the second process can also be obtained with a step size larger than n2, and based on the actual physical memory data of each group of the second process, N3 linear regression values corresponding to the second process are obtained.

[0073] S507: When all of the N3 linear regression values are greater than the second threshold, it is determined that the memory growth trend of the second process meets the predetermined condition.

[0074] In the above steps, N3 and n3 are integers greater than 1, and N2 > N3, n2 < n3. For example, linear programming calculation is performed with every 8 data as a group. Each time, one data is shifted to the right. If the accumulated 5 groups of linear programming values are all greater than 3, it is considered that there is a memory leak; if there is a value less than 0 in the middle, it is cleared and the count is restarted.

[0075] In the embodiments of the present application, since n1 < n2 < n3, therefore, S502 and S503 can also be referred to as determining whether there is a memory leak with a small step size in the second process, S504 and S505 can be referred to as determining whether there is a memory leak with a medium step size in the second process, and S506 and S507 can be referred to as determining whether there is a memory leak with a large step size in the second process.

[0076] In one implementation, based on the actual physical memory data of the second process obtained continuously for multiple times, determining that the memory growth trend of the second process meets a predetermined condition may further include: comparing the actual physical memory data of the second process obtained in two adjacent times, and determining that the memory growth trend of the second process meets the predetermined condition when the number of times the latter actual physical memory data is greater than the former actual physical memory data exceeds a third threshold. For example, compare the actual physical memory data of the second process obtained every two adjacent times. If the latter actual physical memory data is greater than the former actual physical memory data and the cumulative growth is 20 times, it is considered a memory leak. In practical applications, the increase rate of the actual physical memory data occupied by a certain process may not be too fast, but it may be growing slowly all the time. Therefore, by adopting this implementation, this slow growth can be identified and regarded as a memory leak. Thus, the problem of misjudgment and wrong judgment in memory leak detection can be solved, and the effect of minimizing misjudgment can be achieved.

[0077] In the embodiments of the present application, when the first process obtains the actual physical memory data occupied by at least one second process, the span time should be greater than a first value, that is, the span time should not be too short, because too short time will lead to inaccurate judgment. For example, scan once every 2 minutes, take every 4 data as a group, shift one data to the right each time, and a total of 10 groups need to be calculated, then the time span is 20 minutes. The span time for obtaining the actual physical memory data should also be less than a second value, that is, the span time should not be too long, because the longer the time, the later the problem will be discovered.

[0078] After determining that the second process has a memory leak, relevant information of the second process can also be obtained to implement management of the second process. Therefore, in one implementation, after determining that the second process has a memory leak, the method may further include: obtaining at least one of the following information of the second process: first information, second information, and third information.

[0079] Among them, the first information is used to indicate the memory type occupied by the second process; for example, the first information can be meminfo, that is, the content output after executing dumpsys meminfo, which is mainly used to view the general classification of memory occupancy, for example, whether it is caused by Java Heap or Native Heap.

[0080] The second information is used to indicate the usage of the virtual memory area occupied by the second process; for example, the second information can be a showmap, which can be obtained by reading the / proc / pid / smaps file, where each line represents the usage of a continuous virtual memory area, and is mainly used to analyze Native Heap memory leaks.

[0081] The third information indicates the Java objects occupying memory in the second process. For example, the third information may be hprof (short for Heap Profile), a Java memory snapshot file. This information can be used to analyze which Java objects are occupying large amounts of memory, but it only analyzes Java Heap usage. This information is primarily used to perform the next processing operation and further resolve memory leak issues.

[0082] In the above implementation, since the / proc / pid / smaps file may only be readable by processes with root privileges, the first process can obtain the second information of the second process. The first and third information can be obtained by a system service process, for example, a System Server process running on the Android system.

[0083] In an embodiment of the present application, memory leaks can be judged by setting an application program in the application layer of the electronic device (for the sake of convenience of distinction, the application program can be called a memory leak presentation program). In a specific application, the memory leak presentation program can obtain the actual physical memory data of the second process currently running on the electronic device reported by the first process (which can be called a memory leak monitoring daemon process) through the service provided by the system server of the electronic device (for example, it can be called a memory leak management service), and judge whether there is a memory leak in the second process based on the actual physical memory data of each second process obtained multiple times.

[0084] Figure 6 shows a structural diagram of an electronic device in an embodiment of the present application. As shown in Figure 6, the electronic device mainly includes a hardware device 610, an operating system kernel (for example, a Linux kernel) 620, a system server 630, a memory leak monitoring daemon 640 and an application layer 650.

[0085] Hardware device 610 primarily includes a central processing unit (CPU) 611, memory (RAM) 612, and storage device 613. RAM 612 is an internal memory that directly exchanges data with CPU 611, also known as main memory. It can be read and written at any time and is very fast, typically serving as a short-term temporary data storage medium for operating systems or other running programs. A computer first loads a user-requested program or document from a storage disk into memory, and then accesses each piece of information from memory. Because many operations rely on memory, RAM capacity plays a crucial role in system performance.

[0086] The operating system kernel 620 is the main component of the operating system. It is mainly responsible for the core interface between hardware devices and their processes, and manages hardware resources as efficiently as possible. It mainly includes: process manager 621, memory manager 622 and file system 623. Among them, the process manager (Process Manager) 621 is used to manage the life cycle of all processes and maintain the data structure information of the process, including the PID of the process, the memory information PSS occupied by the process, etc. The memory manager (Memory Manager) 622 is used to manage hardware memory resources to ensure that all processes can safely share the main memory area of ​​the machine. The file system (File System) 623, in addition to the so-called narrow files (text files and binary files), directories, devices, sockets, pipes, etc. are all files. Each running process is also a file, which is the Proc file system.

[0087] System Server 630 includes various Activity Manager Services (AMS) within the Android system. In this embodiment, this primarily involves Memory Leak Management Service 631. Memory Leak Management Service 631 runs within the Android System Server process. It primarily receives data reported by the Memory Leak Monitoring Daemon and then reports it to the Memory Leak Presentation Program via Binder communication. If a memory leak occurs, it can also be used to obtain meminfo and hprof information.

[0088] The memory leak monitoring daemon 640 is a constantly running process with root privileges, allowing it to access content inaccessible to ordinary processes. Its primary purpose is to periodically scan the PSS of all processes in the Proc file system and report these to the memory leak management service via LocalSocket. If a memory leak occurs, it can also be used to retrieve showmap information.

[0089] The application layer 650 primarily includes various applications within the electronic device. In this embodiment, the primary focus is on the memory leak display program 651, which stores regularly reported data in a database and uses piecewise linear regression to calculate whether a memory leak has occurred. Users can also view a memory usage growth curve by opening the application. If a memory leak is detected, the memory leak management service and memory leak monitoring daemon are notified to capture detailed information supporting analysis, and the user is notified via a notification.

[0090] The technical solution provided in the embodiment of the present application is described below in conjunction with the electronic device structure shown in FIG6 .

[0091] FIG7 shows a flow chart of a memory leak detection method provided by an exemplary embodiment of the present application. As shown in FIG7 , the method mainly includes the following steps:

[0092] S701, memory leak presents a program monitoring the power-on and screen-on broadcasts.

[0093] S702: After receiving the broadcast, the memory leak presentation program sends startup parameters to the memory leak management service through Binder communication.

[0094] S703: The memory leak management service sends the startup parameters to the memory leak monitoring daemon process through LocalSocket.

[0095] S704, after receiving the startup parameters, the memory leak daemon process starts to periodically scan the PSS of all processes in the Proc file system.

[0096] S705, after the memory leak daemon obtains the PSS, it reports it to the memory leak management service through LocalSocket.

[0097] S706: The memory leak management service reports the problem to the memory leak presentation program via Binder communication.

[0098] S707: After receiving the data, the memory leak detection program first stores it persistently in the database, using the LRU algorithm to store the most recent 200 data items, and then determines if there is a memory leak. If a screen-off broadcast is received, the program will follow the same process to disable the scheduled scan to reduce power consumption.

[0099] The above startup parameters mainly include the period of scheduled scanning, the minimum reporting threshold, etc. Therefore, the memory leak presenting program can adjust these parameters at any time through the network.

[0100] The scheduled scanning cycle is important. Because scheduled PSS scanning inevitably occupies the CPU, and gaming scenarios are CPU-intensive, it's important to free up as much CPU as possible for gaming. Therefore, by monitoring the phone's operating scenario through a memory leak display program, it's essential to adjust the scanning cycle at any time. For example, collecting PSS data every 2 minutes in non-gaming mode and every 10 minutes in gaming mode can improve gaming performance.

[0101] Minimum reporting threshold. Electronic devices often run numerous processes, many of which occupy very little memory. Therefore, there's no need to report them to the memory leak detector for persistent storage and analysis. This parameter can be used for filtering. For example, data with a PSS of less than 50MB will not be reported, reducing the amount of data to analyze and lowering power consumption.

[0102] FIG8 shows a flow chart of a memory leak detection method provided by an exemplary embodiment of the present application. As shown in FIG8 , the method mainly includes the following steps:

[0103] S801: The memory leak presentation program traverses the PSS of the second process reported by the memory leak monitoring daemon process.

[0104] S802, determine whether there is a small step memory leak in the second process, if not, execute S803, otherwise, execute S806.

[0105] In a specific application, the memory leak detection program can calculate linear regression values ​​for every four PSSs, shifting right one PSS at a time. If the cumulative linear regression values ​​for all ten groups are greater than 3, S806 is executed to determine a memory leak. If the values ​​fall below 0 at any point in the process, the calculation is reset and the count is restarted. A specific scenario is shown in Figure 9a.

[0106] S803, determining whether there is a medium-step memory leak in the second process, if not, executing S804, otherwise, executing S806.

[0107] In a specific application, the memory leak detection program can perform linear regression calculations for every six PSSs, shifting right one PSS at a time. If the cumulative values ​​of all eight linear programs are greater than 3, S806 is executed to determine a memory leak. If any of the values ​​fall below 0, the value is reset and the count is restarted. A specific scenario is shown in Figure 9b, where the fluctuations are relatively large compared to the small step size.

[0108] S804, determining whether there is a large-step memory leak in the second process, if not, executing S805, otherwise, executing S806.

[0109] In a specific application, a memory leak detection program can perform a linear regression calculation for every eight PSS data points, shifting one PSS data point to the right each time. If the cumulative value of the five linear programs is greater than 3, a memory leak is considered. If the value is less than 0 at any point in the process, the value is reset and the count is restarted. A specific scenario is shown in Figure 9c, where the fluctuation is relatively large compared to the medium step size.

[0110] S805: Determine whether the PSS occupied by the second process increases slowly. If not, determine that there is no memory leak in the second process. Otherwise, execute S806.

[0111] In a specific application, the memory leak detection program compares every two adjacent PSS data. If the latter PSS data is larger than the previous one and the increase is 20 times, S806 is executed to determine the memory leak. The specific scenario is shown in Figure 9d. The memory usage increases very slowly but continuously.

[0112] The parameters in the above examples are all highly accurate during actual testing, but they are not necessarily fixed. The key consideration is that the data span should not be too short, as this can lead to inaccurate judgments. For example, if the scan is performed every 2 minutes, with each group consisting of 4 data points, and each data point is shifted right by one, a total of 10 groups need to be calculated, resulting in a time span of 20 minutes. The data span should also not be too long, as longer periods can delay the detection of problems.

[0113] S806: Determine whether a memory leak exists in the second process.

[0114] S807: Capture the meminfo and hprof files of the second process through the memory leak management service.

[0115] S808, capturing the showmap file of the second process through the memory leak monitoring daemon process.

[0116] S809, the memory leak presenting program displays a warning notification.

[0117] Meminfo: Output from executing dumpsys meminfo, primarily used to determine the general classification of memory usage, including whether it's caused by the Java heap or the native heap. Showmap: Reads the / proc / pid / smaps file, where each line represents the usage of a contiguous area of ​​virtual memory. This is primarily used to analyze native heap memory leaks. Hprof: A Java memory snapshot file, capable of analyzing specific Java objects occupying large amounts of memory, is also available. However, it only analyzes Java heap usage.

[0118] Through the above technical solution provided by the embodiment of the present application, all processes of the electronic device can be monitored simultaneously, and memory leaks can be predicted with high accuracy.

[0119] Figure 10 shows a block diagram of a memory leak detection device provided by an exemplary embodiment of the present application. The device can implement all or part of the content of any one of the embodiments shown in Figures 1 to 9. As shown in Figure 10, the memory leak detection device 1000 includes: an acquisition module 1001 and a determination module 1002.

[0120] In an embodiment of the present application, an acquisition module 1001 is used to obtain actual physical memory data of at least one second process periodically reported by a first process, wherein the first process is a process with root ROOT authority, and the at least one second process is a process currently running on the electronic device; a determination module 1002 is used to determine that there is a memory leak in the second process when, based on the actual physical memory data of the second process obtained multiple times in succession, it is determined that the memory growth trend of the second process meets a predetermined condition, wherein the second process is any process of the at least one first process.

[0121] In the embodiment of the present application, the determination module 1001 can obtain the actual physical memory data of the second process, and the determination module 1002 can make a final judgment based on the received information to determine whether the second process has a memory leak problem.

[0122] In one embodiment, the at least one first process includes all processes currently running on the electronic device; or, the at least one first process includes a process whose memory occupied by all processes currently running on the electronic device is greater than a first threshold.

[0123] In one embodiment, the determination module 1002 determines that the memory growth trend of the second process meets a predetermined condition based on the actual physical memory data of the second process obtained multiple times in a row, including: obtaining N1 linear regression values ​​corresponding to the second process based on N1 groups of actual physical memory data of the second process obtained n1 times in a row; when the N1 linear regression values ​​are all greater than a second threshold, determining that the memory growth trend of the second process meets the predetermined condition.

[0124] In one embodiment, the determination module 1002 determines that the memory growth trend of the second process meets the predetermined conditions based on the actual physical memory data of the second process obtained for multiple consecutive times, and also includes: when the N1 linear regression values ​​are all not greater than the second threshold, obtaining N2 linear regression values ​​corresponding to the second process based on N2 groups of actual physical memory data of the second process obtained for n2 consecutive times; when the N2 linear regression values ​​are all greater than the second threshold, determining that the memory growth trend of the second process meets the predetermined conditions.

[0125] In one embodiment, the determination module 1002 determines that the memory growth trend of the second process meets the predetermined condition based on the actual physical memory data of the second process obtained for multiple consecutive times, and also includes: when the N2 linear regression values ​​are not greater than the second threshold, based on N3 groups of actual physical memory data of the second process obtained for n3 consecutive times, obtaining N3 linear regression values ​​corresponding to the second process; when the N3 linear regression values ​​are all greater than the second threshold, determining that the memory growth trend of the second process meets the predetermined condition.

[0126] In one embodiment, in two adjacent groups of actual physical memory data, the latter group of actual memory data includes the last (n-1) actual physical memory data of the former group, and the actual physical memory data of the second process obtained after the last actual physical memory data of the former group, where n is the number of actual physical memory data included in a group of actual physical memory data.

[0127] In one embodiment, the determination module 1002 determines that the memory growth trend of the second process meets the predetermined conditions based on the actual physical memory data of the second process obtained multiple times in a row, and also includes: comparing the actual physical memory data of the second process obtained twice adjacently, and determining that the memory growth trend of the second process meets the predetermined conditions when the number of times the latter actual physical memory data is greater than the previous actual physical memory data exceeds a third threshold.

[0128] In one embodiment, the acquisition module 1001 is also used to obtain at least one of the following information of the second process: first information, the first information is used to indicate the type of memory occupied by the second process; second information, the second information is used to indicate the usage of the virtual memory area occupied by the second process; third information, the third information is used to indicate the Java objects occupying memory in the second process.

[0129] In one implementation, the acquiring module 1001 acquiring the second information includes: acquiring the second information of the second process through the first process.

[0130] In one embodiment, the period for the first process to report actual physical memory data is a non-fixed period.

[0131] The memory leak detection device in the embodiment of the present application can be a device having an operating system. The operating system can be an Android operating system, an iOS operating system, or other possible operating systems, which are not specifically limited in the embodiment of the present application.

[0132] The memory leak detection device provided in the embodiment of the present application can implement each process implemented in the method embodiments of Figures 1 to 8. To avoid repetition, they are not described here.

[0133] Figure 11 shows a block diagram of a computer device 1100 according to an exemplary embodiment of the present application. Computer device 1100 can be implemented as the aforementioned electronic devices, such as a smartphone, tablet computer, laptop computer, desktop computer, smartwatch, and television. Computer device 1100 may also be referred to as user equipment, portable terminal, laptop terminal, desktop terminal, and other names.

[0134] Typically, the computer device 1100 includes a processor 1111 and a memory 1102 .

[0135] The processor 1111 may include one or more processing cores, such as a 4-core processor, a 10-core processor, etc. The processor 1111 may be implemented in at least one hardware form of DSP (Digital Signal Processing), FPGA (Field-Programmable Gate Array), or PLA (Programmable Logic Array). The processor 1111 may also include a main processor and a coprocessor. The main processor is a processor for processing data in the awake state, also known as a CPU (Central Processing Unit); the coprocessor is a low-power processor for processing data in the standby state. In some embodiments, the processor 1111 may be integrated with a GPU (Graphics Processing Unit), which is responsible for rendering and drawing the content to be displayed on the display screen. In some embodiments, the processor 1111 may also include an AI (Artificial Intelligence) processor, which is used to process computing operations related to machine learning.

[0136] The memory 1102 may include one or more computer-readable storage media, which may be non-transitory. The memory 1102 may also include high-speed random access memory and non-volatile memory, such as one or more disk storage devices and flash memory storage devices. In some embodiments, the non-transitory computer-readable storage medium in the memory 1102 is used to store at least one instruction, which is used to be executed by the processor 1111 to implement all or part of the steps in the memory leak detection method shown in the method embodiment of the present application.

[0137] In some embodiments, computer device 1100 may optionally include a peripheral device interface 1103 and at least one peripheral device. Processor 1111, memory 1102, and peripheral device interface 1103 may be connected via a bus or signal lines. Each peripheral device may be connected to peripheral device interface 1103 via a bus, signal lines, or circuit boards. Specifically, the peripheral device may include at least one of a radio frequency circuit 1104, a display screen 1105, a camera assembly 1106, an audio circuit 1107, and a power supply 1108.

[0138] In some embodiments, the computer device 1100 further includes one or more sensors 1109 , including but not limited to an acceleration sensor 1110 , a gyroscope sensor 1111 , a pressure sensor 1112 , an optical sensor 1113 , and a proximity sensor 1114 .

[0139] Those skilled in the art will understand that the structure shown in FIG11 does not constitute a limitation on the electronic device 1100 , and may include more or fewer components than shown, or combine certain components, or adopt a different component arrangement.

[0140] In an exemplary embodiment, a computer-readable storage medium is further provided, wherein the computer-readable storage medium stores at least one computer program, which is loaded and executed by a processor to implement all or part of the steps in the above-mentioned memory leak detection method. For example, the computer-readable storage medium can be a read-only memory (ROM), a random access memory (RAM), a compact disc (CD-ROM), a magnetic tape, a floppy disk, or an optical data storage device.

[0141] In an exemplary embodiment, a computer-readable storage medium is further provided, wherein the computer-readable storage medium stores at least one computer program, which is loaded and executed by a processor to implement all or part of the steps of the above-mentioned memory leak detection method. For example, the computer-readable storage medium can be a read-only memory (ROM), a random access memory (RAM), a compact disc (CD-ROM), a magnetic tape, a floppy disk, or an optical data storage device.

[0142] In an exemplary embodiment, a computer program product is further provided. The computer program product includes at least one computer program. The computer program is loaded by a processor and executes all or part of the steps of the above-mentioned memory leak detection method.

[0143] Those skilled in the art will readily appreciate other embodiments of the present application after considering the specification and practicing the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of the present application that follow the general principles of the present application and include common knowledge or customary techniques in the art not disclosed herein. The description and examples are to be considered merely as exemplary, and the true scope and spirit of the present application are indicated by the claims.

[0144] It should be understood that the present application is not limited to the exact structures described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present application is limited only by the appended claims.

Claims

1. A memory leak detection method, applied to an electronic device, comprising: Acquire actual physical memory data of at least one second process periodically reported by the first process, wherein the first process is a process with root ROOT authority, and the at least one second process is a process currently running on the electronic device; When it is determined that a memory growth trend of a second process satisfies a predetermined condition based on actual physical memory data of the second process obtained multiple times in succession, it is determined that a memory leak exists in the second process, wherein the second process is any one of the at least one first process.

2. The method according to claim 1, wherein: The at least one first process includes all processes currently running on the electronic device; or, the at least one first process includes a process whose memory occupied is greater than a first threshold among all processes currently running on the electronic device.

3. The method according to claim 1, wherein: Determining, based on the actual physical memory data of the second process obtained for multiple consecutive times, whether the memory growth trend of the second process meets a predetermined condition includes: Based on N1 groups of actual physical memory data of the second process obtained for n1 consecutive times, obtaining N1 linear regression values ​​corresponding to the second process; When the N1 linear regression values ​​are all greater than a second threshold, determining that the memory growth trend of the second process meets a predetermined condition; Wherein, N1 and n1 are integers greater than 1.

4. The method according to claim 3, wherein: Determining, based on actual physical memory data of the second process obtained for multiple consecutive times, whether a memory growth trend of the second process meets a predetermined condition, further includes: When the N1 linear regression values ​​are not completely greater than the second threshold, obtaining N2 linear regression values ​​corresponding to the second process based on N2 groups of actual physical memory data of the second process obtained for n2 consecutive times; When the N2 linear regression values ​​are all greater than a second threshold, determining that the memory growth trend of the second process meets a predetermined condition; Where N2 and n2 are integers greater than 1, and N1>N2, n1 <n2。 5. The method according to claim 4, wherein: Determining, based on actual physical memory data of the second process obtained for multiple consecutive times, whether a memory growth trend of the second process meets a predetermined condition, further includes: When the N2 linear regression values ​​are not completely greater than the second threshold, obtaining N3 linear regression values ​​corresponding to the second process based on N3 groups of actual physical memory data of the second process obtained for n3 consecutive times; When N3 of the linear regression values ​​are all greater than a second threshold, determining that the memory growth trend of the second process meets a predetermined condition; Wherein, N3 and n3 are integers greater than 1, and N2>N3, n2 <n3。 6. The method according to any one of claims 3 to 5, wherein: Of two adjacent groups of actual physical memory data, the latter group of actual physical memory data includes the last (n-1) actual physical memory data of the former group, and the actual physical memory data of the second process obtained for the first time after the last actual physical memory data of the former group, where n is the number of actual physical memory data included in a group of actual physical memory data.

7. The method according to any one of claims 1 to 5, wherein: Determining, based on actual physical memory data of the second process obtained for multiple consecutive times, whether a memory growth trend of the second process meets a predetermined condition, further includes: Compare the actual physical memory data of the second process obtained twice adjacently, and when the number of times that the latter actual physical memory data is greater than the former actual physical memory data exceeds a third threshold, determine that the memory growth trend of the second process meets a predetermined condition.

8. The method according to any one of claims 1 to 5, wherein: After determining that the second process has a memory leak, the method further includes: Obtain at least one of the following information of the second process: first information, where the first information is used to indicate a type of memory occupied by the second process; second information, where the second information is used to indicate usage of the virtual memory area occupied by the second process; The third information is used to indicate the Java objects occupying the memory in the second process.

9. The method according to claim 8, wherein: Acquiring the second information of the second process includes: The second information of the second process is obtained through the first process.

10. The method according to any one of claims 1 to 5, wherein: The period in which the first process reports actual physical memory data is a non-fixed period.

11. An electronic device comprising a processor and a memory, wherein the memory stores programs or instructions that can be run on the processor, and when the programs or instructions are executed by the processor, the steps of the memory leak detection method according to any one of claims 1 to 10 are implemented.

12. A readable storage medium storing a program or an instruction, wherein the program or the instruction, when executed by a processor, implements the steps of the memory leak detection method according to any one of claims 1 to 10.

Citation Information

Patent Citations

  • Memory leak detection method, device and equipment

    CN111240954A

  • Memory leak detection method, device and equipment

    CN112214394A

  • Memory leak detection method and device and computer readable storage medium

    CN112445686A

  • Memory leak detection method and electronic equipment

    CN115687035A

  • Memory leak processing method and electronic equipment

    CN115934302A

Cited By

  • Dynamic memory use monitoring method based on service and process

    CN121050975A

  • A method for dynamic monitoring of memory usage based on services and processes

    CN121050975B

  • Memory leak processing method and system of kernel mode system and readable storage medium

    CN121705041A