Method for capturing memory data and electronic equipment

By capturing memory data in the child processes of the target process, the problem of low localization rate in existing methods is solved, achieving more efficient memory leak localization and memory data capture with less performance impact.

CN119271532AActive Publication Date: 2025-01-07HONOR DEVICE CO LTD
View PDF 6 Cites 0 Cited by

Patent Information

Application Number
CN202410009111.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-01-03
Publication Date
2025-01-07
Estimated Expiration
2044-01-03

AI Technical Summary

Technical Problem

Existing stack capture methods cannot fully capture process memory data, resulting in a low rate of memory leak location and potentially degrading the performance of electronic devices.

Method used

When a memory leak occurs in the target process, a target child process is created. Heap memory data is captured in the child process, including all garbage collection root objects (GCRoots) of the target process in the running memory and their reference relationships. The data is then stored in flash memory, and the capture and dump operations are performed using the monitoring service module and stack capture tool.

Benefits of technology

It improves the accuracy of memory leak detection, reduces the performance impact on the target process, avoids stack capture failures, and facilitates subsequent analysis.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119271532A_ABST
    Figure CN119271532A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a method for capturing memory data and electronic equipment. The method comprises the steps that under the condition that memory leakage occurs in a target process, the electronic equipment grabs heap memory data of the target process, and the heap memory data at least comprises all garbage collection root GCRoot objects of the target process in a running memory and reference relations between the objects; and the electronic equipment stores the grabbed heap memory data to a target position of the flash memory. Compared with a traditional stack grabbing method that only heap memory data newly generated after stack grabbing starts are collected, the method for grabbing the memory data, provided by the embodiment of the invention, can grab all GCRoot objects of the target process at the current moment, so that the positioning rate of the memory leakage problem is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of the present application relate to the field of terminal technology, and more particularly to a method for capturing memory data and an electronic device. Background Art

[0002] Memory leak refers to the situation where the memory space dynamically requested by the application during operation is not properly released or recycled when it is no longer needed. Memory leak will cause the memory resources to no longer be effectively used by the application. Over time, the unreleased memory will continue to accumulate and may eventually exhaust the system's available memory, causing application performance degradation or crash.

[0003] When an application is running, the application process will dynamically apply for memory space according to actual needs. Ideally, when the applied memory is no longer needed, the application should use the corresponding function to release the memory. When the application fails to properly manage memory resources and does not release the memory space in time after applying for it, it will cause memory leaks.

[0004] When a process has a memory leak, some tools can be used to capture the process's memory data, such as object reference relationships, memory usage, etc., to help locate the cause of the memory leak. This method is called "stack capture." However, some current stack capture methods cannot fully capture the process's memory data, resulting in a low location rate when using such memory data to locate the cause of the memory leak. Summary of the invention

[0005] The embodiments of the present application provide a method and electronic device for capturing memory data, which can more comprehensively capture the memory data of a process when a memory leak occurs in the process, so as to improve the location rate of the memory leak problem.

[0006] In a first aspect, an embodiment of the present application provides a method for capturing memory data, the method comprising: when a memory leak occurs in the target process, the electronic device captures the heap memory data of the target process, the heap memory data including at least all garbage collection root GCRoot objects of the target process in the running memory, and the reference relationship between the objects; the electronic device stores the captured heap memory data to a target location in the flash memory.

[0007] Compared with the traditional stack capture method that only collects the newly generated heap memory data after the stack capture starts, the method for capturing memory data provided by the embodiment of the present application can capture all GCRoot objects of the target process at the current moment, thereby improving the location rate of memory leak problems.

[0008] In one implementation, when a memory leak occurs in a target process, the electronic device captures the heap memory data of the target process, including: when a memory leak occurs in the target process, the electronic device creates a target subprocess, the target subprocess is a copy of the target process; the electronic device captures the heap memory data in the subprocess. In this way, the electronic device creates a target subprocess and performs a stack capture operation in the target subprocess, rather than performing a stack capture operation in the target process. Therefore, the method for capturing memory data provided in the embodiment of the present application has less impact on the performance of the target process.

[0009] In one implementation, an electronic device includes a maintenance and testing service module and a stack capture tool, the stack capture tool includes a heap tracker module and a monitoring module, the monitoring module runs in a subthread of a target process; when the electronic device detects that a memory leak occurs in the target process, the heap memory data of the target process is captured, including: the maintenance and testing service module detects that a memory leak occurs in the target process, the maintenance and testing service module generates a stack capture configuration when the memory leak occurs in the target process, and the stack capture configuration includes at least the process name of the target process; the maintenance and testing service module sends a stack capture notification to the heap tracker module, and the stack capture notification includes the stack capture configuration; the heap tracker module sends a stack capture signal to the monitoring module based on the stack capture configuration; when the monitoring module detects the stack capture signal, the target process creates a target child process; the target child process is a copy of the target process; and the target child process captures the heap memory data of the target process. In this way, after the monitoring module detects the stack capture signal, the target process creates a target child process and performs a stack capture operation in the target child process instead of performing a stack capture operation in the target process. It can be seen that in the method for capturing memory data provided in the embodiment of the present application, the target process only consumes some time when creating the target child process, and does not consume time on capturing heap memory data. Therefore, the performance impact on the target process is smaller.

[0010] In one implementation, after the maintenance and testing service module generates the stack capture configuration and before the maintenance and testing service module sends the stack capture notification to the heap tracker module, the method further includes: the maintenance and testing service module releases the constraints of the stack capture tool so that the maintenance and testing service module can communicate with the heap tracker module. When the stack capture tool is in a constrained state, the maintenance and testing service module cannot communicate with the heap tracker module and its listening module communicates. Therefore, in order to enable the maintenance and testing service module to communicate with the heap tracker module, the maintenance and testing service module releases the constraints of the stack capture tool before sending the stack capture notification to the heap tracker module, thereby avoiding stack capture failure.

[0011] In one implementation, the electronic device further includes a memory detection module and a signal reinstallation module; before the maintenance service module generates the stack capture configuration, the method further includes: the maintenance service module notifies the memory detection module to reinstall the stack capture signal; the memory detection module sends the reinstallation signal to the signal reinstallation module; the signal reinstallation module receives the reinstallation signal and executes the processing function of the reinstallation stack capture signal. In this way, the application will not block and intercept the stack capture signal, thereby ensuring that the signal takeover module can subsequently receive the stack capture signal sent by the heap tracker module to avoid stack capture failure.

[0012] In one implementation, after the electronic device stores the captured stack memory data in the target location of the flash memory, the method further includes: the maintenance service module notifies the memory detection module to restore the stack capture signal; the memory detection module sends a recovery signal to the signal reinstallation module; the signal reinstallation module receives the recovery signal and restores the stack capture signal to a default state. In this way, the electronic device can be restored to the system environment before the stack capture without causing changes to the original system environment of the electronic device.

[0013] In one implementation, the stack capture tool also includes a tracking module, and the stack capture configuration also includes a buffer-related configuration; when the electronic device detects a memory leak in the target process, the target process's heap memory data is captured, and the monitoring module also includes: the monitoring module monitors the stack capture signal and sends a first data collection notification to the tracking module; the tracking module receives the first data collection notification, and based on the buffer-related configuration, converts the captured heap memory data into heap dump data in a target format, and fills the heap dump data into the tracking buffer of the shared memory. In this way, the heap dump data is stored in the tracking buffer of the shared memory in the form of a file block in the protobuf format, so that the heap dump data can be shared between the target process, the tracking module, the heap tracker module and other modules.

[0014] In one implementation, the electronic device stores the captured heap memory data in a target location of the flash memory, including: the tracking module sends a second data collection notification to the heap tracker module; the heap tracker module forwards the second data collection notification to the maintenance and testing service module; the maintenance and testing service module receives the second data collection notification and stores the heap dump data in the tracking cache buffer to the target location. In this way, the heap dump data is saved from the running memory to the flash memory, so that the heap dump data can be analyzed at any time to locate the cause of the memory leak.

[0015] In one implementation, after the maintenance and testing service module sends the stack capture notification to the heap tracker module, the process further includes: after the maintenance and testing service module waits for a preset period of time, storing the heap dump data in the tracking cache buffer to a target location.

[0016] In one implementation, the electronic device further includes a signal takeover module, which runs in a subthread of the target process; the heap tracker module sends a stack capture signal to the monitoring module based on the stack capture configuration, including: the heap tracker module sends a stack capture signal to the signal takeover module based on the stack capture configuration; the signal takeover module receives the stack capture signal and forwards the stack capture signal to the monitoring module. In this way, the signal takeover module can call the signal processing function to capture and take over the stack capture signal, and forward the stack capture signal to the monitoring module.

[0017] In one implementation, the stack capture configuration includes a unique session name; after the maintenance and testing service module releases the constraints of the stack capture tool and before the maintenance and testing service module sends a stack capture notification to the heap tracer module, the method further includes: the maintenance and testing service module establishes a session communication with the heap tracer module based on the unique session name. In this way, the maintenance and testing service module can use an interface program (such as a tracing SDK) to establish a session communication with the heap tracer module, and subsequent communication behaviors can be performed in the session.

[0018] In one implementation, after the electronic device saves the captured heap memory data to a target location in the flash memory, the method further includes: the maintenance and testing service module clears the session data of the session communication and restores the constraints of the stack capture tool. In this way, the stack capture tool can be restored to the constraint state after the stack capture is completed, so that the stack capture tool will intercept the communication between the maintenance and testing service module and the heap tracker module.

[0019] In one implementation, the heap tracer module includes a communication whitelist, and the process name of the maintenance and testing service module is located in the communication whitelist, so that the communication between the maintenance and testing service module and the heap tracer module is not restricted by the stack capture tool.

[0020] In one implementation, the maintenance and testing service module is located in the local framework layer of the operating system, and the stack capture tool is the perfetto tool.

[0021] In one implementation, the memory detection module is located at the kernel layer of the operating system, and the signal reinstallation module is located at the application layer of the operating system.

[0022] In a second aspect, an embodiment of the present application provides a device for capturing memory data. The device includes a maintenance and testing service module and a stack capture tool. The maintenance and testing service module is used to generate a stack capture configuration corresponding to the target process when a memory leak is detected in the target process; the stack capture tool is used to capture the heap memory data of the target process based on the stack capture configuration, and the heap memory data at least includes all garbage collection root GCRoot objects of the target process in the running memory; the maintenance and testing service module is also used to store the captured heap memory data to the target location of the flash memory.

[0023] In a third aspect, the present application provides an electronic device, comprising a display screen, a memory and one or more processors; the display screen, the memory and the processor are coupled; wherein the memory stores computer program code, the computer program code comprises computer instructions, and when the computer instructions are executed by the processor, the electronic device executes the method for capturing memory data as provided in the first aspect and any possible design method thereof.

[0024] In a fourth aspect, the present application provides a computer-readable storage medium, which includes computer instructions. When the computer instructions are executed on an electronic device, the electronic device executes the method for capturing memory data provided in the first aspect and any possible design thereof.

[0025] In a fifth aspect, the present application provides a computer program product. When the computer program product is run on an electronic device, the electronic device executes the method for capturing memory data provided in the first aspect and any possible design thereof.

[0026] It can be understood that the beneficial effects that can be achieved by the technical solutions provided in the second to fifth aspects provided above can refer to the beneficial effects in the first aspect and any possible design method thereof, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS

[0027] Figure 1 This is a schematic diagram of a scenario that causes a memory leak;

[0028] Figure 2 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application is shown;

[0029] Figure 3 A schematic diagram of a layered architecture of a software system of an electronic device provided in an embodiment of the present application;

[0030] Figure 4 Another schematic diagram of the layered architecture of the software system of the electronic device provided in the embodiment of the present application;

[0031] Figure 5 It is a flowchart of a method for capturing memory data provided by an embodiment of the present application;

[0032] Figure 6 is another flowchart of a method for capturing memory data provided by an embodiment of the present application;

[0033] Figure 7 This is another flowchart of a method for capturing memory data provided by an embodiment of the present application;

[0034] Figure 8It is a structural diagram of a device for capturing memory data provided in an embodiment of the present application. DETAILED DESCRIPTION

[0035] Memory leak refers to the situation where the memory space dynamically requested by the application during operation is not properly released or recycled when it is no longer needed. Memory leak will cause the memory resources to no longer be effectively used by the application. Over time, the unreleased memory will continue to accumulate and may eventually exhaust the system's available memory, causing application performance degradation or crash.

[0036] It should be noted that the memory in the embodiments of the present application refers to the operating memory of the electronic device, such as random access memory (RAM), also known as internal memory.

[0037] During the running of an application, the application process will dynamically apply for memory space according to actual needs. For example, in a JAVA environment, the running memory can be divided into two parts: stack memory and heap memory. When a process creates an object, the JAVA virtual machine will allocate a space in the heap memory to store the object, and allocate a space in the stack memory to store the reference of the object, so as to access the methods and properties of the object.

[0038] Ideally, when the requested memory is no longer needed, the application should use the corresponding function to release the memory. In addition, the operating system can also detect and release no longer used memory through memory management mechanisms such as garbage collection to reclaim memory space. For example, in a JAVA environment, when an object's method is executed, its reference in the stack memory will be destroyed to release memory space, and the garbage collector will periodically scan the heap memory to find objects that are no longer referenced and recycle them to release memory space.

[0039] However, in actual situations, some applications fail to manage memory resources correctly. After the process applies for memory space, it does not release the memory space in time, resulting in memory leaks.

[0040] In addition, when there are mutual references between multiple objects in memory, even if these objects are no longer used, the garbage collector will not recognize these objects as garbage, so the memory space occupied by these objects will not be reclaimed, resulting in memory leaks. For example, Figure 1 This is a schematic diagram of a scenario that causes a memory leak. Figure 1As shown in the figure, the memory contains objects such as X, Y, A, B, and C, among which objects Y, A, B, and C are referenced by object X. Then, if the life cycle of object X is longer than that of object Y, at the end of the life cycle of object Y, since object X still references object Y, the garbage collector will not recycle the memory occupied by object Y. Similarly, if the life cycle of objects A, B, and C is also shorter than that of object Y, then at the end of the life cycle of objects A, B, and C, the garbage collector will not recycle the memory occupied by objects A, B, and C. Therefore, as this situation continues to accumulate, it may cause a large number of useless objects to be unable to be recycled, thereby occupying memory resources and causing memory leaks.

[0041] In addition, some other reasons, such as failure to clean up expired or useless caches in memory in a timely manner, or when the process creates a large number of temporary objects but does not destroy them in time, can also cause memory leaks.

[0042] There is no specific standard for determining whether a process has a memory leak. Generally speaking, the size of the memory occupied by a process can be used to determine whether a memory leak has occurred. For example, if the size of the memory occupied by a process is significantly increased compared to the general case, it can be considered that the process has a memory leak. For example, if a process generally occupies tens of megabytes of memory space, when the memory it occupies reaches hundreds of MB, it can be considered that the process has a memory leak. Furthermore, when the memory occupied by the process continues to increase and reaches the system limit, or the system memory is exhausted, the process is likely to crash. For example, it is found in actual applications that when the memory occupied by the process reaches 500MB, a crash may occur. Therefore, for example, when the memory occupied by the process is close to 500MB, for example, when it reaches 400M, it can be considered that the process has a memory leak. Regarding the determination standard of memory leaks, those skilled in the art can determine it based on actual conditions, and no specific limitation is made here.

[0043] When a process has a memory leak, the heap memory data at the time of the memory leak can be captured, such as the reference relationship of objects, memory usage, etc., to help locate the cause of the memory leak. This method is called "stack capture". However, the current stack capture method has a low locating rate for memory leak problems and can also reduce the performance of electronic devices.

[0044] For example, in the JAVA environment, when a memory leak occurs in the process of an application, the JAVA virtual machine can turn on the stack capture switch. After that, the stack capture-related threads of the application will monitor the heap memory, collect and record the newly generated heap memory data, for example, collect and record the reference relationship and memory usage of the newly created objects after the stack capture switch is turned on. In other words, the stack capture method cannot collect and record the heap memory data that existed before the stack capture switch was turned on. However, when the stack capture switch is turned on, the heap memory occupied by the process usually exceeds several hundred MB, and the objects that really occupy a large amount of memory already exist in the heap memory, causing the stack capture method to find the objects that really occupy a large amount of memory, which in turn leads to a low location rate of memory leak problems.

[0045] In addition, taking the JAVA environment as an example, when a thread needs to allocate and release memory, it needs to first obtain the alloc_tracker_lock lock (the alloc_tracker_lock lock is an internal lock in the JAVA virtual machine, which is used to control the thread's operation behavior on the heap memory). If other threads also want to operate on the memory, they must wait for the alloc_tracker_lock lock to be released to avoid multiple threads operating on the same memory block at the same time. Therefore, in the traditional stack capture solution, the stack capture-related threads of the application need to obtain the alloc_tracker_lock lock when applying for memory, and the main thread of the application frequently applies for memory during the frame drawing process, and also needs to obtain the alloc_tracker_lock lock. At the same time, other child threads of the application also need to obtain the alloc_tracker_lock lock when applying for memory, which causes lock competition between threads of the application, reducing the performance of the electronic device.

[0046] In view of the above problems, an embodiment of the present application provides a method for capturing memory data, which can be applied to electronic devices to improve the location rate of memory leak problems and not reduce the performance of the electronic device during stack capture.

[0047] The technical solution provided by the embodiments of the present application is described in detail below with reference to the accompanying drawings.

[0048] The technical solution provided in the present application can be applied in electronic devices. In some embodiments, the electronic device can be a mobile phone, a tablet computer, a handheld computer, a personal computer (PC), an ultra-mobile personal computer (UMPC), a netbook, a cellular phone, a personal digital assistant (PDA), an augmented reality (AR) device, a virtual reality (VR) device, an artificial intelligence (AI) device, a wearable device, a vehicle-mounted device, a smart home device and / or a smart city device, etc. The embodiments of the present application do not impose any special restrictions on the specific type of the electronic device.

[0049] Figure 2 A schematic structural diagram of an electronic device provided in an embodiment of the present application is shown.

[0050] Reference 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, an earphone interface 170D, a sensor module 180, a button 190, a motor 191, an indicator 192, a display screen 193, a subscriber identification module (SIM) card interface 194, and a camera 195, etc. Among them, the sensor module 180 may include a pressure sensor, a gyroscope sensor, an air pressure sensor, a magnetic sensor, an acceleration sensor, a distance sensor, a proximity light sensor, a fingerprint sensor, a temperature sensor, a touch sensor, an ambient light sensor, a bone conduction sensor, etc.

[0051] The processor 110 may include one or more processing units, for example, the processor 110 may include an application processor (AP), a modem processor, a graphics processor (GPU), an image signal processor (ISP), a controller, a memory, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural-network processing unit (NPU), etc. Different processing units may be independent devices or integrated into one or more processors.

[0052] The controller can be the nerve center and command center of the electronic device. The controller can generate operation control signals according to the instruction operation code and timing signal to complete the control of fetching and executing instructions.

[0053] The processor 110 may also be provided with a memory for storing instructions and data. In some embodiments, the memory in the processor 110 is a cache memory. The memory may store instructions or data that the processor 110 has just used or cyclically used. If the processor 110 needs to use the instruction or data again, it may be directly called from the memory. This avoids repeated access, reduces the waiting time of the processor 110, and thus improves the efficiency of the system.

[0054] In some embodiments, the processor 110 may include one or more interfaces. The interface 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.

[0055] The charging management module 140 is used to receive charging input from a power supply device (e.g., a charger, a laptop power supply, etc.). The charger may be a wireless charger or a wired charger. In some wired charging embodiments, the charging management module 140 may receive charging input from a wired charger through the USB interface 130. In some wireless charging embodiments, the charging management module 140 may receive wireless charging input through a wireless charging coil of the electronic device.

[0056] While charging the battery 142, the charging management module 140 can also power the electronic device through the power management module 141. The battery 142 can be composed of multiple batteries connected in series. The power management module 141 is used to connect the battery 142, the charging management module 140 and the processor 110.

[0057] The power management module 141 is used to connect 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, and supplies power to the processor 110, the internal memory 121, the display screen 193, the camera 195, and the wireless communication module 160. The power management module 141 can also be used to monitor parameters such as the battery voltage, current, battery cycle number, and battery health status (leakage, impedance). In some other embodiments, the power management module 141 can also be set in the processor 110.

[0058] The external memory interface 120 can be used to connect to an external non-volatile memory to expand the storage capacity of the electronic device. The external non-volatile memory communicates with the processor 110 through the external memory interface 120 to implement a data storage function. For example, files such as music and videos are stored in the external non-volatile memory.

[0059] The internal memory 121 may include one or more random access memories (RAM) and one or more non-volatile memories (NVM). The random access memory can be directly read and written by the processor 110, and can be used to store executable programs (such as machine instructions) of the operating system or other running programs, and can also be used to store user and application data. The non-volatile memory can also store executable programs and store user and application data, etc., and can be loaded into the random access memory in advance for direct reading and writing by the processor 110.

[0060] A touch sensor is also called a "touch control device". The touch sensor can be arranged on the display screen 193. The touch sensor and the display screen 193 form a touch screen, also called a "touch control screen". The touch sensor is used to monitor touch operations acting on or near it. The touch sensor can pass the monitored touch operations to the application processor to determine the type of touch event. Visual output related to the touch operation can be provided through the display screen 193. In other embodiments, the touch sensor can also be arranged on the surface of the electronic device, which is different from the position of the display screen 193.

[0061] The pressure sensor is used to sense the pressure signal and can convert the pressure signal into an electrical signal. In some embodiments, the pressure sensor can be set on the display screen 193. There are many types of pressure sensors, such as resistive pressure sensors, inductive pressure sensors, capacitive pressure sensors, etc. When a touch operation is applied to the display screen 193, the electronic device monitors the intensity of the touch operation according to the pressure sensor. The electronic device can also calculate the touch position according to the monitoring signal of the pressure sensor.

[0062] In some embodiments, the electronic device may include 1 or N cameras 195, where N is a positive integer greater than 1. In the embodiment of the present application, the type of camera 195 can be distinguished according to the hardware configuration and the physical location. For example, the camera arranged on the side of the display screen 193 of the electronic device can be called a front camera, and the camera arranged on the side of the back cover of the electronic device can be called a rear camera; for another example, a camera with a short focal length and a larger viewing angle can be called a wide-angle camera, and a camera with a long focal length and a small viewing angle can be called a normal camera. Among them, the length of the focal length and the size of the viewing angle are relative concepts, and there is no specific parameter limitation. Therefore, wide-angle cameras and ordinary cameras are also a relative concept, and can be specifically distinguished according to physical parameters such as focal length and viewing angle.

[0063] The electronic device implements the display function through a GPU, a display screen 193, and an application processor. The GPU is a microprocessor for image editing, connecting the display screen 193 and the application processor. The GPU is used to perform mathematical and geometric calculations for graphics rendering. The processor 110 may include one or more GPUs that execute program instructions to generate or change display information.

[0064] The electronic device can realize the shooting function through the ISP, the camera 195, the video codec, the GPU, the display screen 193 and the application processor. The GPU is used to perform mathematical and geometric calculations for graphics rendering. The processor 110 may include one or more GPUs that execute program instructions to generate or change display information.

[0065] The ISP is used to process the data fed back by the camera 195. For example, when taking a photo, the shutter is opened, and the light is transmitted to the camera photosensitive element through the lens. The light signal is converted into an electrical signal, and the camera photosensitive element transmits the electrical signal to the ISP for processing and converts it into an image visible to the naked eye. The ISP can also perform algorithm optimization on the noise and brightness of the image. The ISP can also optimize the exposure, color temperature and other parameters of the shooting scene. In some embodiments, the ISP can be set in the camera 195. The camera 195 is used to capture static images or videos.

[0066] Digital signal processors are used to process digital signals. In addition to processing digital image signals, they can also process other digital signals. For example, when an electronic device selects a frequency point, a digital signal processor is used to perform Fourier transform on the frequency point energy.

[0067] The display screen 193 is used to display images, videos, etc. The display screen 193 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 or an active-matrix organic light-emitting diode (AMOLED), a flexible light-emitting diode (FLED), Miniled, Microled, Micro-oled, a quantum dot light-emitting diode (QLED), etc. In some embodiments, the electronic device may include 1 or N display screens 193, where N is a positive integer greater than 1.

[0068] In an embodiment of the present application, the display screen 193 can be used to display the interface of the electronic device (for example, the desktop, the lock screen interface, etc.), and display images stored in the electronic device (for example, wallpapers, photos, etc.), or images captured by any one or more cameras 195 in the interface.

[0069] The wireless communication function of the electronic device can be implemented through antenna 1, antenna 2, mobile communication module 150, wireless communication module 160, modem and baseband processor.

[0070] Antenna 1 and antenna 2 are used to transmit and receive electromagnetic wave signals. Each antenna in the electronic device can be used to cover a single or multiple communication frequency bands. Different antennas can also be reused to improve the utilization of the antennas.

[0071] The mobile communication module 150 can provide solutions for wireless communications including 2G / 3G / 4G / 5G, etc., applied to electronic devices. The mobile communication module 150 can receive electromagnetic waves through the antenna 1, and filter, amplify, and process the received electromagnetic waves, and transmit them to the modulation and demodulation processor for demodulation. The mobile communication module 150 can also amplify the signal modulated by the modulation and demodulation processor, and convert it into electromagnetic waves for radiation through the antenna 1. In some embodiments, at least some of the functional modules of the mobile communication module 150 can be set in the processor 110. In some embodiments, at least some of the functional modules of the mobile communication module 150 can be set in the same device as at least some of the modules of the processor 110.

[0072] The modem processor may include a modulator and a demodulator. Among them, the modulator is used to modulate the low-frequency baseband signal to be sent into a medium-high frequency signal. The demodulator is used to demodulate the received electromagnetic wave signal into a low-frequency baseband signal. The demodulator then transmits the demodulated low-frequency baseband signal to the baseband processor for processing. After the low-frequency baseband signal is processed by the baseband processor, it is passed to the application processor. The application processor outputs a sound signal through an audio device (not limited to a speaker 170A, a receiver 170B, etc.), or displays an image or video through a display screen 193. In some embodiments, the modem processor may be an independent device. In other embodiments, the modem processor may be independent of the processor 110 and be set in the same device as the mobile communication module 150 or other functional modules.

[0073] The wireless communication module 160 can provide wireless communication solutions including wireless local area networks (WLAN) (such as wireless fidelity (Wi-Fi) networks), bluetooth (BT), global navigation satellite system (GNSS), frequency modulation (FM), near field communication (NFC), infrared (IR), etc., which are applied to electronic devices. The wireless communication module 160 can be one or more devices integrating at least one communication processing module. The wireless communication module 160 receives electromagnetic waves via the antenna 2, modulates the frequency of the electromagnetic wave signal and performs filtering, and sends the processed signal to the processor 110. The wireless communication module 160 can also receive the signal to be sent from the processor 110, modulate the frequency of it, amplify it, and convert it into electromagnetic waves for radiation through the antenna 2.

[0074] The SIM card interface 194 is used to connect a SIM card. The SIM card can be connected to or disconnected from the electronic device by inserting it into or removing it from the SIM card interface 194. The electronic device can support one or more SIM card interfaces. The SIM card interface 194 can support Nano SIM cards, Micro SIM cards, SIM cards, etc. Multiple cards can be inserted into the same SIM card interface 194 at the same time. The SIM card interface 194 can also be compatible with external memory cards. The electronic device interacts with the network through the SIM card to implement functions such as calls and data communications. One SIM card corresponds to one user number.

[0075] It is understandable that the interface connection relationship between the modules illustrated in the embodiment of the present invention is only a schematic illustration and does not constitute a structural limitation of the electronic device. In other embodiments of the present application, the electronic device may also adopt different interface connection methods in the above embodiments, or a combination of multiple interface connection methods.

[0076] Of course, it is understandable that the above Figure 2 The figure is only an exemplary description when the electronic device is in the form of a mobile phone. If the electronic device is in the form of a tablet computer, a handheld computer, a PC, a PDA, a wearable device (such as a smart watch, a smart bracelet), etc., the structure of the electronic device may include Figure 1 The structure shown in the figure is less than Figure 1 The structures shown in the figure are not limited here. Of course, it can be understood that the above Figure 2 The figure is only an exemplary description when the electronic device is in the form of a mobile phone. If the electronic device is in the form of a tablet computer, a handheld computer, a PC, a PDA, a wearable device (such as a smart watch, a smart bracelet), etc., the structure of the electronic device may include Figure 2 The structure shown in the figure is less than Figure 2 More structures are shown in the figure, which are not limited here.

[0077] It is understandable that, in general, the realization of electronic device functions requires not only hardware support but also software cooperation. The software system of the electronic device can adopt a layered architecture, an event-driven architecture, a micro-core architecture, a micro-service architecture, or a cloud architecture. Taking the system as an example, the software structure of the electronic device is illustrated.

[0078] Figure 3 A schematic diagram of a layered architecture of a software system of an electronic device provided in an embodiment of the present application. The layered architecture divides the software into several layers, each with clear roles and division of labor. The layers communicate with each other through software interfaces (such as APIs).

[0079] In some examples, reference Figure 3 As shown, the software of the electronic device is divided into five layers, from top to bottom, namely the application layer, the application framework layer (also called the application framework layer), the local framework layer (also called the native layer) and the Android runtime (android runtime), the hardware abstraction layer (HAL) and the kernel layer (also called the kernel layer or driver layer).

[0080] The application layer may include a series of applications (APP). Figure 3 As shown, the application layer may include applications such as camera, gallery, calendar, map, WLAN, Bluetooth, music, video, short message, call, navigation, instant messaging, etc.

[0081] The application framework layer provides an application programming interface (API) and a programming framework for the application programs of the application layer. The application framework layer includes some predefined functions or services. For example, the application framework layer may include an activity manager, a window manager, a content provider, an audio service, a view system, a phone manager, a resource manager, a notification manager, a package manager, etc., and the embodiments of the present application do not impose any restrictions on this.

[0082] The local framework layer can include multiple functional modules. For example: surface manager, media library, OpenGL ES, SGL, etc. The surface manager is used to manage the display subsystem and provide fusion of 2D and 3D layers for multiple applications. The media library supports playback and recording of multiple commonly used audio and video formats, as well as static image files, etc. OpenGL ES is used to implement 3D graphics drawing, image rendering, synthesis, and layer processing, etc. SGL is a drawing engine for 2D drawing.

[0083] The Android runtime includes the core library and the ART virtual machine. The Android runtime is responsible for the scheduling and management of the Android system. The core library consists of two parts: one is the function that the Java language needs to call, and the other is the Android core library. The application layer and the application framework layer run in the ART virtual machine. The ART virtual machine executes the Java files of the application layer and the application framework layer as binary files. The ART virtual machine is used to perform object life cycle management, stack management, thread management, security and exception management, and garbage collection.

[0084] The hardware abstraction layer is an interface layer between the operating system kernel and the hardware circuit, and its purpose is to abstract the hardware. It hides the hardware interface details of a specific platform and provides a virtual hardware platform for the operating system, making it hardware-independent and portable on multiple platforms. The HAL layer provides a standard interface to display device hardware functions to the higher-level Java API framework (i.e., the framework layer). The HAL layer contains multiple library modules, each of which implements an interface for a specific type of hardware component, such as: audio HAL audio module, bluetooth HAL Bluetooth module, camera HAL camera module (also called camera HAL or camera hardware abstraction module), sensors HAL sensor module (or sensor service, sensor service).

[0085] The kernel layer is a layer between hardware and software. The kernel layer includes at least display driver, camera driver, audio driver, sensor driver, battery driver, etc., which are not limited in this application. Among them, the sensor driver can specifically include the driver of each sensor included in the electronic device, such as ambient light sensor driver, etc.

[0086] Figure 4 Another schematic diagram of the layered architecture of the software system of the electronic device provided in an embodiment of the present application.

[0087] In some embodiments, Figure 3 and Figure 4 As shown, the software system of the electronic device also includes a maintenance and testing service module, a signal takeover module and a stack capture tool. The maintenance and testing service module can be located in the local framework layer, and the signal takeover module can be located in the application layer.

[0088] The stack capture tool may include, for example, a heap tracker module, a listening module, and a tracing module. Among them, the heap tracker module and the tracing module may be located at the local framework layer, for example, and the listening module may be located at the application layer, for example. Exemplarily, in the Android system, the stack capture tool may adopt the perfetto tool, and accordingly, the heap tracker module may be the heapprofd module in the perfetto tool, the listening module may be the perfetto_hprof_listener module in the perfetto tool, and the tracing module may be the traced module in the perfetto tool. Of course, other stack capture tools may also be used, which are not specifically limited here.

[0089] In some embodiments, the software system of the electronic device further includes a memory detection module and a signal reinstallation module, wherein the memory detection module may also be referred to as a memcheck module, located at the kernel layer; the signal reinstallation module may also be referred to as an allocation_dumper module, located at the application layer.

[0090] In the embodiment of the present application, the signal takeover module and the monitoring module can be sub-threads running in the main process of the application. When the application is started, the sub-threads corresponding to the signal takeover module and the monitoring module can be started, for example, the signal_catcher thread corresponding to the signal takeover module is started, and the perfetto_hprof_listener thread corresponding to the monitoring module is started. The signal reinstallation module runs in the main process of the application.

[0091] It should be noted that multiple applications can be installed in the electronic device, and each application can start the subthreads corresponding to the signal takeover module and the monitoring module in its process when it is started. In the following, for the sake of convenience, the process where the memory leak occurs is called the target process, and the application corresponding to the target process is called the target application.

[0092] The maintenance and testing service module, such as the hiview module, is used to monitor, collect and analyze the operation data of various components and modules on the electronic device. In the embodiment of the present application, the maintenance and testing service module may at least include: generating a stack capture configuration of a stack capture tool, providing an interface program to access the stack capture tool, and dump data dump analysis and other functions.

[0093] The stack capture configuration refers to the configuration used by the stack capture tool when performing the stack capture function. The stack capture configuration may include, for example, the unique session name (Unique Session Name), the stack capture duration, the buffer-related configuration, the data source-related configuration, etc. Among them, the buffer-related configuration may include the buffer size used to cache the stack capture results in the shared memory, the buffer filling strategy, etc. The data source-related configuration may include the data source name, dynamic buffer mapping, heap dump-related configuration, etc. The heap dump-related configuration may include, for example, the process name of the process (i.e., the target process) where the memory leak occurs. The stack capture configuration may be written in the format provided by the stack capture tool, or in other formats that can be recognized by the stack capture tool.

[0094] The interface program is used to access the stack capture tool so that the maintenance and testing service module communicates with the stack capture tool and calls the stack capture tool to capture the stack. Exemplarily, when the stack capture tool is a perfetto tool, the interface program can be a tracing SDK provided by the perfetto tool.

[0095] The dump analysis function includes analyzing the stack capture results and locating the cause of the memory leak. In some examples, the maintenance and testing service module can also upload the dump data to the network side to facilitate online analysis of the stack capture results and online location of the cause of the memory leak.

[0096] A heap tracker module, such as a heapprofd module, can run in an independent process, for example, in a heapprofd process. In an embodiment of the present application, the heap tracker module is used to implement session control functions and stack capture management functions, such as generating stack capture signals. Exemplarily, when a memory leak occurs in the target process, the maintenance service module can send a stack capture notification to the heap tracker module, and when the heap tracker module receives the stack capture notification, it can generate a stack capture signal, such as a java heap graph stack capture signal.

[0097] The session control function is used to implement session communication between the maintenance and testing service module and the heap tracker module, for example, to implement communication between the hiview process and the heap tracker process. Exemplarily, when the heap tracker module receives a stack capture notification from the maintenance and testing service module based on session communication, the heap tracker module can send a stack capture signal. After the stack capture is completed, the heap tracker module can also notify the maintenance and testing service module to obtain the stack capture result from the shared memory.

[0098] Memory detection module, such as memcheck module. Generally speaking, when a memory leak occurs, the memory detection module can send a stack capture signal to the stack capture tool. The stack capture signal can be, for example, signal 44 in the Linux system. There are 31 standard signals in the Linux system, numbered 1-31. Each signal name starts with "SIG" and is followed by a suffix (such as INT, HUP, KILL, etc.). Starting with Linux version 2.2, the Linux system supports 33 different real-time signals, numbered 32-64, but the application program uses SIGRTMIN+n representation instead, where SIGRTMIN is the starting value of the real-time signal and n is the offset relative to the starting value. The 44 signal here is a signal customized in the application program for stack capture management when a memory leak occurs.

[0099] In the embodiment of the present application, the function of the memory detection module is modified. For example, when a memory leak occurs in the target process, the maintenance service module can notify the memory detection module to reinstall the capture stack signal through the ioctl function, and the memory detection module can send a 44 signal to the signal reinstallation module, and use the 44 signal to notify the signal reinstallation module to execute the processing function of reinstalling the capture stack signal. Among them, ioctl is a function in the driver of the electronic device to manage the I / O channel of the device.

[0100] The signal reinstallation module, such as the allocation_dumper module, is used to reinstall the stack capture signal. Exemplarily, the signal reinstallation module can execute the processing function of reinstalling the stack capture signal when receiving the 44 signal sent by the memory detection module. It should be noted here that the reason why the stack capture signal needs to be reinstalled is that some applications will intercept the stack capture signal, resulting in stack capture failure. Therefore, the embodiment of the present application reinstalls the stack capture signal before stack capture, so as to avoid the stack capture signal being intercepted.

[0101] In some embodiments, the signal reinstallation module may not be provided, that is, the processing function of reinstalling the stack capture signal is not executed. In this way, as long as the application does not intercept the stack capture signal, the stack can be captured successfully. In this case, the maintenance service module no longer needs to call the memory detection module through the ioctl function to send the signal reinstallation module 44 signal.

[0102] The signal takeover module, such as the signal_catcher module, is used to take over the stack capture signal from the heap tracker module, such as the java heap graph stack capture signal, and send the stack capture signal to the monitoring module.

[0103] A monitoring module, such as the perfetto_hprof_listener module, is used to monitor the stack capture signal. When the monitoring module receives the stack capture signal sent by the signal takeover module, the target process can create a target child process and capture the heap memory data of the target process in the target child process, such as traversing and capturing the garbage collection root GCRoot object of the target process and the reference relationship between objects.

[0104] A tracing module, such as a traced module, can run in an independent process, for example, in a traced process. In an embodiment of the present application, after capturing the heap memory data, the monitoring module can send a data collection notification to the tracing module. After receiving the data collection notification, the tracing module converts the captured heap memory data into heap dump data in a target format, for example, into a file block in a protobuf format, and fills the heap dump data in the trace buffer of the shared memory. Then, the tracing module can send a data collection notification to the heap tracer module. After receiving the data collection notification, the heap tracer module notifies the maintenance and testing service module to read the data through a session. After receiving the notification from the heap tracer module, the maintenance and testing service module can read the data in the trace buffer of the shared memory, and save the read data in the memory leak log.

[0105] The technical solutions provided in the embodiments of the present application can be implemented in electronic devices having the above-mentioned hardware architecture or software architecture.

[0106] Figure 5 It is a flowchart of a method for capturing memory data provided in an embodiment of the present application.

[0107] Based on the above Figure 4 The software architecture shown below is combined with Figure 5 , the method for capturing memory data provided by the embodiment of the present application is introduced. Figure 5 As shown, the method for capturing memory data may include S201-S215:

[0108] Step S201: the maintenance and testing service module detects that a memory leak occurs in a target process.

[0109] In the embodiment of the present application, the maintenance and testing service module may be a module for monitoring, collecting and analyzing the operation data of each component and module, such as a hiview module. The maintenance and testing service module may be provided by the developer of the operating system or the manufacturer of the electronic device, for example, built into the operating system of the electronic device, or the maintenance and testing service module may be installed separately, and this is not collectively limited here.

[0110] In the embodiment of the present application, the maintenance and detection service module can detect whether a process has a memory leak in at least one way.

[0111] In one implementation, the maintenance service module can monitor the trend of the process's memory usage. If the process's memory usage continues to increase instead of decreasing, the process may have a memory leak. In other words, if the size of the memory occupied by a process is significantly increased compared to normal circumstances, it can be considered that the process has a memory leak. Exemplarily, a threshold can be set for the memory used by the process. If the memory used by the process is greater than the threshold, the process is considered to have a memory leak. Exemplarily, if a process generally occupies tens of megabytes of memory space, the threshold can be set to several hundred MB, such as 400 MB. Then, when the memory it occupies exceeds 400 MB, it can be considered that the process has a memory leak.

[0112] In one implementation, the maintenance service module can also determine whether a process has a memory leak based on whether the process crashes or causes an exception due to memory exhaustion. Generally speaking, if a process occupies too much memory, for example, if the memory occupied exceeds 500MB, it may crash. Then, if a process crashes frequently, it can be considered that it has a memory leak.

[0113] In one implementation, the maintenance service module may also check the number of memory allocations and releases of the process. If a large amount of memory is allocated but the number of releases is small, there may be a memory leak.

[0114] Of course, the maintenance service module or other modules in the electronic device may also use other methods to monitor whether a process has a memory leak. For example, a performance monitoring tool provided by the system may be used to monitor the memory resource usage of the process to determine whether a process has a memory leak. The embodiment of the present application does not specifically limit the method for determining whether a process has a memory leak.

[0115] Step S202: The maintenance and testing service module generates a stack capture configuration corresponding to the target process.

[0116] When the maintenance and testing service module detects that a process has leaked, it can obtain the process name of the target process where the memory leak occurs, and generate a stack capture configuration for capturing the stack of the target process.

[0117] In the embodiment of the present application, the stack capture configuration may be provided in a format that can be recognized and parsed by the stack capture tool. Then, for different stack capture tools, the format of the stack capture configuration and the information contained therein may be different.

[0118] For example, taking the stack capture tool as the perfetto tool, the stack capture configuration may include the following contents:

[0119] (1) Trace related configuration, for example, may include:

[0120] The unique session name unique_session_name is the session name used by the maintenance service module (such as the hiview module) and the heap tracker module (such as the heapprofd module) for session communication. Its fields are exemplified as follows:

[0121] unique_session_name:hiview_get_perfetto_hprof

[0122] The duration of the stack capture, duration_ms, is the running time of the stack capture tool, such as the running time of the perfetto tool. Its fields are as follows:

[0123] duration_ms: 100s

[0124] (2) Buffer related configuration, for example, may include:

[0125] Buffer size size_kb, that is, the buffer size allocated in shared memory for heap dump data. Its fields are as follows:

[0126] size_kb: 512M

[0127] Fill_policy is the fill policy of the buffer. Its fields are as follows:

[0128] fill_policy: perfetto::protos::gen::TraceConfig_BufferConfig_FillPolicy_RING_BUFFER

[0129] Exemplarily, there are three filling strategies: UNSPECIFIED, RING_BUFFER, and DISCARD. UNSPECIFIED means that no filling strategy is specified, RING_BUFFER means that the buffer behaves like a ring buffer. When the buffer is full, the newly written data will overwrite and replace the oldest data in the buffer. RING_BUFFER is the default filling strategy, and DISCARD means that the buffer stops accepting data after it is full, and further write attempts will be discarded.

[0130] (3) Data source data_source mutable related configuration, for example, may include:

[0131] Data source name. Its fields are as follows:

[0132] name: android.java_hprof

[0133] Dynamic buffer mapping target_buffer is used to define the location of the data source in the buffer. In the simplest case, only one buffer can be defined, and all data sources will record data in this buffer. Of course, when there are multiple data sources, different target_buffer values ​​can be set for different data sources to separate different data sources into different buffers. Its fields are exemplified as follows:

[0134] target_buffer: 0

[0135] java_hprof_config: see (4)

[0136] (4) Heap dump related configuration JavaHprofConfig, for example, may include:

[0137] The process command line process_cmdline is used to configure the process name of the process where the memory leak occurs (i.e. the target process). Its fields are as follows:

[0138] process_cmdline: The process name of the process where the memory leak occurs

[0139] It should be noted that the fields in the stack capture configuration can be statically configured or dynamically configured when it is determined that a memory leak occurs in the process, or some fields in the stack capture configuration are statically configured and other fields are dynamically configured. Exemplarily, the statically configurable fields may include (1) to (3) in the above stack capture configuration, and the dynamically configurable fields may include (4) in the above stack capture configuration.

[0140] In other implementations, the capture stack configuration may include more content or less content, or the capture stack configuration may also have other formats, which is not limited in the embodiments of the present application.

[0141] After the maintenance and testing service module generates the stack capture configuration of the target process, the stack capture configuration can be saved in the flash memory in the form of a configuration file, or the stack capture configuration can be loaded into the memory for reading and parsing with the stack capture tool.

[0142] Step S203: the maintenance and testing service module releases the constraints of the stack capture tool, and enables the monitoring module to start monitoring the stack capture signal.

[0143] Generally speaking, when there is no need to capture the stack of a process, the stack capture tool is in a constrained state. For example, if the stack capture tool is a perfetto tool, this state can be called the existence of a perfetto constraint.

[0144] When the stack capture tool is in a constrained state, the stack capture tool will restrict the communication between the maintenance and testing service module and the heap tracker module. That is to say, when the stack capture tool is in a constrained state, the maintenance and testing service module and the heap tracker module cannot communicate. Therefore, in order to facilitate the subsequent use of the stack capture tool for stack capture, the maintenance and testing service module first releases the constraints of the stack capture tool after generating the stack capture configuration, such as releasing the perfetto constraint, so that the maintenance and testing service module can communicate with the heap tracker module.

[0145] Step S204: the maintenance and testing service module establishes a session communication with the heap tracker module based on the stack capture configuration.

[0146] In an embodiment of the present application, the maintenance and testing service module may include an interface program for accessing the stack capture tool. The maintenance and testing service module may establish a session communication with the stack tracker module through the interface program, so as to send a stack capture notification to the stack tracker module later. Exemplarily, the maintenance and testing service module and the tracker module may establish a session communication using the unique session name unique_session_name in the stack capture configuration, and subsequent communications between the two are implemented in the session.

[0147] Exemplarily, when the stack capture tool is the perfetto tool, the maintenance service module may include a tracing SDK. The maintenance service module (eg, the hiview module) may establish a session communication with a heap tracer module (eg, the heapprofd module) through the tracing SDK.

[0148] It should be noted here that in the perfetto tool, the communication between the hiview module and the heapprofd module is subject to the constraints of the perfetto tool. That is to say, under the constraints of the perfetto tool, the heapprofd module may not be able to communicate normally with the hiview module.

[0149] To this end, the heap tracker module of the embodiment of the present application may include a communication whitelist, the process name of the maintenance service module is located in the communication whitelist, and the communication between the process located in the communication whitelist and the heap tracker module is not constrained by other processes, that is, the communication between the maintenance service module and the heap tracker module is not constrained by the stack capture tool. In this way, by using the whitelist mechanism, the hiview module is added to the communication whitelist of the heapprofd module, so that the communication between the hiview module and the heapprofd module is not constrained by the perfetto tool.

[0150] Step S205: the maintenance and testing service module sends a stack capture notification to the stack tracker module.

[0151] After the maintenance and testing service module establishes session communication with the heap tracker module, the maintenance and testing service module may send a stack capture notification to the heap tracker module based on the session, where the stack capture notification includes a stack capture configuration.

[0152] Step S206: The heap tracker module receives the stack capture notification and sends a stack capture signal to the signal takeover module.

[0153] After receiving the stack capture notification, the tracker module can parse the stack capture configuration and send a stack capture signal to the signal takeover module running in the subprocess of the target process based on the target process in the stack capture configuration. The signal takeover module hereinafter refers to the signal takeover module running in the subprocess of the target process.

[0154] Taking the JAVA environment as an example, the stack capture signal may be, for example, a java heap graph stack capture signal.

[0155] Step S207: the signal taking over module receives the stack capture signal and forwards the stack capture signal to the monitoring module.

[0156] In an embodiment of the present application, the signal takeover module can call a signal processing function to capture and take over the stack capture signal. After the signal takeover module captures the stack capture signal, the stack capture signal can be forwarded to the monitoring module in the signal takeover module running in the subprocess of the target process. The signal takeover module hereinafter refers to the monitoring module running in the subprocess of the target process.

[0157] Step S208: When the monitoring module monitors the stack capture signal, the target process creates a target child process.

[0158] After the target process is started, the monitoring module starts to monitor the stack capture signal. After the monitoring module monitors the stack capture signal, the target process can create a target child process. Exemplarily, after the monitoring module monitors the stack capture signal, the target process can create a target child process by fork call. The target child process is a copy of the target process and has almost the same code, data, and stack. However, the target child process is independent of the target process, each has its own execution environment, and does not affect each other.

[0159] Step S209: the target child process captures the heap memory data of the target process.

[0160] The target subprocess can traverse and capture the heap memory data of the target process at the current moment in the memory, for example, traverse and capture the objects in the heap memory of the target subprocess at the current moment, the reference relationship between the objects, the memory usage, etc. Taking the JAVA environment as an example, the target subprocess can capture the GCRoot objects in the running memory of the target process at the current moment, the reference relationship between the objects, etc. Among them, the target subprocess can capture at least part of the GCRoot objects in the running memory of the target process at the current moment, and the reference relationship between the objects, for example, all the GCRoot objects in the running memory of the target process at the current moment, and the reference relationship between the objects can be captured. It can be understood that whether it is capturing part of the GCRoot objects in the running memory of the target process at the current moment, and the reference relationship between the objects, or capturing all the GCRoot objects in the running memory of the target process at the current moment, and the reference relationship between the objects, it includes the objects generated by the target process before the current moment, and the reference relationship between the objects, so it can improve the accuracy of locating the memory leak problem.

[0161] Furthermore, the target child process can perform a dump operation on the captured heap memory data, that is, save the captured heap memory data into the shared memory.

[0162] In this way, the target process creates a target child process and performs stack grabbing operations in the target child process instead of performing stack grabbing operations in the target process. In this way, the target process only consumes some time when creating the target child process and does not consume time on grabbing heap memory data. Therefore, the performance impact on the target process is smaller, or in other words, the performance of the target process is hardly affected.

[0163] In one implementation, the target child process can traverse the garbage collection root GCRoot of the target process in memory, find all root objects identified as GCRoot, that is, GCRoot objects, and dump all GCRoot objects of all target processes captured at the current moment. Among them, the GCRoot object is a concept in the field of garbage collection (Garbage Collection, GC), which refers to the object identified as a "root object (RootObject)" during the garbage collection process. The root object is the starting point of garbage collection. Through the root object, all reference relationships in the program can be traced, so as to determine which objects are reachable and which objects are unreachable.

[0164] It should be noted that in the embodiment of the present application, when the target child process captures the stack, what is captured is the heap memory data of the target process at the current time (i.e. the time when the stack capture starts). The captured heap memory data at least includes all the GCRoot objects of the target process in the memory, and the reference relationship between the objects, that is, it includes the GCRoot objects that the target process has generated before the stack capture starts. Therefore, compared with the traditional stack capture method that only collects the newly generated heap memory data after the stack capture starts, the method provided in the embodiment of the present application captures more comprehensive heap memory data, and therefore, can improve the location rate of memory leak problems.

[0165] Step S210: the monitoring module sends a data collection notification to the tracking module.

[0166] In the specific implementation. The monitoring module can send a data collection notification to the tracking module when it monitors the stack capture signal; or, the monitoring module can also send a data collection notification to the tracking module when the target child process captures the GCRoot object of the target process; or, the monitoring module can also send a data collection notification to the tracking module after the target child process finishes capturing the GCRoot object of the target process. The embodiment of the present application does not specifically limit the timing when the monitoring module sends the data collection notification to the tracking module.

[0167] Step S211, the tracking module receives the data collection notification from the monitoring module, converts the captured heap memory data into heap dump data in a target format, and fills the heap dump data into the tracking cache buffer of the shared memory.

[0168] After receiving the data collection notification, the tracking module can access the shared memory based on the buffer-related configurations, such as buffer size, filling strategy, etc., and the data source-related configurations, such as data source name, dynamic buffer mapping, etc., and convert the captured heap memory data into heap dump data in the target format, and fill the heap dump data into the tracking cache buffer of the shared memory.

[0169] Exemplarily, the target format can be a protobuf format, that is, the tracing module can convert the captured heap memory data into heap dump data in a protobuf format, and store the heap dump data in the form of a protobuf format file block in a tracing buffer of a shared memory, thereby facilitating the sharing of the heap dump data between modules such as the target process, tracing module, and heap tracker module.

[0170] Step S212: The tracking module sends a data collection notification to the heap tracker module.

[0171] In some implementations, the monitoring module may send a data collection notification to the heap tracker module after the heap dump data is saved to the tracking buffer of the shared memory. Alternatively, the monitoring module may send a data collection notification to the heap tracker module earlier, for example, when receiving a data collection notification from the monitoring module, the monitoring module sends a data collection notification to the heap tracker module.

[0172] The embodiment of the present application does not specifically limit the timing when the tracking module sends the data collection notification to the stack tracker module.

[0173] Step S213: the stack tracker module receives the data collection notification from the tracking module and forwards the data collection notification to the maintenance and testing service module.

[0174] Step S214: The maintenance and testing service module receives the data collection notification and stores the heap dump data in the shared memory to a target location in the flash memory.

[0175] The target location is, for example, a preset memory leak log directory in the flash memory.

[0176] In some implementations, upon receiving the data collection notification, the maintenance and testing service module may immediately obtain the heap dump data from the shared memory, and store the obtained heap dump data in the memory leak log directory.

[0177] In other implementations, the maintenance and testing service module may also wait for a preset time after sending the stack capture notification, then obtain the heap dump data from the shared memory, and store the obtained heap dump data in the memory leak log directory. The preset time is greater than or equal to the time consumed by the stack capture tool from starting to capture the stack to filling the stack capture result (i.e., the heap dump data) into the tracking cache buffer of the shared memory. In this way, it can be ensured that the maintenance and testing service module successfully obtains the heap dump data from the shared memory.

[0178] Exemplarily, the maintenance and testing service module can set a cycle timer, for example, each cycle timer is 50 seconds. After sending the stack capture notification, the maintenance and testing service module can wait for at least one cycle timer, for example, two cycle timers, that is, 100 seconds, and then obtain the heap dump data from the shared memory, and store the heap dump data in the memory leak log directory. In this way, the waiting time of the maintenance and testing service module for capture and dumping can be ensured.

[0179] In some implementations, the heap dump data in the memory leak log directory may have a preset name, such as pid-java-heap-graph, where pid refers to the process id of the target process, and different processes have different process ids, so as to distinguish the heap dump data corresponding to different processes.

[0180] It should be noted that in the embodiment of the present application, shared memory refers to the shared storage space in the running memory (such as RAM) of the electronic device, and the memory leak log directory is located in the flash memory of the electronic device, that is, the maintenance service module saves the heap dump data from the running memory to the flash memory. In order to analyze the heap dump data at any time and locate the cause of the memory leak.

[0181] In some implementations, the maintenance and testing service module may also upload the heap dump data to a cloud server to facilitate online analysis of the cause of the memory leak.

[0182] Step S215: The maintenance and testing service module cleans up the session data and restores the constraints of the stack capture tool.

[0183] After the maintenance and testing service module saves the heap dump data in the memory leak log directory, the entire stack capture process ends. At this time, the maintenance and testing service module can clean up the session data of the session communication with the heap tracker module, end the session communication with the heap tracker module, and restore the stack capture tool to the constrained state, for example, restore the perfetto constraint.

[0184] The method provided by the embodiment of the present application is that after the maintenance and testing service module detects that a memory leak occurs in the target process, a stack capture tool (such as the perfetto tool) is called to capture the stack of the target process. The stack capture tool captures the heap memory data of the target process and performs a dump operation to obtain the heap dump data. The maintenance and testing service module saves the heap dump data in the memory leak log directory of the flash memory to facilitate the location analysis of the cause of the memory leak. Compared with the traditional stack capture method, the method provided by the embodiment of the present application can capture all the GCRoot objects of the target process at the current moment and the reference relationship between its objects, thereby improving the location rate of the memory leak problem. In addition, the method provided by the embodiment of the present application is that after the monitoring module monitors the stack capture signal, the target process creates a target child process by fork call, performs stack capture in the target child process, and performs stack capture in the target process. Therefore, the target process only consumes some time when creating the target child process, and does not consume time on capturing heap memory data, which has less impact on the performance of the target process.

[0185] It is worth noting that some applications will intercept the stack capture signal to avoid being captured by the stack capture tool. For example, some applications can shield the stack capture signal by calling the signal or sigaction method. In order to avoid the stack capture signal being intercepted and ensure the stack capture is successful, in some embodiments, the stack capture signal can be reinstalled before the stack is captured.

[0186] Figure 6 This is another flowchart of a method for capturing memory data provided in an embodiment of the present application.

[0187] Combination Figure 5 and Figure 6 As shown, in Figure 5 Based on the method flow shown, Figure 6 The method flow shown adds a step of reinstalling the stack capture signal between step S201 and step S202, that is, adding steps S301-S303, and adds a step of restoring the stack capture signal after step S215, that is, adding steps S304-S306. Specifically:

[0188] Step S301: the maintenance service module notifies the memory detection module to reinstall the stack capture signal.

[0189] Taking the Android system as an example, the maintenance service module can notify the memory detection module through the ioctl function that the stack capture signal needs to be reinstalled.

[0190] Step S302, the memory detection module sends a reinstallation signal to the signal reinstallation module;

[0191] Taking the Android system as an example, the reinstall signal can be, for example, the 44 signal in the Linux system, and of course, can also be other signals in the Linux system, which are not specifically limited here. In short, the embodiment of the present application utilizes an original signal to notify the signal reinstallation module to reinstall the capture stack signal.

[0192] Step S303: the signal reassembly module receives the reassembly signal and executes a processing function of the reassembly stack capture signal.

[0193] The signal reload module can execute the processing function of the reload stack capture signal when receiving the reload signal sent by the memory detection module. In this way, the application can no longer shield the stack capture signal by calling the signal function or the sigaction function, thereby ensuring that the signal takeover module can receive the stack capture signal sent by the heap tracker module in the future, avoiding stack capture failure.

[0194] Step S304: the maintenance and testing service module notifies the memory detection module to resume capturing stack signals.

[0195] Taking the Android system as an example, the maintenance service module can notify the memory detection module through the ioctl function that the stack capture signal needs to be restored.

[0196] Step S305: the memory detection module sends a recovery signal to the signal reinstallation module.

[0197] Taking the Android system as an example, the recovery signal may be, for example, the 44 signal in the Linux system, or other signals in the Linux system, which are not specifically limited here. In short, the embodiment of the present application utilizes an original signal to notify the signal reloading module to recover the stack capture signal.

[0198] Step S306: The signal reassembly module receives the recovery signal and restores the stack capture signal to a default state.

[0199] The signal reinstallation module can restore the stack capture signal to a default state when receiving the recovery signal sent by the memory detection module. In this way, the electronic device can be restored to the system environment before the stack capture without causing changes to the original system environment of the electronic device.

[0200] According to the method provided in the embodiment of the present application, after the maintenance service module detects a memory leak in the target process, before calling the stack capture tool to capture the stack of the target process, the signal reloading module can be called to reload the stack capture signal to prevent the stack capture signal of the stack capture tool from being intercepted by the target application, thereby avoiding stack capture failure.

[0201] Figure 7 This is another flowchart of a method for capturing memory data provided in an embodiment of the present application.

[0202] like Figure 7 As shown, in one embodiment, the method for capturing memory data may include S401-S401:

[0203] Step S401: When a memory leak occurs in the target process, the electronic device captures the heap memory data of the target process.

[0204] The heap memory data at least includes all garbage collection root GCRoot objects of the target process in the running memory and the reference relationship between the objects.

[0205] In one implementation, when a memory leak occurs in a target process, the electronic device may create a target child process and capture the heap memory data in the child process, wherein the target child process is a copy of the target process.

[0206] The specific implementation of step S401 may refer to the related descriptions of steps S201 to S211 in the aforementioned embodiment, which will not be repeated here.

[0207] Step S403: The electronic device stores the captured heap memory data to a target location in the flash memory.

[0208] The specific implementation of step S403 may refer to the related descriptions of steps S212 to S215 in the aforementioned embodiment, which will not be repeated here.

[0209] In one implementation, after detecting a memory leak in the target process, the electronic device can also reinstall the stack capture signal before capturing the heap memory data of the target process. The specific implementation of reinstalling the stack capture signal can refer to the relevant description of steps S301-S303 in the above embodiment, which will not be repeated here.

[0210] In one implementation, after the electronic device saves the captured heap memory data to the target location in the flash memory, it can also restore the capture stack signal. The specific implementation of restoring the capture stack signal can refer to the relevant description of steps S304-S306 in the above embodiment, which will not be repeated here.

[0211] The embodiments provided in the present application above introduce various schemes of the method for capturing memory data provided in the present application. It is understandable that, in order to realize the above functions, the electronic device includes hardware structures and / or software modules corresponding to the execution of each function. Those skilled in the art should easily realize that, in combination with the units and algorithm steps of each example described in the embodiments disclosed herein, the present application can be implemented in the form of hardware or a combination of hardware and computer software. Whether a function is executed in the form of hardware or computer software driving hardware depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present application.

[0212] The embodiment of the present application can divide the functional modules of the above-mentioned electronic device according to the above-mentioned method example. For example, each functional module can be divided corresponding to each function, or two or more functions can be integrated into one processing module. The above-mentioned integrated module can be implemented in the form of hardware or in the form of software functional modules. It should be noted that the division of modules in the embodiment of the present invention is schematic and is only a logical function division. There may be other division methods in actual implementation.

[0213] Figure 8 is a schematic diagram of a device for capturing memory data provided by an embodiment of the present application. In one embodiment, the electronic device can Figure 8 The hardware devices shown in the figure implement the corresponding functions. Figure 8 As shown, the device for capturing memory data may include: a display screen 801 , a memory 802 , a processor 803 and a communication module 804 . The above components may be connected via one or more communication buses 805 .

[0214] In one embodiment, the display screen 801 may include a display panel 8011 and a touch sensor 8012, wherein the display panel 8011 is used to display an image, and the touch sensor 8012 may pass the detected touch operation to the application processor to determine the type of touch event, and provide a visual output related to the touch operation through the display panel 8011. The processor 803 may include one or more processing units, for example: the processor 803 may include an application processor, a modem processor, a graphics processor, an image signal processor, a controller, a video codec, a digital signal processor, a baseband processor, and / or a neural network processor, etc. Among them, different processing units may be independent devices or integrated in one or more processors. The memory 802 is coupled to the processor 803 and is used to store various software programs and / or multiple groups of instructions. The memory 802 may include a volatile memory and / or a non-volatile memory. When the software programs and / or multiple groups of instructions in the memory 802 are executed by the processor 803, the electronic device implements the method steps in the above-mentioned embodiments and their implementation methods:

[0215] The embodiment of the present application also provides a computer-readable storage medium, in which instructions are stored. When the computer-readable storage medium is run on a computer, the computer executes the above-mentioned methods.

[0216] The embodiment of the present application also provides a computer program product including instructions, which, when executed on a computer, enables the computer to execute the above-mentioned methods.

[0217] The embodiment of the present application also provides a chip system, which includes a processor for supporting the above-mentioned device to implement the functions involved in the above-mentioned aspects, for example, generating or processing the information involved in the above-mentioned method. In one possible design, the chip system also includes a memory for storing computer instructions and data necessary for the multi-device collaborative device. The chip system can be composed of a chip, or it can include a chip and other discrete devices.

[0218] Through the description of the above implementation methods, technical personnel in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional modules is used as an example. In actual applications, the above-mentioned 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.

[0219] In the several embodiments provided in the present application, it should be understood that the disclosed devices / equipment and methods can be implemented in other ways. For example, the device / equipment embodiments described above are only schematic. For example, the modules or module divisions are only a logical function division. There may be other division methods in actual implementation, such as multiple modules or components can be combined or integrated into another device, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or modules, which can be electrical, mechanical or other forms.

[0220] The modules described as separate components may or may not be physically separated, and the components shown as modules may be one physical module or multiple physical modules, that is, they may be located in one place or distributed in multiple different places. Some or all of the modules may be selected according to actual needs to achieve the purpose of the present embodiment.

[0221] In addition, each functional module in each embodiment of the present application can be integrated into a processing module, or each module can exist physically separately, or two or more modules can be integrated into one module. The above integrated modules can be implemented in the form of hardware or software functional modules.

[0222] If the integrated module is implemented in the form of a software function module and sold or used as an independent product, it can be stored in a readable storage medium. Based on this understanding, the technical solution of the embodiment of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium, including several instructions to enable a device (which can be a single-chip microcomputer, chip, etc.) or a processor (processor) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read only memory (ROM), random access memory (RAM), disk or optical disk and other media that can store program code.

[0223] The above contents are only specific implementation methods of the present application, but the protection scope of the present application is not limited thereto. Any changes or substitutions within the technical scope disclosed in the present application shall be included in the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the protection scope of the claims.

Claims

1. A method for capturing memory data, characterized in that: include: When a memory leak occurs in a target process, the electronic device captures heap memory data of the target process, wherein the heap memory data at least includes all garbage collection root GCRoot objects of the target process in the running memory and reference relationships between the objects; The electronic device stores the captured heap memory data in a target location of the flash memory.

2. The method according to claim 1, characterized in that When a memory leak occurs in a target process, the electronic device captures heap memory data of the target process, including: When a memory leak occurs in the target process, the electronic device creates a target sub-process, where the target sub-process is a copy of the target process; The electronic device captures the heap memory data in the sub-process.

3. The method according to claim 1, characterized in that The electronic device comprises a maintenance service module and a stack capture tool, the stack capture tool comprises a heap tracker module and a monitoring module, the monitoring module runs in a subthread of the target process; when a memory leak occurs in the target process, the electronic device captures the heap memory data of the target process, including: The maintenance and testing service module detects that a memory leak occurs in the target process; When a memory leak occurs in the target process, the maintenance and testing service module generates a stack capture configuration corresponding to the target process, wherein the stack capture configuration at least includes a process name of the target process; The maintenance and testing service module sends a stack capture notification to the stack tracker module, wherein the stack capture notification includes the stack capture configuration; The stack tracker module sends a stack capture signal to the monitoring module based on the stack capture configuration; When the monitoring module monitors the stack capture signal, the target process creates a target child process, and the target child process is a copy of the target process; The target child process captures the heap memory data.

4. The method according to claim 3, characterized in that After the maintenance and testing service module generates the stack capture configuration and before the maintenance and testing service module sends the stack capture notification to the stack tracker module, the method further includes: The maintenance and testing service module releases the constraints of the stack capture tool so that the maintenance and testing service module can communicate with the stack tracker module.

5. The method according to claim 3, characterized in that: The electronic device also includes a memory detection module and a signal reinstallation module; Before the maintenance and testing service module generates the stack capture configuration, the method further includes: The maintenance and testing service module notifies the memory detection module to reinstall the stack capture signal; The memory detection module sends a reinstallation signal to the signal reinstallation module; The signal reassembly module receives the reassembly signal and executes a processing function for reassembling the stack capture signal.

6. The method according to claim 5, characterized in that After the electronic device stores the captured heap memory data in a target location of the flash memory, the method further includes: The maintenance and testing service module notifies the memory detection module to restore the stack capture signal; The memory detection module sends a recovery signal to the signal reassembly module; The signal reassembly module receives the recovery signal and restores the stack capture signal to a default state.

7. The method according to claim 3, characterized in that The stack capture tool further includes a tracking module, and the stack capture configuration further includes a buffer-related configuration; when a memory leak occurs in the target process, the electronic device captures the heap memory data of the target process, and further includes: The monitoring module monitors the stack capture signal and sends a first data collection notification to the tracking module; The tracking module receives the first data collection notification, converts the captured heap memory data into heap dump data in a target format based on the buffer-related configuration, and fills the heap dump data into the tracking cache buffer of the shared memory.

8. The method according to claim 7, characterized in that The electronic device stores the captured heap memory data in a target location of the flash memory, including: The tracking module sends a second data collection notification to the stack tracker module; The stack tracker module forwards the second data collection notification to the maintenance and testing service module; The maintenance and testing service module receives the second data collection notification and stores the heap dump data in the tracking cache buffer to the target location.

9. The method according to claim 8, characterized in that After the maintenance and testing service module sends the stack capture notification to the heap tracker module, the method further includes: after the maintenance and testing service module waits for a preset time period, storing the heap dump data in the tracking cache buffer to the target location.

10. The method according to claim 3, characterized in that: The electronic device further includes a signal takeover module, and the signal takeover module runs in a subthread of the target process; The stack tracker module sends a stack capture signal to the monitoring module based on the stack capture configuration, including: The stack tracker module sends the stack capture signal to the signal takeover module based on the stack capture configuration; The signal taking over module receives the stack capture signal and forwards the stack capture signal to the monitoring module.

11. The method according to claim 4, characterized in that The stack capture configuration includes a unique session name; after the maintenance and testing service module releases the constraints of the stack capture tool and before the maintenance and testing service module sends a stack capture notification to the stack tracker module, the method further includes: The maintenance service module establishes session communication with the heap tracker module based on the unique session name.

12. The method according to claim 11, characterized in that After the electronic device saves the captured heap memory data to a target location in the flash memory, the method further includes: The maintenance and testing service module cleans up the session data of the session communication and restores the constraints of the stack capture tool.

13. The method according to any one of claims 3 to 12, characterized in that: The heap tracker module includes a communication whitelist, and the process name of the maintenance service module is located in the communication whitelist, so that the communication between the maintenance service module and the heap tracker module is not restricted by the stack capture tool.

14. The method according to any one of claims 3 to 12, characterized in that: The maintenance and testing service module is located in the local framework layer of the operating system, and the stack capture tool is the perfetto tool.

15. The method according to claim 5, characterized in that The memory detection module is located at the kernel layer of the operating system, and the signal reinstallation module is located at the application layer of the operating system.

16. An electronic device, characterized in that: include: A memory and one or more processors; the memory is coupled to the processor; wherein the memory stores computer program code, the computer program code includes computer instructions, and when the computer instructions are executed by the processor, the electronic device executes the method for capturing memory data as described in any one of claims 1-15.

Citation Information

Patent Citations

  • System and method of computer automatic memory management

    CN101046755A

  • Memory leak monitoring method, device and electronic equipment

    CN112035314A

  • Visual analysis and management method and system for JavaScript memory, equipment and storage medium

    CN113688189A

  • Program memory monitoring method and device, computer equipment and storage medium

    CN114153690A

  • Memory application method and electronic equipment

    CN116661987A