A method of grabbing memory data and an electronic device
By creating a child process within the target process to capture heap memory data, and utilizing a combination of the maintenance service module and stack capture tool, the problem of low memory leak location rate and performance degradation in existing technologies is solved, achieving more efficient memory leak location.
Patent Information
- Application Number
- CN202410009111.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-01-03
- Publication Date
- 2025-11-28
- Estimated Expiration
- 2044-01-03
AI Technical Summary
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.
When a memory leak occurs in the target process, a target child process is created. Heap memory data is captured in the child process. By using a combination of the monitoring service module and the stack capture tool, including the heap tracer module, the listening module, and the tracing module, the heap memory data of the target process can be fully captured and stored by generating stack capture configuration and signal processing.
This improves the accuracy of locating memory leaks while reducing the performance impact on the target process, avoiding the performance degradation issues of traditional methods.
Smart Images

Figure CN119271532B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] Embodiments of the present application relate to the technical field of terminal, and in particular, to a method for grabbing memory data and an electronic device. BACKGROUND
[0002] Memory leakage refers to the case that the memory space dynamically applied by an application during running is not properly released or recycled when it is no longer needed. Memory leakage will cause the memory resource to be unable to be effectively utilized by the application, and with the passage of time, the un-released memory is continuously accumulated, which may eventually exhaust the available memory of the system, causing the performance of the application to decline or crash.
[0003] During the running of the application, the process of the application will dynamically apply 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 the memory resource, the memory space is not released in time after being applied, which will cause memory leakage.
[0004] When the process has memory leakage, some tools can be used to grab the memory data of the process, such as the reference relationship of objects, the memory occupation, etc., to help locate the cause of memory leakage. This method is called "stack grabbing". However, the current stack grabbing method cannot comprehensively grab the memory data of the process, which causes the problem of low positioning rate when the memory leakage is located by using such memory data. SUMMARY
[0005] Embodiments of the present application provide a method for grabbing memory data and an electronic device, which can comprehensively grab the memory data of the process when the process has memory leakage, so as to improve the positioning rate of the memory leakage problem.
[0006] In a first aspect, embodiments of the present application provide a method for grabbing memory data, which comprises: an electronic device grabbing the heap memory data of a target process when the target process has memory leakage, the heap memory data at least including all garbage collection root GCRoot objects of the target process in the running memory and the reference relationship between objects; and the electronic device storing the grabbed heap memory data to a target position of the flash memory.
[0007] Compared with the traditional stack grabbing method which only collects the newly generated heap memory data after the start of stack grabbing, the method for grabbing memory data provided by embodiments of the present application can grab all GCRoot objects of the target process at the current time, thereby improving the positioning rate of the memory leakage problem.
[0008] In an implementation, the electronic device, in a case where a memory leak occurs in a target process, captures heap memory data of the target process, including: the electronic device, in a case where a memory leak occurs in a target process, creating a target child process, the target child process being a copy of the target process; and the electronic device capturing the heap memory data in the child process. In this way, the electronic device creates a target child process and performs the stack capture operation in the target child process instead of in the target process, and thus the method for capturing memory data provided by the embodiments of the present application has less impact on the performance of the target process.
[0009] In an implementation, the electronic device includes a telemetry service module and a stack capture tool, the stack capture tool including a heap tracker module and a listener module, the listener module running in a child thread of the target process; and the electronic device, in a case where a memory leak occurs in a target process, capturing heap memory data of the target process, including: the telemetry service module detecting that a memory leak occurs in the target process, the telemetry service module generating a stack capture configuration in a case where a memory leak occurs in the target process, the stack capture configuration including at least a process name of the target process; the telemetry service module sending a stack capture notification to the heap tracker module, the stack capture notification including the stack capture configuration; the heap tracker module sending a stack capture signal to the listener module based on the stack capture configuration; and the target process creating a target child process in a case where the listener module listens to the stack capture signal; the target child process being a copy of the target process; and the target child process capturing the heap memory data of the target process. In this way, after the listener module listens to the stack capture signal, the target process creates a target child process and performs the stack capture operation in the target child process instead of in the target process. As can be seen, the method for capturing memory data provided by the embodiments of the present application only consumes some time when the target process creates the target child process, and does not consume time in capturing the heap memory data, and thus has less impact on the performance of the target process.
[0010] In an implementation, after the telemetry service module generates the stack capture configuration and before the telemetry service module sends the stack capture notification to the heap tracker module, the method further includes: the telemetry service module releasing a constraint of the stack capture tool, so that the telemetry service module can communicate with the heap tracker module. When the stack capture tool is in a constrained state, the telemetry service module cannot communicate with the heap tracker module or the listener module, and thus, in order to enable the telemetry service module to communicate with the heap tracker module, the telemetry service module releases the constraint of the stack capture tool before sending the stack capture notification to the heap tracker module, thereby avoiding stack capture failure.
[0011] In an implementation manner, the electronic device further comprises a memory detection module and a signal reloading module; before the stack configuration is generated by the maintenance service module, the method further comprises: the maintenance service module informs the memory detection module to reload the stack signal; the memory detection module sends the reloading signal to the signal reloading module; the signal reloading module receives the reloading signal and executes a processing function of reloading the stack signal. In this way, the application program cannot shield and intercept the stack signal, so that the signal takeover module can receive the stack signal sent by the heap tracker module in the subsequent process, and the stack failure is avoided.
[0012] In an implementation manner, after the electronic device stores the captured heap memory data to the target position of the flash memory, the method further comprises: the maintenance service module informs the memory detection module to restore the stack signal; the memory detection module sends the restoring signal to the signal reloading module; the signal reloading module receives the restoring signal and restores the stack signal to the default state. In this way, the electronic device can be restored to the system environment before the stack, and the original system environment of the electronic device is not changed.
[0013] In an implementation manner, the stack tool further comprises a tracking module, and the stack configuration further comprises a buffer related configuration; when the electronic device detects that the target process has a memory leak, the heap memory data of the target process is captured, and the method further comprises: the listening module listens to the stack 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 buffer 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 protobuf format file block, so that the heap dump data is shared between the target process, the tracking module, the heap tracker module and the like.
[0014] In an implementation manner, the electronic device stores the captured heap memory data to the target position of the flash memory, comprising: 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 service module; the maintenance service module receives the second data collection notification and stores the heap dump data in the tracking buffer buffer to the target position. 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 an implementation manner, after the maintenance service module sends the stack notification to the heap tracker module, the method further comprises: the maintenance service module waits for a preset time length, and then stores the heap dump data in the tracking buffer buffer to the target position.
[0016] In an implementation manner, the electronic device further comprises a signal interception module running in a sub-thread of the target process; the heap tracker module sends the stack trace signal to the monitoring module based on the stack trace configuration, comprising: the heap tracker module sends the stack trace signal to the signal interception module based on the stack trace configuration; the signal interception module receives the stack trace signal and forwards the stack trace signal to the monitoring module. In this way, the signal interception module can call the signal processing function to capture and intercept the stack trace signal, and forward the stack trace signal to the monitoring module
[0017] In an implementation manner, the stack trace configuration comprises a unique session name; after the stack trace tool is released from the constraint of the telemetry service module, before the telemetry service module sends the stack trace notification to the heap tracker module, the method further comprises: the telemetry service module establishes a session communication with the heap tracker module based on the unique session name. In this way, the telemetry service module can establish a session communication with the heap tracker module by using an interface program (such as a tracing SDK), and subsequent communication behaviors can be performed in the session.
[0018] In an implementation manner, after the electronic device saves the captured heap memory data to a target position in the flash memory, the method further comprises: the telemetry service module cleans up session data of the session communication and restores the constraint of the stack trace tool. In this way, the stack trace tool can be restored to the constraint state after the stack trace ends, so that the stack trace tool intercepts the communication between the telemetry service module and the heap tracker module.
[0019] In an implementation manner, the heap tracker module comprises a communication white list, and the process name of the telemetry service module is located in the communication white list, so that the communication between the telemetry service module and the heap tracker module is not constrained by the stack trace tool.
[0020] In an implementation manner, the telemetry service module is located in a local framework layer of an operating system, and the stack trace tool is a perfetto tool.
[0021] In an implementation manner, the memory detection module is located in a kernel layer of an operating system, and the signal reinstallation module is located in an 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 comprises a telemetry service module and a stack trace tool. The telemetry service module is configured to generate a stack trace configuration corresponding to a target process when detecting that the target process has a memory leak; the stack trace tool is configured to capture heap memory data of the target process based on the stack trace configuration, the heap memory data at least comprising all garbage collection root GCRoot objects of the target process in the running memory; and the telemetry service module is further configured to store the captured heap memory data to a target position in a 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 codes, the computer program codes comprise computer instructions, when the computer instructions are executed by the processor, the electronic device executes the method for grabbing memory data provided by the first aspect and any possible design of the first aspect.
[0024] In a fourth aspect, the present application provides a computer readable storage medium, which comprises computer instructions, when the computer instructions are run on an electronic device, the electronic device executes the method for grabbing memory data provided by the first aspect and any possible design of the first aspect.
[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 grabbing memory data provided by the first aspect and any possible design of the first aspect.
[0026] It can be understood that the beneficial effects achieved by the technical solutions provided by the second aspect to the fifth aspect can refer to the beneficial effects in the first aspect and any possible design of the first aspect, which will not be repeated here. BRIEF DESCRIPTION OF DRAWINGS
[0027] Figure 1 is a schematic diagram of a scenario causing memory leakage;
[0028] Figure 2 shows a structural schematic diagram of an electronic device provided by the embodiments of the present application;
[0029] Figure 3 is a layered architecture schematic diagram of a software system of the electronic device provided by the embodiments of the present application;
[0030] Figure 4 is another layered architecture schematic diagram of a software system of the electronic device provided by the embodiments of the present application;
[0031] Figure 5 is a flow schematic diagram of a method for grabbing memory data provided by the embodiments of the present application;
[0032] Figure 6 is another flow schematic diagram of a method for grabbing memory data provided by the embodiments of the present application;
[0033] Figure 7 is still another flow schematic diagram of a method for grabbing memory data provided by the embodiments of the present application;
[0034] Figure 8is a structural schematic diagram of a device for grabbing memory data provided by an embodiment of the present application. DETAILED DESCRIPTION
[0035] Memory leakage refers to the case that the memory space dynamically applied by an application during running is not properly released or recycled when it is no longer needed. Memory leakage will cause the memory resource to be unable to be effectively utilized by the application, and with the passage of time, the un-released memory is continuously accumulated, and eventually the available memory of the system can be exhausted, causing the performance of the application to decline or crash.
[0036] It should be noted that the memory in the embodiments of the present application refers to the running memory of an electronic device, for example, a random access memory (RAM), also known as an internal memory.
[0037] During the running of an application, the process of the application will dynamically apply memory space according to actual needs. For example, in a JAVA environment, the running memory can be divided into stack memory and heap memory. When a process creates an object, the JAVA virtual machine will allocate a block of space in the heap memory to store the object, and allocate a block of space in the stack memory to store the reference of the object, so as to access the methods and attributes of the object.
[0038] Ideally, when the applied memory is no longer needed, the application should use the corresponding function to release the memory, and in addition, the operating system can also detect and release the memory that is no longer used through memory management mechanisms such as garbage collection, to recycle the memory space. For example, in a JAVA environment, when the method of an object is executed, its reference in the stack memory will be destroyed to release the memory space, and the garbage collector will periodically scan the heap memory to find out the objects that are no longer referenced and recycle them to release the memory space.
[0039] However, in actual situations, some applications fail to properly manage memory resources, and after the process applies memory space, the memory space is not released in time, resulting in memory leakage.
[0040] In addition, when there is mutual reference between multiple objects in the memory, even if these objects are no longer used, the garbage collector will not identify these objects as garbage, and therefore will not recycle the memory space occupied by these objects, resulting in memory leakage. For example, Figure 1 is a schematic diagram of a scenario that causes memory leakage. As shown in Figure 1As shown, the memory contains objects X, Y, A, B, and C, wherein the objects Y, A, B, and C are referenced by the object X. Then, if the life cycle of the object X is longer than that of the object Y, the garbage collector will not recycle the memory occupied by the object Y at the end of the life cycle of the object Y because the object X still references the object Y. Similarly, if the life cycles of the objects A, B, and C are also shorter than that of the object Y, the garbage collector will not recycle the memory occupied by the objects A, B, and C at the end of the life cycles of the objects A, B, and C. Therefore, as the situation accumulates, a large number of useless objects can not be recycled, thereby occupying the memory resources and causing memory leakage.
[0041] In addition, other reasons, such as not cleaning the expired or useless cache in the memory in time, creating a large number of temporary objects by the process but not destroying them in time, and the like, can also cause memory leakage.
[0042] There is no specific standard for determining whether a process has memory leakage. Generally, the memory size occupied by a process can be used to determine whether the process has memory leakage. For example, if the memory size occupied by a process is significantly increased compared to the general case, it can be considered that the process has memory leakage. For example, if a process generally occupies tens of megabytes (MB) of memory space, when the memory occupied by the process reaches hundreds of MB, it can be considered that the process has memory leakage. Further, when the memory occupied by the process continues to increase and reaches the system limit or exhausts the system memory, the process is likely to crash. For example, it is found in practical applications that when the memory occupied by the process reaches 500 MB, the process is likely to crash. Therefore, for example, when the memory occupied by the process approaches 500 MB, such as 400 MB, it can be considered that the process has memory leakage. The skilled person in the art can determine the determination standard of memory leakage based on the actual situation, which is not limited here.
[0043] When a process has memory leakage, the heap memory data at the time of memory leakage, such as the reference relationship of the objects, the memory occupation, and the like, can be captured to help locate the cause of memory leakage. This method is called "stack grabbing". However, the current stack grabbing method has the problem of low positioning rate of memory leakage, and also reduces the performance of the electronic device.
[0044] For example, in a JAVA environment, when a process of an application program has a memory leak, the JAVA virtual machine can open a stack grabbing switch. Thereafter, a stack grabbing related thread of the application program listens to the heap memory, collects and records newly generated heap memory data, such as the reference relationship and memory occupation of a newly created object after the stack grabbing switch is opened. That is, the stack grabbing method cannot collect and record the heap memory data that has existed before the stack grabbing switch is opened. However, when the stack grabbing switch is opened, the heap memory occupied by the process usually exceeds several hundred MB, and the object that actually occupies a large amount of memory already exists in the heap memory, which causes the stack grabbing method to find the object that actually occupies a large amount of memory, and further causes the positioning rate of the memory leak problem to be low.
[0045] In addition, for example in a JAVA environment, when a thread needs to perform an operation such as memory allocation and release, 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 operation behavior of the thread on the heap memory) needs to be acquired. If other threads also want to perform an operation on the memory, the alloc_tracker_lock lock needs to be released, so as to avoid multiple threads from simultaneously performing an operation on the same memory block. Therefore, in the traditional stack grabbing scheme, the stack grabbing related thread of the application program needs to acquire the alloc_tracker_lock lock when applying for memory, the main thread of the application program needs to acquire the alloc_tracker_lock lock when frequently applying for memory during the frame drawing process, and other sub-threads of the application program also need to acquire the alloc_tracker_lock lock when applying for memory. Therefore, the application program causes lock competition among the threads, and reduces the performance of the electronic device.
[0046] To solve the above problems, the embodiment of the present application provides a method for grabbing memory data, which can be applied to an electronic device, so as to improve the positioning rate of a memory leak problem and not reduce the performance of the electronic device during stack grabbing.
[0047] The technical solutions provided by the embodiments of the present application are described in detail below with reference to the drawings.
[0048] The technical solutions provided in the present application can be applied in an electronic device. 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, and an electronic device such as 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, an in-vehicle device, a smart home device, and / or a smart city device. The present embodiments do not specially limit the specific type of the electronic device.
[0049] Figure 2 A structural schematic diagram of an electronic device provided in the present application is shown.
[0050] Referring to Figure 2 As shown in the figure, the electronic device can 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 loudspeaker 170A, a receiver 170B, a microphone 170C, a headset interface 170D, a sensor module 180, a key 190, a motor 191, an indicator 192, a display screen 193, a subscriber identification module (SIM) card interface 194, and a camera 195. The sensor module 180 can include a pressure sensor, a gyroscope sensor, a barometric 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, and the like.
[0051] The processor 110 can include one or more processing units, for example: the processor 110 can include an application processor (AP), a modem processor, a graphics processing unit (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 can be independent devices or integrated in 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 instruction operation codes and timing signals, and complete the control of fetching and executing instructions.
[0053] The memory in the processor 110 can also be provided to store instructions and data. In some embodiments, the memory in the processor 110 is a cache memory. The memory can save instructions or data that the processor 110 has just used or repeatedly uses. If the processor 110 needs to use the instructions or data again, it can be directly called from the memory. This avoids repeated access and reduces the waiting time of the processor 110, thereby improving the efficiency of the system.
[0054] In some embodiments, the processor 110 can include one or more interfaces. The interface can 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 configured to receive charging input from a power supply device (e.g., a charger, a notebook power supply, etc.). The charger can be a wireless charger or a wired charger. In some embodiments of wired charging, the charging management module 140 can receive charging input from a wired charger through the USB interface 130. In some embodiments of wireless charging, the charging management module 140 can receive wireless charging input through a wireless charging coil of the electronic device.
[0056] The charging management module 140 can supply power to the electronic device through the power management module 141 while charging the battery 142. The battery 142 can include a plurality of batteries connected in series.
[0057] The power management module 141 is configured 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 to supply power to the processor 110, the internal memory 121, the display 193, the camera 195, and the wireless communication module 160. The power management module 141 can also be configured to monitor parameters such as voltage, current, battery cycle count, battery health status (leakage, impedance), etc. of the battery. In some other embodiments, the power management module 141 can also be disposed in the processor 110.
[0058] The external memory interface 120 can be configured to connect 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 store data. For example, music, video, and other files can be stored in the external non-volatile memory.
[0059] The internal memory 121 can include one or more random access memories (RAMs) and one or more non-volatile memories (NVMs). The random access memory can be directly readable and writable by the processor 110, and can be used to store executable programs (e.g., machine instructions) of an operating system or other programs running in the background, and can also be used to store data of users and application programs, etc. The non-volatile memory can also store executable programs and data of users and application programs, etc., and can be loaded into the random access memory in advance for direct reading and writing by the processor 110.
[0060] Touch sensor, also called "touch device". The touch sensor can be disposed on the display screen 193, and the touch sensor and the display screen 193 form a touch screen, also called "touch screen". The touch sensor is used to monitor the touch operation acting on or near the touch sensor. The touch sensor can transmit the monitored touch operation to the application processor to determine the touch event type. The visual output related to the touch operation can be provided through the display screen 193. In other embodiments, the touch sensor can also be disposed on the surface of the electronic device, which is different from the position where the display screen 193 is located.
[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 disposed 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 there is a touch operation acting on the display screen 193, the electronic device monitors the touch operation intensity according to the pressure sensor. The electronic device can also calculate the position of the touch according to the monitoring signal of the pressure sensor.
[0062] In some embodiments, the electronic device can include one or N cameras 195, and N is a positive integer greater than 1. In the embodiments of the present application, the types of the camera 195 can be distinguished according to the hardware configuration and the physical position. For example, the camera disposed on the side of the display screen 193 of the electronic device can be called a front camera, and the camera disposed on the side of the back cover of the electronic device can be called a rear camera; for another example, the camera with a short focal length and a large field of view can be called a wide-angle camera, and the camera with a long focal length and a small field of view can be called a normal camera. Wherein, the length of the focal length and the size of the field of view are relative concepts, and there is no specific parameter limitation, so the wide-angle camera and the normal camera are also a relative concept, and can be distinguished according to the physical parameters such as focal length and field of view.
[0063] The electronic device realizes the display function through the GPU, the display screen 193, and the application processor, etc. The GPU is a microprocessor for image editing, connected to the display screen 193 and the application processor. The GPU is used to perform mathematical and geometric calculations for graphics rendering. The processor 110 can 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, etc. The GPU is used to perform mathematical and geometric calculations for graphics rendering. The processor 110 can include one or more GPUs that execute program instructions to generate or change display information.
[0065] ISP is used to process the data fed back by the camera 195. For example, when taking a photo, the shutter is opened, 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 conversion into a visible image. ISP can also algorithmically optimize the noise and brightness of the image. ISP can also optimize the exposure, color temperature and other parameters of the shooting scene. In some embodiments, the ISP can be provided in the camera 195. The camera 195 is used to capture still images or videos.
[0066] The digital signal processor is used to process digital signals, in addition to being able to process digital image signals, it can also process other digital signals. For example, when the electronic device selects a frequency point, the digital signal processor is used to perform Fourier transform on the frequency point energy, etc.
[0067] The display screen 193 is used to display images, videos, etc. The display screen 193 includes a display panel. The display panel can adopt a liquid crystal display (LCD), an organic light-emitting diode (OLED), an active-matrix organic light emitting diode (AMOLED), a flexible light-emitting diode (FLED), a Miniled, a Microled, a Micro-oled, a quantum dot light emitting diode (QLED), etc. In some embodiments, the electronic device can include one or N display screens 193, and N is a positive integer greater than 1.
[0068] In the embodiments 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 the images (such as wallpaper, photos, etc.) from the storage of the electronic device or the images taken by any one or more cameras 195 in the interface.
[0069] The wireless communication function of the electronic device can be realized through the antenna 1, the antenna 2, the mobile communication module 150, the wireless communication module 160, the modem and the baseband processor, etc.
[0070] The antenna 1 and the 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 multiplexed to improve the utilization rate of the antennas.
[0071] The mobile communication module 150 can provide a solution for wireless communication including 2G / 3G / 4G / 5G, etc. applied to the electronic device. The mobile communication module 150 can receive electromagnetic waves by the antenna 1, and perform filtering, amplification, etc. on the received electromagnetic waves, and transfer the processed signals to the modem processor for demodulation. The mobile communication module 150 can also amplify the signals modulated by the modem processor, and radiate the signals as electromagnetic waves through the antenna 1. In some embodiments, at least part of the functional modules of the mobile communication module 150 can be disposed in the processor 110. In some embodiments, at least part of the functional modules of the mobile communication module 150 can be disposed in the same device as at least part of the modules of the processor 110.
[0072] The modem processor can include a modulator and a demodulator. The modulator is configured to modulate a low-frequency baseband signal to be transmitted into a medium-high frequency signal. The demodulator is configured to demodulate a 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. The low-frequency baseband signal processed by the baseband processor is transmitted to the application processor. The application processor outputs a sound signal through an audio device (not limited to the speaker 170A, the microphone 170B, etc.), or displays an image or a video through the display screen 193. In some embodiments, the modem processor can be a separate device. In other embodiments, the modem processor can be independent of the processor 110, and disposed in the same device as the mobile communication module 150 or other functional modules.
[0073] The wireless communication module 160 can provide a solution for wireless communication including wireless local area networks (WLAN) (such as wireless fidelity (Wi-Fi) network), Bluetooth (BT), global navigation satellite system (GNSS), frequency modulation (FM), near field communication (NFC), infrared (IR) technology, etc. applied to the electronic device. The wireless communication module 160 can be one or more devices integrated with at least one communication processing module. The wireless communication module 160 receives electromagnetic waves via the antenna 2, performs frequency modulation and filtering processing on the electromagnetic wave signals, and transmits the processed signals to the processor 110. The wireless communication module 160 can also receive signals to be transmitted from the processor 110, perform frequency modulation and amplification, and radiate the signals as electromagnetic waves through the antenna 2.
[0074] The SIM card interface 194 is configured to connect a SIM card. The SIM card can be connected to or disconnected from the electronic device by being inserted into or pulled out of the SIM card interface 194. The electronic device can support one or more SIM card interfaces. The SIM card interface 194 can support a Nano SIM card, a Micro SIM card, a SIM card, or the like. 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 an external storage card. The electronic device interacts with a network through the SIM card to implement functions such as call and data communication. One SIM card corresponds to one user number.
[0075] It can be understood that the interface connection relationship between the modules shown in the embodiments of the present application is only illustrative and does not constitute a structural limitation on the electronic device. In other embodiments of the present application, the electronic device can also use different interface connection modes or a combination of multiple interface connection modes.
[0076] Of course, it can be understood that the above Figure 2 only exemplarily illustrates the case where 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 or a smart bracelet), or another device form, the structure of the electronic device can include fewer structures than those shown in the above Figure 1 , or can include more structures than those shown in the above Figure 1 , which is not limited herein. Of course, it can be understood that the above Figure 2 only exemplarily illustrates the case where 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 or a smart bracelet), or another device form, the structure of the electronic device can include fewer structures than those shown in the above Figure 2 , or can include more structures than those shown in the above Figure 2 , which is not limited herein.
[0077] It can be understood that, in general, the implementation of the functions of the electronic device needs the support of hardware and the cooperation of software. The software system of the electronic device can use a layered architecture, an event-driven architecture, a microkernel architecture, a microservice architecture, or a cloud architecture. The embodiments of the present application exemplarily illustrate the software structure of the electronic device by taking a layered architecture as an example.
[0078] Figure 3 A layered architecture diagram of the software system of the electronic device provided in the embodiments of the present application is shown. The layered architecture divides the software into several layers, and each layer has a clear role and division of labor. The layers communicate with each other through a software interface (such as an API).
[0079] In some examples, referring to Figure 3 The software of the electronic device is divided into five layers from top to bottom as shown in the figure: an application layer, an application framework layer (also referred to as an application framework layer), a native framework layer (also referred to as a native layer), an Android runtime, a hardware abstraction layer (HAL), and a kernel layer (also referred to as a kernel layer or a driver layer).
[0080] The application layer can include a series of applications (APPs) as shown in the figure. Figure 3 The application layer can include camera, gallery, calendar, map, WLAN, Bluetooth, music, video, short message, call, navigation, instant messaging, etc. applications.
[0081] The application framework layer provides application programming interfaces (APIs) and programming frameworks for the applications of the application layer. The application framework layer includes some pre-defined functions or services. For example, the application framework layer can include activity managers, window managers, content providers, audio services, view systems, phone managers, resource managers, notification managers, package managers, etc., and the present application embodiments do not make any limitation thereto.
[0082] The native framework layer can include a plurality of functional modules. For example: a surface manager, media libraries, OpenGL ES, SGL, etc. The surface manager is used to manage the display subsystem and provides 2D and 3D layer fusion for a plurality of applications. The media libraries support a plurality of commonly used audio, video format playback and recording, and static image files, etc. OpenGL ES is used to realize three-dimensional graphics drawing, image rendering, synthesis, and layer processing, etc. SGL is a 2D drawing engine.
[0083] The Android runtime includes core libraries and an ART virtual machine. The Android runtime is responsible for scheduling and management of the Android system. The core libraries include two parts: one part is the function functions required to be called by the java language, and the other part is the core library of the Android. 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 into binary files. The ART virtual machine is used to perform object lifecycle management, stack management, thread management, security and exception management, and garbage collection, etc.
[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 the specific platform, provides a virtual hardware platform for the operating system, and makes it hardware-independent, which can be ported on multiple platforms. The HAL layer provides a standard interface to display the device hardware functions to the higher-level Java API framework (i.e., the framework layer). The HAL layer includes 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 referred to as camera HAL or camera hardware abstraction module), sensors HAL sensor module (or sensor service).
[0085] The kernel layer is the layer between hardware and software. The kernel layer at least includes display drivers, camera drivers, audio drivers, sensor drivers, battery drivers, etc., which are not limited by the present application. Among them, the sensor driver can specifically include the driver of each sensor included in the electronic device, such as an ambient light sensor driver, etc.
[0086] Figure 4 Another schematic diagram of the layered architecture of the software system of the electronic device provided by the embodiments of the present application.
[0087] In some embodiments, as shown in Figure 3 and Figure 4 The software system of the electronic device further includes a measurement service module, a signal takeover module, and a stack grabbing tool. The measurement 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 grabbing tool may, for example, include a heap tracker module, a listening module, and a tracking module. Among them, the heap tracker module and the tracking module may, for example, be located in the local framework layer, and the listening module may, for example, be located in the application layer. For example, in the android system, the stack grabbing tool can use the perfetto tool, and accordingly, the heap tracker module can be the heapprofd module in the perfetto tool, the listening module can be the perfetto_hprof_listener module in the perfetto tool, and the tracking module can be the traced module in the perfetto tool. Of course, other stack grabbing tools can also be used, which are not specifically limited here.
[0089] In some embodiments, the software system of the electronic device further comprises a memory detection module and a signal reloader module. The memory detection module, which can also be referred to as a memcheck module, is located at the kernel layer, and the signal reloader module, which can also be referred to as an allocation_dumper module, is located at the application layer.
[0090] In the embodiments of the present application, the signal catcher module and the listener module can be sub-threads running in the main process of the application. When the application is started, the signal catcher module and the listener module can start the corresponding sub-threads, for example, the signal_catcher thread corresponding to the signal catcher module and the perfetto_hprof_listener thread corresponding to the listener module. The signal reloader module runs in the main process of the application.
[0091] It should be noted that the electronic device can have multiple applications installed, and each application can start the sub-threads corresponding to the signal catcher module and the listener module in its process when it is started. Hereinafter, for the sake of convenience, the process in which a memory leak occurs will be referred to as a target process, and the application corresponding to the target process will be referred to as a target application.
[0092] The hiview module is a health monitoring service module, which is used to monitor, collect and analyze the running data of various components and modules on the electronic device. In the embodiments of the present application, the health monitoring service module can at least include the following functions: generating a stack configuration of a stack tool, providing an interface program for accessing the stack tool, and analyzing dump data.
[0093] The stack configuration refers to the configuration used by the stack tool when performing the stack function. The stack configuration can include, for example, a unique session name (Unique Session Name), a stack duration, a buffer related configuration, a data source related configuration, etc. The buffer related configuration can include, for example, the size of the buffer used to cache the stack result in the shared memory, the filling strategy of the buffer, etc. The data source related configuration can include, for example, the data source name, the dynamic buffer mapping, the heap dump related configuration, etc. The heap dump related configuration can include, for example, the process name of the process in which the memory leak occurs (i.e., the target process), etc. The stack configuration can be written in the format provided by the stack tool or in other formats that can be recognized by the stack tool.
[0094] The interface program is used to access the stack tool, so that the health monitoring service module can communicate with the stack tool and call the stack tool to perform the stack. For example, when the stack tool is the perfetto tool, the interface program can be the tracing SDK provided by the perfetto tool.
[0095] The dump analysis function includes analyzing the stack trace result and locating the memory leak cause. In some examples, the maintenance service module can also upload the dump data to the network side to facilitate online analysis of the stack trace result and online locating of the memory leak cause.
[0096] The heap tracker module, such as the heapprofd module, can run in a separate process, such as the heapprofd process. In the embodiments of the present application, the heap tracker module is used to implement session control function and stack trace management function, such as generating a stack trace signal. For example, when a memory leak occurs in the target process, the maintenance service module can send a stack trace notification to the heap tracker module. Upon receiving the stack trace notification, the heap tracker module can generate a stack trace signal, such as a java heap graph stack trace signal.
[0097] The session control function is used to implement session communication between the maintenance service module and the heap tracker module, such as implementing communication between the hiview process and the heapprofd process. For example, when the heap tracker module receives a stack trace notification from the maintenance service module based on the session communication, the heap tracker module can issue a stack trace signal. After the stack trace ends, the heap tracker module can also notify the maintenance service module to obtain the stack trace result from the shared memory.
[0098] The memory detection module, such as the memcheck module. Generally, when a memory leak occurs, the memory detection module can send a stack trace signal to the stack trace tool, such as the 44 signal in the Linux system. There are 31 standard signals in the Linux system, numbered 1-31. Each signal is named with the prefix "SIG" followed by a suffix (such as INT, HUP, KILL, etc.). Since Linux 2.2, Linux supports 33 different real-time signals, numbered 32-64, but the application program uses the SIGRTMIN+n notation 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 defined in the application program for stack trace management when a memory leak occurs.
[0099] In the embodiments 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 reload the stack trace signal through the ioctl function. The memory detection module can send the 44 signal to the signal reloading module to notify the signal reloading module to execute the processing function of reloading the stack trace signal. The ioctl is a function in the driver of the electronic device for managing the I / O channel of the device.
[0100] The signal rearming module, for example, the allocation_dumper module, is configured to rearm the stack trace signal. For example, the signal rearming module can execute a rearming function of the stack trace signal when receiving the 44 signal sent by the memory detection module. It should be noted that the stack trace signal needs to be re-armed because some applications may intercept the stack trace signal, resulting in a failed stack trace. Therefore, the embodiment of the present application re-arms the stack trace signal before the stack trace, so that the stack trace signal can be avoided to be intercepted.
[0101] In some embodiments, the signal rearming module can also not be set, that is, the rearming function of the stack trace signal is not executed. In this way, as long as the application does not intercept the stack trace signal, the stack trace can be successfully performed. In this case, the memory detection module no longer needs to send the 44 signal to the signal rearming module through the ioctl function.
[0102] The signal catching module, for example, the signal_catcher module, is configured to catch the stack trace signal, for example, the java heap graph stack trace signal, from the heap tracer module and send the stack trace signal to the listener module.
[0103] The listener module, for example, the perfetto_hprof_listener module, is configured to listen to the stack trace signal. When the listener module receives the stack trace signal sent by the signal catching module, the target process can create a target child process, in which the heap memory data of the target process is captured, for example, the garbage collection root GCRoot object and the reference relationship between the objects of the target process are traversed and captured.
[0104] The tracing module, for example, the traced module, can run in a separate process, for example, in the traced process. In the embodiment of the present application, after the heap memory data is captured, the listener 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 the 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 service module to read the data through the session. After receiving the notification from the heap tracer module, the 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 an electronic device with the hardware architecture or the software architecture described above.
[0106] Figure 5 FIG. 1 is a flowchart of a method for grabbing memory data provided by an embodiment of the present application.
[0107] Based on the software architecture shown in FIG. 1, the method for grabbing memory data provided by an embodiment of the present application is described below. Figure 4 Figure 5 The method for grabbing memory data provided by an embodiment of the present application is described below with reference to FIG. 2. As shown in FIG. 2, the method for grabbing memory data can include steps S201-S215. Figure 5
[0108] In step S201, the health monitoring service module detects that a target process has a memory leak.
[0109] In an embodiment of the present application, the health monitoring service module can be a module for monitoring, collecting and analyzing running data of various components and modules, for example, an hiview module. The health monitoring service module can be provided by a developer of an operating system or a manufacturer of an electronic device, for example, built into an operating system of the electronic device, or the health monitoring service module can also be installed separately, which is not collectively limited here.
[0110] In an embodiment of the present application, the health monitoring service module can detect whether a process has a memory leak through at least one way.
[0111] In an implementation manner, the health monitoring service module can monitor the trend of memory usage of a process, and if the memory usage of the process continuously increases without decreasing, the process can have a memory leak. Or, if the memory size occupied by a process significantly increases compared to a general case, it can be considered that the process has a memory leak. For example, a threshold value can be set for the memory used by the process, and if the memory used by the process is greater than the threshold value, it is considered that the process has a memory leak. For example, if a process generally occupies tens of megabytes (MB) of memory space, the threshold value can be set to hundreds of MB, for example, 400 MB, and when the memory occupied by the process exceeds 400 MB, it can be considered that the process has a memory leak.
[0112] In an implementation manner, the health monitoring service module can also determine whether a process has a memory leak according to whether the process checks whether the process crashes or triggers an exception due to memory exhaustion. Generally, if a process occupies too much memory, for example, occupies more than 500 MB of memory, it can crash. Therefore, if a process frequently crashes, it can be considered that the process has a memory leak.
[0113] In an implementation, the maintenance and measurement service module can also check the number of memory allocations and releases of the process. If there are a large number of memory allocations and a small number of memory releases, there can be a memory leak.
[0114] Of course, the maintenance and measurement service module or other modules in the electronic device can also use other ways to monitor whether the process has a memory leak. For example, a performance monitoring tool provided by the system can be used to monitor the memory resource usage of the process to determine whether the process has a memory leak. The embodiments of the present application do not make specific limitations on the method of determining whether the process has a memory leak.
[0115] In step S202, the maintenance and measurement service module generates a stack capture configuration corresponding to the target process.
[0116] When the maintenance and measurement service module detects that a process has a leak, the process name of the target process that has a memory leak can be obtained, and a stack capture configuration for capturing the stack of the target process can be generated.
[0117] In the embodiments of the present application, the stack capture configuration can be provided in a format that can be recognized and parsed by the stack capture tool. Therefore, for different stack capture tools, the format in the stack capture configuration and the information contained therein can be different.
[0118] For example, taking the perfetto tool as an example, the stack capture configuration can include the following contents:
[0119] (1) Trace related configuration, for example, can include:
[0120] Unique session name unique_session_name, that is, the session name used by the maintenance and measurement service module (for example, the hiview module) and the heap tracker module (for example, the heapprofd module) for session communication. An example of the field is as follows:
[0121] Unique_session_name: hiview_get_perfetto_hprof
[0122] Stack duration duration_ms, that is, the running duration of the stack capture tool, for example, the running duration of the perfetto tool. An example of the field is as follows:
[0123] Duration_ms: 100s
[0124] (2) Buffer related configuration, for example, can include:
[0125] Buffer size size_kb, that is, the buffer size allocated in the shared memory for heap dump data. An example of the field is as follows:
[0126] size_kb: 512M
[0127] The fill policy (fill_policy) is the strategy for filling the buffer. Its fields are exemplified below:
[0128] fill_policy: perfetto::protos::gen::TraceConfig_BufferConfig_FillPolicy_RING_BUFFER
[0129] For example, there are three filling strategies: UNSPECIFIED, RING_BUFFER, and DISCARD. Among them, UNSPECIFIED means that no filling strategy is specified, RING_BUFFER means that the buffer behaves like a circular buffer. When the buffer is full, newly written data will overwrite and replace the oldest data in the buffer. RING_BUFFER is the default filling strategy, and DISCARD means that data will stop being accepted when the buffer is full, and further write attempts will be discarded.
[0130] (3) Data source mutable related configurations, for example, may include:
[0131] Data source name. Its fields are exemplified below:
[0132] name: android.java_hprof
[0133] The dynamic buffer mapping `target_buffer` defines the location of the data source within 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, separating them 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, which may include, for example:
[0137] The `process_cmdline` command line is used to configure the process name of the process (i.e., the target process) that caused the memory leak. Its fields are exemplified below:
[0138] process_cmdline: 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 the process has a memory leak, or part of the fields in the stack capture configuration are statically configured and part of the fields are dynamically configured. For example, the statically configurable fields can include (1)-(3) in the stack capture configuration described above, and the dynamically configurable fields can include (4) in the stack capture configuration described above.
[0140] In other implementations, the stack capture configuration can include more or less content, or the stack capture configuration can have other formats, which are not limited in the embodiments of the present application.
[0141] After the maintenance 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 by the stack capture tool.
[0142] In step S203, the maintenance service module releases the constraint of the stack capture tool, so that the monitoring module starts to monitor the stack capture signal.
[0143] Generally, when the process does not need to be captured, the stack capture tool is in a constrained state. For example, if the stack capture tool is the perfetto tool, this state can be referred to as the existence of perfetto constraint.
[0144] When the stack capture tool is in the constrained state, the stack capture tool limits the communication between the maintenance service module and the heap tracker module, that is, the maintenance service module and the heap tracker module cannot communicate when the stack capture tool is in the constrained state. Therefore, in order to facilitate subsequent stack capture using the stack capture tool, the maintenance service module releases the constraint of the stack capture tool, such as the perfetto constraint, after generating the stack capture configuration, so that the maintenance service module can communicate with the heap tracker module.
[0145] In step S204, the maintenance service module establishes session communication with the heap tracker module based on the stack capture configuration.
[0146] In the embodiments of the present application, the maintenance service module can include an interface program for accessing the stack capture tool. The maintenance service module can establish session communication with the heap tracker module through the interface program, so as to facilitate subsequent sending of stack capture notifications to the heap tracker module. For example, the maintenance service module and the tracker module can establish session communication using the unique session name unique_session_name in the stack capture configuration, and subsequent communication between the two is implemented in the session.
[0147] For example, when the stack grabbing tool is the perfetto tool, the tracing SDK can be included in the tracing service module. The tracing service module (e.g., the hiview module) can establish session communication with the heap tracker module (e.g., the heapprofd module) through the tracing SDK.
[0148] It should be noted 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, the heapprofd module can not be able to normally communicate with the hiview module under the constraints of the perfetto tool.
[0149] To this end, the heap tracker module of the embodiment of the present application can include a communication white list, and the process name of the tracing service module is located in the communication white list. The communication between the processes located in the communication white list and the heap tracker module is not subject to the constraints of other processes, that is, the communication between the tracing service module and the heap tracker module is not subject to the constraints of the stack grabbing tool. In this way, by using the white list mechanism and adding the hiview module to the communication white list of the heapprofd module, the communication between the hiview module and the heapprofd module is not subject to the constraints of the perfetto tool.
[0150] In step S205, the tracing service module sends a stack grabbing notification to the heap tracker module.
[0151] After the tracing service module and the heap tracker module establish session communication, the tracing service module can send a stack grabbing notification to the heap tracker module based on the session, and the stack grabbing notification includes stack grabbing configuration.
[0152] In step S206, the heap tracker module receives the stack grabbing notification and sends a stack grabbing signal to the signal takeover module.
[0153] After the heap tracker module receives the stack grabbing notification, the heap tracker module can parse the stack grabbing configuration and send a stack grabbing signal to the signal takeover module running in the child process of the target process based on the process of the target process in the stack grabbing configuration. The signal takeover module in the following refers to the signal takeover module running in the child process of the target process.
[0154] For example, in a JAVA environment, the stack grabbing signal can be a java heap graph stack grabbing signal.
[0155] In step S207, the signal takeover module receives the stack grabbing signal and forwards the stack grabbing signal to the listener module.
[0156] In the embodiments of the present application, the signal takeover module can invoke a signal processing function to capture and take over the stack signal. After the signal takeover module captures the stack signal, the signal takeover module can forward the stack signal to a listening module in the signal takeover module running in the child process of the target process. Hereinafter, the signal takeover module refers to the listening module running in the child process of the target process.
[0157] In step S208, the target process creates a target child process when the listening module captures the stack signal.
[0158] The listening module starts to listen to the stack signal after the target process is started. After the listening module captures the stack signal, the target process can create a target child process. For example, after the listening module captures the stack signal, the target process can create a target child process by means of a fork call. The target child process is a copy of the target process and has almost the same code, data and stack, etc. However, the target child process and the target process are independent of each other and have their own execution environment and do not affect each other.
[0159] In step S209, the target child process captures the heap memory data of the target process.
[0160] The target child process can traverse and capture the heap memory data of the target process at the current time point in the memory, such as traversing and capturing the objects in the heap memory of the target child process at the current time point, the reference relationship between the objects, the memory occupation, etc. For example, in a JAVA environment, the target child process can capture the GCRoot object in the running memory of the target process at the current time point, the reference relationship between the objects, etc. The target child process can capture at least part of the GCRoot object in the running memory of the target process at the current time point and the reference relationship between the objects, such as capturing all the GCRoot object in the running memory of the target process at the current time point and the reference relationship between the objects. It can be understood that whether capturing part of the GCRoot object in the running memory of the target process at the current time point and the reference relationship between the objects or capturing all the GCRoot object in the running memory of the target process at the current time point and the reference relationship between the objects, the objects generated before the current time point of the target process and the reference relationship between the objects are included, so that the accuracy of the memory leakage problem positioning can be improved.
[0161] Further, the target child process can perform a dump operation on the captured heap memory data, i.e., save the captured heap memory data to the shared memory.
[0162] In this way, the target process creates a target child process, and the stack grabbing operation is performed in the target child process instead of the target process, so that the target process only consumes some time when the target child process is created, and does not consume time on the stack grabbing operation, and therefore, the performance of the target process is less affected, or almost not affected.
[0163] In an implementation manner, the target child process can traverse the garbage collection root GCRoot of the target process in the memory, find all root objects that are identified as GCRoot, that is, GCRoot objects, and perform a dump operation on all GCRoot objects of the target process at the current time.
[0164] It should be noted that in the embodiment of the present application, when the target child process performs the stack grabbing, the stack grabbing is performed on the heap memory data of the target process at the current time (that is, the time when the stack grabbing starts), and the heap memory data at least includes all GCRoot objects of the target process in the memory and the reference relationship between the objects, that is, the GCRoot objects generated by the target process before the stack grabbing starts. Therefore, compared with the traditional stack grabbing method that only collects the heap memory data generated after the stack grabbing starts, the method provided in the embodiment of the present application can collect more comprehensive heap memory data, and therefore, the positioning rate of the memory leakage problem can be improved.
[0165] In step S210, the listening module sends a data collection notification to the tracking module.
[0166] In a specific implementation, the listening module can send the data collection notification to the tracking module when the stack grabbing signal is listened to, or the listening module can send the data collection notification to the tracking module when the target child process grabs the GCRoot object of the target process, or the listening module can send the data collection notification to the tracking module after the target child process finishes grabbing the GCRoot object of the target process. The timing of sending the data collection notification from the listening module to the tracking module is not limited in the embodiment of the present application.
[0167] In step S211, the tracking module receives the data collection notification from the listening module, converts the grabbed 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., convert the captured heap memory data into heap dump data in the target format, and fill the heap dump data into the tracking buffer buffer of the shared memory.
[0169] For example, the target format can be protobuf format, that is, the tracking module can convert the captured heap memory data into heap dump data in protobuf format, and store the heap dump data in the form of protobuf format file blocks in the tracking buffer of the shared memory, thereby facilitating the sharing of heap dump data among the target process, the tracking module, the heap tracker module, etc.
[0170] In step S212, the tracking module sends a data collection notification to the heap tracker module.
[0171] In some implementations, the listening module can send the 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 listening module can send the data collection notification to the heap tracker module earlier, for example, when receiving the data collection notification from the listening module, the data collection notification is sent to the heap tracker module.
[0172] The timing of the tracking module sending the data collection notification to the heap tracker module is not limited in the embodiments of the present application.
[0173] In step S213, the heap tracker module receives the data collection notification from the tracking module and forwards the data collection notification to the maintenance service module.
[0174] In step S214, the maintenance service module receives the data collection notification and stores the heap dump data in the shared memory to the target position of the flash memory.
[0175] For example, the target position is a preset memory leak log directory in the flash memory.
[0176] In some implementations, the maintenance service module can immediately obtain the heap dump data from the shared memory when receiving the data collection notification, and store the obtained heap dump data in the memory leak log directory.
[0177] In other implementations, the maintenance and testing service module can wait a preset time after sending the stack capture notification before retrieving the heap dump data from shared memory and storing the 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 the stack capture to filling the shared memory trace buffer with the capture results (i.e., the heap dump data). This ensures that the maintenance and testing service module successfully retrieves the heap dump data from shared memory.
[0178] For example, the maintenance monitoring service module can be set to a loop timer, such as a loop duration of 50 seconds. After sending a stack capture notification, the maintenance monitoring service module can wait for at least one loop timer cycle, for example, two loop timer cycles, i.e., 100 seconds, before retrieving the heap dump data from shared memory and storing the heap dump data in the memory leak log directory. This ensures that the maintenance monitoring service module captures and dumps the data during the waiting time.
[0179] In some implementations, 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. Different processes have different process IDs to distinguish the heap dump data corresponding to different processes.
[0180] It should be noted that in this embodiment, shared memory refers to the shared storage space within the electronic device's running memory (e.g., RAM), while the memory leak log directory is located in the electronic device's flash memory. In other words, the maintenance and testing service module saves the heap dump data from the running memory to the flash memory. This facilitates the analysis of the heap dump data at any time to pinpoint the cause of the memory leak.
[0181] In some implementations, the maintenance and testing service module can also upload heap dump data to a cloud server to facilitate online analysis of the causes of memory leaks.
[0182] Step S215: The maintenance and testing service module cleans up session data and restores the constraints of the stack capture tool.
[0183] After the maintenance service module saves the heap dump data in the memory leak log directory, the entire stack capture process ends. At this point, the maintenance service module can clean up the session data of the session communication with the heap tracer module, end the session communication with the heap tracer module, and restore the stack capture tool to the constraint state, such as restoring the perfetto constraint.
[0184] The method provided in this application embodiment involves a monitoring service module detecting a memory leak in a target process. The module then uses a stack capture tool (e.g., the perfetto tool) to capture the target process's heap memory data and dump it. The monitoring service module saves this heap dump data in a memory leak log directory on flash memory for analysis and localization of the cause of the memory leak. Compared to traditional stack capture methods, the method provided in this application embodiment can capture all GCRoot objects of the target process at the current moment and their reference relationships, thereby improving the accuracy of memory leak location. Furthermore, the method provided in this application embodiment involves the target process creating a child process via a fork call after the monitoring module detects the stack capture signal. Stack capture is performed within the child process, while the target process itself performs the stack capture. Therefore, the target process only consumes time when creating the child process, not when capturing heap memory data, resulting in a smaller impact on the target process's performance.
[0185] It is worth noting that some applications intercept stack capture signals to prevent themselves from being captured by stack capture tools. For example, some applications can block stack capture signals by calling the `signal` or `sigaction` methods. To prevent stack capture signals from being intercepted and to ensure successful stack capture, in some implementations, the stack capture signals can be reloaded before the stack capture process.
[0186] Figure 6 This is another flowchart illustrating a method for capturing memory data provided in an embodiment of this application.
[0187] Combination Figure 5 and Figure 6 As shown, in Figure 5 Based on the method and flow shown, Figure 6 The illustrated method flow adds a step of reinstalling the stack grab signal between steps S201 and S202, i.e., steps S301-S303, and adds a step of restoring the stack grab signal after step S215, i.e., steps S304-S306. Specifically:
[0188] Step S301: The maintenance and testing service module notifies the memory detection module to reload the stack capture signal.
[0189] Taking the Android system as an example, the maintenance and testing service module can use the ioctl function to notify the memory detection module that the stack capture signal needs to be reinstalled.
[0190] In step S302, the memory detection module sends a reinstallation signal to the signal reinstallation module;
[0191] For example, the Android system, the reinstall signal may be, for example, the 44 signal in the Linux system, of course, it may also be other signals in the Linux system, which is not limited here. In summary, the embodiments of the present application use an original signal to notify the signal reinstall module to reinstall the stack signal.
[0192] In step S303, the signal reinstall module receives the reinstall signal and executes the processing function of reinstalling the stack signal.
[0193] The signal reinstall module can execute the processing function of reinstalling the stack signal when receiving the reinstall signal sent by the memory detection module. In this way, the application program cannot shield the stack signal by calling the signal function or the sigaction function, thereby ensuring that the signal takeover module can receive the stack signal sent by the heap tracker module in the subsequent process, and avoiding stack failure.
[0194] In step S304, the maintenance service module notifies the memory detection module to restore the stack signal.
[0195] For example, the Android system, the maintenance service module can notify the memory detection module to restore the stack signal through the ioctl function.
[0196] In step S305, the memory detection module sends a restore signal to the signal reinstall module.
[0197] For example, the Android system, the restore signal may be, for example, the 44 signal in the Linux system, of course, it may also be other signals in the Linux system, which is not limited here. In summary, the embodiments of the present application use an original signal to notify the signal reinstall module to restore the stack signal.
[0198] In step S306, the signal reinstall module receives the restore signal and restores the stack signal to the default state.
[0199] The signal reinstall module can restore the stack signal to the default state when receiving the restore signal sent by the memory detection module. In this way, the electronic device can be restored to the system environment before the stack, and the original system environment of the electronic device will not be changed.
[0200] The method provided by the embodiments of the present application can detect the target process memory leakage, and then call the signal reinstall module to reinstall the stack signal before calling the stack tool to stack the target process, so as to avoid the stack signal of the stack tool being intercepted by the target application, thereby avoiding stack failure.
[0201] Figure 7 is another flowchart of a method for capturing memory data provided by the embodiments of the present application.
[0202] As Figure 7 shown, in one embodiment, the method of grabbing memory data can include S401-S401:
[0203] Step S401, the electronic device in the case of memory leakage in the target process, grab the heap memory data of the target process.
[0204] Wherein, the heap memory data at least includes all the garbage collection root GCRoot object of the target process in the running memory and the reference relationship between objects.
[0205] In an implementation, the electronic device in the case of memory leakage in the target process, can create a target sub-process, and grab the heap memory data in the sub-process, wherein the target sub-process is a copy of the target process.
[0206] The specific implementation of step S401 can refer to the related description of steps S201-S211 in the foregoing embodiments, which will not be repeated here.
[0207] Step S403, the electronic device stores the grabbed heap memory data to the target position of the flash memory.
[0208] The specific implementation of step S403 can refer to the related description of steps S212-S215 in the foregoing embodiments, which will not be repeated here.
[0209] In an implementation, the electronic device can first reinstall the stack signal before grabbing the heap memory data of the target process after detecting that the target process has memory leakage. The specific implementation of reinstalling the stack signal can refer to the related description of steps S301-S303 in the foregoing embodiments, which will not be repeated here.
[0210] In an implementation, the electronic device can restore the stack signal after saving the grabbed heap memory data to the target position in the flash memory. The specific implementation of restoring the stack signal can refer to the related description of steps S304-S306 in the foregoing embodiments, which will not be repeated here.
[0211] The above embodiments provided by the present application introduce various schemes of the method for grabbing memory data provided by the present application. It can be understood that, in order to implement the above functions, the electronic device comprises hardware structure and / or software module corresponding to 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 in the present application, the present application can be implemented in the form of hardware or combination of hardware and computer software. Whether a certain function is implemented in the form of hardware or computer software driving hardware depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of the present application.
[0212] The embodiments of the present application can divide the functional modules of the above-mentioned electronic device according to the above-mentioned method examples. For example, each functional module can be divided according to each function, or two or more functions can be integrated in one processing module. The above-mentioned integrated module can be realized in the form of hardware or software functional module. It should be noted that the division of modules in the embodiments of the present application is illustrative, and is only a logical functional division. When actually implemented, there can be another division manner.
[0213] Figure 8 FIG. 1 is a structural schematic diagram of a device for grabbing memory data provided by an embodiment of the present application. In an embodiment, the electronic device can realize the corresponding functions through the hardware device shown in FIG. 1. As shown in FIG. 1, the device for grabbing memory data can include a display screen 101, a memory 102, a processor 103, and a communication module 104. The above-mentioned devices can be connected through one or more communication buses 105. Figure 8 Figure 8
[0214] In an embodiment, the display screen 801 can include a display panel 8011 and a touch sensor 8012, where the display panel 8011 is configured to display images, and the touch sensor 8012 can pass a detected touch operation to an application processor to determine a touch event type, and provide visual output related to the touch operation through the display panel 8011. The processor 803 can include one or more processing units, for example: the processor 803 can 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. Different processing units can be independent devices, or can be integrated into one or more processors. The memory 802 is coupled to the processor 803, and is configured to store various software programs and / or sets of instructions. When the software programs and / or sets of instructions in the memory 802 are executed by the processor 803, the electronic device implements the method steps in the above embodiments and their implementation manners:
[0215] The embodiments of the present application also provide a computer readable storage medium, which stores instructions, and when the instructions are executed on a computer, the computer executes the method of the above aspects.
[0216] The embodiments of the present application also provide a computer program product containing instructions, and when the instructions are executed on a computer, the computer executes the method of the above aspects.
[0217] The embodiments of the present application also provide a chip system, which includes a processor configured to support the functions involved in the above aspects, for example, generating or processing the information involved in the above method. In a possible design, the chip system further includes a memory configured to store computer instructions and data necessary for the multi-device collaboration device. The chip system can be composed of a chip, or can include a chip and other discrete devices.
[0218] Through the above description of the embodiments, those skilled in the art can clearly understand that, for the convenience and brevity of description, only the division of the above functional modules is taken as an example for illustration, and in actual application, the above functions can be completed by different functional modules according to needs, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above.
[0219] In several embodiments provided in the present application, it should be understood that the disclosed apparatuses / devices and methods can be implemented in other manners. For example, the embodiments of the apparatuses / devices described above are merely schematic. For example, the division of the modules or the division of the functions of the modules can be different, for example, multiple modules or components can be combined or integrated into another device, or some features can be ignored or not executed. In addition, the displayed or discussed mutual couplings or direct couplings or communication connections can be indirect couplings or communication connections through some interfaces, devices or modules, and can be in electrical, mechanical or other forms.
[0220] The modules illustrated as separated components can or can not be physically separated, and the components illustrated as modules can be one physical module or multiple physical modules, i.e., can be located in one place or distributed in multiple different places. Some or all of the modules can be selected according to actual needs to achieve the purpose of the embodiments.
[0221] In addition, each functional module in the embodiments of the present application can be integrated into one processing module, or each module can exist physically independently, or two or more modules can be integrated into one module. The integrated module can be realized in the form of hardware or in the form of a software functional module.
[0222] When the integrated module is realized in the form of a software functional module and sold or used as an independent product, it can be stored in a readable storage medium. Based on this understanding, the technical solutions of the embodiments of the present application essentially or the parts that make contributions to the prior art or the whole or part of the technical solutions can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for making a device (which can be a single-chip microcomputer, a chip, etc.) or a processor execute all or part of the steps of the methods described in the embodiments of the present application. The foregoing storage medium includes: a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, and various media that can store program codes.
[0223] The above is merely specific embodiments of the present application, but the protection scope of the present application is not limited thereto, and any changes or replacements within the technical scope disclosed in the present application should be covered in the protection scope of the present application. Therefore, the protection scope of the present application should be subject to the protection scope of the claims.
Claims
1. A method for retrieving data from memory, characterized in that, The electronic device includes a maintenance and testing service module and a stack capture tool. The stack capture tool includes a heap tracer module and a listening module. The listening module runs in a sub-thread of the target process and includes: The maintenance and testing service module in the local framework layer of the electronic device detected a memory leak in the target process. When the target process experiences a memory leak, the maintenance and testing service module generates a stack capture configuration corresponding to the target process. 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 stack tracer module in the local framework layer, and the stack capture notification includes the stack capture configuration. Based on the stack capture configuration, the stack tracer module sends a stack capture signal to the listening module in the application layer of the electronic device. When the monitoring module detects the stack capture signal, the target process creates a target child process, which is a copy of the target process. The target subprocess fetches heap memory data and stores the fetched heap memory data in a target location in flash memory; the heap memory data includes at least all garbage collection root (GCRoot) objects of the target process in the running memory, as well as the reference relationships between the objects; 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 tracer module, the method further includes: The maintenance and testing service module removes the constraints of the stack grabbing tool, enabling the maintenance and testing service module to communicate with the heap tracer module; The stack capture tool also includes a tracing module, and the stack capture configuration also includes buffer-related configurations; the electronic device, in the event of a memory leak in the target process, captures the heap memory data of the target process, and further includes: The monitoring module detects the stack capture signal and sends a first data acquisition notification to the tracking module; Upon receiving the first data acquisition notification, the tracking module converts the captured heap memory data into heap dump data in the target format based on the buffer-related configuration, and fills the heap dump data into the tracking buffer of the shared memory.
2. The method according to claim 1, characterized in that, The electronic device also includes a memory detection module and a signal reloading 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 reinstallation module receives the reinstallation signal and executes the processing function to reinstall the stack capture signal.
3. The method according to claim 2, characterized in that, After the electronic device stores the captured heap memory data into the 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 reinstallation module; The signal reassembly module receives the recovery signal and restores the stack capture signal to its default state.
4. The method according to claim 3, characterized in that, The electronic device stores the captured heap memory data into a target location in the flash memory, including: The tracking module sends a second data acquisition notification to the heap tracker module; The heap tracker module forwards the second data acquisition notification to the maintenance and testing service module; Upon receiving the second data acquisition notification, the maintenance and testing service module stores the heap dump data in the tracking buffer into the target location.
5. The method according to claim 4, 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 waiting for a preset time, the maintenance and testing service module stores the heap dump data in the tracking buffer into the target location.
6. The method according to claim 1, characterized in that, The electronic device also includes a signal takeover module, which runs in a sub-thread of the target process; The heap tracer module sends a stack capture signal to the listening module based on the stack capture configuration, including: The stack tracer module sends the stack grab signal to the signal takeover module based on the stack grab configuration; The signal takeover module receives the stack capture signal and forwards the stack capture signal to the monitoring module.
7. The method according to claim 2, 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 heap tracer module, the method further includes: The maintenance service module establishes a session communication with the heap tracer module based on the unique session name.
8. The method according to claim 7, characterized in that, After the electronic device saves the captured heap memory data to the 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.
9. The method according to any one of claims 1-8, characterized in that, The heap tracer module includes a communication whitelist, in which the process name of the maintenance and testing service module is located, so that the communication between the maintenance and testing service module and the heap tracer module is not constrained by the stack capture tool.
10. The method according to claim 9, 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.
11. The method according to claim 2, characterized in that, The memory detection module is located in the kernel layer of the operating system, and the signal reloading module is located in the application layer of the operating system.
12. An electronic device, characterized in that, include: A memory and one or more processors; the memory is coupled to the processors; wherein the memory stores computer program code, the computer program code including computer instructions, which, when executed by the processor, cause the electronic device to perform the method of fetching memory data as described in any one of claims 1-11.
Citation Information
Patent Citations
Memory leak monitoring method, device and electronic equipment
CN112035314A
Memory application method and electronic equipment
CN116661987A