Method for garbage collection and electronic device
By detecting the heap data compression ratio and CPU load, and delaying GC execution, the problem of high resource consumption in Java Virtual Machine garbage collection in low memory scenarios is solved, thus improving the smoothness and stability of the system.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-04-19
- Publication Date
- 2026-03-17
AI Technical Summary
The existing Java Virtual Machine garbage collection process consumes a lot of CPU resources in low-memory scenarios, affecting system smoothness and stability, and easily leading to system crashes.
By detecting the heap data compression ratio and CPU load of the process, garbage collection is delayed, and different delay strategies are adopted, including raising the first threshold and performing lightweight or full GC, to reduce the number of GCs and resource consumption.
It reduces system resource consumption, improves system smoothness and stability, and prevents system crashes.
Smart Images

Figure CN114968836B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of operating systems, and more particularly to methods and electronic devices for garbage collection. Background Technology
[0002] Electronic devices can install and use various applications (apps) or services written in languages such as Java. Due to Java's cross-platform compatibility and openness, to ensure compatibility across multiple platforms, electronic devices running Java-based apps need to create a Java Virtual Machine (JVM). The JVM contains Java's garbage collection (GC) thread. When the heap memory used by the app or service is insufficient, the JVM will start the GC thread to perform garbage collection (GC). During the execution of the GC thread, it scans the heap areas allocated by the JVM to determine whether the data in those areas is referenced. This extensive scanning work consumes significant CPU resources.
[0003] Furthermore, when electronic devices are in low-memory environments, the kernel compresses heap data according to the Least Recently Used (LRU) algorithm. Then, for the compressed heap data, the GC thread needs to decompress it before determining if the decompressed data is still referenced. During decompression, the GC thread also needs to request additional memory from the system to store the data generated during the decompression process. Understandably, the system's memory is already strained at this time, and the memory allocation during decompression further exacerbates the memory shortage. Moreover, the GC thread's decompression behavior also prolongs the overall memory reclamation time and increases CPU resource consumption. Therefore, the existing GC process can affect the smoothness and stability of system operation, potentially leading to system crashes. Summary of the Invention
[0004] The garbage collection method and electronic device provided in this application can reduce the system resources (including memory resources and CPU resources) occupied by the electronic device when performing GC, which helps to ensure the smoothness and stability of the system.
[0005] To achieve the above objectives, the embodiments of this application provide the following technical solutions:
[0006] Firstly, this application discloses a garbage collection (GC) method, comprising: an electronic device running an application (APP); when the APP's process requests allocation of first memory, the electronic device detects whether the value of the heap memory expected to be occupied by the process divided by the target utilization rate is equal to or greater than a first threshold, wherein the heap memory expected to be occupied by the process includes the heap memory already occupied by the process and the first memory; when the value of the heap memory expected to be occupied by the process divided by the target utilization rate is equal to or greater than the first threshold, the electronic device detects whether the heap area data compression ratio corresponding to the process is equal to or greater than a second threshold; when the heap area data compression ratio corresponding to the process is equal to or greater than the second threshold, and the current CPU load of the electronic device is greater than a third threshold, the electronic device delays the process from performing garbage collection.
[0007] Understandably, when electronic devices are in low-memory environments, the kernel compresses heap data according to a certain strategy (e.g., LRU). When the electronic device performs garbage collection (GC), it may need to decompress the compressed heap data. This decompression process consumes system memory, exacerbating memory pressure, and also consumes system CPU resources. Therefore, performing GC under high CPU load (i.e., the electronic device's current CPU load exceeds the third threshold) can easily affect system smoothness and stability, and may even cause system crashes. To address this, the electronic device calculates the heap data compression ratio for each process and then uses different delay strategies based on the compression ratio and CPU load to delay GC execution, thereby improving system smoothness and stability and preventing system crashes.
[0008] In one possible implementation, when the heap data compression ratio corresponding to the process is equal to or greater than the second threshold, and the current CPU load of the electronic device is greater than the third threshold, the electronic device delays the execution of garbage collection for the process, including: when the heap data compression ratio corresponding to the process is equal to or greater than the second threshold, and the current CPU load of the electronic device is greater than the third threshold, the electronic device raises the first threshold.
[0009] Understandably, raising the first threshold means raising the upper limit of heap memory usage for this process. In some scenarios, raising the first threshold can ensure that the expected heap memory usage of this process divided by the target utilization is less than the raised first threshold, thus allowing for the allocation of the first memory segment. In this scenario, the electronic device reduces the number of times it performs lightweight garbage collection (GC) by raising the first threshold.
[0010] In other scenarios, even after raising the first threshold, the estimated heap memory usage of this process divided by the target utilization rate may still be equal to or greater than the raised first threshold. For example, other tasks within this process may also request heap memory allocation during the same time period, causing the estimated heap memory usage of this process divided by the target utilization rate to still be equal to or greater than the raised first threshold. Therefore, the initial memory allocation cannot be completed, and lightweight garbage collection (GC) still needs to be performed to reclaim more memory. It is evident that in this scenario, the electronic device also delays the execution of lightweight GC by raising the first threshold, avoiding execution when the CPU load is too heavy, thus contributing to system smoothness and stability.
[0011] In one possible implementation, after the electronic device raises the first threshold, the method further includes: the electronic device detecting whether the value of the heap memory expected to be occupied by the process divided by the target utilization rate is equal to or greater than the raised first threshold; when the value of the heap memory expected to be occupied by the process divided by the target utilization rate is equal to or greater than the raised first threshold, the electronic device performs lightweight GC.
[0012] Understandably, compared to a full-scale garbage collection (GC), lightweight GC on electronic devices consumes fewer memory and CPU resources. Since lightweight GC does not scan all heap data, the amount of compressed data scanned is also less. Therefore, the additional memory and CPU resources consumed by the compaction process on electronic devices are not significant, which helps ensure system smoothness and stability.
[0013] In one possible implementation, after the electronic device performs lightweight GC, the method further includes: the electronic device detecting whether the value of the heap memory expected to be occupied by the process divided by the target utilization is equal to or greater than the increased first threshold; when the value of the heap memory expected to be occupied by the process divided by the target utilization is equal to or greater than the increased first threshold, the electronic device further increases the first threshold.
[0014] In other words, after a lightweight garbage collection (GC) is performed on an electronic device, if the current memory situation still cannot meet the demand—that is, the ratio of the expected heap memory usage of this process to the target utilization rate is still equal to or greater than the first threshold—then the electronic device raises the first threshold again. By raising the first threshold again, the number of times the electronic device performs a full GC is reduced, or the timing of a full GC is extended.
[0015] In one possible implementation, after the electronic device raises the first threshold again, the method further includes: when the value of the heap memory that the process is expected to occupy divided by the target utilization rate is equal to or greater than the first threshold after the second increase, the electronic device checks the heap area data compression ratio corresponding to the process again; the electronic device adopts different strategies according to the heap area data compression ratio corresponding to the process and delays the execution of full-scale garbage collection (GC).
[0016] In other words, this application proposes a method for detecting the heap data compression ratio, enabling the GC thread of the electronic device kernel to perceive the compression level of the heap data (i.e., the heap data compression ratio), thereby facilitating efficient GC management based on the compression level of the heap data. When the heap data compression level varies, different strategies are employed based on the current CPU load of the electronic device to reduce the number of GC (lightweight GC and full GC) executions or delay the timing of GC (lightweight GC and full GC) executions, avoiding GC execution under high CPU load. Therefore, this application reduces the system resources (including memory and CPU resources) occupied by the electronic device during GC execution, which helps ensure system smoothness and stability.
[0017] In one possible implementation, the electronic device employs different strategies based on the heap data compression ratio of the process to delay the execution of full-scale garbage collection (GC). These strategies include: when the heap data compression ratio of the process is in the first interval, the electronic device avoids peak CPU load periods to perform full-scale GC; when the heap data compression ratio of the process is in the second interval, the electronic device performs full-scale GC when the CPU is idle; and when the heap data compression ratio of the process is in the third interval, the electronic device freezes the process and, upon receiving an instruction to unfreeze the process, unfreezes the process and performs full-scale GC.
[0018] In one possible implementation, after the electronic device raises the first threshold, the method further includes: when the value of the heap memory expected to be occupied by the process divided by the target utilization rate is equal to or greater than the raised first threshold, the electronic device re-detects the heap area data compression ratio corresponding to the process; the electronic device adopts different strategies according to the heap area data compression ratio corresponding to the process to delay the execution of full-scale garbage collection (GC).
[0019] In one possible implementation, the electronic device employs different strategies based on the heap data compression ratio of the process to delay the execution of full-scale garbage collection (GC). These strategies include: when the heap data compression ratio of the process is in the first interval, the electronic device avoids peak CPU load periods to perform full-scale GC; when the heap data compression ratio of the process is in the second interval, the electronic device performs full-scale GC when the CPU is idle; and when the heap data compression ratio of the process is in the third interval, the electronic device freezes the process and, upon receiving an instruction to unfreeze the process, unfreezes the process and performs full-scale GC.
[0020] In one possible implementation, the method further includes: when a process requests heap memory, the electronic device marks the heap memory requested by the process.
[0021] In one possible implementation, the method further includes: during the compression process of the electronic device, the electronic device records the amount of data in the compressed heap area.
[0022] In one possible implementation, the process's heap data compression ratio is the ratio of the amount of compressed heap data in the process to the total amount of heap data in the process.
[0023] In a second aspect, an electronic device is provided, comprising: a processor, a memory, and a touchscreen, wherein the memory and the touchscreen are coupled to the processor, the memory being used to store computer program code, the computer program code including computer instructions, wherein when the processor reads the computer instructions from the memory, the electronic device performs the method as described in the foregoing aspects and any possible implementation thereof.
[0024] Thirdly, an apparatus is provided, included in an electronic device, having the function of implementing the behavior of the electronic device in any of the above aspects and possible implementations. This function can be implemented in hardware or by hardware executing corresponding software. The hardware or software includes at least one module or unit corresponding to the above function. For example, a detection module or unit, a determination module or unit, and a processing module or unit, etc.
[0025] Fourthly, a computer-readable storage medium is provided, including computer instructions that, when executed on an electronic device, cause the electronic device to perform the methods described in the foregoing aspects and any possible implementation thereof.
[0026] Fifthly, a computer program product is provided that, when run on a computer, causes the computer to perform the methods described in the foregoing aspects and any of their possible implementations.
[0027] A sixth aspect provides a chip system including a processor, which, when executing instructions, performs the methods described in the foregoing aspects and any possible implementation thereof.
[0028] The technical effects achievable by the electronic device provided in the second aspect, the apparatus provided in the third aspect, the computer-readable storage medium provided in the fourth aspect, the computer program product provided in the fifth aspect, and the chip system provided in the sixth aspect are described in the first aspect and any possible implementation thereof, and will not be repeated here. Attached Figure Description
[0029] Figure 1 A schematic diagram of the hardware structure of an electronic device provided in an embodiment of this application;
[0030] Figure 2 A schematic diagram of the software structure of an electronic device provided in an embodiment of this application;
[0031] Figure 3 This is a schematic diagram illustrating the process of an electronic device performing garbage collection (GC) according to an embodiment of this application.
[0032] Figure 4 A schematic diagram illustrating the process of performing GC on another electronic device provided in an embodiment of this application;
[0033] Figure 5 This is a schematic diagram of a chip system provided in an embodiment of this application. Detailed Implementation
[0034] In the description of the embodiments of this application, unless otherwise stated, " / " means "or". For example, A / B can mean A or B. The "and / or" in this document is merely a description of the linkage relationship of related objects, indicating that there can be three relationships. For example, A and / or B can mean: A exists alone, A and B exist simultaneously, and B exists alone.
[0035] Hereinafter, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of indicated technical features. Therefore, a feature defined as "first" or "second" may explicitly or implicitly include one or more of that feature. In the description of embodiments of this application, unless otherwise stated, "a plurality of" means two or more.
[0036] In the embodiments of this application, the terms "exemplary" or "for example" are used to indicate that something is an example, illustration, or description. Any embodiment or design that is described as "exemplary" or "for example" in the embodiments of this application should not be construed as being more preferred or advantageous than other embodiments or design. Specifically, the use of the terms "exemplary" or "for example" is intended to present the relevant concepts in a specific manner.
[0037] This application provides a garbage collection (GC) method applicable to electronic devices with one or more apps installed. Exemplary examples of the electronic devices in this application include mobile phones, tablets, personal computers (PCs), personal digital assistants (PDAs), smartwatches, netbooks, wearable electronic devices, augmented reality (AR) devices, virtual reality (VR) devices, in-vehicle devices, smart screens, smart cars, smart speakers, robots, etc. This application does not impose any special limitations on the specific form of the electronic device.
[0038] Figure 1 A schematic diagram of the structure of the electronic device 100 is shown.
[0039] Electronic device 100 may include processor 110, external memory interface 120, internal memory 121, universal serial bus (USB) interface 130, charging management module 140, power management module 141, battery 142, antenna 1, antenna 2, mobile communication module 150, wireless communication module 160, audio module 170, speaker 170A, receiver 170B, microphone 170C, headphone jack 170D, sensor module 180, button 190, motor 191, indicator 192, camera 193, display screen 194, and 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 the present invention 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 inter-integrated circuit (I2C) interfaces, inter-integrated circuit sound (I2S) interfaces, pulse code modulation (PCM) interfaces, universal asynchronous receiver / transmitter (UART) interfaces, mobile industry processor interfaces (MIPI), general-purpose input / output (GPIO) interfaces, subscriber identity module (SIM) interfaces, and / or universal serial bus (USB) interfaces, etc. It is understood that the interface connection relationships between the modules illustrated in the embodiments of the present invention 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.
[0045] 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 the 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 via the power management module 141.
[0046] 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.
[0047] 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.
[0048] 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.
[0049] 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.
[0050] 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.
[0051] 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.
[0052] 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.
[0053] 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.
[0054] Electronic device 100 can perform shooting functions through ISP, camera 193, video codec, GPU, display 194 and application processor.
[0055] The ISP is used to process data fed back by the camera 193. The camera 193 is used to capture still images or videos. The digital signal processor (DSP) is used to process digital signals, including digital image signals and other digital signals. For example, when the electronic device 100 selects a frequency point, the DSP performs a Fourier transform on the frequency energy. The video codec is used to compress or decompress digital video. The electronic device 100 can support one or more video codecs. Thus, the electronic device 100 can play or record videos in various encoding formats, such as Moving Picture Experts Group (MPEG) 1, MPEG 2, MPEG 3, MPEG 4, etc.
[0056] 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.
[0057] 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.
[0058] 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.
[0059] 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.
[0060] The software system of electronic device 100 can adopt a layered architecture, event-driven architecture, microkernel architecture, microservice architecture, or cloud architecture. For example, the software system of electronic device 100 can be HarmonyOS, Android, etc. This embodiment of the invention uses the layered architecture Android system as an example to illustrate the software structure of electronic device 100.
[0061] Figure 2 This is a software structure block diagram of the electronic device 100 according to an embodiment of the present invention.
[0062] 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 five layers, from top to bottom: the application layer (application layer), the application framework layer (framework layer), the Android runtime and system libraries, the Hardware Abstraction Layer (HAL), and the kernel layer.
[0063] (1) Application layer
[0064] The application layer can include a series of application packages. For example... Figure 2 As shown, the application package may include applications such as camera, gallery, calendar, call, map, navigation, WLAN, Bluetooth, music, video, and SMS.
[0065] (2) Framework layer
[0066] The framework layer provides application programming interfaces (APIs) and programming frameworks for applications in the application layer. The framework layer includes some predefined functions.
[0067] like Figure 2 As shown, the framework layer may include an activity manager, window manager, content provider, view system, phone manager, resource manager, notification manager, etc.
[0068] The application includes several management components: Activity Manager, Window Manager, and Resource Manager. Activity Manager is responsible for launching all application processes, starting the four main components (activity, service, broadcast receiver, and content provider), switching between applications and between interfaces within an application, and managing the lifecycle of various components. Window Manager manages window applications. It can obtain screen size, determine the presence of a status bar, lock the screen, and capture screenshots. Resource Manager stores and retrieves data, making it accessible to applications. This data can include videos, images, audio, made and received calls, browsing history and bookmarks, and phone books. View System includes visual controls, such as controls for displaying text and controls for displaying images. View System is used to build applications. The display interface can consist of one or more views. For example, a display interface including a text notification icon can include views for displaying text and views for displaying images. Phone Manager provides communication functionality for the electronic device 100, such as managing call status (including connected and disconnected). Resource Manager provides various resources for the application, such as localized strings, icons, images, layout files, video files, etc. The notification manager allows applications to display notifications in the status bar. These notifications can be used to deliver informational messages and can disappear automatically after a short pause, requiring no user interaction. For example, the notification manager can be used to notify users of download completion or message alerts. The notification manager can also display notifications as icons or scrolling text in the top status bar, such as notifications from background applications, or as dialog boxes on the screen. Examples include displaying text messages in the status bar, emitting sounds, vibrating electronic devices, and flashing indicator lights.
[0069] (3) Android Runtime and system libraries
[0070] like Figure 2 As shown, the Android Runtime includes core libraries and a virtual machine. The Android runtime is responsible for the scheduling and management of the Android system.
[0071] The core library consists of two parts: one part is the functionalities that need to be called by the Java language, and the other part is the Android core library.
[0072] The application layer and framework layer run in a virtual machine. The virtual machine executes the Java files of the application layer and framework layer as binary files. The virtual machine is used to perform functions such as object lifecycle management, stack management, thread management, security and exception management, and garbage collection.
[0073] Let's take electronic device 100 running APP1 as an example. When electronic device 100 runs APP1, it typically starts one or more processes of APP1 (such as process 1 corresponding to the APP1 interface, process 2 corresponding to the APP1 background service). Each process corresponds to a virtual machine, and each process contains a GC thread. This GC thread is responsible for monitoring the usage of the virtual machine heap memory corresponding to its process and determining whether to start the GC process based on the usage of the virtual machine heap memory corresponding to its process. It is understandable that in some scenarios, when the user switches APP1 to run in the background, the electronic device may still continue to run one or more background service processes corresponding to APP1. These processes continue to occupy the memory of the electronic device, and these processes may still continue to request new heap memory, etc.
[0074] Typically, when APP1 starts its process, it sets a maximum heap memory usage limit for that process (denoted as threshold 1), which is the maximum heap memory threshold that the process can use. When the value of the heap memory allocated by the kernel to the process divided by the target heap memory utilization rate is greater than or equal to threshold 1, the process will start a GC thread to reclaim the process's heap memory. It is understood that when there is a suitable ratio between the heap memory already used (or occupied) and the maximum heap memory that the process can use (including the heap memory already used by the process and the free heap memory) after each GC (i.e., the target heap memory utilization rate, usually 0.75, which can be simply referred to as the target utilization rate), the number of GC cycles can be minimized. This target heap memory utilization rate can be a default value, a uniform setting by the operating system for all processes, or a setting by the operating system for different apps or different processes; this embodiment does not limit this.
[0075] In some technical solutions, the GC performed by the GC thread can include lightweight GC (such as Background YoungGC) and full GC (Full GC). When scanning heap data, lightweight GC only scans the data in the heap memory allocated by the current process (hereinafter referred to as heap data) within the time period from the last GC execution to the current GC trigger. Full GC, on the other hand, scans all heap data allocated by the current process. Figure 3 The diagram shown illustrates the garbage collection (GC) process performed by an electronic device, specifically including:
[0076] 301. A memory allocation request from a process has been received.
[0077] 302. Determine the value of dividing the expected heap memory usage of this process by the target utilization rate. Figure 3Check if the value (represented by "estimated heap memory usage / target utilization" for this process) is less than threshold 1. If it is equal to or greater than threshold 1, proceed to step 303; otherwise, proceed to step 314.
[0078] Each time a process requests memory allocation, a check of the heap memory it is using is triggered. It's important to note that the heap memory the process expects to use in this step includes both the heap memory it has already used and the heap memory it is requesting this time. In other words, determining whether the value of the process's expected heap memory divided by the target utilization rate is less than a threshold of 1 is equivalent to determining whether, if the requested heap memory is allocated to the process, the heap memory it uses and the maximum heap memory it can use (including the heap memory it has already used and the free heap memory) can still maintain a utilization rate no higher than the target utilization rate. If it exceeds the target utilization rate, a lightweight garbage collection (GC) is triggered, i.e., step 303 is executed. If it does not exceed the target utilization rate, memory can be allocated, i.e., step 314 is executed.
[0079] 303. Triggering a lightweight GC.
[0080] The electronic device scans the heap memory newly requested by the process within a specific time period to determine if the data in the heap memory has been recently referenced. The electronic device can then reclaim the least recently referenced heap memory using a strategy such as LRU. This increases the amount of free heap memory available to the process. The specific time period refers to the period from the last garbage collection (GC) performed by the electronic device (including lightweight GC and full GC) to the current lightweight GC.
[0081] Understandably, lightweight GC scans less heap data, resulting in less CPU resource overhead.
[0082] 304. Attempt to allocate memory.
[0083] 305. Determine again whether the value of the expected heap memory usage of this process divided by the target utilization rate is less than the threshold 1. If it is equal to or greater than the threshold 1, proceed to step 306; if it is less than the threshold 1, proceed to step 314.
[0084] 306. Increase the threshold by 1.
[0085] Understandably, after performing a lightweight garbage collection (GC), this process reclaims some heap memory. If it still cannot complete the current memory allocation request at this point, the threshold is increased by 1. Increasing the threshold by 1 means raising the upper limit of heap memory usage for this process, which may then make it possible to complete the current memory allocation request.
[0086] 307. Try allocating memory again.
[0087] 308. Determine again whether the value of the expected heap memory usage of this process divided by the target utilization rate is less than the threshold 1. If it is equal to or greater than the threshold 1, proceed to step 309; if it is less than the threshold 1, proceed to step 314.
[0088] It should be noted that in some scenarios, multiple tasks may run concurrently within this process, and these tasks may request heap memory allocation simultaneously or sequentially. When this task executes its current memory allocation request, the upper limit of heap memory usage for this process (i.e., the maximum heap memory) is increased by raising the threshold by 1. However, other tasks may allocate the required heap memory in advance (i.e., the heap memory already occupied by this process increases), resulting in the value of the expected heap memory usage of this process (the memory requested by this task plus the heap memory already occupied by this process) divided by the target utilization rate still exceeding the threshold by 1. Therefore, this task's current memory allocation request cannot be completed.
[0089] 309. Triggered a full-scale GC.
[0090] 310. Lower the threshold by 1.
[0091] It should be noted that a full GC scans more heap data, which can release more heap memory and increase the amount of free heap memory. Therefore, after a full GC is performed, electronic devices can adjust the threshold 1 in a timely manner to ensure the utilization of heap memory.
[0092] 311. Try allocating memory again.
[0093] 312. Determine again whether the value of the expected heap memory usage of this process divided by the target utilization rate is less than the threshold 1. If it is equal to or greater than the threshold 1, proceed to step 313; if it is less than the threshold 1, proceed to step 314.
[0094] 313. Trigger the Out of Memory (OOM) mechanism.
[0095] When the OOM mechanism is triggered, the electronic device cleans up all processes that request heap memory, and this process ends.
[0096] 314. Allocate memory.
[0097] Therefore, in this technical solution, electronic devices can reduce the number of GC executions and minimize the CPU resources consumed by GC execution by performing GC in stages and combining this with the method of raising the threshold by 1.
[0098] System libraries can include multiple functional modules. For example: surface manager, media libraries, 3D graphics processing libraries (e.g., OpenGL ES), 2D graphics engines (e.g., SGL), etc.
[0099] The Surface Manager manages the display subsystem and provides fusion of 2D and 3D layers for multiple applications. The Media Library supports playback and recording of various common audio and video formats, as well as still image files. It supports multiple audio and video encoding formats, such as MPEG4, H.264, MP3, AAC, AMR, JPG, and PNG. The 3D Graphics Processing Library implements 3D graphics drawing, image rendering, compositing, and layer processing. The 2D Graphics Engine is the drawing engine for 2D graphics.
[0100] (4)HAL
[0101] HAL (Hardware Abstraction Layer) is used to abstract hardware. HAL hides the hardware interface details of a specific platform, providing the operating system with a virtual hardware platform that is hardware independent and portable across multiple platforms. In other words, any hardware-related operations required by the upper layer need to call HAL-related APIs, such as audio / video interfaces, GPS interfaces, voice interfaces, Wi-Fi interfaces, and others.
[0102] (5) Kernel layer
[0103] The kernel layer is the layer between hardware and software.
[0104] The kernel layer includes a memory management module, processes / threads, audio / video drivers, GPS drivers, display drivers, Wi-Fi drivers, etc. The memory management module is used to allocate and reclaim system memory, and compress heap data, among other things.
[0105] The technical solutions involved in the following embodiments can all be implemented in the electronic device 100 having the above-described hardware and software architecture.
[0106] Considering that when an electronic device is in a low-memory scenario (i.e., the available memory of the electronic device is below a threshold of 2), the kernel of the electronic device will compress the heap data according to a certain strategy (e.g., LRU). Therefore, when the electronic device performs garbage collection (GC), it may need to decompress the compressed heap data. The decompression process consumes system memory, exacerbating system memory pressure, and also consumes system CPU resources. It is evident that performing GC under high CPU load can easily affect system smoothness and stability, and may even cause system crashes. To address this, this application provides a GC method in which the electronic device calculates the compression ratio of the heap data for each process, and then, based on the compression ratio of the heap data and the CPU load, adopts different delay strategies to delay the execution of GC, thereby improving system smoothness and stability and avoiding system crashes.
[0107] like Figure 4 The diagram shown is a flowchart illustrating a GC method according to an embodiment of this application. The method includes:
[0108] 401. The APP process requests the allocation of the first memory.
[0109] For example, a task in the process of an APP requests the allocation of first memory. The first memory to be allocated can be memory of a specified size for the task or memory of the system default size. This application embodiment does not limit this.
[0110] 402. Determine the value of dividing the expected heap memory usage of this process by the target utilization rate. Figure 4 Check if the value (represented by the expected heap memory usage of this process / target utilization) is less than threshold 1. If it is equal to or greater than threshold 1, proceed to step 403; otherwise, proceed to step 417.
[0111] Each time an app process requests memory allocation, it triggers a check of the heap memory it is currently using. It's important to note that the heap memory the process is expected to use in this step includes both the heap memory it has already used and the heap memory requested this time. That is, the expected heap memory size = the actual heap memory size used by the process + the size of the first memory allocation. In other words, determining whether the expected heap memory divided by the target utilization rate is less than a threshold of 1 is equivalent to determining whether allocating the requested first memory to the process would maintain the ideal utilization rate (i.e., the target utilization rate). If the target utilization rate can be maintained (i.e., not higher than the target utilization rate), memory can be allocated, and step 417 is executed. If the ideal utilization rate cannot be maintained (i.e., higher than the target utilization rate), then different delayed GC strategies need to be adopted based on the heap data compression ratio and CPU load, i.e., step 403 and subsequent steps are executed.
[0112] 403. Check if the heap data compression ratio is equal to or greater than the threshold 3. If the heap data compression ratio is less than the threshold 3, trigger a lightweight GC, i.e., execute step 407. If the heap data compression ratio is equal to or greater than the threshold 3, further determine the current CPU load, i.e., execute step 404.
[0113] The heap data compression ratio refers to the ratio of the size of the compressed heap data in this process to the size of all heap data in this process (including compressed and uncompressed heap data).
[0114] It should be noted that when a process requests allocation of virtual machine heap memory, it calls a memory allocator (e.g., the mmap allocator) to map kernel-mode memory to user-mode memory and marks it as anonymous page memory. Anonymous page memory refers to memory dynamically allocated during the application's process execution that does not have a filename marker. In this embodiment, when a process calls a memory allocator (e.g., the mmap allocator) to allocate anonymous page memory, the anonymous page memory allocated by the process can be marked, making it easier for the kernel layer to identify that the anonymous page memory is heap memory, facilitating subsequent calculation of the heap data compression ratio. In a specific implementation, when a process requests allocation of virtual machine heap memory, for example, it marks the vma_name field in the anonymous page memory as the first value. When the vma_name field is the first value, it indicates that the anonymous page is heap memory. Thus, the kernel can identify the heap data based on the vma_name field. For example, the kernel can set a parameter Object1 to count the amount of heap data requested by the process (e.g., in pages).
[0115] Additionally, the kernel can set another parameter, Object2, to count the amount of heap data compressed in the current process (e.g., in pages). Object2 is initialized to 0. When the kernel compresses memory data, it can determine whether the data being compressed is heap data based on the vma_name field. It's important to note that the kernel compresses memory data in pages. Therefore, when the kernel determines that the compressed memory data is heap data, Object2 is incremented by one. In this way, the kernel can count the size of the compressed heap data and calculate the heap data compression ratio. That is, the heap data compression ratio = Object2 divided by Object1.
[0116] As mentioned earlier, when the kernel compresses the heap data, the electronic device performs garbage collection (GC) (including lightweight GC and full GC) to decompress the compressed heap data. However, decompression requires additional memory resources and consumes more CPU resources. Therefore, this process or system can set a threshold of 3. When the heap data compression ratio is low (i.e., the heap data compression ratio is less than the threshold of 3), the additional memory resources requested and CPU resources consumed by the electronic device due to decompression are not high and will not affect the normal operation of the system. In this case, the electronic device can directly perform lightweight GC, i.e., proceed to step 407. When the heap data compression ratio is high (i.e., the heap data compression ratio is equal to or greater than the threshold of 3), the electronic device needs to further determine the current CPU load, i.e., proceed to step 404.
[0117] 404. Determine if the CPU load exceeds the threshold 4.
[0118] If the CPU load (also known as CPU utilization) exceeds threshold 4 (e.g., 40%), it indicates that the current CPU load is too heavy, and it is not advisable to perform lightweight GC immediately. Lightweight GC can be delayed by raising the threshold by 1, i.e., proceed to step 405. If the CPU load does not exceed threshold 4, lightweight GC can be performed directly, i.e., proceed to step 407.
[0119] 405. Increase the threshold by 1.
[0120] In one embodiment, the electronic device can use a step-by-step method to increase the value of threshold 1. In this embodiment, the electronic device can use a default step size and a preset number of times, or receive a user-defined step size and a preset number of times. For example, the electronic device can first increase threshold 1 once in a step size (e.g., 2M), and then determine whether the value of the heap memory expected to be occupied by the process divided by the target utilization rate is less than the increased threshold 1. If it is still equal to or greater than the increased threshold 1, then the threshold 1 is increased again in a step size (e.g., increased again by 2M, accumulating an increase of 4M). When the number of times the electronic device increases threshold 1 is less than the preset number (e.g., 3 times), it again determines whether the value of the heap memory expected to be occupied by the process divided by the target utilization rate is less than the increased threshold 1. If it is still equal to or greater than the increased threshold 1, then the threshold 1 is increased again. If the number of times the electronic device increases threshold 1 is equal to the preset number of times, step 406 is executed. It should be noted that during any increase of threshold 1, if it is detected that the value of the expected heap memory occupied by this process divided by the target utilization rate is less than the adjusted threshold 1, then the increase of threshold 1 will be stopped again, and the first memory will be allocated directly, i.e., step 417 will be executed.
[0121] 406. Determine whether the value of the expected heap memory usage of this process divided by the target utilization rate is less than the threshold 1. If it is equal to or greater than the threshold 1, proceed to step 407; if it is less than the threshold 1, proceed to step 417.
[0122] Understandably, raising the threshold by 1 means increasing the upper limit of heap memory usage for this process. In some scenarios, raising the threshold by 1 ensures that the expected heap memory usage of this process divided by the target utilization is less than the raised threshold by 1, thus allowing for the allocation of the first memory segment. In this scenario, the electronic device reduces the number of times it performs lightweight garbage collection (GC) by raising the threshold by 1.
[0123] In other scenarios, even after increasing the threshold by 1, the estimated heap memory usage of this process divided by the target utilization rate may still be equal to or greater than the increased threshold by 1. For example, other tasks within this process may also request heap memory allocation during the same time period, causing the estimated heap memory usage of this process divided by the target utilization rate to still be equal to or greater than the increased threshold by 1. Therefore, the initial memory allocation cannot be completed, and a lightweight garbage collection (GC) is still required to reclaim more memory. It is evident that in this scenario, the electronic device also delays the execution of lightweight GC by increasing the threshold by 1, avoiding execution when the CPU load is too heavy, thus contributing to system smoothness and stability.
[0124] 407. Perform lightweight garbage collection.
[0125] The electronic device scans the heap memory newly requested by the process within a specific time period to determine whether the data in the heap memory has been recently referenced. Then, the electronic device can use a strategy such as LRU to reclaim the least recently referenced heap memory. The specific time period refers to the period from the last GC (including lightweight GC and full GC) performed by the electronic device to the current lightweight GC.
[0126] Understandably, compared to a full-scale garbage collection (GC), lightweight GC on electronic devices consumes fewer memory and CPU resources. Since lightweight GC does not scan all heap data, the amount of compressed data scanned is also less. Therefore, the additional memory and CPU resources consumed by the compaction process on electronic devices are not significant, which helps ensure system smoothness and stability.
[0127] 408. Determine whether the value of the expected heap memory usage of this process divided by the target utilization rate is less than the threshold 1. If it is equal to or greater than the threshold 1, proceed to step 409; if it is less than the threshold 1, proceed to step 417.
[0128] 409. Increase the threshold by 1.
[0129] In other words, after a lightweight garbage collection (GC) is performed on an electronic device, if the current memory situation still cannot meet the demand—that is, the ratio of the expected heap memory usage of this process to the target utilization rate is still equal to or greater than threshold 1—then the electronic device raises threshold 1 again. By raising threshold 1 again, the number of times the electronic device performs a full GC is reduced, or the timing of a full GC is extended.
[0130] 410. Determine whether the value of the expected heap memory usage of this process divided by the target utilization rate is less than the threshold 1. If it is equal to or greater than the threshold 1, proceed to step 411; if it is less than the threshold 1, proceed to step 417.
[0131] In some scenarios, increasing the threshold by 1 ensures that the expected heap memory usage of the current process divided by the target utilization is less than the increased threshold by 1, thus enabling the initial memory allocation. In this scenario, by increasing the threshold by 1, the electronic device reduces the number of full garbage collection (GC) operations performed by one.
[0132] In other scenarios, even after increasing the threshold by 1, the ratio of the expected heap memory usage of the current process to the target utilization rate may still be equal to or greater than the increased threshold by 1. For example, multiple tasks may run concurrently within the current process, and these tasks may request heap memory allocation simultaneously or sequentially. While the current task is executing its memory allocation request, increasing the threshold by 1 raises the upper limit of heap memory usage for the current process (i.e., the maximum heap memory). However, other tasks may allocate the required heap memory earlier (i.e., the heap memory already used by the current process increases), resulting in the expected heap memory usage of the current process (the memory requested by the current task plus the heap memory already used by the current process) divided by the target utilization rate still exceeding the threshold by 1. Therefore, the current memory allocation request cannot be completed. Consequently, a lightweight garbage collection (GC) is still required to reclaim more memory. Thus, in this scenario, the electronic device also delays the execution of a full GC by increasing the threshold by 1.
[0133] 411. Detect the data compression ratio of the stack area.
[0134] In other words, if the electronic device has already performed a lightweight GC, and the memory situation is still insufficient after increasing the threshold by 1, the electronic device cannot immediately perform a full-scale GC. This is because a full-scale GC requires significant memory and CPU resources, and the electronic device needs to scan all heap data. When the amount of compressed data in the heap is large, the additional memory and CPU resources required for decompression will exacerbate the system's memory pressure, affecting the stability and smoothness of system operation, and may even cause system crashes. This application will adopt different strategies to delay the execution of a full-scale GC based on the compression ratio of the heap data, that is, to execute any one of the following steps 412, 413, or 414a depending on the situation.
[0135] For details on the specific method for detecting the data compression ratio of the stack area, please refer to the description of the relevant content in step 403, which will not be repeated here.
[0136] 412. When the heap data compression ratio is low, such as when the heap data compression ratio is less than the threshold of 5, perform full-level GC during peak CPU load periods.
[0137] In some embodiments, when a low heap data compression ratio is detected (e.g., in interval 1, where interval 1 is, for example, less than threshold 5, and threshold 5 is, for example, 30%), CPU load continues to be monitored. When the CPU load is too heavy, for example, when the CPU load is greater than threshold 7 (which can be the same as or greater than threshold 4 in step 404), full-scale GC is paused to avoid performing full-scale GC during peak CPU load periods, ensuring system smoothness and stability. CPU load is continuously monitored, and when the CPU load is below threshold 7, the electronic device can perform full-scale GC. It is understood that peak CPU load periods include the time when the CPU starts an app, or the time when the CPU performs heavy tasks (e.g., face unlock, image recognition, speech and semantic recognition, graphics rendering, etc.), or the time when the CPU performs a large number of tasks simultaneously.
[0138] In other embodiments, when a low heap data compression ratio is detected (e.g., less than a threshold of 5, where threshold 5 is, for example, 30%), the electronic device can also directly delay the execution of a full-scale GC. The delay duration can be determined based on the foreground or core process currently running on the electronic device. It should be noted that the embodiments of this application do not specifically limit the specific scheme for delaying the execution of a full-scale GC.
[0139] 413. When the heap data compression ratio is moderate, such as when the heap data compression ratio is equal to or greater than threshold 5 and less than threshold 6, perform a full-level GC when the CPU is idle.
[0140] When a low heap data compression ratio is detected (e.g., within range 2, where range 2 is between thresholds 5 and 6, where threshold 5 is 30% and threshold 6 is 50%), CPU load is continuously monitored. When the CPU load is heavy, for example, greater than threshold 8 (threshold 8 is less than threshold 7), full-scale GC is paused. Electronic devices can perform full-scale GC when the CPU is idle to ensure system smoothness and stability. Here, CPU idle time refers to the time the CPU waits for the I / O device to complete the I / O request after issuing it.
[0141] 414a. When the heap data compression ratio is high, such as when the heap data compression ratio is equal to or greater than the threshold 6, freeze this process.
[0142] When a high heap data compression ratio is detected (e.g., in range 3, where range 3 is equal to or greater than threshold 6, and threshold 6 is 50%), if the electronic device continues to perform a full-scale GC, the amount of heap data that needs to be decompressed will be large. This decompression process will consume significant memory and CPU resources, severely impacting system smoothness and stability, and may even lead to system crashes. Therefore, in this scenario, the electronic device directly freezes its current process, pausing the full-scale GC from the source of memory allocation (of course, this process also cannot perform lightweight GC). In a specific implementation, the electronic device's kernel can set the current process's state to stopped in the process list and save the process's context for later unfreezing.
[0143] Understandably, it is usually the background process that is frozen, so the user will not be aware that the process is frozen and it will not affect the user experience.
[0144] 414b. Upon receiving an instruction to unfreeze this process, unfreeze this process and perform a full-scale garbage collection.
[0145] In some scenarios, when an electronic device receives an instruction to unfreeze its current process, it unfreezes the process and performs a full garbage collection (GC). Instructions to unfreeze the current process include, but are not limited to, instructions from the user to switch the corresponding app to the foreground.
[0146] Optionally, after steps 412, 413, and 414b, when the electronic device performs a full GC, it can further lower the threshold 1. This is because a full GC scans more heap data, which can release more heap memory, increasing the amount of free heap memory. Therefore, after performing a full GC, the electronic device can appropriately lower the threshold 1 to ensure the utilization rate of heap memory.
[0147] 415. Determine whether the value of the expected heap memory usage of this process divided by the target utilization rate is less than the threshold 1. If it is equal to or greater than the threshold 1, proceed to step 416; if it is less than the threshold 1, proceed to step 417.
[0148] 416. Triggering the memory overflow mechanism.
[0149] When the memory overflow mechanism is triggered, the electronic device cleans up all processes that request heap memory, and the process ends.
[0150] 417. Allocate the first memory.
[0151] In summary, this application proposes a method for detecting the heap data compression ratio, enabling the GC thread of the electronic device kernel to perceive the compression level of the heap data (i.e., the heap data compression ratio), thereby facilitating efficient GC management based on the compression level of the heap data. When the heap data compression level varies, different strategies are employed based on the current CPU load of the electronic device to reduce the number of GC (lightweight GC and full GC) executions or delay the timing of GC (lightweight GC and full GC) executions, avoiding GC execution under high CPU load. Therefore, this application reduces the system resources (including memory and CPU resources) consumed by the electronic device during GC execution, contributing to system smoothness and stability.
[0152] It should also be noted that steps 401-417 described above are only one embodiment provided by this application. In other embodiments, some steps in steps 401-417 may be omitted, the execution order between steps may be changed, or other combinations of steps may be made if the scheme does not contradict each other. For example, in some embodiments, step 405 may be omitted, or step 407 may be omitted, or step 409 may be omitted. For another example, in some other embodiments, step 403 may be replaced by step 411, and the electronic device may adopt different strategies to delay GC based on the heap data compression ratio. For example, when the heap data compression ratio is low, lightweight GC can be performed directly. When the heap data compression ratio is high, the method of raising the threshold by 1 is used to delay the execution of lightweight GC. Other variations or substitutions within the technical scope disclosed in this application are all covered within the protection scope of this application and will not be listed here.
[0153] This application also provides a chip system, such as... Figure 5 As shown, the chip system includes at least one processor 1101 and at least one interface circuit 1102. The processor 1101 and the interface circuit 1102 are interconnected via lines. For example, the interface circuit 1102 can be used to receive signals from other devices (e.g., the memory of electronic device 100). As another example, the interface circuit 1102 can be used to send signals to other devices (e.g., the processor 1101). Exemplarily, the interface circuit 1102 can read instructions stored in the memory and send those instructions to the processor 1101. When the instructions are executed by the processor 1101, the electronic device can perform the various steps performed by the electronic device 100 (e.g., a mobile phone) in the above embodiments. Of course, the chip system may also include other discrete components, which are not specifically limited in this application embodiment.
[0154] This application also provides an apparatus included in an electronic device, which has the function of implementing the behavior of the electronic device in any of the methods described above. This function can be implemented in hardware or by hardware executing corresponding software. The hardware or software includes at least one module or unit corresponding to the above function. For example, a detection module or unit, a determination module or unit, and a processing module or unit, etc.
[0155] This application also provides a computer storage medium including computer instructions that, when executed on an electronic device, cause the electronic device to perform any of the methods described in the above embodiments.
[0156] This application also provides a computer program product that, when run on a computer, causes the computer to perform any of the methods described in the above embodiments.
[0157] It is understood that the aforementioned electronic devices, etc., include hardware structures and / or software modules corresponding to the execution of each function in order to achieve the above-mentioned functions. Those skilled in the art should readily recognize that, based on the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein, the embodiments of this application can be implemented in hardware or a combination of hardware and computer software. Whether a function is executed by hardware or by computer software driving hardware depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of the embodiments of this invention.
[0158] This application embodiment can divide the above-mentioned electronic device into functional modules according to the method example described above. For example, each function can be divided into its own functional module, or two or more functions can be integrated into one processing module. The integrated module can be implemented in hardware or as a software functional module. It should be noted that the module division in this embodiment is illustrative and only represents one logical functional division; other division methods may be used in actual implementation.
[0159] Through the above description of the embodiments, those skilled in the art will clearly understand that, for the sake of convenience and brevity, only the division of the above functional modules is used as an example. In practical applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above. The specific working process of the system, device, and unit described above can be referred to the corresponding process in the foregoing method embodiments, and will not be repeated here.
[0160] In the embodiments of this application, the functional units can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0161] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solutions of the embodiments of this application, essentially, or the parts that contribute to the prior art, or all or part of the technical solutions, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) or processor to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as flash memory, portable hard disk, read-only memory, random access memory, magnetic disk, or optical disk.
[0162] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any changes or substitutions within the technical scope disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A method of garbage collection (GC), the method comprising: The method comprises: The electronic device runs an application APP; When a process of the APP requests to allocate a first memory, the electronic device detects whether a value of heap memory that the process is expected to occupy divided by a target utilization rate is equal to or greater than a first threshold, wherein the heap memory that the process is expected to occupy comprises heap memory that the process has occupied and the first memory; the target utilization rate is a proportion of heap memory occupied by the process of the APP to maximum heap memory available to the process of the APP; the maximum heap memory available to the process of the APP comprises heap memory that the process of the APP has used and idle heap memory; When the value of the heap memory that the process is expected to occupy divided by the target utilization rate is equal to or greater than the first threshold, the electronic device detects whether a heap area data compression ratio corresponding to the process is equal to or greater than a second threshold; When the heap area data compression ratio corresponding to the process is equal to or greater than the second threshold and a current CPU load of the electronic device is greater than a third threshold, the electronic device delays the process from performing garbage collection.
2. The method of claim 1, wherein, When the heap area data compression ratio corresponding to the process is equal to or greater than the second threshold and the current CPU load of the electronic device is greater than the third threshold, the electronic device adjusts the first threshold upward. After the electronic device adjusts the first threshold upward, the method further comprises:
3. The method of claim 2, wherein, The electronic device detects whether a value of heap memory that the process is expected to occupy divided by a target utilization rate is equal to or greater than the first threshold after the adjustment; When the value of the heap memory that the process is expected to occupy divided by the target utilization rate is equal to or greater than the first threshold after the adjustment, the electronic device performs light GC. After the electronic device performs light GC, the method further comprises:
4. The method of claim 3, wherein, The electronic device detects whether a value of heap memory that the process is expected to occupy divided by a target utilization rate is equal to or greater than the first threshold after the adjustment; When the value of the heap memory that the process is expected to occupy divided by the target utilization rate is equal to or greater than the first threshold after the adjustment, the electronic device adjusts the first threshold upward again. After the electronic device adjusts the first threshold upward again, the method further comprises:
5. The method of claim 4, wherein, When the value of the heap memory that the process is expected to occupy divided by the target utilization rate is equal to or greater than the first threshold after the adjustment, the electronic device detects the heap area data compression ratio corresponding to the process again; The electronic device adopts different strategies according to the heap area data compression ratio corresponding to the process to delay performing full GC. The electronic device adopts different strategies according to the heap area data compression ratio corresponding to the process to delay performing full GC, comprising:
6. The method of claim 5, wherein, When the heap area data compression ratio corresponding to the process is in a first interval, the electronic device avoids performing full GC during a peak period of CPU load; When the heap area data compression ratio corresponding to the process is in a second interval, the electronic device performs full GC during a non-peak period of CPU load; and When the heap area data compression ratio corresponding to the process is in a third interval, the electronic device performs full GC during a non-peak period of CPU load and during a peak period of CPU load. When the data compression ratio of the heap area corresponding to the process is in the second interval, the electronic device performs full-scale garbage collection when the CPU is idle. When the data compression ratio of the heap area corresponding to the process is in the third interval, the electronic device freezes the process, and after receiving an instruction to unfreeze the process, unfreezes the process and performs full-scale garbage collection.
7. The method of claim 2, wherein, After the electronic device adjusts the first threshold, the method further comprises: When the heap memory occupied by the process is equal to or greater than the first threshold after the adjustment, the electronic device detects the data compression ratio of the heap area corresponding to the process again. The electronic device adopts different strategies according to the data compression ratio of the heap area corresponding to the process to delay the execution of full-scale garbage collection.
8. The method of claim 7, wherein, The electronic device adopts different strategies according to the data compression ratio of the heap area corresponding to the process to delay the execution of full-scale garbage collection, comprising: When the data compression ratio of the heap area corresponding to the process is in the first interval, the electronic device avoids the peak period of CPU load to perform full-scale garbage collection. When the data compression ratio of the heap area corresponding to the process is in the second interval, the electronic device performs full-scale garbage collection when the CPU is idle. When the data compression ratio of the heap area corresponding to the process is in the third interval, the electronic device freezes the process, and after receiving an instruction to unfreeze the process, unfreezes the process and performs full-scale garbage collection.
9. The method according to any one of claims 1 to 8, characterized in that, The method further comprises: When the process applies for heap memory, the electronic device marks the heap memory applied for by the process.
10. The method of claim 9, wherein, The method further comprises: When the electronic device compresses the heap area data of the process, the electronic device records the data amount of the compressed heap area data.
11. The method of any one of claims 1-8, 10, wherein, The data compression ratio of the heap area of the process is the ratio of the data amount of the compressed heap area data in the process to the data amount of all heap area data in the process.
12. An electronic device, comprising: Comprising: A processor, a memory, and a touch screen, the memory, the touch screen, and the processor are coupled, the memory is used to store computer program code, the computer program code comprises computer instructions, when the processor reads the computer instructions from the memory, so that the electronic device executes the garbage collection GC method as claimed in any one of claims 1-11.
13. A computer readable storage medium characterized by, Comprising computer instructions, when the computer instructions run on an electronic device, the electronic device executes the garbage collection GC method as claimed in any one of claims 1-11.
14. A chip system, characterized by Comprising one or more processors, when the one or more processors execute instructions, the one or more processors execute the garbage collection GC method as claimed in any one of claims 1-11.
Citation Information
Patent Citations
Tail latency aware foreground garbage collection algorithm
US20180210825A1
System and method to manage and share managed runtime memory for JAVA virtual machine
US20190042406A1