Method for detecting thread leakage, electronic device and computer readable storage medium

By generating and aggregating thread leak logs in the Android system, the application lag caused by thread leaks was resolved, the detection capability and location accuracy of thread leaks were improved, and the user experience was enhanced.

CN119248541BActive Publication Date: 2026-04-14HONOR DEVICE CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
HONOR DEVICE CO LTD
Filing Date
2024-01-26
Publication Date
2026-04-14

AI Technical Summary

Technical Problem

Existing technologies cannot effectively detect and locate thread leaks, leading to application lag and a decline in user experience. This is especially true in Android application development, where the main thread cannot respond to user operations in a timely manner when it is blocked or occupied by a non-main thread.

Method used

After a thread leak occurs in the target process, a first thread leak log is generated, and a second thread leak log stored in the bridge node is obtained. The first and second thread leak logs are aggregated into a third thread leak log, which records thread information and call stack to improve detection capabilities.

Benefits of technology

It enables efficient detection and localization of thread leaks in the Android system, improving application performance and user experience. It can aggregate thread information and call stack into a single log, facilitating problem localization and analysis.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119248541B_ABST
    Figure CN119248541B_ABST
Patent Text Reader

Abstract

Embodiments of the present application provide a thread leakage detection method, an electronic device and a computer readable storage medium. In the thread leakage detection method, after the electronic device determines that a target process has thread leakage, a first thread leakage log is generated, then a second thread leakage log stored in a bridge node is acquired, and the first thread leakage log and the second thread leakage log are aggregated into a third thread leakage log. Thus, the first thread leakage log recording thread information and the second thread leakage log recording the call stack of the leakage thread can be aggregated into one log after the target process has thread leakage, and the detection capability of thread leakage and the capability of locating the thread with problems are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of smart terminal technology, and in particular to a method for detecting thread leaks, an electronic device, and a computer-readable storage medium. Background Technology

[0002] In application development, taking Android applications as an example, thread leaks typically refer to a situation where the main thread of a process is blocked or occupied by a non-main thread (e.g., a background thread or a child thread), causing the main thread to be unable to respond to user actions in a timely manner, thus affecting application performance and user experience. The main thread refers to the first thread created after the application's process starts, while non-main threads are those created after the first thread.

[0003] Thread leaks can cause application lag or unresponsive interfaces, severely impacting application performance and user experience. Therefore, special care must be taken to avoid thread leaks during application development. Summary of the Invention

[0004] This application provides a method, electronic device, and computer-readable storage medium for detecting thread leaks, which aggregates a thread leak log that records thread information and a log that records the thread's call stack into a single log after a thread leak occurs in the target process, thereby improving the detection capability of thread leaks.

[0005] In a first aspect, embodiments of this application provide a method for detecting thread leaks, comprising: after determining that a thread leak has occurred in a target process, generating a first thread leak log; wherein the first thread leak log stores information about threads already started in the target process; obtaining a second thread leak log stored in a bridge node; wherein the second thread leak log records the call stack of the leaking threads in the target process; the leaking threads include newly started threads in the target process after a first number reaches a preset first threshold, the first number being the number of threads already started in the target process; and aggregating the first thread leak log and the second thread leak log into a third thread leak log.

[0006] In the above thread leak detection method, after the electronic device determines that a thread leak has occurred in the target process, it generates a first thread leak log, then obtains the second thread leak log stored in the bridge node, and aggregates the first thread leak log and the second thread leak log into a third thread leak log. This allows the first thread leak log that records thread information and the second thread leak log that records the call stack of the leaking thread to be aggregated into one log after a thread leak occurs in the target process, thereby improving the thread leak detection capability.

[0007] In one possible implementation, before obtaining the second thread leak log stored in the bridge node, the method further includes: obtaining a first number of threads that have been started in the target process; after the first number reaches a preset first threshold, obtaining the call stack of the leaking thread in the target process; generating the second thread leak log based on the call stack of the leaking thread; and storing the second thread leak log in the bridge node.

[0008] In one possible implementation, generating the second thread leak log based on the call stack of the leaking thread includes: obtaining the number of calls for each call stack based on the call stack of the leaking thread; and sorting the call stack according to the number of calls to generate the second thread leak log.

[0009] In one possible implementation, after obtaining the second thread leak log stored in the bridge node, the method further includes: deleting the second thread leak log stored in the bridge node.

[0010] In one possible implementation, determining that the target process has a thread leak includes: obtaining a first number of threads that the target process has started; if the first number is greater than or equal to a pre-set second threshold, then determining that the target process has a thread leak; wherein the second threshold is greater than or equal to the first threshold.

[0011] In one possible implementation, generating the first thread leak log includes: obtaining information about the threads that the target process has started; and generating the first thread leak log based on the thread information.

[0012] In one possible implementation, generating the first thread leak log based on the thread information includes: sorting the information of the threads that the target process has started in order to generate the first thread leak log.

[0013] In one possible implementation, the thread information includes: the thread name of the thread; sorting the information of the threads that have been started in the target process includes: obtaining a second number of threads corresponding to each thread name based on the thread name; and sorting the information of the threads corresponding to each thread name based on the second number.

[0014] In one possible implementation, after sorting the information of the thread corresponding to each thread name according to the second quantity, the method further includes: if there are at least two thread names with the same number of threads among the threads started by the target process, then sorting the information of the threads corresponding to the at least two thread names according to the at least two thread names.

[0015] In one possible implementation, the thread information further includes: the thread's runtime; after sorting the thread information corresponding to each thread name according to the second quantity, the method further includes: sorting the thread information corresponding to the first thread name according to the runtime; wherein, the first thread name includes thread names whose corresponding thread count is greater than 1 among the thread names of the threads already started in the target process.

[0016] In one possible implementation, the thread information further includes: the creation time of the thread; after sorting the information of the thread corresponding to the first thread name according to the runtime, it further includes: if at least two threads in the information of the thread corresponding to the first thread name have the same runtime, then sort the information of the at least two threads according to the creation time.

[0017] In one possible implementation, after aggregating the first thread leak log and the second thread leak log into a third thread leak log, the method further includes: if the application corresponding to the target process has registered for log sharing, then sharing the third thread leak log with the application.

[0018] In one possible implementation, after aggregating the first thread leak log and the second thread leak log into a third thread leak log, the method further includes: locating the thread in the target process where the problem occurred based on the third thread leak log.

[0019] Secondly, embodiments of this application provide an electronic device, including: one or more processors; a memory; multiple application programs; and one or more computer programs, wherein the one or more computer programs are stored in the memory, and the one or more computer programs include instructions that, when executed by the electronic device, cause the electronic device to perform the method provided in the first aspect.

[0020] It should be understood that the second aspect of the embodiments of this application is consistent with the technical solution of the first aspect of the embodiments of this application, and the beneficial effects achieved by each aspect and the corresponding feasible implementation are similar, and will not be described again.

[0021] Thirdly, embodiments of this application provide a computer-readable storage medium storing a computer program that, when run on a computer, causes the computer to perform the method provided in the first aspect.

[0022] Fourthly, embodiments of this application provide a computer program that, when executed by a computer, performs the method provided in the first aspect.

[0023] In one possible design, the program in the fourth aspect can be stored wholly or partially on a storage medium packaged with the processor, or it can be stored wholly or partially on a memory not packaged with the processor. Attached Figure Description

[0024] Figure 1 A schematic diagram of a thread leak detection scheme provided by existing related technologies;

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

[0026] Figure 3 A software structure block diagram of an electronic device 100 provided in one embodiment of this application;

[0027] Figure 4 A flowchart illustrating a thread leak detection method provided in one embodiment of this application;

[0028] Figure 5 A flowchart illustrating a thread leak detection method provided in another embodiment of this application;

[0029] Figure 6 A flowchart illustrating a thread leak detection method provided in yet another embodiment of this application;

[0030] Figure 7 A flowchart of a thread leak detection method provided in another embodiment of this application. Detailed Implementation

[0031] The terminology used in the implementation section of this application is for the purpose of explaining specific embodiments of this application only, and is not intended to limit this application.

[0032] When an application installed on an electronic device is launched, it starts a process, referred to as the target process in the following description. Generally, one application corresponds to one target process. Multiple threads can be started within a target process to execute tasks. After a task is completed, the thread executing that task is released. However, if the thread executing the task is not released after completion, it will continue to occupy memory. If the number of threads that have completed their tasks but are not released gradually increases, it will lead to a thread leak in the target process. This increases the memory usage of the target process, causes the application to lag, and may even cause the application to crash. The "problematic thread" mentioned in the following description can include threads that cause thread leaks in the target process, such as threads that have completed their tasks but were not released.

[0033] Therefore, special attention needs to be paid to avoiding thread leaks during application development. Figure 1A schematic diagram of a thread leak detection scheme provided by existing related technologies, such as... Figure 1 As shown, after performing thread leak detection on the target process, the thread leak threshold (ThresholdValue) is obtained. If the number of threads started by the target process is greater than or equal to the thread leak threshold, it can be determined that the target process has a thread leak fault, and then the thread leak log can be obtained.

[0034] Specifically, thread leak detection is performed at the native layer. Therefore, after detecting a thread leak in the target process, the native layer can obtain information about the threads already started by the target process and generate a native layer thread leak log. Additionally, after the number of threads started by the target process reaches a certain number, for newly started threads, the Java layer (i.e., the application layer) obtains the call stack of these newly started threads and generates a Java layer thread leak log. However, the native layer thread leak log and the Java layer thread leak log are separate. As shown in Table 1, the native layer thread leak log records information about the threads already started by the target process but does not record the corresponding call stack information, while the Java layer thread leak log only contains the thread name and call stack, without other thread information. In existing related technologies, it is impossible to correlate the logs from the two layers to locate and analyze the problematic thread. Furthermore, existing related technologies also cannot share the native layer and Java layer thread leak logs with third-party applications installed on electronic devices.

[0035] Table 1

[0036] Thread leak log defect The native layer obtains information about the threads already started in the target process. No corresponding call stack information. Get the call stack of the newly started thread in the Java layer. Only the thread name and call stack are provided; no other information about the thread is available.

[0037] Based on the above problems, this application provides a method for detecting thread leaks, which can aggregate the thread leak log (such as the thread leak log of the Native layer mentioned above) that records thread information and the log that records the call stack of the thread (such as the thread leak log of the Java layer mentioned above) into a single log after a thread leak occurs in the target process, thereby improving the detection capability of thread leaks.

[0038] The thread leakage detection method provided in this application can be applied to electronic devices, such as smartphones, tablets, wearable devices, in-vehicle devices, augmented reality (AR) / virtual reality (VR) devices, laptops, ultra-mobile personal computers (UMPCs), netbooks, or personal digital assistants (PDAs). This application does not impose any restrictions on the specific type of electronic device.

[0039] For example, Figure 2 This is a schematic diagram of the structure of an electronic device provided in one embodiment of this application, as shown below. Figure 2 As shown, the electronic device 100 may include a processor 110, an external memory interface 120, an internal memory 121, a universal serial bus (USB) interface 130, a charging management module 140, a power management module 141, a battery 142, an antenna 1, an antenna 2, a mobile communication module 150, a wireless communication module 160, an audio module 170, a speaker 170A, a receiver 170B, a microphone 170C, a headphone jack 170D, a sensor module 180, buttons 190, a motor 191, an indicator 192, a camera 193, a display screen 194, and a subscriber identification module (SIM) card interface 195, etc. The sensor module 180 may include a pressure sensor 180A, a gyroscope sensor 180B, a barometric pressure sensor 180C, a magnetic sensor 180D, an accelerometer sensor 180E, a distance sensor 180F, a proximity sensor 180G, a fingerprint sensor 180H, a temperature sensor 180J, a touch sensor 180K, an ambient light sensor 180L, a bone conduction sensor 180M, etc.

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

[0041] Processor 110 may include one or more processing units, such as application processors (APs), modem processors, graphics processing units (GPUs), image signal processors (ISPs), controllers, video codecs, digital signal processors (DSPs), baseband processors, and / or neural network processing units (NPUs). These different processing units may be independent devices or integrated into one or more processors.

[0042] The controller can generate operation control signals based on the instruction opcode and timing signals to complete the control of instruction fetching and execution.

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

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

[0045] USB port 130 is a USB standard compliant interface, specifically a Mini USB port, Micro USB port, USB Type-C port, etc. USB port 130 can be used to connect a charger to charge electronic device 100, and can also be used for data transfer between electronic device 100 and peripheral devices. It can also be used to connect headphones for audio playback. This interface can also be used to connect other electronic devices, such as AR devices.

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

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

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

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

[0050] Antenna 1 and antenna 2 are used to transmit and receive electromagnetic wave signals. Each antenna in electronic device 100 can be used to cover one or more communication frequency bands. Different antennas can also be multiplexed to improve antenna utilization. For example, antenna 1 can be multiplexed as a diversity antenna for a wireless local area network. In some other embodiments, the antennas can be used in conjunction with tuning switches.

[0051] The mobile communication module 150 can provide solutions for wireless communication, including 2G / 3G / 4G / 5G, applied to the electronic device 100. The mobile communication module 150 may include at least one filter, switch, power amplifier, low noise amplifier (LNA), etc. The mobile communication module 150 can receive electromagnetic waves via antenna 1, and perform filtering, amplification, and other processing on the received electromagnetic waves before transmitting them to a modem processor for demodulation. The mobile communication module 150 can also amplify the signal modulated by the modem processor and convert it into electromagnetic waves for radiation via antenna 1. In some embodiments, at least some functional modules of the mobile communication module 150 may be housed in the processor 110. In some embodiments, at least some functional modules of the mobile communication module 150 and at least some modules of the processor 110 may be housed in the same device.

[0052] The modem processor may include a modulator and a demodulator. The modulator modulates the low-frequency baseband signal to be transmitted into a mid-to-high frequency signal. The demodulator demodulates the received electromagnetic wave signal into a low-frequency baseband signal. The demodulator then transmits the demodulated low-frequency baseband signal to the baseband processor for processing. After processing by the baseband processor, the low-frequency baseband signal is transmitted to the application processor. The application processor outputs sound signals through an audio device (not limited to speaker 170A, receiver 170B, etc.) or displays images or videos through the display screen 194. In some embodiments, the modem processor may be a separate device. In other embodiments, the modem processor may be independent of the processor 110 and may be housed in the same device as the mobile communication module 150 or other functional modules.

[0053] The wireless communication module 160 can provide solutions for wireless communication applications on the electronic device 100, including wireless local area networks (WLANs) (such as wireless fidelity (Wi-Fi) networks), Bluetooth (BT), global navigation satellite system (GNSS), frequency modulation (FM), near field communication (NFC), and infrared (IR) technologies. The wireless communication module 160 can be one or more devices integrating at least one communication processing module. The wireless communication module 160 receives electromagnetic waves via antenna 2, performs frequency modulation and filtering of the electromagnetic wave signals, and sends the processed signal to processor 110. The wireless communication module 160 can also receive signals to be transmitted from processor 110, perform frequency modulation and amplification, and convert them into electromagnetic waves for radiation via antenna 2.

[0054] In some embodiments, antenna 1 of electronic device 100 is coupled to mobile communication module 150, and antenna 2 is coupled to wireless communication module 160, enabling electronic device 100 to communicate with networks and other devices via wireless communication technology. The wireless communication technology may include Global System for Mobile Communications (GSM), General Packet Radio Service (GPRS), Code Division Multiple Access (CDMA), Wideband Code Division Multiple Access (WCDMA), Time Division Code Division Multiple Access (TD-SCDMA), Long Term Evolution (LTE), BT, GNSS, WLAN, NFC, FM, and / or IR technologies, etc. The GNSS may include the Global Positioning System (GPS), the Global Navigation Satellite System (GLONASS), the BeiDou Navigation Satellite System (BDS), the Quasi-Zenith Satellite System (QZSS), and / or satellite-based augmentation systems (SBAS).

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

[0056] Display screen 194 is used to display images, videos, etc. Display screen 194 includes a display panel. The display panel may be a liquid crystal display (LCD), an organic light-emitting diode (OLED), an active-matrix organic light-emitting diode (AMOLED), a flexible light-emitting diode (FLED), a miniature LED, a microLED, a quantum dot light-emitting diode (QLED), etc. In some embodiments, electronic device 100 may include one or N displays 194, where N is a positive integer greater than 1.

[0057] Electronic device 100 can perform shooting functions through ISP, camera 193, video codec, GPU, display 194 and application processor.

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

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

[0060] Digital signal processors (DSPs) are used to process digital signals. Besides digital image signals, they can also process other digital signals. For example, when electronic device 100 selects a frequency, the DSP can perform Fourier transforms on the frequency energy.

[0061] Video codecs are used to compress or decompress digital video. Electronic device 100 may support one or more video codecs. Thus, electronic device 100 can play or record videos in various encoding formats, such as Moving Picture Experts Group (MPEG) 1, MPEG2, MPEG3, MPEG4, etc.

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

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

[0064] Internal memory 121 can be used to store computer executable program code, which includes instructions. Internal memory 121 may include a program storage area and a data storage area. The program storage area may store the operating system, at least one application program required for a function (such as sound playback, image playback, etc.), etc. The data storage area may store data created during the use of electronic device 100 (such as audio data, phonebook, etc.). Furthermore, internal memory 121 may include high-speed random access memory, and may also include non-volatile memory, such as at least one disk storage device, flash memory device, universal flash storage (UFS), etc. Processor 110 executes various functional applications and data processing of electronic device 100 by running instructions stored in internal memory 121 and / or instructions stored in memory located in the processor.

[0065] Electronic device 100 can implement audio functions, such as music playback and recording, through audio module 170, speaker 170A, receiver 170B, microphone 170C, headphone jack 170D, and application processor.

[0066] The audio module 170 is used to convert digital audio information into analog audio signals for output, and also to convert analog audio input into digital audio signals. The audio module 170 can also be used for encoding and decoding audio signals. In some embodiments, the audio module 170 may be located in the processor 110, or some functional modules of the audio module 170 may be located in the processor 110.

[0067] The speaker 170A, also known as a "loudspeaker," is used to convert audio electrical signals into sound signals. The electronic device 100 can listen to music or make hands-free calls through the speaker 170A.

[0068] The receiver 170B, also known as the "earpiece," is used to convert audio electrical signals into sound signals. When the electronic device 100 answers a telephone call or voice message, the receiver 170B can be brought close to the ear to listen to the voice.

[0069] Microphone 170C, also known as a "microphone" or "voice transducer," is used to convert sound signals into electrical signals. When making a phone call or sending a voice message, the user can speak by bringing their mouth close to microphone 170C, inputting the sound signal into microphone 170C. Electronic device 100 may have at least one microphone 170C. In some embodiments, electronic device 100 may have two microphones 170C, which, in addition to collecting sound signals, can also perform noise reduction. In other embodiments, electronic device 100 may also have three, four, or more microphones 170C, which can collect sound signals, reduce noise, identify the sound source, and perform directional recording, etc.

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

[0071] Buttons 190 include a power button, volume buttons, etc. Buttons 190 can be mechanical buttons or touch-sensitive buttons. Electronic device 100 can receive button input and generate key signal inputs related to user settings and function control of electronic device 100.

[0072] Motor 191 can generate vibration alerts. Motor 191 can be used for incoming call vibration alerts or for touch vibration feedback. For example, different vibration feedback effects can correspond to touch operations performed on different applications (such as taking photos, playing audio, etc.). Motor 191 can also correspond to different vibration feedback effects for touch operations performed on different areas of the display screen 194. Different application scenarios (such as time reminders, receiving messages, alarm clocks, games, etc.) can also correspond to different vibration feedback effects. The touch vibration feedback effect can also be customized.

[0073] Indicator 192 can be an indicator light, used to indicate charging status, power changes, or to indicate messages, missed calls, notifications, etc.

[0074] The SIM card interface 195 is used to connect a SIM card. The SIM card can be inserted into or removed from the SIM card interface 195 to make contact with and separate from the electronic device 100. The electronic device 100 can support one or N SIM card interfaces, where N is a positive integer greater than 1. The SIM card interface 195 can support Nano SIM cards, Micro SIM cards, SIM cards, etc. Multiple cards can be inserted into the same SIM card interface 195 simultaneously. The multiple cards can be of the same or different types. The SIM card interface 195 is also compatible with different types of SIM cards. The SIM card interface 195 is also compatible with external memory cards. The electronic device 100 interacts with the network through the SIM card to realize functions such as calls and data communication. In some embodiments, the electronic device 100 uses an eSIM, i.e., an embedded SIM card. The eSIM card can be embedded in the electronic device 100 and cannot be separated from the electronic device 100.

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

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

[0077] A layered architecture divides software into several layers, each with a clear role and function. Layers communicate with each other through software interfaces. In some embodiments, the Android system is divided into four layers, from top to bottom: the Java layer (i.e., the application layer), the application framework layer, the native layer, and the kernel layer.

[0078] After the number of threads already started in the target process reaches a preset first threshold, any newly started threads in the target process can be referred to as leaked threads. The size of the first threshold can be set by the implementation based on system performance and / or implementation requirements; this embodiment does not limit the size of the first threshold. For example, assuming the first threshold can be 10, then after the number of threads already started in the target process reaches 10, any newly started threads in the target process, such as the eleventh thread, the twelfth thread, and so on, can all be referred to as leaked threads.

[0079] like Figure 3 As shown in this embodiment, the Java layer obtains the call stack of the leaking thread and stores the second thread leak log, which stores the call stack of the leaking thread, in the bridge node of the kernel layer. The specific storage location can be "proc / self / thread_stat". The thread's call stack records the order in which the thread calls functions, that is, which function the thread jumps from to which function during task execution. Generally, each thread has its own corresponding call stack, and the call stacks of different threads may be the same or different.

[0080] The Native layer performs thread leak detection. After determining that a thread leak has occurred in the target process, the Native layer generates a first thread leak log, which stores information about the threads that have been started by the target process. Then, the Native layer obtains a second thread leak log from the bridge node and aggregates the first and second thread leak logs into a third thread leak log. This makes the thread leak logs obtained by the electronic device 100 no longer separate, which can improve the thread leak detection capability.

[0081] For ease of understanding, the following embodiments of this application will be described using the following methods: Figure 2 and Figure 3 Taking the electronic device with the structure shown as an example, and in conjunction with the accompanying drawings and application scenarios, the thread leakage detection method provided in this application embodiment will be specifically described.

[0082] Figure 4 A flowchart of a thread leak detection method provided in one embodiment of this application is shown below. Figure 4 As shown, the above-mentioned thread leak detection methods may include:

[0083] Step 401: After determining that a thread leak has occurred in the target process, the electronic device 100 generates the first thread leak log.

[0084] The first thread leak log stores information about the threads already started by the target process. See also... Figure 3 The operation of generating the first thread leak log by electronic device 100 is performed at the Native layer.

[0085] Step 402: Electronic device 100 obtains the second thread leak log stored in the bridge node.

[0086] The second thread leak log records the call stack of leaking threads in the target process. As mentioned above, leaking threads include newly started threads in the target process after the first number reaches a preset first threshold, where the first number is the number of threads already started in the target process. The size of the first threshold can be set by the implementation based on system performance and / or implementation requirements; this embodiment does not limit the size of the first threshold. For example, assuming the first threshold is 10, then after the first number of threads already started in the target process reaches 10, newly started threads in the target process, such as the eleventh thread, the twelfth thread, and so on, can be called leaking threads.

[0087] See Figure 3 The aforementioned second thread leak log is generated at the Java layer and stored at the bridge node by the Java layer. The operation of the electronic device 100 to obtain the second thread leak log is performed at the Native layer. Thus, after determining that a thread leak has occurred in the target process, the electronic device 100 can obtain not only the first thread leak log generated at the Native layer, but also the second thread leak log generated at the Java layer from the bridge node. This allows it to aggregate the first thread leak log recording thread information and the second thread leak log recording the call stack of the leaking thread into a single log, improving the ability to detect thread leaks and locate the problematic thread.

[0088] Step 403: Electronic device 100 aggregates the first thread leak log and the second thread leak log into a third thread leak log.

[0089] In this embodiment, after aggregating the first thread leak log and the second thread leak log into the third thread leak log, the electronic device 100 can temporarily store the third thread leak log at the location " / data / log / diagnosis_temp / ".

[0090] In some examples, after step 403, if the application corresponding to the target process has registered for log sharing, the electronic device 100 can share the aforementioned third-thread leak log with the application. This allows the third-thread leak log to be shared with third-party applications. Furthermore, the third-thread leak log includes not only information about threads already started in the target process but also the call stack of the leaking thread, making it easier for third-party applications to locate the problematic thread. Specifically, the path for sharing the third-thread leak log with third-party applications can be configured as "data / user / 0 / application package name / files".

[0091] In some examples, after step 403, the electronic device 100 can locate the problematic thread in the target process based on the aforementioned third-thread leak log. Since the third-thread leak log includes not only information about threads already started in the target process but also the call stack of the leaking thread, it improves the ability to detect thread leaks and locate the problematic thread. In other words, in this embodiment, after obtaining the third-thread leak log, regardless of whether the log is shared with a third-party application, it can be analyzed to locate the problematic thread.

[0092] In the above-mentioned thread leak detection method, after the electronic device 100 determines that a thread leak has occurred in the target process, it generates a first thread leak log, then obtains the second thread leak log stored in the bridge node, and aggregates the first thread leak log and the second thread leak log into a third thread leak log. This enables the aggregation of the first thread leak log that records thread information and the second thread leak log that records the call stack of the leaking thread into one log after a thread leak occurs in the target process, thereby improving the ability to detect thread leaks and locate the problematic thread.

[0093] Figure 5 A flowchart of a thread leak detection method provided in another embodiment of this application is shown below. Figure 5 As shown, this application Figure 4 In the illustrated embodiment, before step 402, the following may also be included:

[0094] Step 501: Electronic device 100 obtains the first number of threads that have been started in the target process.

[0095] Step 502: After the first quantity reaches a preset first threshold, the electronic device 100 obtains the call stack of the leaked thread in the target process;

[0096] Step 502: Electronic device 100 generates a second thread leak log based on the call stack of the leaked thread.

[0097] Step 503, electronic device 100 stores the second thread leak log in the bridge node.

[0098] See Figure 3 Steps 501 to 503 are executed by the electronic device 100 in the Java layer, which enables the call stack of the leaked thread obtained by the Java layer to be stored in the bridge node. In this way, the Native layer can obtain the aforementioned second thread leak log from the bridge node.

[0099] In a specific implementation, step 502 can be: the electronic device 100 obtains the number of calls for each call stack according to the call stack of the aforementioned leaked thread; and sorts the call stack according to the number of calls to generate a second thread leak log.

[0100] In some examples, after the first number of threads started by the target process reaches the first threshold, the Java layer can record the thread name, call stack, and call count of the first leaked thread started by the target process, as shown in Table 2.

[0101] Table 2

[0102] Thread name Call stack The number of calls on the call stack a1 Call stack 1 1

[0103] Next, for the second leaking thread started by the target process, the Java layer can also record the thread name, call stack, and call count of the second leaking thread. Here, it is assumed that the call stack of the second leaking thread is the same as that of the first leaking thread. Then, there is no need to record the call stack of the second leaking thread again. Just increment the call count of the call stack of the first leaking thread by 1, as shown in Table 3.

[0104] Table 3

[0105]

[0106] Next, for the third leaking thread started by the target process, the Java layer continues to record the thread name, call stack, and number of calls of the third leaking thread. The call stack of the third leaking thread is different from the call stack of the first two leaking threads. Therefore, the records are shown in Table 4.

[0107] Table 4

[0108]

[0109] Similarly, for each leaking thread started by the target process, the Java layer records the thread name, call stack, and the number of calls made on the call stack, forming a data structure similar to Table 4.

[0110] After recording the thread name, call stack, and call count of the leaking thread, the Java layer can also sort the call stack in descending order based on the call count of each call stack.

[0111] Referring to Table 4, call stack 1 has 2 call counts, while call stack 2 has 1 call count. Therefore, call stack 1 should be placed before call stack 2. Alternatively, if at least two call stacks have the same number of calls, they can be sorted according to the string corresponding to the call stack. For example, the string corresponding to the call stack can be hashed, and then the call stack can be sorted based on the resulting hash value.

[0112] After sorting, the Java layer can further compress the sorted call stack to generate a second thread leak log. The Java layer then stores this second thread leak log in the kernel layer's bridge node. Compressing the sorted call stack reduces the size of the second thread leak log, saving storage space in the kernel layer's bridge node.

[0113] Since the call stack stored in the second thread leak log is compressed, in step 403 of this embodiment, the electronic device 100 can aggregate the first thread leak log and the second thread leak log into a third thread leak log as follows: The electronic device 100 first decompresses the call stack stored in the second thread leak log, then stores the decompressed call stack and the thread information recorded in the first thread leak log into a log file, and then compresses the log file to generate the third thread leak log, thereby saving the memory space occupied by the third thread leak log.

[0114] In addition, in this embodiment, after step 402, the electronic device 100 can also delete the second thread leak log stored in the bridge node, thereby saving storage space in the bridge node. That is, the bridge node can be configured to store the second thread leak log of the Java layer only once, and delete it after it is retrieved from the bridge node by the Native layer.

[0115] Figure 6 A flowchart of a thread leak detection method provided in another embodiment of this application is shown below. Figure 6 As shown, this application Figure 4 In the illustrated embodiment, step 401 may include:

[0116] Step 601: Electronic device 100 obtains the first number of threads that have been started in the target process.

[0117] Step 602: If the first quantity is greater than or equal to a preset second threshold, the electronic device 100 determines that a thread leak has occurred in the target process.

[0118] Wherein, the second threshold is greater than or equal to the first threshold. The size of the second threshold can be set by the system based on system performance and / or implementation requirements during specific implementation. In this embodiment, the size of the second threshold is not limited, as long as the second threshold is greater than or equal to the first threshold.

[0119] Step 603: Electronic device 100 obtains information about the threads that have been started in the target process.

[0120] Step 604: Electronic device 100 generates a first thread leak log based on the information of the aforementioned thread.

[0121] Specifically, the electronic device 100 may generate a first thread leak log based on the aforementioned thread information by sorting the information of the threads that the target process has started in order to generate the first thread leak log.

[0122] In some examples, the information about the aforementioned threads may include the thread name of the aforementioned thread; thus, sorting the information about the threads that have been started by the target process can be as follows: the electronic device 100 obtains a second number of threads corresponding to each thread name based on the thread name, and sorts the information about the threads corresponding to each thread name based on the second number. Specifically, the electronic device 100 can sort the information about the threads corresponding to each thread name in descending order based on the second number.

[0123] After sorting the thread information corresponding to each thread name according to the second quantity, if at least two thread names have the same number of threads among the threads already started in the target process, the thread information corresponding to the at least two thread names can be sorted according to the at least two thread names. Specifically, the electronic device 100 can sort the thread information corresponding to the at least two thread names in ascending order according to the letters contained in the strings corresponding to the at least two thread names.

[0124] In some examples, the information of the aforementioned thread may also include: the runtime of the aforementioned thread; thus, after sorting the information of the thread corresponding to each thread name according to the second quantity, the electronic device 100 may also sort the information of the thread corresponding to the first thread name according to the runtime; wherein, the first thread name may be a thread name whose corresponding thread count is greater than 1 among the thread names of the threads already started in the target process.

[0125] The runtime of the aforementioned thread is the CPU usage time of the thread. The shorter the runtime of the aforementioned thread, the greater the possibility that the thread will cause a thread leak in the target process, thereby improving the ability to locate the problematic thread.

[0126] In terms of specific implementation, consulting the kernel documentation "kernel_platform / msm-kernel / Documentation / filesystems / proc.rst" reveals that in the Android system, " / proc / <pid> / task / <tid>The " / stat" file records all thread runtime information, where "utime" represents the thread's runtime. In this embodiment, adding the thread's runtime to the thread information recorded in the first thread leak log can improve the ability to locate the problematic thread.

[0127] In some examples, the information of the aforementioned thread may also include: the creation time of the aforementioned thread; thus, after sorting the information of the thread corresponding to the first thread name according to the runtime, if at least two threads in the information of the thread corresponding to the first thread name have the same runtime, the electronic device 100 sorts the information of the aforementioned at least two threads according to the aforementioned creation time.

[0128] As described above, the thread information recorded in the first thread leak log can include the thread name, runtime, and creation time of the threads already started in the target process. Of course, the thread information recorded in the first thread leak log can also include more information, such as thread identifiers, etc., but this embodiment does not limit this. Since multiple threads can be started in the target process, each thread has its own thread name, but different threads may have the same thread name. Therefore, the electronic device 100 can first count the second number of threads corresponding to each thread name, and then sort the thread information based on the second number. Then, if at least two thread names have the same number of threads, the thread information can be sorted based on these at least two thread names. Finally, for the threads corresponding to the first thread name, they can be further sorted based on the thread's runtime and creation time.

[0129] For example, assuming there are 5 threads corresponding to thread name "d1", 5 threads corresponding to thread name "e2", and 3 threads corresponding to thread name "f1", then in the first thread leak log, the information for the thread corresponding to "d1" will be listed first, followed by the information for the thread corresponding to "e2", and finally the information for the thread corresponding to "f1". Furthermore, for the 5 threads corresponding to "d1", their information can be sorted according to their runtime. If two or more threads have the same runtime, the electronic device 100 can sort the information of these two or more threads based on their creation time. Similarly, the 5 threads corresponding to "e2" and the 3 threads corresponding to "f1" can be sorted in the same way, which will not be elaborated further here.

[0130] Figure 7 A flowchart of a thread leak detection method provided in another embodiment of this application is shown below. Figure 7 As shown, the above-mentioned thread leak detection methods may include:

[0131] Step 701: Electronic device 100 obtains the first number of threads that have been started in the target process.

[0132] Step 702: After the first quantity reaches a preset first threshold, the electronic device 100 obtains the call stack of the leaked thread in the target process.

[0133] Step 703: Electronic device 100 generates a second thread leak log based on the call stack of the leaked thread.

[0134] In step 704, electronic device 100 stores the second thread leak log in the bridge node.

[0135] The operations in steps 701 to 704 are executed by the electronic device 100 at the Java layer.

[0136] Step 705: Electronic device 100 obtains the first number of threads that have been started in the target process.

[0137] Step 706: If the first quantity is greater than or equal to a preset second threshold, the electronic device 100 determines that a thread leak has occurred in the target process.

[0138] Step 707: Electronic device 100 obtains information about the threads that have been started in the target process.

[0139] Step 708: Electronic device 100 generates a first thread leak log based on the information of the aforementioned thread.

[0140] Step 709: Electronic device 100 obtains the second thread leak log stored in the bridge node.

[0141] In step 710, the electronic device 100 aggregates the first thread leak log and the second thread leak log into a third thread leak log. Then, it executes either step 711 or step 712.

[0142] Step 711: If the application corresponding to the target process has registered for log sharing, the electronic device 100 will share the aforementioned third-thread leaked logs with the aforementioned application.

[0143] Step 712: Based on the aforementioned third thread leak log, electronic device 100 locates the thread in the target process where the problem occurred.

[0144] Among them, the operations of steps 705 to 712 are executed by the electronic device 100 at the Native layer. Steps 705 to 708 and steps 701 to 704 can be executed in parallel or sequentially. In this embodiment, there is no limitation on the execution order of steps 705 to 708 and steps 701 to 704.

[0145] Additionally, after step 709, the following may also be included:

[0146] Step 713: Electronic device 100 deletes the second thread leak log stored in the aforementioned bridge node.

[0147] The operation in step 713 is performed by the electronic device 100 in the bridge node of the kernel layer.

[0148] It is understood that some or all of the steps or operations in the above embodiments are merely examples, and other operations or variations thereof can be performed in the embodiments of this application. Furthermore, the steps may be performed in different orders as presented in the above embodiments, and it is not necessary to perform all the operations in the above embodiments.

[0149] This application also provides a computer-readable storage medium storing a computer program that, when run on a computer, causes the computer to execute this application. Figures 4-7 The method provided in the illustrated embodiment.

[0150] This application also provides a computer program product, which includes a computer program that, when run on a computer, causes the computer to execute this application. Figures 4-7 The method provided in the illustrated embodiment.

[0151] In this embodiment, "at least one" refers to one or more, and "more than one" refers to two or more. "And / or" describes the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent the existence of A alone, A and B simultaneously, or B alone. A and B can be singular or plural. The character " / " generally indicates that the preceding and following related objects have an "or" relationship. "At least one of the following" and similar expressions refer to any combination of these items, including any combination of single or plural items. For example, at least one of a, b, and c can represent: a, b, c, a and b, a and c, b and c, or a and b and c, where a, b, and c can be single or multiple.

[0152] Those skilled in the art will recognize that the units and algorithm steps described in the embodiments disclosed herein can be implemented using electronic hardware, computer software, or a combination of electronic hardware and software. Whether these functions are implemented in hardware or software 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 this application.

[0153] Those skilled in the art will understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.

[0154] In the several embodiments provided in this application, any function, if implemented as a software functional unit and sold or used as an independent product, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, essentially, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0155] The above description is merely a specific embodiment of this application. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the protection scope of this application. The protection scope of this application should be determined by the protection scope of the claims.< / tid> < / pid>

Claims

1. A method for detecting thread leaks, characterized in that, Applied to electronic devices, the electronic devices include a Java layer, a native layer, and a kernel layer, wherein the kernel layer includes a bridge node, and the thread leak detection method includes: After determining that a thread leak has occurred in the target process, the local layer generates a first thread leak log; wherein, the first thread leak log stores information about the threads that have been started by the target process; The local layer obtains the second thread leakage log stored in the bridge node; wherein, the second thread leakage log records the call stack of the leaking thread in the target process, the second thread leakage log is generated and saved to the bridge node by the Java layer, and the call stack of the leaking thread is obtained by the Java layer; the leaking thread includes the newly started thread in the target process after the first number reaches a preset first threshold, the first number being the number of threads already started in the target process; The local layer aggregates the first thread leak log and the second thread leak log into a third thread leak log; The local layer, after aggregating the first thread leak log and the second thread leak log into a third thread leak log, further includes: the local layer locating the thread in the target process where the problem occurred based on the third thread leak log; if the application corresponding to the target process has registered for log sharing, then the third thread leak log is shared with the application. Before the local layer obtains the second thread leak log stored in the bridge node, the process further includes: The Java layer obtains the first number of threads that have been started in the target process; After the first quantity reaches a preset first threshold, the Java layer obtains the call stack of the leaked thread in the target process; The Java layer generates the second thread leak log based on the call stack of the leaking thread; The Java layer stores the second thread leak log in the bridge node of the kernel layer; The Java layer generates the second thread leak log based on the call stack of the leaking thread, including: The Java layer obtains the number of calls for each call stack based on the call stack of the leaked thread; The Java layer sorts the call stack according to the number of calls to generate the second thread leak log; The local layer generates the first thread leak log, which includes: The local layer obtains information about the threads that have been started by the target process; The local layer sorts the information of the threads that the target process has started in order to generate a first thread leak log; The information of the thread includes: the thread name of the thread; The sorting of information on the threads already started by the target process includes: Based on the thread name, obtain the second number of threads corresponding to each thread name; Based on the second quantity, sort the information of the thread corresponding to each thread name; After sorting the information of the thread corresponding to each thread name according to the second quantity, the process further includes: If at least two thread names have the same number of threads among the threads that have been started in the target process, then the information of the threads corresponding to the at least two thread names is sorted according to the at least two thread names. The thread information further includes: the thread's runtime; after sorting the thread information corresponding to each thread name according to the second quantity, it also includes: Based on runtime, the information of the thread corresponding to the first thread name is sorted; wherein, the first thread name includes the thread name whose corresponding thread count is greater than 1 among the thread names of the threads already started in the target process; The thread information further includes: the thread's creation time; after sorting the thread information corresponding to the first thread name according to runtime, it also includes: If at least two threads have the same runtime in the information of the thread corresponding to the first thread name, then the information of the at least two threads is sorted according to the creation time.

2. The method according to claim 1, characterized in that, After the local layer obtains the second thread leak log stored in the bridge node, it also includes: The bridge node deletes the second thread leak log stored in the bridge node.

3. The method according to claim 1, characterized in that, The local layer determines that the target process has a thread leak by including: The local layer obtains the first number of threads that the target process has started; If the first quantity is greater than or equal to a preset second threshold, the local layer determines that the target process has experienced a thread leak; wherein the second threshold is greater than or equal to the first threshold.

4. An electronic device, characterized in that, include: One or more processors; Memory; Multiple applications; And one or more computer programs, wherein the one or more computer programs are stored in the memory, the one or more computer programs including instructions that, when executed by the processor, cause the electronic device to perform the method as described in any one of claims 1-3.

5. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when run on a computer, causes the computer to perform the method as described in any one of claims 1-3.