WO2023202429A1 - Garbage recycling method and electronic device - Google Patents

Garbage recycling method and electronic device Download PDF

Info

Publication number
WO2023202429A1
WO2023202429A1 PCT/CN2023/087634 CN2023087634W WO2023202429A1 WO 2023202429 A1 WO2023202429 A1 WO 2023202429A1 CN 2023087634 W CN2023087634 W CN 2023087634W WO 2023202429 A1 WO2023202429 A1 WO 2023202429A1
Authority
WO
WIPO (PCT)
Prior art keywords
electronic device
heap
threshold
memory
compression ratio
Prior art date
Application number
PCT/CN2023/087634
Other languages
French (fr)
Chinese (zh)
Inventor
季柯丞
Original Assignee
华为技术有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 华为技术有限公司 filed Critical 华为技术有限公司
Publication of WO2023202429A1 publication Critical patent/WO2023202429A1/en

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F12/00Accessing, addressing or allocating within memory systems or architectures
    • G06F12/02Addressing or allocation; Relocation
    • G06F12/0223User address space allocation, e.g. contiguous or non contiguous base addressing
    • G06F12/023Free address space management
    • G06F12/0238Memory management in non-volatile memory, e.g. resistive RAM or ferroelectric memory
    • G06F12/0246Memory management in non-volatile memory, e.g. resistive RAM or ferroelectric memory in block erasable memory, e.g. flash memory

Definitions

  • This application relates to the field of operating systems, and in particular to garbage collection methods and electronic devices.
  • Various applications (APPs) or services (services) written using Java language can be installed on electronic devices.
  • APPs applications
  • services services
  • electronic devices need to create a Java virtual machine (virtual machine, JVM) when running an APP written in the Java language.
  • JVM virtual machine
  • JVM virtual machine
  • 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 GC. During the execution of the GC thread, the GC thread scans the heap area data allocated by the JVM to determine whether the heap area data is referenced. Among them, a large amount of scanning work will occupy a large amount of CPU resources.
  • the kernel will also compress the heap area data according to the least recently used (Least Recently Used, LRU). Then, for the compressed heap area data, the GC thread needs to decompress the compressed heap area data before it can determine whether the decompressed heap area data is referenced. When the GC thread decompresses, it also needs to apply for additional memory from the system to store the data during the decompression process. It is understandable that the system memory is already very tight at this time, and the behavior of applying for memory during decompression further aggravates the system memory tension. Moreover, the decompression behavior of the GC thread also prolongs the overall time-consuming of memory recycling and increases the cost of CPU resources. It can be seen that the existing GC process will affect the smoothness and stability of the system operation and easily cause the system to crash.
  • LRU least recently used
  • the garbage collection method and electronic equipment provided by this application can reduce the system resources (including memory resources and CPU resources) occupied by the electronic equipment when executing GC, which is beneficial to ensuring the smoothness and stability of the system.
  • this application provides a method for garbage collection GC.
  • the method includes: an electronic device runs an application APP; when the APP process requests to allocate the first memory, the electronic device detects that the heap memory expected to be occupied by the process is divided by the target utilization.
  • the electronic device detects whether the heap data compression ratio corresponding to the process is equal to or greater than the second threshold; 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 The device delays the process to perform garbage collection.
  • the kernel of the electronic device will compress the heap area data according to a certain strategy (such as LRU). Then, when the electronic device performs GC, it may be necessary to decompress the compressed heap area data. The decompression process not only consumes the system's memory, aggravates the system memory tension, but also consumes the system's CPU resources. It can be seen that if in a scenario with high CPU load (that is, the current CPU of electronic equipment Executing GC when the load is greater than the third threshold) can easily affect the fluency and stability of the system, and may even cause the system to crash.
  • a certain strategy such as LRU
  • the electronic device counts the compression ratio of the heap data of each process, and then uses different delay strategies to delay the execution of GC based on the compression ratio of the heap data and the load of the CPU, thus improving the fluency and stability of the system. , to avoid system crash problems.
  • the electronic device delays the process to perform garbage collection, including: when the process When the corresponding heap area data compression ratio 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 increases the first threshold.
  • raising the first threshold means raising the upper limit of heap memory used by this process.
  • the value of the heap memory expected to be occupied by the process divided by the target utilization can be smaller than the raised first threshold, and then the allocation of the first memory can be completed.
  • the electronic device reduces the number of times the electronic device performs lightweight GC by raising the first threshold.
  • the value of the heap memory expected to be occupied by this process divided by the target utilization may still be equal to or greater than the first threshold after the raise.
  • other tasks of this process may also apply for allocation of heap memory during this time period, causing the value of the heap memory expected to be occupied by this process divided by the target utilization to still be equal to or greater than the first threshold after the increase. Then, the first memory allocation cannot be completed, and lightweight GC still needs to be executed to reclaim more memory.
  • the electronic device also delays the opportunity for the electronic device to execute lightweight GC by raising the first threshold, thereby avoiding executing lightweight GC when the current CPU load is too heavy, which is conducive to ensuring the smoothness and stability of the system. stability.
  • the method further includes: the electronic device detects 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 raised first threshold; When the value of the heap memory expected to be occupied by the electronic device detection process divided by the target utilization is equal to or greater than the raised first threshold, the electronic device performs a lightweight GC.
  • the method further includes: the electronic device detects 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 first threshold after the increase. ; 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 raised first threshold, the electronic device raises the first threshold again.
  • the electronic device after the electronic device performs lightweight GC, if the current memory situation of the electronic device still cannot meet the demand, that is, the value of the heap memory expected to be occupied by this process divided by the target utilization 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 full-scale GC is again reduced, or the opportunity for the electronic device to perform full-scale GC is extended.
  • the method further includes: 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 first threshold after being raised again, The electronic device again detects the heap data compression ratio corresponding to the process; the electronic device adopts different strategies based on the heap data compression ratio corresponding to the process and delays the execution of full-scale garbage collection GC.
  • the embodiment of the present application proposes a method for detecting the compression ratio of heap area data, so that the GC thread of the electronic device core can sense the compression level of the heap area data (that is, the compression ratio of the heap area data), thereby facilitating the heap area data compression ratio according to the heap area data compression ratio.
  • the compression level of area data enables efficient GC control.
  • different strategies are adopted based on the current CPU load of the electronic device to reduce the number of times the electronic device performs GC (lightweight GC and full-weight GC) or delay the execution of GC (lightweight GC) by the electronic device.
  • level GC and full-scale GC) to prevent electronic equipment from executing GC under high CPU load. Therefore, this application reduces the system resources (including memory resources and CPU resources) occupied by the electronic device when executing GC, which is beneficial to ensuring the smoothness and stability of the system.
  • the electronic device adopts different strategies according to the heap data compression ratio corresponding to the process to delay the execution of full-scale garbage collection GC, including: when the heap data compression ratio corresponding to the process is in the first interval , the electronic device performs full-scale GC to avoid the peak period of CPU load; when the heap data compression ratio corresponding to the process is in the second interval, the electronic device performs full-scale GC when the CPU is idle; when the heap data compression ratio corresponding to the process When the compression ratio is in the third range, the electronic device freezes the process, and after receiving the instruction to unfreeze the process, unfreezes the process and executes a full-scale GC.
  • the method further includes: 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 raised first threshold, the electronic device The heap data compression ratio corresponding to the process is detected again; the electronic device adopts different strategies based on the heap data compression ratio corresponding to the process to delay the execution of full-scale garbage collection GC.
  • the electronic device adopts different strategies according to the heap data compression ratio corresponding to the process to delay the execution of full-scale garbage collection GC, including: when the heap data compression ratio corresponding to the process is in the first interval , the electronic device performs full-scale GC to avoid the peak period of CPU load; when the heap data compression ratio corresponding to the process is in the second interval, the electronic device performs full-scale GC when the CPU is idle; when the heap data compression ratio corresponding to the process When the compression ratio is in the third range, the electronic device freezes the process, and after receiving the instruction to unfreeze the process, unfreezes the process and executes a full-scale GC.
  • the method further includes: when the process applies for heap memory, the electronic device marks the heap memory applied for by the process.
  • the method further includes: when the electronic device compresses the heap data of the process, the electronic device records the data amount of the compressed heap data.
  • the heap area data compression ratio of a process is the ratio of the data amount of compressed heap area data in the process to the data amount of all heap area data in the process.
  • an electronic device including: a processor, a memory and a touch screen.
  • the memory, the touch screen are coupled to the processor.
  • the memory is used to store computer program code.
  • the computer program code includes a computer program. Instructions, when the processor reads the computer instructions from the memory, so that the electronic device executes the method described in the above aspect and any possible implementation manner thereof.
  • a third aspect is to provide a device, which is included in an electronic device and has the function of realizing the behavior of the electronic device in any of the above aspects and possible implementation methods.
  • This function can be implemented by hardware, or it can be implemented by hardware executing corresponding software.
  • the hardware or software includes at least one module or unit corresponding to the above functions. For example, a detection module or unit, a determination module or unit, a processing module or unit, etc.
  • a computer-readable storage medium including computer instructions.
  • the computer instructions are stored in a computer
  • the electronic device is caused to perform the method described in the above aspect and any possible implementation manner.
  • a computer program product is provided.
  • the computer program product When the computer program product is run on a computer, it causes the computer to execute the method described in the above aspects and any of the possible implementations.
  • a sixth aspect provides a chip system, including a processor.
  • the processor executes instructions, the processor executes the method described in the above aspects and any of the possible implementations.
  • Figure 1 is a schematic diagram of the hardware structure of an electronic device provided by an embodiment of the present application.
  • Figure 2 is a schematic diagram of the software structure of an electronic device provided by an embodiment of the present application.
  • Figure 3 is a schematic flowchart of an electronic device performing GC according to an embodiment of the present application.
  • Figure 4 is a schematic flowchart of another electronic device performing GC according to an embodiment of the present application.
  • FIG. 5 is a schematic structural diagram of a chip system provided by an embodiment of the present application.
  • first and second are used for descriptive purposes only and cannot be understood as indicating or implying relative importance or implicitly indicating the quantity of indicated technical features. Therefore, features defined as “first” and “second” may explicitly or implicitly include one or more of these features. In the description of the embodiments of this application, unless otherwise specified, "plurality” means two or more.
  • the embodiment of the present application provides a method of garbage collection GC, which can be applied to electronic devices installed with one or more APPs.
  • the electronic device in the embodiment of the present application may be a mobile phone, a tablet computer, a personal computer (PC), a personal digital assistant (PDA), a smart watch, a netbook, a wearable electronic device, an enhanced Reality technology (augmented reality, AR) equipment, virtual reality (VR) equipment, vehicle-mounted equipment, smart screens, smart cars, smart speakers, robots, etc.
  • This application does not place special restrictions on the specific forms of this electronic equipment.
  • FIG. 1 shows a schematic structural diagram of an electronic device 100 .
  • 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 , mobile communication module 150, wireless communication module 160, audio module 170, Yang Speaker 170A, receiver 170B, microphone 170C, headphone interface 170D, sensor module 180, button 190, motor 191, indicator 192, camera 193, display screen 194, and subscriber identification module (subscriber identification module, SIM) card interface 195, etc. .
  • 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 , mobile communication module 150, wireless communication module 160, audio module 170, Yang Speaker 170A, receiver 170B, microphone 170C, headphone interface 170D, sensor module 180, button 190, motor 19
  • the sensor module 180 may include a pressure sensor 180A, a gyro sensor 180B, an air pressure sensor 180C, a magnetic sensor 180D, an acceleration sensor 180E, a distance sensor 180F, a proximity light sensor 180G, a fingerprint sensor 180H, a temperature sensor 180J, a touch sensor 180K, and ambient light. Sensor 180L, bone conduction sensor 180M, etc.
  • the structure illustrated in the embodiment of the present invention does not constitute a specific limitation on the electronic device 100 .
  • the electronic device 100 may include more or fewer components than shown in the figures, or some components may be combined, some components may be separated, or some components may be arranged differently.
  • the components illustrated may be implemented in hardware, software, or a combination of software and hardware.
  • the processor 110 may include one or more processing units.
  • the processor 110 may include an application processor (application processor, AP), a modem processor, a graphics processing unit (GPU), and an image signal processor. (image signal processor, ISP), controller, video codec, digital signal processor (digital signal processor, DSP), baseband processor, and/or neural network processor (neural-network processing unit, NPU), etc.
  • application processor application processor, AP
  • modem processor graphics processing unit
  • GPU graphics processing unit
  • image signal processor image signal processor
  • ISP image signal processor
  • controller video codec
  • digital signal processor digital signal processor
  • DSP digital signal processor
  • baseband processor baseband processor
  • neural network processor neural-network processing unit
  • the controller can generate operation control signals based on the instruction operation code and timing signals to complete the control of fetching and executing instructions.
  • the processor 110 may also be provided with a memory for storing instructions and data.
  • the memory in processor 110 is cache memory. This memory may hold instructions or data that have been recently used or recycled by processor 110 . If the processor 110 needs to use the instructions or data again, it can be called directly from the memory. Repeated access is avoided and the waiting time of the processor 110 is reduced, thus improving the efficiency of the system.
  • processor 110 may include one or more interfaces.
  • Interfaces may include integrated circuit (inter-integrated circuit, I2C) interface, integrated circuit built-in audio (inter-integrated circuit sound, I2S) interface, pulse code modulation (pulse code modulation, PCM) interface, universal asynchronous receiver and transmitter (universal asynchronous receiver/transmitter (UART) interface, mobile industry processor interface (MIPI), general-purpose input/output (GPIO) interface, subscriber identity module (SIM) interface, and /or universal serial bus (USB) interface, etc.
  • I2C integrated circuit
  • I2S integrated circuit built-in audio
  • PCM pulse code modulation
  • UART universal asynchronous receiver and transmitter
  • MIPI mobile industry processor interface
  • GPIO general-purpose input/output
  • SIM subscriber identity module
  • USB universal serial bus
  • the charging management module 140 is used to receive charging input from the charger.
  • the charger can be a wireless charger or a wired charger.
  • the charging management module 140 may receive charging input from the wired charger through the USB interface 130 .
  • the charging management module 140 may receive wireless charging input through the wireless charging coil of the electronic device 100 . While the charging management module 140 charges the battery 142, it can also provide power to the electronic device through the power management module 141.
  • the power management module 141 is used to connect the battery 142, the charging management module 140 and the processor 110. Power management module 141 receives input from battery 142 and/or charge management module 140 for processor 110, internal memory 121, The display screen 194, the camera 193, the wireless communication module 160, etc. are powered. The power management module 141 can also be used to monitor battery capacity, battery cycle times, battery health status (leakage, impedance) and other parameters. In some other embodiments, the power management module 141 may also be provided in the processor 110 . In other embodiments, the power management module 141 and the charging management module 140 may also be provided in the same device.
  • the wireless communication function of the electronic device 100 can be implemented through the antenna 1, the antenna 2, the mobile communication module 150, the wireless communication module 160, the modem processor and the baseband processor.
  • Antenna 1 and Antenna 2 are used to transmit and receive electromagnetic wave signals.
  • Each antenna in electronic device 100 may be used to cover a single or multiple communication frequency bands. Different antennas can also be reused to improve antenna utilization. For example: Antenna 1 can be reused as a diversity antenna for a wireless LAN. In other embodiments, antennas may be used in conjunction with tuning switches.
  • the mobile communication module 150 can provide solutions for wireless communication including 2G/3G/4G/5G applied on 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 through the antenna 1, perform filtering, amplification and other processing on the received electromagnetic waves, and transmit them to the 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 through the antenna 1 for radiation.
  • at least part of the functional modules of the mobile communication module 150 may be disposed in the processor 110 .
  • at least part of the functional modules of the mobile communication module 150 and at least part of the modules of the processor 110 may be provided in the same device.
  • a modem processor may include a modulator and a demodulator.
  • the modulator is used to modulate the low-frequency baseband signal to be sent into a medium-high frequency signal.
  • the demodulator is used to demodulate the received electromagnetic wave signal into a low-frequency baseband signal.
  • the demodulator then transmits the demodulated low-frequency baseband signal to the baseband processor for processing.
  • the application processor outputs sound signals through audio devices (not limited to speaker 170A, receiver 170B, etc.), or displays images or videos through display screen 194.
  • the modem processor may be a stand-alone device.
  • the modem processor may be independent of the processor 110 and may be provided in the same device as the mobile communication module 150 or other functional modules.
  • the wireless communication module 160 can provide applications on the electronic device 100 including wireless local area networks (WLAN) (such as wireless fidelity (Wi-Fi) network), Bluetooth (bluetooth, BT), and global navigation satellites.
  • WLAN wireless local area networks
  • System global navigation satellite system, GNSS
  • frequency modulation frequency modulation, FM
  • near field communication technology near field communication, NFC
  • infrared technology infrared, IR
  • the wireless communication module 160 may be one or more devices integrating at least one communication processing module.
  • the wireless communication module 160 receives electromagnetic waves via the antenna 2 , frequency modulates and filters the electromagnetic wave signals, and sends the processed signals to the processor 110 .
  • the wireless communication module 160 can also receive the signal to be sent from the processor 110, frequency modulate it, amplify it, and convert it into electromagnetic waves through the antenna 2 for radiation.
  • the electronic device 100 implements display functions through a GPU, a display screen 194, an application processor, and the like.
  • the GPU is an image processing microprocessor and is connected to the display screen 194 and the application processor. GPUs are used to perform mathematical and geometric calculations for graphics rendering.
  • Processor 110 may include one or more GPUs that execute program instructions to generate or alter display information.
  • the display screen 194 is used to display images, videos, etc.
  • Display 194 includes a display panel.
  • the display panel can use a liquid crystal display (LCD), an organic light-emitting diode (organic light-emitting diode, OLED), active matrix organic light emitting diode or active matrix organic light emitting diode (active-matrix organic light emitting diode, AMOLED), flexible light-emitting diode (flex light-emitting diode, FLED), Miniled, MicroLed, Micro -oLed, quantum dot light emitting diodes (QLED), etc.
  • the electronic device 100 may include 1 or N display screens 194, where N is a positive integer greater than 1.
  • the electronic device 100 can implement the shooting function through an ISP, a camera 193, a video codec, a GPU, a display screen 194, an application processor, and the like.
  • the ISP is used to process the data fed back by the camera 193.
  • Camera 193 is used to capture still images or video.
  • Digital signal processors are used to process digital signals. In addition to digital image signals, they can also process other digital signals. For example, when the electronic device 100 selects a frequency point, the digital signal processor is used to perform Fourier transform on the frequency point energy.
  • Video codecs are used to compress or decompress digital video. Electronic device 100 may support one or more video codecs. In this way, the electronic device 100 can play or record videos in multiple encoding formats, such as moving picture experts group (MPEG) 1, MPEG2, MPEG3, MPEG4, etc.
  • MPEG moving picture experts group
  • the external memory 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 memory interface 120 to implement the data storage function. Such as saving music, videos, etc. files in external memory card.
  • Internal memory 121 may be used to store computer executable program code, which includes instructions.
  • the internal memory 121 may include a program storage area and a data storage area.
  • the stored program area can store an operating system, at least one application program required for a function (such as a sound playback function, an image playback function, etc.).
  • the storage data area may store data created during use of the electronic device 100 (such as audio data, phone book, etc.).
  • the 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.
  • the processor 110 executes various functional applications and data processing of the electronic device 100 by executing instructions stored in the internal memory 121 and/or instructions stored in a memory provided in the processor.
  • the electronic device 100 can implement audio functions through the audio module 170, the speaker 170A, the receiver 170B, the microphone 170C, the headphone interface 170D, and the application processor. Such as music playback, recording, etc.
  • the buttons 190 include a power button, a volume button, etc.
  • Key 190 may be a mechanical key. It can also be a touch button.
  • the electronic device 100 may receive key inputs and generate key signal inputs related to user settings and function control of the electronic device 100 .
  • the software system of the electronic device 100 may adopt a layered architecture, an event-driven architecture, a microkernel architecture, a microservice architecture, or a cloud architecture.
  • the software system of the electronic device 100 may be the Hongmeng system, Android system, etc.
  • This embodiment of the present invention takes the Android system with a layered architecture as an example to illustrate the software structure of the electronic device 100 .
  • FIG. 2 is a software structure block diagram of the electronic device 100 according to the embodiment of the present invention.
  • the layered architecture divides the software into several layers, and each layer has clear roles and division of labor.
  • the layers communicate through software interfaces.
  • the Android system is divided into five layers, from top to bottom: application layer (referred to as application layer), application framework layer (referred to as framework layer), Android Runtime (Android Runtime) and system library. Hardware Abstraction Layer (HAL) and kernel layer.
  • application layer referred to as application layer
  • framework layer application framework layer
  • Android Runtime Android Runtime
  • HAL Hardware Abstraction Layer
  • kernel layer kernel layer.
  • the application layer can include a series of application packages. As shown in Figure 2, the application package can include camera, gallery, calendar, calling, map, navigation, WLAN, Bluetooth, music, video, short message and other applications.
  • the framework layer provides application programming interface (API) and programming framework for applications in the application layer.
  • API application programming interface
  • the framework layer includes some predefined functions.
  • the framework layer can include activity manager, window manager, content provider, view system, phone manager, resource manager, notification manager, etc.
  • the activity manager is responsible for the startup of all APP processes, the startup of the four major components of the APP (such as activity, service, broadcast receiver, content provider), the interface between APPs and within the APP. switching and the life cycle of various components.
  • a window manager is used to manage window programs. The window manager can obtain the display size, determine whether there is a status bar, lock the screen, capture the screen, etc.
  • Content providers are used to store and retrieve data and make this data accessible to applications. Said data can include videos, images, audio, calls made and received, browsing history and bookmarks, phone books, etc.
  • the view system includes visual controls, such as controls that display text, controls that display pictures, etc.
  • a view system can be used to build applications.
  • the display interface can be composed of one or more views.
  • a display interface including a text message notification icon may include a view for displaying text and a view for displaying pictures.
  • the phone manager is used to provide communication functions of the electronic device 100 . For example, call status management (including connected, hung up, etc.).
  • the resource manager provides various resources to applications, such as localized strings, icons, pictures, layout files, video files, etc.
  • the notification manager allows applications to display notification information in the status bar, which can be used to convey notification-type messages and can automatically disappear after a short stay without user interaction. For example, the notification manager is used to notify download completion, message reminders, etc.
  • the notification manager can also be notifications that appear in the status bar at the top of the system in the form of charts or scroll bar text, such as notifications for applications running in the background, or notifications that appear on the screen in the form of conversation windows. For example, text information is prompted in the status bar, a beep sounds, the electronic device vibrates, the indicator light flashes, etc.
  • Android Runtime includes core libraries and virtual machines. Android Runtime is responsible for the scheduling and management of the Android system.
  • the core library contains two parts: one is the functional functions that need to be called by the Java language, and the other is the core library of Android.
  • the application layer and framework layer run in virtual machines.
  • the virtual machine executes the java files of the application layer and framework layer into binary files.
  • the virtual machine is used to perform object life cycle management, stack management, thread management, security and exception management, and garbage collection and other functions.
  • the electronic device 100 runs APP1 as an example for explanation.
  • one or more processes of APP1 will usually be started (such as process 1 corresponding to the interface of APP1, process 2 corresponding to the background service of APP1).
  • Each process will correspond to a virtual machine, and each process will correspond to a virtual machine.
  • Each process contains a GC thread.
  • the GC thread is responsible for monitoring the usage of the virtual machine heap memory corresponding to this process, and determining whether to start the GC process based on the usage of the virtual machine heap memory corresponding to this process. It is understandable that in some scenarios, when the user switches APP1 to run in the background, the electronic device may still keep running processes corresponding to one or more background services of APP1. These processes continue to occupy the memory of the electronic device, and these processes It is still possible to continue to apply for new heap memory, etc.
  • APP1 when APP1 starts a process, it will set the upper limit of the heap memory usage of the process (recorded as threshold 1), which is the maximum heap memory threshold that this process can use.
  • threshold 1 the maximum heap memory threshold that this process can use.
  • the process will start the GC thread to reclaim the heap memory of the process.
  • the target utilization rate of heap memory usually 0.75, which can be referred to as the target utilization rate
  • the target heap memory utilization can be a default value, or it can be set uniformly by the operating system for all processes, or it can also be set by the operating system separately for different APPs or different processes. The embodiments of this application do not limit this. .
  • the GC performed by the GC thread can include lightweight GC (such as Background Young GC) and full-scale GC (Full GC).
  • lightweight GC such as Background Young GC
  • full-scale GC Full-scale GC scans all heap area data requested by this process.
  • Figure 3 it is a schematic diagram of an electronic device performing a GC process, which specifically includes:
  • step 302. Determine whether the value of the heap memory expected to be occupied by this process divided by the target utilization (the value is represented by "heap memory expected to be occupied by this process/target utilization" in Figure 3) is less than the threshold 1. If it is equal to or greater than the threshold value 1, then step 303 is executed. If it is less than the threshold value 1, then step 314 is executed.
  • the heap memory expected to be occupied by this process in this step includes the heap memory actually occupied by this process and the heap memory requested by this process this time.
  • judging whether the heap memory expected to be occupied by this process divided by the target utilization is less than the threshold 1 is equivalent to judging whether the heap memory occupied by this process and the heap memory occupied by this process can be used by this process if the heap memory requested this time is allocated to the process.
  • the maximum heap memory including the heap memory occupied by this process and the free heap memory
  • the lightweight GC is triggered, that is, step 303 is executed. If it is not higher than the target utilization, the memory can be allocated, that is, step 314 is performed.
  • Trigger lightweight GC Trigger lightweight GC.
  • the electronic device scans the heap memory newly requested by this process within a specific time period to determine whether the data in the heap memory has been referenced recently. Then the electronic device can use a strategy such as LRU to reclaim the heap memory that has been least recently referenced. Then, the free heap memory available to this process increases.
  • the specific time period refers to the time period from the last time the electronic device executed GC (including lightweight GC and full-scale GC) to the time before the current execution of lightweight GC.
  • step 305 Determine again whether the value of the heap memory expected to be occupied by this process divided by the target utilization is less than the threshold 1. If it is equal to or greater than the threshold value 1, then step 306 is executed. If it is less than the threshold value 1, then step 314 is executed.
  • this process reclaims some heap memory. If this memory allocation request still cannot be completed at this time, increase the threshold to 1. It is understandable that raising the threshold by 1 means that the upper limit of heap memory used by this process has been raised, and then it is possible to complete this memory allocation request.
  • step 309 is executed. If it is less than the threshold value 1, then step 314 is executed.
  • the full-scale GC scans more data in the heap area, which releases more heap memory and increases the free heap memory. Therefore, after executing the full-scale GC, the electronic device can lower the threshold 1 in a timely manner. Ensure heap memory utilization.
  • step 312. Determine again whether the value of the heap memory expected to be occupied by this process divided by the target utilization is less than the threshold 1. If it is equal to or greater than the threshold value 1, then step 313 is executed. If it is less than the threshold value 1, then step 314 is executed.
  • the electronic device cleans up all processes that have applied for heap memory, and this process ends.
  • the electronic device can reduce the number of GC executions and minimize the CPU resources occupied by GC executions by executing GCs of different magnitudes step by step and combining it with the method of raising the threshold 1. .
  • System libraries can include multiple functional modules. For example: surface manager (surface manager), media libraries (Media Libraries), 3D graphics processing libraries (for example: OpenGL ES), 2D graphics engines (for example: SGL), etc.
  • the surface manager is used to manage the display subsystem and provides the integration of 2D and 3D layers for multiple applications.
  • the media library supports playback and recording of a variety of commonly used audio and video formats, as well as static image files, etc.
  • the media library can support a variety of audio and video encoding formats, such as: MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, etc.
  • the 3D graphics processing library is used to implement 3D graphics drawing, image rendering, composition, and layer processing.
  • 2D Graphics Engine is a drawing engine for 2D drawing.
  • HAL used to abstract hardware. HAL hides the hardware interface details of a specific platform and provides a virtual hardware platform for the operating system, making it hardware-independent and portable on multiple platforms. In other words, all hardware-related operations required by the upper layer require calling HAL-related APIs, such as audio and video interfaces, GPS interfaces, call interfaces, Wi-Fi interfaces, and other interfaces.
  • HAL-related APIs such as audio and video interfaces, GPS interfaces, call interfaces, Wi-Fi interfaces, and other interfaces.
  • the kernel layer is the layer between hardware and software.
  • the kernel layer includes memory management modules, processes/threads, audio and video drivers, GPS drivers, display drivers, Wi-Fi drivers, etc.
  • the memory management module is used to realize the allocation and recycling of system memory, compress heap area data, etc.
  • the kernel of the electronic device will compress the heap area data according to a certain strategy (such as LRU). Then, when the electronic device performs GC, it may be necessary to decompress the compressed heap area data. The decompression process not only consumes the system's memory, aggravates the system memory tension, but also consumes the system's CPU resources. It can be seen that if GC is executed under a high CPU load scenario, it is easy to affect the fluency and stability of the system, and may even cause the system to crash. To this end, embodiments of the present application provide a GC method. The electronic device counts the compression ratio of the heap area data of each process, and then uses different delay strategies to delay execution according to the compression ratio of the heap area data and the load of the CPU. GC, thereby improving the fluency and stability of the system and avoiding system crashes.
  • a certain strategy such as LRU
  • a schematic flow chart of a GC method is provided for an embodiment of the present application.
  • the method includes:
  • the APP process requests to allocate the first memory.
  • a certain task in the APP process requests to allocate the first memory.
  • the first memory requested to be allocated can be the memory of the specified size for the task, or it can be the memory of the default size of the system. This application embodiment does not limit this. .
  • step 403 is executed. If it is less than the threshold value 1, then step 417 is executed.
  • the proportion of the maximum heap memory can still maintain the ideal utilization (ie, the target utilization). If the target utilization can be maintained, that is, not higher than the target utilization, memory can be allocated, that is, step 417 is performed. If the ideal utilization cannot be maintained, that is, higher than the target utilization, you need to further adopt different delayed GC strategies based on the heap area data compression ratio and CPU load, that is, perform step 403 and subsequent steps.
  • step 407 If the heap area data compression ratio is less than the threshold 3, lightweight GC is triggered, that is, step 407 is executed. If the heap area data compression ratio is equal to or greater than the threshold 3, the current CPU load is further determined, that is, step 404 is performed.
  • the heap area data compression ratio refers to the ratio of the size of the compressed heap area data in this process to the size of all heap area data in this process (including compressed heap area data and uncompressed heap area data).
  • a process when a process applies to allocate virtual machine heap memory, it will map the kernel-mode memory to user-mode memory by calling a memory allocator (such as the mmap allocator) and mark it as anonymous page memory.
  • anonymous page memory refers to the memory that is dynamically allocated during the running of the application process and does not have a file name tag.
  • a memory allocator such as a mmap allocator
  • the anonymous page memory allocated by the process can be marked to facilitate the kernel layer to identify the anonymous page memory as heap memory. Subsequent calculation of heap area data compression ratio.
  • a process when a process applies to allocate virtual machine heap memory, for example, it marks the vma_name field in the anonymous page memory as the first value.
  • the field of vma_name is the first value
  • the kernel can identify the heap area data based on the vma_name field. For example, the kernel can set a parameter Object1 to count the amount of heap area data allocated by this process (for example, the unit is pages).
  • the kernel can also set another parameter Object2, which is used to count the amount of compressed heap area data in this process (for example, the unit is pages).
  • the initial value of Object2 is 0.
  • the process or system can set a threshold of 3.
  • the compression ratio of the heap area data is not high (that is, the compression ratio of the heap area data is less than the threshold 3)
  • the additional memory resources requested by the electronic device for performing the decompression action and the CPU resources consumed are not high, and will not affect the system. of normal operation.
  • the electronic device can directly execute the lightweight GC, that is, step 407 is executed.
  • the compression ratio of the heap area data is high (that is, the heap area data compression ratio is equal to or greater than the threshold 3)
  • the electronic device needs to further determine the current CPU load, that is, step 404 is performed.
  • the CPU load also called CPU occupancy
  • threshold 4 for example, 40%
  • the electronic device may use a stepwise method to increase the value of threshold 1.
  • the electronic device may adopt a default set step size and a preset number of times, or receive a step size and a preset number of times set by the user.
  • the electronic device can first increase the threshold 1 once in steps (for example, 2M), and then determine whether the value of the heap memory expected to be occupied by the process divided by the target utilization is less than the increased threshold 1. If it is still equal to or greater than the raised threshold 1, raise the threshold 1 again in steps (for example, raise it again by 2M, and the cumulative raise has been 4M).
  • step 406 is executed. It should be noted that during any process of raising the threshold 1, if it is detected that the heap memory expected to be occupied by this process divided by the target utilization is less than the adjusted threshold 1, it will stop raising the threshold 1 again and directly allocate the first memory, that is, perform step 417.
  • step 407 Determine whether the value of the heap memory expected to be occupied by this process divided by the target utilization is less than the threshold 1. If it is equal to or greater than the threshold value 1, then step 407 is executed. If it is less than the threshold value 1, then step 417 is executed.
  • raising the threshold by 1 means raising the upper limit of heap memory used by this process.
  • the value of the heap memory expected to be occupied by this process divided by the target utilization can be smaller than the raised threshold 1, and then the allocation of the first memory can be completed.
  • the electronic device raises the threshold 1 The method reduces the number of times electronic equipment performs lightweight GC.
  • the electronic device after raising the threshold 1, the value of the heap memory expected to be occupied by this process divided by the target utilization may still be equal to or greater than the raised threshold 1.
  • other tasks of this process may also apply for heap memory allocation during this time period, causing the value of the heap memory expected to be occupied by this process divided by the target utilization to still be equal to or greater than the increased threshold of 1.
  • the first memory allocation cannot be completed, and lightweight GC still needs to be executed to reclaim more memory.
  • the electronic device also delays the timing of the electronic device to perform lightweight GC by raising the threshold 1, avoiding the execution of lightweight GC when the current CPU load is too heavy, which is conducive to ensuring the smoothness and stability of the system. sex.
  • the electronic device scans the heap memory newly requested by this process within a specific time period to determine whether the data in the heap memory has been referenced recently. Then the electronic device can use a strategy such as LRU to reclaim the heap memory that has been least recently referenced.
  • the specific time period refers to the time period from the last time the electronic device executed GC (including lightweight GC and full-scale GC) to the time before the current execution of lightweight GC.
  • step 408 Determine whether the value of the heap memory expected to be occupied by this process divided by the target utilization is less than the threshold 1. If it is equal to or greater than the threshold value 1, then step 409 is executed. If it is less than the threshold value 1, then step 417 is executed.
  • the electronic device performs lightweight GC
  • the electronic device increases threshold 1 again.
  • the threshold 1 By raising the threshold 1 again, the number of times the electronic device performs full-scale GC is again reduced, or the opportunity for the electronic device to perform full-scale GC is extended.
  • step 411 Determine whether the value of the heap memory expected to be occupied by this process divided by the target utilization is less than the threshold 1. If it is equal to or greater than the threshold value 1, then step 411 is executed. If it is less than the threshold value 1, then step 417 is executed.
  • the electronic device reduces the need for the electronic device to perform a full-scale GC by raising the threshold value 1.
  • the value of the heap memory expected to be occupied by this process divided by the target utilization may still be equal to or greater than the raised threshold 1.
  • the upper limit of heap memory usage of this process is increased by raising the threshold 1, that is, the maximum heap memory, but other tasks may be allocated the required heap memory in advance (that is, this process The occupied heap memory increases), causing the heap memory expected to be occupied by this process at this time (the memory allocated by this task and the heap memory already occupied by this process) divided by the target utilization to still be higher than the threshold 1, still This memory allocation request for this task cannot be completed. Then, it is still necessary to perform lightweight GC to reclaim more memory. It can be seen that in this scenario, the electronic device also delays the opportunity for the electronic device to perform full-scale GC by raising the threshold 1.
  • the electronic device has performed a lightweight GC, and after raising the threshold to 1, if the memory of the electronic device still cannot meet the demand, the electronic device cannot immediately perform a full-scale GC.
  • the electronic device occupies a large amount of memory resources and CPU resources when executing full-scale GC, and because the electronic device needs to scan all heap area data.
  • the additional memory resources and CPU resources required by the electronic device for decompression will increase the memory tension of the system, affect the stability and fluency of system operation, and even cause the system to crash.
  • This application will use different strategies to delay the electronic device from performing full-scale GC according to the compression ratio of the heap area data, that is, perform any of the following steps 412, 413 or 414a respectively.
  • step 403 For the specific method of detecting the heap area data compression ratio, please refer to the description of relevant content in step 403, which will not be described again here.
  • the load of the CPU is continued to be detected.
  • the CPU load is greater than the threshold 7 (which can be the same as the threshold 4 in step 404, or greater than the threshold 4 in step 404)
  • the execution of the full-scale GC is suspended to avoid execution during the peak period of the CPU load.
  • Full-scale GC ensures system fluency and stability. The load of the CPU is continuously detected. When the CPU load is lower than the threshold 7, the electronic device can perform full-scale GC.
  • the peak period of CPU load includes the time period when the CPU starts the APP, or the time period when the CPU performs heavy load tasks (such as face unlocking tasks, image recognition tasks, speech semantic recognition tasks, graphics rendering tasks, etc.). Or the time period when the CPU performs a large number of tasks at the same time, etc.
  • the electronic device when it is detected that the heap area data compression ratio is low (for example, less than the threshold 5, and the threshold 5 is, for example, 30%), the electronic device can also directly delay the time to execute the full-scale GC.
  • the length of the delay can be based on The foreground process or core process currently running on the electronic device is determined. It should be noted that the embodiments of the present application do not specifically limit the specific solution for delaying the execution of full-scale GC.
  • the heap area data compression ratio is low (for example, it is located in interval 2, for example, interval 2 is between threshold 5 and threshold 6, threshold 5 is, for example, 30%, and threshold 6 is, for example, 50%), continue to detect the load of the CPU.
  • the CPU load is heavy, for example, the CPU load is greater than threshold 8 (threshold 8 is less than threshold 7), the execution of full-scale GC is suspended.
  • the electronic device can execute full-scale GC when the CPU is idle to ensure the smoothness and stability of the system. .
  • CPU idle refers to the time that the CPU waits for the I/O device to complete the I/O request after issuing the I/O request.
  • the electronic device When it is detected that the heap data compression ratio is high (for example, in interval 3, and interval 3 is, for example, equal to or greater than threshold 6, and threshold 6 is, for example, 50%), if the electronic device continues to perform full-scale GC, the heap area data that needs to be decompressed The amount is also large, and it requires a large amount of memory resources and CPU resources due to the decompression behavior, which will seriously affect the fluency and stability of the system, and even cause the system to crash. Therefore, in this scenario, the electronic device directly freezes this process, suspends this process from the source of memory allocation, and performs full-scale GC (of course, this process cannot perform lightweight GC).
  • the kernel of the electronic device can set the status of the process to stopped in the process list and save the process. The context of the process so that it can be unfrozen later.
  • instructions for unfreezing this process include, but are not limited to, instructions for the user to switch the APP corresponding to this process to run in the foreground.
  • the electronic device can also lower the threshold 1. This is because the full-scale GC scans more data in the heap area and releases more heap memory, which increases the free heap memory. Therefore, after executing the full-scale GC, the electronic device can lower the threshold 1 in a timely manner to ensure Heap memory utilization.
  • step 415 Determine whether the value of the heap memory expected to be occupied by this process divided by the target utilization is less than the threshold 1. If it is equal to or greater than the threshold value 1, then step 416 is executed. If it is less than the threshold value 1, then step 417 is executed.
  • the electronic device cleans up all processes that have applied for heap memory, and the process ends.
  • the embodiments of this application propose a method for detecting the compression ratio of heap area data, so that the GC thread of the electronic device core can perceive the compression level of the heap area data (ie, the compression ratio of the heap area data), thereby facilitating the heap area data compression ratio according to the heap area data compression ratio.
  • Data compression level for efficient GC control When the compression levels of heap area data are different, different strategies are adopted based on the current CPU load of the electronic device to reduce the number of times the electronic device performs GC (lightweight GC and full-weight GC) or delay the execution of GC (lightweight GC) by the electronic device. level GC and full-scale GC) to prevent electronic equipment from executing GC under high CPU load. Therefore, this application reduces the system resources (including memory resources and CPU resources) occupied by the electronic device when executing GC, which is beneficial to ensuring the smoothness and stability of the system.
  • step 405 may be omitted, or step 407 may be omitted, or step 409 may be omitted.
  • step 403 can be replaced by step 411, and the electronic device can use different strategies to delay GC according to the heap area data compression ratio. For example, when the heap area data compression ratio is low, lightweight GC can be performed directly. When the heap area data compression is relatively high, the method of raising the threshold 1 is used to delay the execution of lightweight GC.
  • Other changes or substitutions within the technical scope disclosed in this application are covered by the protection scope of this application, and will not be listed here.
  • the chip system includes at least one processor 1101 and at least one interface circuit 1102 .
  • the processor 1101 and the interface circuit 1102 may be interconnected by wires.
  • interface circuitry 1102 may be used to receive signals from other devices, such as the memory of electronic device 100 .
  • interface circuit 1102 may be used to send signals to other devices (eg, processor 1101).
  • the interface circuit 1102 can read instructions stored in the memory and send the instructions to the processor 1101.
  • the electronic device can be caused to perform various steps performed by the electronic device 100 (such as a mobile phone) in the above embodiments.
  • the chip system may also include other discrete devices. The embodiments of this application There is no specific limit on this.
  • An embodiment of the present application also provides a device, which is included in an electronic device and has the function of realizing the behavior of the electronic device in any of the methods in the above embodiments.
  • This function can be implemented by hardware, or it can be implemented by hardware executing corresponding software.
  • the hardware or software includes at least one module or unit corresponding to the above functions. For example, a detection module or unit, a determination module or unit, a processing module or unit, etc.
  • Embodiments of the present application also provide a computer storage medium that includes computer instructions.
  • the computer instructions When the computer instructions are run on an electronic device, the electronic device causes the electronic device to perform any of the methods in the above embodiments.
  • Embodiments of the present application also provide a computer program product.
  • the computer program product When the computer program product is run on a computer, it causes the computer to perform any of the methods in the above embodiments.
  • the above-mentioned electronic devices include hardware structures and/or software modules corresponding to each function.
  • Persons skilled in the art should easily realize that, in conjunction with the units and algorithm steps of each example described in the embodiments disclosed herein, the embodiments of the present application can be implemented in the form of hardware or a combination of hardware and computer software. Whether a function is performed by hardware or computer software driving the hardware depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementations should not be considered to be beyond the scope of the embodiments of the present invention.
  • Embodiments of the present application can divide the above-mentioned electronic equipment into functional modules according to the above-mentioned method examples.
  • each functional module can be divided corresponding to each function, or two or more functions can be integrated into one processing module.
  • the above integrated modules can be implemented in the form of hardware or software function modules. It should be noted that the division of modules in the embodiment of the present invention is schematic and is only a logical function division. In actual implementation, there may be other division methods.
  • Each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically alone, or two or more units may be integrated into one unit.
  • the above integrated units can be implemented in the form of hardware or software functional units.
  • the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it may be stored in a computer-readable storage medium.
  • the technical solutions of the embodiments of the present application are essentially or contribute to the existing technology, or all or part of the technical solution can be embodied in the form of a software product.
  • the computer software product is stored in a storage device.
  • the medium includes several instructions to cause a computer device (which may be a personal computer, a server, or a network device, etc.) or a processor to execute all or part of the steps of the methods described in various embodiments of this application.
  • the aforementioned storage media include: flash memory, mobile hard disk, read-only memory, random access memory, magnetic disk or optical disk and other media that can store program codes.

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Memory System (AREA)

Abstract

A garbage recycling method and an electronic device, relating to the field of operating systems, and capable of reducing system resources occupied by the whole machine when an electronic device executes GC, and ensuring the fluency and stability of the system. The method comprises: an electronic device collects statistics about a compression ratio of heap area data of each process, then adopts different delay policies according to the compression ratio of the heap area data and a load condition of a CPU, and delays execution of GC.

Description

垃圾回收的方法及电子设备Garbage recycling methods and electronic equipment
本申请要求于2022年04月19日提交国家知识产权局、申请号为202210409365.4、申请名称为“垃圾回收的方法及电子设备”的中国专利申请的优先权,其全部内容通过引用结合在本申请中。This application claims priority to the Chinese patent application submitted to the State Intellectual Property Office on April 19, 2022, with application number 202210409365.4 and application name "Waste Recycling Method and Electronic Equipment", the entire content of which is incorporated into this application by reference. middle.
技术领域Technical field
本申请涉及操作系统领域,尤其涉及垃圾回收的方法及电子设备。This application relates to the field of operating systems, and in particular to garbage collection methods and electronic devices.
背景技术Background technique
电子设备上可安装使用例如Java语言编写的各种各样的应用程序(application,APP)或服务(service)。因为Java语言的跨平台性以及开放性,为了在多种平台上实现兼容,电子设备在运行使用Java语言编写的APP时,需要创建Java虚拟机(virtual machine,JVM),JVM中存在有Java的垃圾回收(garbage collection,GC)线程。当APP或服务使用的堆内存不足时,JVM将启动GC线程执行GC。在GC线程的执行过程中,GC线程会扫描JVM分配的堆区数据以判断堆区数据是否被引用。其中,大量的扫描工作会占用大量的CPU资源。Various applications (APPs) or services (services) written using Java language, for example, can be installed on electronic devices. Because of the cross-platform and openness of the Java language, in order to achieve compatibility on multiple platforms, electronic devices need to create a Java virtual machine (virtual machine, JVM) when running an APP written in the Java language. There is Java in the JVM. 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 GC. During the execution of the GC thread, the GC thread scans the heap area data allocated by the JVM to determine whether the heap area data is referenced. Among them, a large amount of scanning work will occupy a large amount of CPU resources.
此外,当电子设备处于低内存场景时,内核还会按照最近最少使用(Least Recently Used,LRU)将堆区数据进行压缩。那么,针对压缩后的堆区数据,GC线程需要先对压缩后的堆区数据进解压,才能判断解压后的堆区数据是否被引用。GC线程解压时还需要向系统申请额外的内存用于存放解压过程中的数据。可以理解的是,此时系统的内存本就很紧张,解压时申请内存的行为进一步加剧了系统内存的紧张程度。并且,GC线程的解压行为也延长了内存回收的整体耗时,加大了CPU资源的开销。可见,现有的GC过程会影响系统运行的流畅性和稳定性,容易造成系统崩溃。In addition, when the electronic device is in a low-memory scenario, the kernel will also compress the heap area data according to the least recently used (Least Recently Used, LRU). Then, for the compressed heap area data, the GC thread needs to decompress the compressed heap area data before it can determine whether the decompressed heap area data is referenced. When the GC thread decompresses, it also needs to apply for additional memory from the system to store the data during the decompression process. It is understandable that the system memory is already very tight at this time, and the behavior of applying for memory during decompression further aggravates the system memory tension. Moreover, the decompression behavior of the GC thread also prolongs the overall time-consuming of memory recycling and increases the cost of CPU resources. It can be seen that the existing GC process will affect the smoothness and stability of the system operation and easily cause the system to crash.
发明内容Contents of the invention
本申请提供的垃圾回收的方法及电子设备,可以降低了电子设备在执行GC时整机占用的系统资源(包括内存资源和CPU资源),有利于保证系统的流畅性和稳定性。The garbage collection method and electronic equipment provided by this application can reduce the system resources (including memory resources and CPU resources) occupied by the electronic equipment when executing GC, which is beneficial to ensuring the smoothness and stability of the system.
为了实现上述目的,本申请实施例提供了以下技术方案:In order to achieve the above objectives, the embodiments of the present application provide the following technical solutions:
第一方面、本申请一种垃圾回收GC的方法,该方法包括:电子设备运行应用APP;当APP的进程请求分配第一内存时,电子设备检测进程预计占用的堆内存除以目标利用率的数值是否等于或大于第一阈值,其中,进程预计占用的堆内存包括进程已占用的堆内存和第一内存;当进程预计占用的堆内存除以目标利用率的数值等于或大于第一阈值时,电子设备检测进程对应的堆区数据压缩比是否等于或大于第二阈值;当进程对应的堆区数据压缩比等于或大于第二阈值,且电子设备当前的CPU负载大于第三阈值时,电子设备延迟进程执行垃圾回收。In the first aspect, this application provides a method for garbage collection GC. The method includes: an electronic device runs an application APP; when the APP process requests to allocate the first memory, the electronic device detects that the heap memory expected to be occupied by the process is divided by the target utilization. Whether the value is equal to or greater than the first threshold, where the heap memory expected to be occupied by the process includes the heap memory 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 is equal to or greater than the first threshold , the electronic device detects whether the heap data compression ratio corresponding to the process is equal to or greater than the second threshold; 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 The device delays the process to perform garbage collection.
可以理解的,考虑到电子设备处于低内存场景时,电子设备的内核会按照一定策略(例如LRU)对堆区数据进行压缩。那么,当电子设备执行GC时,可能需要对压缩的堆区数据进行解压,解压的过程既要消耗系统的内存,加重系统内存的紧张,又要消耗系统的CPU资源。可见,如果在CPU高负载的场景(即电子设备当前的CPU 负载大于第三阈值)下执行GC,容易影响系统的流畅性和稳定性,甚至可能造成系统崩溃。为此,电子设备统计各个进程的堆区数据的压缩比,而后根据堆区数据的压缩比和CPU的负载情况,采用不同的延迟策略,延迟执行GC,由此提升系统的流畅性和稳定性,避免系统崩溃的问题。It is understandable that when the electronic device is in a low-memory scenario, the kernel of the electronic device will compress the heap area data according to a certain strategy (such as LRU). Then, when the electronic device performs GC, it may be necessary to decompress the compressed heap area data. The decompression process not only consumes the system's memory, aggravates the system memory tension, but also consumes the system's CPU resources. It can be seen that if in a scenario with high CPU load (that is, the current CPU of electronic equipment Executing GC when the load is greater than the third threshold) can easily affect the fluency and stability of the system, and may even cause the system to crash. To this end, the electronic device counts the compression ratio of the heap data of each process, and then uses different delay strategies to delay the execution of GC based on the compression ratio of the heap data and the load of the CPU, thus improving the fluency and stability of the system. , to avoid system crash problems.
在一种可能的实现方式中,当进程对应的堆区数据压缩比等于或大于第二阈值,且电子设备当前的CPU负载大于第三阈值时,电子设备延迟进程执行垃圾回收,包括:当进程对应的堆区数据压缩比等于或大于第二阈值,且电子设备当前的CPU负载大于第三阈值时,电子设备上调第一阈值。In a possible implementation, 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 delays the process to perform garbage collection, including: when the process When the corresponding heap area data compression ratio 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 increases the first threshold.
可以理解的是,上调第一阈值意味着上调了本进程使用堆内存的上限。在一些场景中,上调第一阈值后,能够使得本进程预计占用的堆内存除以目标利用率的数值小于上调后的第一阈值,那么能够完成第一内存的分配。在该场景下,电子设备通过上调第一阈值的方法减少了电子设备执行轻量级GC的次数。It is understandable that raising the first threshold means raising the upper limit of heap memory used by this process. In some scenarios, after raising the first threshold, the value of the heap memory expected to be occupied by the process divided by the target utilization can be smaller than the raised first threshold, and then the allocation of the first memory can be completed. In this scenario, the electronic device reduces the number of times the electronic device performs lightweight GC by raising the first threshold.
在其他一些场景中,上调第一阈值后,本进程预计占用的堆内存除以目标利用率的数值仍然可能等于或大于上调后的第一阈值。例如,本进程的其他任务可能在该时间段也申请分配了堆内存,造成本进程预计占用的堆内存除以目标利用率的数值仍然等于或大于上调后的第一阈值。那么,不能完成第一内存的分配,仍然需要执行轻量级GC回收更多的内存。可见在该场景下,电子设备也通过上调第一阈值的方法延迟了电子设备执行轻量级GC的时机,避免在当前CPU负载过重时执行轻量级GC,有利于保证系统的流畅性和稳定性。In some other scenarios, after the first threshold is raised, the value of the heap memory expected to be occupied by this process divided by the target utilization may still be equal to or greater than the first threshold after the raise. For example, other tasks of this process may also apply for allocation of heap memory during this time period, causing the value of the heap memory expected to be occupied by this process divided by the target utilization to still be equal to or greater than the first threshold after the increase. Then, the first memory allocation cannot be completed, and lightweight GC still needs to be executed to reclaim more memory. It can be seen that in this scenario, the electronic device also delays the opportunity for the electronic device to execute lightweight GC by raising the first threshold, thereby avoiding executing lightweight GC when the current CPU load is too heavy, which is conducive to ensuring the smoothness and stability of the system. stability.
在一种可能的实现方式中,在电子设备上调第一阈值后,该方法还包括:电子设备检测进程预计占用的堆内存除以目标利用率的数值是否等于或大于上调后的第一阈值;当电子设备检测进程预计占用的堆内存除以目标利用率的数值等于或大于上调后的第一阈值时,电子设备执行轻量级GC。In a possible implementation, after the electronic device raises the first threshold, the method further includes: the electronic device detects 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 raised first threshold; When the value of the heap memory expected to be occupied by the electronic device detection process divided by the target utilization is equal to or greater than the raised first threshold, the electronic device performs a lightweight GC.
可以理解的是,相较于全量级GC,电子设备执行轻量级GC占用的内存资源和CPU资源本身较少。由于电子设备执行轻量级GC时并未扫描全部的堆区数据,那么扫描到的压缩数据也较少,因电子设备压缩行为占用的额外的内存资源和CPU资源也不会太多,有利于保证系统的流畅性和稳定性。It is understandable that, compared to full-weight GC, electronic devices performing lightweight GC occupy less memory resources and CPU resources themselves. Since the electronic device does not scan all the heap area data when executing lightweight GC, the scanned compressed data is also less, and the additional memory resources and CPU resources occupied by the compression behavior of the electronic device will not be too much, which is beneficial to Ensure system fluency and stability.
在一种可能的实现方式中,在电子设备执行轻量级GC之后,该方法还包括:电子设备检测进程预计占用的堆内存除以目标利用率的数值是否等于或大于上调后的第一阈值;当进程预计占用的堆内存除以目标利用率的数值等于或大于上调后的第一阈值时,电子设备再次上调第一阈值。In a possible implementation, after the electronic device performs the lightweight GC, the method further includes: the electronic device detects 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 first threshold after the increase. ; 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 raised first threshold, the electronic device raises the first threshold again.
也就是说,在电子设备执行轻量级GC后,如果电子设备当前的内存情况仍然不能满足需求,即本进程预计占用的堆内存除以目标利用率的数值仍等于或大于第一阈值,那么电子设备再次上调第一阈值。通过再次上调第一阈值的方法,再次减少电子设备执行全量级GC的次数,或者延长电子设备执行全量级GC的时机。That is to say, after the electronic device performs lightweight GC, if the current memory situation of the electronic device still cannot meet the demand, that is, the value of the heap memory expected to be occupied by this process divided by the target utilization 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 full-scale GC is again reduced, or the opportunity for the electronic device to perform full-scale GC is extended.
在一种可能的实现方式中,在电子设备再次上调第一阈值之后,该方法还包括:当进程预计占用的堆内存除以目标利用率的数值等于或大于再次上调后的第一阈值时,电子设备再次检测进程对应的堆区数据压缩比;电子设备根据进程对应的堆区数据压缩比采用不同的策略,延迟执行全量级垃圾回收GC。 In a possible implementation, after the electronic device raises the first threshold again, the method further includes: 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 first threshold after being raised again, The electronic device again detects the heap data compression ratio corresponding to the process; the electronic device adopts different strategies based on the heap data compression ratio corresponding to the process and delays the execution of full-scale garbage collection GC.
也就是说,本申请实施例提出一种检测堆区数据压缩比的方法,使得电子设备内核的GC线程可以感知堆区数据的压缩量级(即堆区数据的压缩比),从而便于根据堆区数据的压缩量级,进行高效的GC管控。在堆区数据的压缩量级不同的情况下,结合电子设备当前CPU的负载采用不同策略减少电子设备执行GC(轻量级GC和全量级GC)的次数或者延迟电子设备执行GC(轻量级GC和全量级GC)的时机,避免电子设备在CPU高负载的情况下执行GC。由此,本申请降低了电子设备在执行GC时整机占用的系统资源(包括内存资源和CPU资源),有利于保证系统的流畅性和稳定性。That is to say, the embodiment of the present application proposes a method for detecting the compression ratio of heap area data, so that the GC thread of the electronic device core can sense the compression level of the heap area data (that is, the compression ratio of the heap area data), thereby facilitating the heap area data compression ratio according to the heap area data compression ratio. The compression level of area data enables efficient GC control. When the compression levels of heap area data are different, different strategies are adopted based on the current CPU load of the electronic device to reduce the number of times the electronic device performs GC (lightweight GC and full-weight GC) or delay the execution of GC (lightweight GC) by the electronic device. level GC and full-scale GC) to prevent electronic equipment from executing GC under high CPU load. Therefore, this application reduces the system resources (including memory resources and CPU resources) occupied by the electronic device when executing GC, which is beneficial to ensuring the smoothness and stability of the system.
在一种可能的实现方式中,电子设备根据进程对应的堆区数据压缩比采用不同的策略,延迟执行全量级垃圾回收GC,包括:当进程对应的堆区数据压缩比位于第一区间时,电子设备避开CPU负载的高峰期执行全量级GC;当进程对应的堆区数据压缩比位于第二区间时,电子设备在CPU空闲时执行全量级GC;当进程对应的堆区数据压缩比位于第三区间时,电子设备冻结进程,并在接收到解冻进程的指令后,解冻进程并执行全量级GC。In one possible implementation, the electronic device adopts different strategies according to the heap data compression ratio corresponding to the process to delay the execution of full-scale garbage collection GC, including: when the heap data compression ratio corresponding to the process is in the first interval , the electronic device performs full-scale GC to avoid the peak period of CPU load; when the heap data compression ratio corresponding to the process is in the second interval, the electronic device performs full-scale GC when the CPU is idle; when the heap data compression ratio corresponding to the process When the compression ratio is in the third range, the electronic device freezes the process, and after receiving the instruction to unfreeze the process, unfreezes the process and executes a full-scale GC.
在一种可能的实现方式中,在电子设备上调第一阈值后,该方法还包括:当进程预计占用的堆内存除以目标利用率的数值等于或大于上调后的第一阈值时,电子设备再次检测进程对应的堆区数据压缩比;电子设备根据进程对应的堆区数据压缩比采用不同的策略,延迟执行全量级垃圾回收GC。In a 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 is equal to or greater than the raised first threshold, the electronic device The heap data compression ratio corresponding to the process is detected again; the electronic device adopts different strategies based on the heap data compression ratio corresponding to the process to delay the execution of full-scale garbage collection GC.
在一种可能的实现方式中,电子设备根据进程对应的堆区数据压缩比采用不同的策略,延迟执行全量级垃圾回收GC,包括:当进程对应的堆区数据压缩比位于第一区间时,电子设备避开CPU负载的高峰期执行全量级GC;当进程对应的堆区数据压缩比位于第二区间时,电子设备在CPU空闲时执行全量级GC;当进程对应的堆区数据压缩比位于第三区间时,电子设备冻结进程,并在接收到解冻进程的指令后,解冻进程并执行全量级GC。In one possible implementation, the electronic device adopts different strategies according to the heap data compression ratio corresponding to the process to delay the execution of full-scale garbage collection GC, including: when the heap data compression ratio corresponding to the process is in the first interval , the electronic device performs full-scale GC to avoid the peak period of CPU load; when the heap data compression ratio corresponding to the process is in the second interval, the electronic device performs full-scale GC when the CPU is idle; when the heap data compression ratio corresponding to the process When the compression ratio is in the third range, the electronic device freezes the process, and after receiving the instruction to unfreeze the process, unfreezes the process and executes a full-scale GC.
在一种可能的实现方式中,该方法还包括:在进程申请堆内存时,电子设备对进程申请的堆内存进行标记。In a possible implementation manner, the method further includes: when the process applies for heap memory, the electronic device marks the heap memory applied for by the process.
在一种可能的实现方式中,该方法还包括:在电子设备压缩进程的堆区数据时,电子设备记录压缩的堆区数据的数据量。In a possible implementation, the method further includes: when the electronic device compresses the heap data of the process, the electronic device records the data amount of the compressed heap data.
在一种可能的实现方式中,进程的堆区数据压缩比,为进程中压缩的堆区数据的数据量与本进程中全部堆区数据的数据量的比值。In one possible implementation, the heap area data compression ratio of a process is the ratio of the data amount of compressed heap area data in the process to the data amount of all heap area data in the process.
第二方面、提供一种电子设备,包括:处理器、存储器和触摸屏,所述存储器、所述触摸屏与所述处理器耦合,所述存储器用于存储计算机程序代码,所述计算机程序代码包括计算机指令,当所述处理器从所述存储器中读取所述计算机指令,以使得电子设备执行如上述方面及其中任一种可能的实现方式中所述的方法。In a second aspect, an electronic device is provided, including: a processor, a memory and a touch screen. The memory, the touch screen are coupled to the processor. The memory is used to store computer program code. The computer program code includes a computer program. Instructions, when the processor reads the computer instructions from the memory, so that the electronic device executes the method described in the above aspect and any possible implementation manner thereof.
第三方面、提供一种装置,该装置包含在电子设备中,该装置具有实现上述方面及可能的实现方式中任一方法中电子设备行为的功能。该功能可以通过硬件实现,也可以通过硬件执行相应的软件实现。硬件或软件包括至少一个与上述功能相对应的模块或单元。例如,检测模块或单元、确定模块或单元、以及处理模块或单元等。A third aspect is to provide a device, which is included in an electronic device and has the function of realizing the behavior of the electronic device in any of the above aspects and possible implementation methods. This function can be implemented by hardware, or it can be implemented by hardware executing corresponding software. The hardware or software includes at least one module or unit corresponding to the above functions. For example, a detection module or unit, a determination module or unit, a processing module or unit, etc.
第四方面、提供一种计算机可读存储介质,包括计算机指令,当计算机指令在电 子设备上运行时,使得电子设备执行如上述方面及其中任一种可能的实现方式中所述的方法。In the fourth aspect, a computer-readable storage medium is provided, including computer instructions. When the computer instructions are stored in a computer When running on the sub-device, the electronic device is caused to perform the method described in the above aspect and any possible implementation manner.
第五方面、提供一种计算机程序产品,当计算机程序产品在计算机上运行时,使得计算机执行如上述方面中及其中任一种可能的实现方式中所述的方法。In a fifth aspect, a computer program product is provided. When the computer program product is run on a computer, it causes the computer to execute the method described in the above aspects and any of the possible implementations.
第六方面、提供一种芯片系统,包括处理器,当处理器执行指令时,处理器执行如上述方面中及其中任一种可能的实现方式中所述的方法。A sixth aspect provides a chip system, including a processor. When the processor executes instructions, the processor executes the method described in the above aspects and any of the possible implementations.
上述第二方面提供的电子设备、第三方面提供的装置、第四方面提供的计算机可读存储介质、第五方面提供的计算机程序产品,以及第六方面提供的芯片系统所能达到的技术效果,请参考上述第一方面以及第一方面中任一可能的实现方式中所描述的技术效果,这里不再赘述。The technical effects achieved by the electronic equipment provided by the second aspect, the device provided by the third aspect, the computer-readable storage medium provided by the fourth aspect, the computer program product provided by the fifth aspect, and the chip system provided by the sixth aspect , please refer to the technical effects described in the above first aspect and any possible implementation of the first aspect, which will not be described again here.
附图说明Description of the drawings
图1为本申请实施例提供的一种电子设备的硬件结构示意图;Figure 1 is a schematic diagram of the hardware structure of an electronic device provided by an embodiment of the present application;
图2为本申请实施例提供的一种电子设备的软件结构示意图;Figure 2 is a schematic diagram of the software structure of an electronic device provided by an embodiment of the present application;
图3为本申请实施例提供的一种电子设备执行GC的流程示意图;Figure 3 is a schematic flowchart of an electronic device performing GC according to an embodiment of the present application;
图4为本申请实施例提供的另一种电子设备执行GC的流程示意图;Figure 4 is a schematic flowchart of another electronic device performing GC according to an embodiment of the present application;
图5为本申请实施例提供的一种芯片系统的结构示意图。FIG. 5 is a schematic structural diagram of a chip system provided by an embodiment of the present application.
具体实施方式Detailed ways
在本申请实施例的描述中,除非另有说明,“/”表示或的意思,例如,A/B可以表示A或B;本文中的“和/或”仅仅是一种描述关联对象的联动关系,表示可以存在三种关系,例如,A和/或B,可以表示:单独存在A,同时存在A和B,单独存在B这三种情况。In the description of the embodiments of this application, unless otherwise stated, "/" means or, for example, A/B can mean A or B; "and/or" in this article is just a linkage describing related objects. Relationship means that three relationships can exist. For example, A and/or B can mean: A exists alone, A and B exist simultaneously, and B exists alone.
以下,术语“第一”、“第二”仅用于描述目的,而不能理解为指示或暗示相对重要性或者隐含指明所指示的技术特征的数量。由此,限定有“第一”、“第二”的特征可以明示或者隐含地包括一个或者更多个该特征。在本申请实施例的描述中,除非另有说明,“多个”的含义是两个或两个以上。Hereinafter, the terms “first” and “second” are used for descriptive purposes only and cannot be understood as indicating or implying relative importance or implicitly indicating the quantity of indicated technical features. Therefore, features defined as "first" and "second" may explicitly or implicitly include one or more of these features. In the description of the embodiments of this application, unless otherwise specified, "plurality" means two or more.
在本申请实施例中,“示例性的”或者“例如”等词用于表示作例子、例证或说明。本申请实施例中被描述为“示例性的”或者“例如”的任何实施例或设计方案不应被解释为比其它实施例或设计方案更优选或更具优势。确切而言,使用“示例性的”或者“例如”等词旨在以具体方式呈现相关概念。In the embodiments of this application, words such as "exemplary" or "for example" are used to represent examples, illustrations or explanations. Any embodiment or design described as "exemplary" or "such as" in the embodiments of the present application is not to be construed as preferred or advantageous over other embodiments or designs. Rather, use of the words "exemplary" or "such as" is intended to present the concept in a concrete manner.
本申请实施例提供一种垃圾回收GC的方法,可应用于安装有一个或多个APP的电子设备。示例性的,本申请实施例中电子设备例如可以为手机、平板电脑、个人计算机(personal computer,PC)、个人数字助理(personal digital assistant,PDA)、智能手表、上网本、可穿戴电子设备、增强现实技术(augmented reality,AR)设备、虚拟现实(virtual reality,VR)设备、车载设备、智慧屏、智能汽车、智能音响、机器人等,本申请对该电子设备的具体形式不做特殊限制。The embodiment of the present application provides a method of garbage collection GC, which can be applied to electronic devices installed with one or more APPs. Illustratively, the electronic device in the embodiment of the present application may be a mobile phone, a tablet computer, a personal computer (PC), a personal digital assistant (PDA), a smart watch, a netbook, a wearable electronic device, an enhanced Reality technology (augmented reality, AR) equipment, virtual reality (VR) equipment, vehicle-mounted equipment, smart screens, smart cars, smart speakers, robots, etc. This application does not place special restrictions on the specific forms of this electronic equipment.
图1示出了电子设备100的结构示意图。FIG. 1 shows a schematic structural diagram of an electronic device 100 .
电子设备100可以包括处理器110,外部存储器接口120,内部存储器121,通用串行总线(universal serial bus,USB)接口130,充电管理模块140,电源管理模块141,电池142,天线1,天线2,移动通信模块150,无线通信模块160,音频模块170,扬 声器170A,受话器170B,麦克风170C,耳机接口170D,传感器模块180,按键190,马达191,指示器192,摄像头193,显示屏194,以及用户标识模块(subscriber identification module,SIM)卡接口195等。其中传感器模块180可以包括压力传感器180A,陀螺仪传感器180B,气压传感器180C,磁传感器180D,加速度传感器180E,距离传感器180F,接近光传感器180G,指纹传感器180H,温度传感器180J,触摸传感器180K,环境光传感器180L,骨传导传感器180M等。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 , mobile communication module 150, wireless communication module 160, audio module 170, Yang Speaker 170A, receiver 170B, microphone 170C, headphone interface 170D, sensor module 180, button 190, motor 191, indicator 192, camera 193, display screen 194, and subscriber identification module (subscriber identification module, SIM) card interface 195, etc. . The sensor module 180 may include a pressure sensor 180A, a gyro sensor 180B, an air pressure sensor 180C, a magnetic sensor 180D, an acceleration sensor 180E, a distance sensor 180F, a proximity light sensor 180G, a fingerprint sensor 180H, a temperature sensor 180J, a touch sensor 180K, and ambient light. Sensor 180L, bone conduction sensor 180M, etc.
可以理解的是,本发明实施例示意的结构并不构成对电子设备100的具体限定。在本申请另一些实施例中,电子设备100可以包括比图示更多或更少的部件,或者组合某些部件,或者拆分某些部件,或者不同的部件布置。图示的部件可以以硬件,软件或软件和硬件的组合实现。It can be understood that the structure illustrated in the embodiment of the present invention does not constitute a specific limitation on the electronic device 100 . In other embodiments of the present application, the electronic device 100 may include more or fewer components than shown in the figures, or some components may be combined, some components may be separated, or some components may be arranged differently. The components illustrated may be implemented in hardware, software, or a combination of software and hardware.
处理器110可以包括一个或多个处理单元,例如:处理器110可以包括应用处理器(application processor,AP),调制解调处理器,图形处理器(graphics processing unit,GPU),图像信号处理器(image signal processor,ISP),控制器,视频编解码器,数字信号处理器(digital signal processor,DSP),基带处理器,和/或神经网络处理器(neural-network processing unit,NPU)等。其中,不同的处理单元可以是独立的器件,也可以集成在一个或多个处理器中。The processor 110 may include one or more processing units. For example, the processor 110 may include an application processor (application processor, AP), a modem processor, a graphics processing unit (GPU), and an image signal processor. (image signal processor, ISP), controller, video codec, digital signal processor (digital signal processor, DSP), baseband processor, and/or neural network processor (neural-network processing unit, NPU), etc. Among them, different processing units can be independent devices or integrated in one or more processors.
控制器可以根据指令操作码和时序信号,产生操作控制信号,完成取指令和执行指令的控制。The controller can generate operation control signals based on the instruction operation code and timing signals to complete the control of fetching and executing instructions.
处理器110中还可以设置存储器,用于存储指令和数据。在一些实施例中,处理器110中的存储器为高速缓冲存储器。该存储器可以保存处理器110刚用过或循环使用的指令或数据。如果处理器110需要再次使用该指令或数据,可从所述存储器中直接调用。避免了重复存取,减少了处理器110的等待时间,因而提高了系统的效率。The processor 110 may also be provided with a memory for storing instructions and data. In some embodiments, the memory in processor 110 is cache memory. This memory may hold instructions or data that have been recently used or recycled by processor 110 . If the processor 110 needs to use the instructions or data again, it can be called directly from the memory. Repeated access is avoided and the waiting time of the processor 110 is reduced, thus improving the efficiency of the system.
在一些实施例中,处理器110可以包括一个或多个接口。接口可以包括集成电路(inter-integrated circuit,I2C)接口,集成电路内置音频(inter-integrated circuit sound,I2S)接口,脉冲编码调制(pulse code modulation,PCM)接口,通用异步收发传输器(universal asynchronous receiver/transmitter,UART)接口,移动产业处理器接口(mobile industry processor interface,MIPI),通用输入输出(general-purpose input/output,GPIO)接口,用户标识模块(subscriber identity module,SIM)接口,和/或通用串行总线(universal serial bus,USB)接口等。可以理解的是,本发明实施例示意的各模块间的接口连接关系,只是示意性说明,并不构成对电子设备100的结构限定。在本申请另一些实施例中,电子设备100也可以采用上述实施例中不同的接口连接方式,或多种接口连接方式的组合。In some embodiments, processor 110 may include one or more interfaces. Interfaces may include integrated circuit (inter-integrated circuit, I2C) interface, integrated circuit built-in audio (inter-integrated circuit sound, I2S) interface, pulse code modulation (pulse code modulation, PCM) interface, universal asynchronous receiver and transmitter (universal asynchronous receiver/transmitter (UART) interface, mobile industry processor interface (MIPI), general-purpose input/output (GPIO) interface, subscriber identity module (SIM) interface, and /or universal serial bus (USB) interface, etc. It can be understood that the interface connection relationships between the modules illustrated in the embodiment of the present invention are only schematic illustrations and do not constitute a structural limitation of the electronic device 100 . In other embodiments of the present application, the electronic device 100 may also adopt different interface connection methods in the above embodiments, or a combination of multiple interface connection methods.
充电管理模块140用于从充电器接收充电输入。其中,充电器可以是无线充电器,也可以是有线充电器。在一些有线充电的实施例中,充电管理模块140可以通过USB接口130接收有线充电器的充电输入。在一些无线充电的实施例中,充电管理模块140可以通过电子设备100的无线充电线圈接收无线充电输入。充电管理模块140为电池142充电的同时,还可以通过电源管理模块141为电子设备供电。The charging management module 140 is used to receive charging input from the charger. Among them, the charger can be a wireless charger or a wired charger. In some wired charging embodiments, the charging management module 140 may receive charging input from the wired charger through the USB interface 130 . In some wireless charging embodiments, the charging management module 140 may receive wireless charging input through the wireless charging coil of the electronic device 100 . While the charging management module 140 charges the battery 142, it can also provide power to the electronic device through the power management module 141.
电源管理模块141用于连接电池142,充电管理模块140与处理器110。电源管理模块141接收电池142和/或充电管理模块140的输入,为处理器110,内部存储器121, 显示屏194,摄像头193,和无线通信模块160等供电。电源管理模块141还可以用于监测电池容量,电池循环次数,电池健康状态(漏电,阻抗)等参数。在其他一些实施例中,电源管理模块141也可以设置于处理器110中。在另一些实施例中,电源管理模块141和充电管理模块140也可以设置于同一个器件中。The power management module 141 is used to connect the battery 142, the charging management module 140 and the processor 110. Power management module 141 receives input from battery 142 and/or charge management module 140 for processor 110, internal memory 121, The display screen 194, the camera 193, the wireless communication module 160, etc. are powered. The power management module 141 can also be used to monitor battery capacity, battery cycle times, battery health status (leakage, impedance) and other parameters. In some other embodiments, the power management module 141 may also be provided in the processor 110 . In other embodiments, the power management module 141 and the charging management module 140 may also be provided in the same device.
电子设备100的无线通信功能可以通过天线1,天线2,移动通信模块150,无线通信模块160,调制解调处理器以及基带处理器等实现。The wireless communication function of the electronic device 100 can be implemented through the antenna 1, the antenna 2, the mobile communication module 150, the wireless communication module 160, the modem processor and the baseband processor.
天线1和天线2用于发射和接收电磁波信号。电子设备100中的每个天线可用于覆盖单个或多个通信频带。不同的天线还可以复用,以提高天线的利用率。例如:可以将天线1复用为无线局域网的分集天线。在另外一些实施例中,天线可以和调谐开关结合使用。Antenna 1 and Antenna 2 are used to transmit and receive electromagnetic wave signals. Each antenna in electronic device 100 may be used to cover a single or multiple communication frequency bands. Different antennas can also be reused to improve antenna utilization. For example: Antenna 1 can be reused as a diversity antenna for a wireless LAN. In other embodiments, antennas may be used in conjunction with tuning switches.
移动通信模块150可以提供应用在电子设备100上的包括2G/3G/4G/5G等无线通信的解决方案。移动通信模块150可以包括至少一个滤波器,开关,功率放大器,低噪声放大器(low noise amplifier,LNA)等。移动通信模块150可以由天线1接收电磁波,并对接收的电磁波进行滤波,放大等处理,传送至调制解调处理器进行解调。移动通信模块150还可以对经调制解调处理器调制后的信号放大,经天线1转为电磁波辐射出去。在一些实施例中,移动通信模块150的至少部分功能模块可以被设置于处理器110中。在一些实施例中,移动通信模块150的至少部分功能模块可以与处理器110的至少部分模块被设置在同一个器件中。The mobile communication module 150 can provide solutions for wireless communication including 2G/3G/4G/5G applied on 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 through the antenna 1, perform filtering, amplification and other processing on the received electromagnetic waves, and transmit them to the 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 through the antenna 1 for radiation. In some embodiments, at least part of the functional modules of the mobile communication module 150 may be disposed in the processor 110 . In some embodiments, at least part of the functional modules of the mobile communication module 150 and at least part of the modules of the processor 110 may be provided in the same device.
调制解调处理器可以包括调制器和解调器。其中,调制器用于将待发送的低频基带信号调制成中高频信号。解调器用于将接收的电磁波信号解调为低频基带信号。随后解调器将解调得到的低频基带信号传送至基带处理器处理。低频基带信号经基带处理器处理后,被传递给应用处理器。应用处理器通过音频设备(不限于扬声器170A,受话器170B等)输出声音信号,或通过显示屏194显示图像或视频。在一些实施例中,调制解调处理器可以是独立的器件。在另一些实施例中,调制解调处理器可以独立于处理器110,与移动通信模块150或其他功能模块设置在同一个器件中。A modem processor may include a modulator and a demodulator. Among them, the modulator is used to modulate the low-frequency baseband signal to be sent into a medium-high frequency signal. The demodulator is used to demodulate the received electromagnetic wave signal into a low-frequency baseband signal. The demodulator then transmits the demodulated low-frequency baseband signal to the baseband processor for processing. After the low-frequency baseband signal is processed by the baseband processor, it is passed to the application processor. The application processor outputs sound signals through audio devices (not limited to speaker 170A, receiver 170B, etc.), or displays images or videos through display screen 194. In some embodiments, the modem processor may be a stand-alone device. In other embodiments, the modem processor may be independent of the processor 110 and may be provided in the same device as the mobile communication module 150 or other functional modules.
无线通信模块160可以提供应用在电子设备100上的包括无线局域网(wireless local area networks,WLAN)(如无线保真(wireless fidelity,Wi-Fi)网络),蓝牙(bluetooth,BT),全球导航卫星系统(global navigation satellite system,GNSS),调频(frequency modulation,FM),近距离无线通信技术(near field communication,NFC),红外技术(infrared,IR)等无线通信的解决方案。无线通信模块160可以是集成至少一个通信处理模块的一个或多个器件。无线通信模块160经由天线2接收电磁波,将电磁波信号调频以及滤波处理,将处理后的信号发送到处理器110。无线通信模块160还可以从处理器110接收待发送的信号,对其进行调频,放大,经天线2转为电磁波辐射出去。The wireless communication module 160 can provide applications on the electronic device 100 including wireless local area networks (WLAN) (such as wireless fidelity (Wi-Fi) network), Bluetooth (bluetooth, BT), and global navigation satellites. System (global navigation satellite system, GNSS), frequency modulation (frequency modulation, FM), near field communication technology (near field communication, NFC), infrared technology (infrared, IR) and other wireless communication solutions. The wireless communication module 160 may be one or more devices integrating at least one communication processing module. The wireless communication module 160 receives electromagnetic waves via the antenna 2 , frequency modulates and filters the electromagnetic wave signals, and sends the processed signals to the processor 110 . The wireless communication module 160 can also receive the signal to be sent from the processor 110, frequency modulate it, amplify it, and convert it into electromagnetic waves through the antenna 2 for radiation.
电子设备100通过GPU,显示屏194,以及应用处理器等实现显示功能。GPU为图像处理的微处理器,连接显示屏194和应用处理器。GPU用于执行数学和几何计算,用于图形渲染。处理器110可包括一个或多个GPU,其执行程序指令以生成或改变显示信息。The electronic device 100 implements display functions through a GPU, a display screen 194, an application processor, and the like. The GPU is an image processing microprocessor and is connected to the display screen 194 and the application processor. GPUs are used to perform mathematical and geometric calculations for graphics rendering. Processor 110 may include one or more GPUs that execute program instructions to generate or alter display information.
显示屏194用于显示图像,视频等。显示屏194包括显示面板。显示面板可以采用液晶显示屏(liquid crystal display,LCD),有机发光二极管(organic light-emitting diode, OLED),有源矩阵有机发光二极体或主动矩阵有机发光二极体(active-matrix organic light emitting diode的,AMOLED),柔性发光二极管(flex light-emitting diode,FLED),Miniled,MicroLed,Micro-oLed,量子点发光二极管(quantum dot light emitting diodes,QLED)等。在一些实施例中,电子设备100可以包括1个或N个显示屏194,N为大于1的正整数。The display screen 194 is used to display images, videos, etc. Display 194 includes a display panel. The display panel can use a liquid crystal display (LCD), an organic light-emitting diode (organic light-emitting diode, OLED), active matrix organic light emitting diode or active matrix organic light emitting diode (active-matrix organic light emitting diode, AMOLED), flexible light-emitting diode (flex light-emitting diode, FLED), Miniled, MicroLed, Micro -oLed, quantum dot light emitting diodes (QLED), etc. In some embodiments, the electronic device 100 may include 1 or N display screens 194, where N is a positive integer greater than 1.
电子设备100可以通过ISP,摄像头193,视频编解码器,GPU,显示屏194以及应用处理器等实现拍摄功能。The electronic device 100 can implement the shooting function through an ISP, a camera 193, a video codec, a GPU, a display screen 194, an application processor, and the like.
ISP用于处理摄像头193反馈的数据。摄像头193用于捕获静态图像或视频。数字信号处理器用于处理数字信号,除了可以处理数字图像信号,还可以处理其他数字信号。例如,当电子设备100在频点选择时,数字信号处理器用于对频点能量进行傅里叶变换等。视频编解码器用于对数字视频压缩或解压缩。电子设备100可以支持一种或多种视频编解码器。这样,电子设备100可以播放或录制多种编码格式的视频,例如:动态图像专家组(moving picture experts group,MPEG)1,MPEG2,MPEG3,MPEG4等。The ISP is used to process the data fed back by the camera 193. Camera 193 is used to capture still images or video. Digital signal processors are used to process digital signals. In addition to digital image signals, they can also process other digital signals. For example, when the electronic device 100 selects a frequency point, the digital signal processor is used to perform Fourier transform on the frequency point energy. Video codecs are used to compress or decompress digital video. Electronic device 100 may support one or more video codecs. In this way, the electronic device 100 can play or record videos in multiple encoding formats, such as moving picture experts group (MPEG) 1, MPEG2, MPEG3, MPEG4, etc.
外部存储器接口120可以用于连接外部存储卡,例如Micro SD卡,实现扩展电子设备100的存储能力。外部存储卡通过外部存储器接口120与处理器110通信,实现数据存储功能。例如将音乐,视频等文件保存在外部存储卡中。The external memory 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 memory interface 120 to implement the data storage function. Such as saving music, videos, etc. files in external memory card.
内部存储器121可以用于存储计算机可执行程序代码,所述可执行程序代码包括指令。内部存储器121可以包括存储程序区和存储数据区。其中,存储程序区可存储操作系统,至少一个功能所需的应用程序(比如声音播放功能,图像播放功能等)等。存储数据区可存储电子设备100使用过程中所创建的数据(比如音频数据,电话本等)等。此外,内部存储器121可以包括高速随机存取存储器,还可以包括非易失性存储器,例如至少一个磁盘存储器件,闪存器件,通用闪存存储器(universal flash storage,UFS)等。处理器110通过运行存储在内部存储器121的指令,和/或存储在设置于处理器中的存储器的指令,执行电子设备100的各种功能应用以及数据处理。Internal memory 121 may be used to store computer executable program code, which includes instructions. The internal memory 121 may include a program storage area and a data storage area. Among them, the stored program area can store an operating system, at least one application program required for a function (such as a sound playback function, an image playback function, etc.). The storage data area may store data created during use of the electronic device 100 (such as audio data, phone book, etc.). In addition, the 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. The processor 110 executes various functional applications and data processing of the electronic device 100 by executing instructions stored in the internal memory 121 and/or instructions stored in a memory provided in the processor.
电子设备100可以通过音频模块170,扬声器170A,受话器170B,麦克风170C,耳机接口170D,以及应用处理器等实现音频功能。例如音乐播放,录音等。The electronic device 100 can implement audio functions through the audio module 170, the speaker 170A, the receiver 170B, the microphone 170C, the headphone interface 170D, and the application processor. Such as music playback, recording, etc.
按键190包括开机键,音量键等。按键190可以是机械按键。也可以是触摸式按键。电子设备100可以接收按键输入,产生与电子设备100的用户设置以及功能控制有关的键信号输入。The buttons 190 include a power button, a volume button, etc. Key 190 may be a mechanical key. It can also be a touch button. The electronic device 100 may receive key inputs and generate key signal inputs related to user settings and function control of the electronic device 100 .
电子设备100的软件系统可以采用分层架构,事件驱动架构,微核架构,微服务架构,或云架构。例如,该电子设备100的软件系统可以是鸿蒙系统、Android系统等。本发明实施例以分层架构的Android系统为例,示例性说明电子设备100的软件结构。The software system of the electronic device 100 may adopt a layered architecture, an event-driven architecture, a microkernel architecture, a microservice architecture, or a cloud architecture. For example, the software system of the electronic device 100 may be the Hongmeng system, Android system, etc. This embodiment of the present invention takes the Android system with a layered architecture as an example to illustrate the software structure of the electronic device 100 .
图2是本发明实施例的电子设备100的软件结构框图。FIG. 2 is a software structure block diagram of the electronic device 100 according to the embodiment of the present invention.
分层架构将软件分成若干个层,每一层都有清晰的角色和分工。层与层之间通过软件接口通信。在一些实施例中,将Android系统分为五层,从上至下分别为应用程序层(简称应用层),应用程序框架层(简称框架层),安卓运行时(Android Runtime)和系统库,硬件抽象层(Hardware Abstraction Layer,HAL)以及内核层。The layered architecture divides the software into several layers, and each layer has clear roles and division of labor. The layers communicate through software interfaces. In some embodiments, the Android system is divided into five layers, from top to bottom: application layer (referred to as application layer), application framework layer (referred to as framework layer), Android Runtime (Android Runtime) and system library. Hardware Abstraction Layer (HAL) and kernel layer.
(1)应用层 (1)Application layer
应用层可以包括一系列应用程序包。如图2所示,应用程序包可以包括相机,图库,日历,通话,地图,导航,WLAN,蓝牙,音乐,视频,短信息等应用程序。The application layer can include a series of application packages. As shown in Figure 2, the application package can include camera, gallery, calendar, calling, map, navigation, WLAN, Bluetooth, music, video, short message and other applications.
(2)框架层(2)Frame layer
框架层为应用层的应用程序提供应用编程接口(application programming interface,API)和编程框架。框架层包括一些预先定义的函数。The framework layer provides application programming interface (API) and programming framework for applications in the application layer. The framework layer includes some predefined functions.
如图2所示,框架层可以包括活动管理器,窗口管理器,内容提供器,视图系统,电话管理器,资源管理器,通知管理器等。As shown in Figure 2, the framework layer can include activity manager, window manager, content provider, view system, phone manager, resource manager, notification manager, etc.
其中,活动管理器,用于负责所有APP进程的启动,APP四大组件(如活动activity、服务service、广播接收者broadcast receiver、内容提供者content provider)的启动,APP之间及APP内部界面之间切换和各种组件的生命周期。窗口管理器用于管理窗口程序。窗口管理器可以获取显示屏大小,判断是否有状态栏,锁定屏幕,截取屏幕等。内容提供器用来存放和获取数据,并使这些数据可以被应用程序访问。所述数据可以包括视频,图像,音频,拨打和接听的电话,浏览历史和书签,电话簿等。视图系统包括可视控件,例如显示文字的控件,显示图片的控件等。视图系统可用于构建应用程序。显示界面可以由一个或多个视图组成的。例如,包括短信通知图标的显示界面,可以包括显示文字的视图以及显示图片的视图。电话管理器用于提供电子设备100的通信功能。例如通话状态的管理(包括接通,挂断等)。资源管理器为应用程序提供各种资源,比如本地化字符串,图标,图片,布局文件,视频文件等等。通知管理器使应用程序可以在状态栏中显示通知信息,可以用于传达告知类型的消息,可以短暂停留后自动消失,无需用户交互。比如通知管理器被用于告知下载完成,消息提醒等。通知管理器还可以是以图表或者滚动条文本形式出现在系统顶部状态栏的通知,例如后台运行的应用程序的通知,还可以是以对话窗口形式出现在屏幕上的通知。例如在状态栏提示文本信息,发出提示音,电子设备振动,指示灯闪烁等。Among them, the activity manager is responsible for the startup of all APP processes, the startup of the four major components of the APP (such as activity, service, broadcast receiver, content provider), the interface between APPs and within the APP. switching and the life cycle of various components. A window manager is used to manage window programs. The window manager can obtain the display size, determine whether there is a status bar, lock the screen, capture the screen, etc. Content providers are used to store and retrieve data and make this data accessible to applications. Said data can include videos, images, audio, calls made and received, browsing history and bookmarks, phone books, etc. The view system includes visual controls, such as controls that display text, controls that display pictures, etc. A view system can be used to build applications. The display interface can be composed of one or more views. For example, a display interface including a text message notification icon may include a view for displaying text and a view for displaying pictures. The phone manager is used to provide communication functions of the electronic device 100 . For example, call status management (including connected, hung up, etc.). The resource manager provides various resources to applications, such as localized strings, icons, pictures, layout files, video files, etc. The notification manager allows applications to display notification information in the status bar, which can be used to convey notification-type messages and can automatically disappear after a short stay without user interaction. For example, the notification manager is used to notify download completion, message reminders, etc. The notification manager can also be notifications that appear in the status bar at the top of the system in the form of charts or scroll bar text, such as notifications for applications running in the background, or notifications that appear on the screen in the form of conversation windows. For example, text information is prompted in the status bar, a beep sounds, the electronic device vibrates, the indicator light flashes, etc.
3、Android Runtime和系统库3. Android Runtime and system libraries
如图2所示,Android Runtime包括核心库和虚拟机。Android Runtime负责安卓系统的调度和管理。As shown in Figure 2, Android Runtime includes core libraries and virtual machines. Android Runtime is responsible for the scheduling and management of the Android system.
核心库包含两部分:一部分是java语言需要调用的功能函数,另一部分是安卓的核心库。The core library contains two parts: one is the functional functions that need to be called by the Java language, and the other is the core library of Android.
应用层和框架层运行在虚拟机中。虚拟机将应用层和框架层的java文件执行为二进制文件。虚拟机用于执行对象生命周期的管理,堆栈管理,线程管理,安全和异常的管理,以及垃圾回收等功能。The application layer and framework layer run in virtual machines. The virtual machine executes the java files of the application layer and framework layer into binary files. The virtual machine is used to perform object life cycle management, stack management, thread management, security and exception management, and garbage collection and other functions.
以电子设备100运行APP1为例进行说明。当电子设备100运行APP1时,通常会启动APP1的一个或多个进程(如APP1的界面对应的进程1,APP1的后台服务对应的进程2),每个进程都将对应一个虚拟机,且每个进程中都包含一个GC线程。该GC线程负责监控本进程对应的虚拟机堆内存的使用情况,并根据本进程对应的虚拟机堆内存的使用情况确定是否启动GC过程。可以理解的是,在一些场景中,当用户将APP1切换到后台运行时,电子设备仍然可能保持运行APP1的一个或多个后台服务对应的进程,这些进程持续占用电子设备的内存,并且这些进程仍然可能继续申请新的堆内存等。 The electronic device 100 runs APP1 as an example for explanation. When the electronic device 100 runs APP1, one or more processes of APP1 will usually be started (such as process 1 corresponding to the interface of APP1, process 2 corresponding to the background service of APP1). Each process will correspond to a virtual machine, and each process will correspond to a virtual machine. Each process contains a GC thread. The GC thread is responsible for monitoring the usage of the virtual machine heap memory corresponding to this process, and determining whether to start the GC process based on the usage of the virtual machine heap memory corresponding to this process. It is understandable that in some scenarios, when the user switches APP1 to run in the background, the electronic device may still keep running processes corresponding to one or more background services of APP1. These processes continue to occupy the memory of the electronic device, and these processes It is still possible to continue to apply for new heap memory, etc.
通常,APP1启动进程时,会设置该进程的堆内存的使用上限(记为阈值1),即本进程能够使用的最大堆内存阈值。当内核为该进程分配的堆内存除以堆内存目标利用率后的数值大于或等于阈值1时,该进程将启动GC线程对该进程的堆内存进行回收。可以理解的是,当每次GC之后已经使用(或称为占用)的堆内存和本进程能够使用的最大堆内存(包括本进程已使用的堆内存和空闲的堆内存)有一个合适的比例(即堆内存目标利用率,通常为0.75,可简称为目标利用率),可以尽量地减少GC的次数。该堆内存目标利用率可以为默认值,也可以为操作系统为所有进程统一设置的,或者还可以为操作系统为不同的APP或者不同的进程分别设置的,本申请实施例对此不做限定。Usually, when APP1 starts a process, it will set the upper limit of the heap memory usage of the process (recorded as threshold 1), which is the maximum heap memory threshold that this process can use. When the heap memory allocated by the kernel for the process divided by the heap memory target utilization is greater than or equal to the threshold 1, the process will start the GC thread to reclaim the heap memory of the process. It can be understood that there is an appropriate ratio between the heap memory that has been used (or occupied) after each GC and the maximum heap memory that this process can use (including the heap memory that has been used by this process and the free heap memory). (That is, the target utilization rate of heap memory, usually 0.75, which can be referred to as the target utilization rate), can reduce the number of GC as much as possible. The target heap memory utilization can be a default value, or it can be set uniformly by the operating system for all processes, or it can also be set by the operating system separately for different APPs or different processes. The embodiments of this application do not limit this. .
在一些技术方案中,GC线程执行的GC可以包括轻量级GC(如Background Young GC)和全量级GC(Full GC)。其中,在扫描堆区数据时,轻量级GC仅扫描上一次GC执行后到本次触发GC之前的时间段内,本进程申请的堆内存中的数据(后续简称为堆区数据)。全量级GC则是扫描本进程申请的全部堆区数据。如图3所示,为一种电子设备执行GC过程的示意图,具体包括:In some technical solutions, the GC performed by the GC thread can include lightweight GC (such as Background Young GC) and full-scale GC (Full GC). Among them, when scanning the heap area data, the lightweight GC only scans the data in the heap memory applied for by this process in the period from the last GC execution to before the GC is triggered this time (hereinafter referred to as the heap area data). Full-scale GC scans all heap area data requested by this process. As shown in Figure 3, it is a schematic diagram of an electronic device performing a GC process, which specifically includes:
301、接收到进程的内存分配请求。301. Receive a memory allocation request from the process.
302、判断本进程预计占用的堆内存除以目标利用率的数值(图3中用本进程“预计占用的堆内存/目标利用率”表示该数值)是否小于阈值1。如果等于或大于阈值1,则执行步骤303,如果小于阈值1,则执行步骤314。302. Determine whether the value of the heap memory expected to be occupied by this process divided by the target utilization (the value is represented by "heap memory expected to be occupied by this process/target utilization" in Figure 3) is less than the threshold 1. If it is equal to or greater than the threshold value 1, then step 303 is executed. If it is less than the threshold value 1, then step 314 is executed.
进程每次申请分配内存时,均会触发对本进程占用的堆内存进行检查。需要说明的是,本步骤中本进程预计占用的堆内存包括本进程实际已占用的堆内存以及本进程本次申请的堆内存。换言之,此时判断本进程预计占用的堆内存除以目标利用率的数值是否小于阈值1,相当于判断如果为进程分配了本次申请的堆内存,本进程占用的堆内存和本进程能够使用的最大堆内存(包括本进程已占用的堆内存和空闲的堆内存)是否还能够维持不高于目标利用率的目标。如果高于目标利用率,则触发轻量级GC,即执行步骤303。如果不高于目标利用率,则可以分配内存,即执行步骤314。Every time a process applies to allocate memory, it will trigger a check of the heap memory occupied by the process. It should be noted that the heap memory expected to be occupied by this process in this step includes the heap memory actually occupied by this process and the heap memory requested by this process this time. In other words, at this time, judging whether the heap memory expected to be occupied by this process divided by the target utilization is less than the threshold 1 is equivalent to judging whether the heap memory occupied by this process and the heap memory occupied by this process can be used by this process if the heap memory requested this time is allocated to the process. Whether the maximum heap memory (including the heap memory occupied by this process and the free heap memory) can still maintain the goal of not being higher than the target utilization. If it is higher than the target utilization, the lightweight GC is triggered, that is, step 303 is executed. If it is not higher than the target utilization, the memory can be allocated, that is, step 314 is performed.
303、触发轻量级GC。303. Trigger lightweight GC.
电子设备针对本进程在特定时间段内新申请的堆内存进行扫描,判断堆内存中的数据是否最近被引用过,然后电子设备可以采用例如LRU的策略回收最近最少引用的堆内存。那么,本进程可用的空闲堆内存增多了。其中,特定时间段是指从电子设备上一次执行GC(包括轻量级GC和全量级GC)后到本次执行轻量级GC前的时间段。The electronic device scans the heap memory newly requested by this process within a specific time period to determine whether the data in the heap memory has been referenced recently. Then the electronic device can use a strategy such as LRU to reclaim the heap memory that has been least recently referenced. Then, the free heap memory available to this process increases. The specific time period refers to the time period from the last time the electronic device executed GC (including lightweight GC and full-scale GC) to the time before the current execution of lightweight GC.
可以理解的是,轻量级GC所扫描的堆区数据较少,CPU资源的开销较少。It is understandable that lightweight GC scans less heap area data and consumes less CPU resources.
304、尝试分配内存。304. Try to allocate memory.
305、再次判断本进程预计占用的堆内存除以目标利用率的数值是否小于阈值1。如果等于或大于阈值1,则执行步骤306,如果小于阈值1,则执行步骤314。305. Determine again whether the value of the heap memory expected to be occupied by this process divided by the target utilization is less than the threshold 1. If it is equal to or greater than the threshold value 1, then step 306 is executed. If it is less than the threshold value 1, then step 314 is executed.
306、上调阈值1。306. Increase threshold 1.
可以理解的是,当执行轻量级GC后,本进程回收了一些堆内存。此时若仍不能完成本次内存分配请求,则上调阈值1。可以理解的是,上调阈值1意味着上调了本进程使用堆内存的上限,则接下来就有可能能够完成本次内存分配请求。It is understandable that after executing lightweight GC, this process reclaims some heap memory. If this memory allocation request still cannot be completed at this time, increase the threshold to 1. It is understandable that raising the threshold by 1 means that the upper limit of heap memory used by this process has been raised, and then it is possible to complete this memory allocation request.
307、再次尝试分配内存。 307. Try to allocate memory again.
308、再次判断本进程预计占用的堆内存除以目标利用率的数值是否小于阈值1。如果等于或大于阈值1,则执行步骤309,如果小于阈值1,则执行步骤314。308. Determine again whether the value of the heap memory expected to be occupied by this process divided by the target utilization is less than the threshold 1. If it is equal to or greater than the threshold value 1, then step 309 is executed. If it is less than the threshold value 1, then step 314 is executed.
需要说明的是,在一些场景中,本进程中可能有多个任务并行,那么多个任务可能同时或先后申请分配堆内存。那么在本任务执行本次内存分配请求时,通过上调阈值1的方法提高了本进程的堆内存的使用上限,即最大堆内存,但可能其他任务提前分配到所需的堆内存(即本进程已占用的堆内存增大),从而造成此时本进程预计占用的堆内存(本任务申请分配的内存和本进程已经占用的堆内存)除以目标利用率的数值仍然高于阈值1,仍然不能完成本任务的本次内存分配请求。It should be noted that in some scenarios, there may be multiple tasks running in parallel in this process, and then multiple tasks may apply to allocate heap memory at the same time or one after another. Then when this task executes this memory allocation request, the upper limit of heap memory usage of this process is increased by raising the threshold 1, that is, the maximum heap memory, but other tasks may be allocated the required heap memory in advance (that is, this process The occupied heap memory increases), causing the heap memory expected to be occupied by this process at this time (the memory allocated by this task and the heap memory already occupied by this process) divided by the target utilization to still be higher than the threshold 1, still This memory allocation request for this task cannot be completed.
309、触发全量级GC。309. Trigger full-scale GC.
310、下调阈值1。310. Lower the threshold 1.
需要说明的是,全量级GC扫描到堆区数据更多,通过能够释放更多的堆内存,使得空闲的堆内存增多,因此在执行全量级GC后电子设备可以适时地下调阈值1,保证堆内存的利用率。It should be noted that the full-scale GC scans more data in the heap area, which releases more heap memory and increases the free heap memory. Therefore, after executing the full-scale GC, the electronic device can lower the threshold 1 in a timely manner. Ensure heap memory utilization.
311、再次尝试分配内存。311. Try to allocate memory again.
312、再次判断本进程预计占用的堆内存除以目标利用率的数值是否小于阈值1。如果等于或大于阈值1,则执行步骤313,如果小于阈值1,则执行步骤314。312. Determine again whether the value of the heap memory expected to be occupied by this process divided by the target utilization is less than the threshold 1. If it is equal to or greater than the threshold value 1, then step 313 is executed. If it is less than the threshold value 1, then step 314 is executed.
313、触发内存溢出(out of memory,OOM)机制。313. Trigger the memory overflow (out of memory, OOM) mechanism.
在触发OOM机制时电子设备清理所有申请堆内存的进程,本流程结束。When the OOM mechanism is triggered, the electronic device cleans up all processes that have applied for heap memory, and this process ends.
314、分配内存。314. Allocate memory.
由此可见,在该技术方案中,电子设备可通过分步执行不同量级的GC,以及与上调阈值1的方法相结合的方法来减少执行GC的次数,以及尽量减少执行GC占用的CPU资源。It can be seen that in this technical solution, the electronic device can reduce the number of GC executions and minimize the CPU resources occupied by GC executions by executing GCs of different magnitudes step by step and combining it with the method of raising the threshold 1. .
系统库可以包括多个功能模块。例如:表面管理器(surface manager),媒体库(Media Libraries),三维图形处理库(例如:OpenGL ES),2D图形引擎(例如:SGL)等。System libraries can include multiple functional modules. For example: surface manager (surface manager), media libraries (Media Libraries), 3D graphics processing libraries (for example: OpenGL ES), 2D graphics engines (for example: SGL), etc.
其中,表面管理器用于对显示子系统进行管理,并且为多个应用程序提供了2D和3D图层的融合。媒体库支持多种常用的音频,视频格式回放和录制,以及静态图像文件等。媒体库可以支持多种音视频编码格式,例如:MPEG4,H.264,MP3,AAC,AMR,JPG,PNG等。三维图形处理库用于实现三维图形绘图,图像渲染,合成,和图层处理等。2D图形引擎是2D绘图的绘图引擎。Among them, the surface manager is used to manage the display subsystem and provides the integration of 2D and 3D layers for multiple applications. The media library supports playback and recording of a variety of commonly used audio and video formats, as well as static image files, etc. The media library can support a variety of audio and video encoding formats, such as: MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, etc. The 3D graphics processing library is used to implement 3D graphics drawing, image rendering, composition, and layer processing. 2D Graphics Engine is a drawing engine for 2D drawing.
(4)HAL(4)HAL
HAL,用于将硬件抽象化。HAL隐藏了特定平台的硬件接口细节,为操作系统提供虚拟硬件平台,使其具有硬件无关性,可在多种平台上进行移植。也就是说,上层所需使需要的有关硬件的操作都需要调用HAL相关的API,例如音视频接口、GPS接口、通话接口、Wi-Fi接口以及其他接口。HAL, used to abstract hardware. HAL hides the hardware interface details of a specific platform and provides a virtual hardware platform for the operating system, making it hardware-independent and portable on multiple platforms. In other words, all hardware-related operations required by the upper layer require calling HAL-related APIs, such as audio and video interfaces, GPS interfaces, call interfaces, Wi-Fi interfaces, and other interfaces.
(5)内核层(5) Kernel layer
内核层是硬件和软件之间的层。The kernel layer is the layer between hardware and software.
内核层包括内存管理模块,进程/线程,音视频驱动、GPS驱动、显示驱动、Wi-Fi驱动等。其中,内存管理模块,用于实现系统内存的分配与回收,压缩堆区数据等。The kernel layer includes memory management modules, processes/threads, audio and video drivers, GPS drivers, display drivers, Wi-Fi drivers, etc. Among them, the memory management module is used to realize the allocation and recycling of system memory, compress heap area data, etc.
以下实施例中所涉及的技术方案均可以在具有上述硬件架构和软件架构的电子设 备100中实现。The technical solutions involved in the following embodiments can be implemented in electronic devices with the above hardware architecture and software architecture. Implemented in 100.
考虑到电子设备处于低内存场景(即电子设备的可用内存低于阈值2)时,电子设备的内核会按照一定策略(例如LRU)对堆区数据进行压缩。那么,当电子设备执行GC时,可能需要对压缩的堆区数据进行解压,解压的过程既要消耗系统的内存,加重系统内存的紧张,又要消耗系统的CPU资源。可见,如果在CPU高负载的场景下执行GC,容易影响系统的流畅性和稳定性,甚至可能造成系统崩溃。为此,本申请实施例提供的一种GC的方法,电子设备统计各个进程的堆区数据的压缩比,而后根据堆区数据的压缩比和CPU的负载情况,采用不同的延迟策略,延迟执行GC,由此提升系统的流畅性和稳定性,避免系统崩溃的问题。Considering that when the electronic device is in a low memory scenario (that is, the available memory of the electronic device is lower than the threshold 2), the kernel of the electronic device will compress the heap area data according to a certain strategy (such as LRU). Then, when the electronic device performs GC, it may be necessary to decompress the compressed heap area data. The decompression process not only consumes the system's memory, aggravates the system memory tension, but also consumes the system's CPU resources. It can be seen that if GC is executed under a high CPU load scenario, it is easy to affect the fluency and stability of the system, and may even cause the system to crash. To this end, embodiments of the present application provide a GC method. The electronic device counts the compression ratio of the heap area data of each process, and then uses different delay strategies to delay execution according to the compression ratio of the heap area data and the load of the CPU. GC, thereby improving the fluency and stability of the system and avoiding system crashes.
如图4所示,为本申请实施例提供了一种GC的方法的流程示意图,该方法包括:As shown in Figure 4, a schematic flow chart of a GC method is provided for an embodiment of the present application. The method includes:
401、APP的进程请求分配第一内存。401. The APP process requests to allocate the first memory.
示例性的,APP的进程中某个任务请求分配第一内存,请求分配的第一内存可以是该任务指定大小的内存,也可以是系统默认大小的内存,本申请实施例对此不做限定。For example, a certain task in the APP process requests to allocate the first memory. The first memory requested to be allocated can be the memory of the specified size for the task, or it can be the memory of the default size of the system. This application embodiment does not limit this. .
402、判断本进程预计占用的堆内存除以目标利用率的数值(图4中用本进程预计占用的堆内存/目标利用率表示该数值)是否小于阈值1。如果等于或大于阈值1,则执行步骤403,如果小于阈值1,则执行步骤417。402. Determine whether the value of the heap memory expected to be occupied by this process divided by the target utilization (the value is represented by the heap memory expected to be occupied by this process/target utilization in Figure 4) is less than the threshold 1. If it is equal to or greater than the threshold value 1, then step 403 is executed. If it is less than the threshold value 1, then step 417 is executed.
APP的进程每次申请分配内存时,均会触发对本进程占用的堆内存进行检查。需要说明的是,本步骤中本进程预计占用的堆内存包括本进程实际已占用的堆内存以及本次申请的堆内存。即,本进程预计占用的堆内存的大小=本进程实际占用的堆内存的大小+第一内存的大小。换言之,此时判断本进程预计占用的堆内存除以目标利用率数值是否小于阈值1,相当于判断如果为进程分配了本次申请的第一内存,本进程占用的堆内存和本进程能够使用的最大堆内存(包括本进程已占用的堆内存和空闲的堆内存)的比例是否还能够维持理想的利用率(即目标利用率)。如果能够维持目标利用率,即不高于目标利用率,则可以分配内存,即执行步骤417。如果不能维持理想的利用率,即高于目标利用率,则需要进一步根据堆区数据压缩比和CPU的负载情况,采用不同的延迟GC的策略,即执行步骤403以及之后的步骤。Every time the APP process applies for memory allocation, it will trigger a check of the heap memory occupied by this process. It should be noted that the heap memory expected to be occupied by this process in this step includes the heap memory actually occupied by this process and the heap memory applied for this time. That is, the size of the heap memory expected to be occupied by this process = the size of the heap memory actually occupied by this process + the size of the first memory. In other words, at this time, judging whether the heap memory expected to be occupied by this process divided by the target utilization value is less than the threshold 1 is equivalent to judging whether the heap memory occupied by this process and the heap memory occupied by this process can be used by this process if the first memory requested this time is allocated to the process. Whether the proportion of the maximum heap memory (including the heap memory occupied by this process and the free heap memory) can still maintain the ideal utilization (ie, the target utilization). If the target utilization can be maintained, that is, not higher than the target utilization, memory can be allocated, that is, step 417 is performed. If the ideal utilization cannot be maintained, that is, higher than the target utilization, you need to further adopt different delayed GC strategies based on the heap area data compression ratio and CPU load, that is, perform step 403 and subsequent steps.
403、检测堆区数据压缩比是否等于或大于阈值3。如果堆区数据压缩比小于阈值3,则触发轻量级GC,即执行步骤407。如果堆区数据压缩比等于或大于阈值3,则进一步判断当前CPU的负载,即执行步骤404。403. Check whether the heap area data compression ratio is equal to or greater than threshold 3. If the heap area data compression ratio is less than the threshold 3, lightweight GC is triggered, that is, step 407 is executed. If the heap area data compression ratio is equal to or greater than the threshold 3, the current CPU load is further determined, that is, step 404 is performed.
其中,堆区数据压缩比,是指本进程中压缩的堆区数据的大小与本进程中全部堆区数据(包括压缩的堆区数据和未压缩的堆区数据)的大小的比值。Among them, the heap area data compression ratio refers to the ratio of the size of the compressed heap area data in this process to the size of all heap area data in this process (including compressed heap area data and uncompressed heap area data).
需要说明的是,进程在申请分配虚拟机堆内存时,会通过调用内存分配器(例如mmap分配器)将内核态内存映射为用户态内存,并标记为匿名页内存。其中,匿名页内存是指应用的进程运行过程中动态分配的且没有文件名称标记的内存。在本申请实施例中,在进程调用内存分配器(例如mmap分配器)分配匿名页内存时,可以对本进程分配的匿名页内存进行标记,便于内核层识别出该匿名页内存为堆内存,便于后续计算堆区数据压缩比。在一个具体的实现中,进程在申请分配虚拟机堆内存时,例如将匿名页内存中vma_name的字段标记为第一值。当vma_name的字段为第一值 时,表明该匿名页为堆内存。如此,内核可以根据vma_name的字段识别出堆区数据。例如,内核可设置一参数Object1,用于统计本进程申请分配的堆区数据量(例如单位为页)。It should be noted that when a process applies to allocate virtual machine heap memory, it will map the kernel-mode memory to user-mode memory by calling a memory allocator (such as the mmap allocator) and mark it as anonymous page memory. Among them, anonymous page memory refers to the memory that is dynamically allocated during the running of the application process and does not have a file name tag. In the embodiment of the present application, when a process calls a memory allocator (such as a mmap allocator) to allocate anonymous page memory, the anonymous page memory allocated by the process can be marked to facilitate the kernel layer to identify the anonymous page memory as heap memory. Subsequent calculation of heap area data compression ratio. In a specific implementation, when a process applies to allocate virtual machine heap memory, for example, it marks the vma_name field in the anonymous page memory as the first value. When the field of vma_name is the first value When, it indicates that the anonymous page is heap memory. In this way, the kernel can identify the heap area data based on the vma_name field. For example, the kernel can set a parameter Object1 to count the amount of heap area data allocated by this process (for example, the unit is pages).
另外,内核还可以设置另一个参数Object2,用于统计本进程中压缩的堆区数据量(例如单位为页)。Object2的初始值为0。当内核压缩内存数据时,可以根据vma_name的字段判断当前压缩的数据是否为堆区数据。还需要说明的是,内核是以页为单位对内存数据进行压缩的。因此,当内核确定压缩的内存数据为堆区数据时,Object2加一。如此,可以内核可以统计出压缩的堆区数据的大小,进而计算出堆区数据压缩比。即,堆区数据压缩比=Object2除以Object1。In addition, the kernel can also set another parameter Object2, which is used to count the amount of compressed heap area data in this process (for example, the unit is pages). The initial value of Object2 is 0. When the kernel compresses memory data, it can determine whether the currently compressed data is heap area data based on the vma_name field. It should also be noted that the kernel compresses memory data in units of pages. Therefore, when the kernel determines that the compressed memory data is heap area data, Object2 is incremented by one. In this way, the kernel can count the size of the compressed heap data, and then calculate the heap data compression ratio. That is, the heap area data compression ratio = Object2 divided by Object1.
如前文所述,当内核对堆区数据进行压缩后,电子设备执行GC(包括轻量级GC和全量级GC)时,会对压缩的堆区数据进行解压,然而解压动作需要额外申请内存资源以及消耗更多的CPU资源。为此,本进程或者系统可以设置阈值3。当堆区数据的压缩比不高(即,堆区数据压缩比小于阈值3)时,那么电子设备因执行解压动作额外申请的内存资源以及消耗的CPU资源均不高,并不会影响到系统的正常运行。那么电子设备可以直接执行轻量级GC,即执行步骤407。当堆区数据的压缩比较高(即堆区数据压缩比等于或大于阈值3)时,电子设备需要进一步判断当前的CPU负载情况,即执行步骤404。As mentioned above, when the kernel compresses the heap data and the electronic device performs GC (including lightweight GC and full-weight GC), the compressed heap data will be decompressed. However, the decompression operation requires additional application for memory. resources and consume more CPU resources. For this purpose, the process or system can set a threshold of 3. When the compression ratio of the heap area data is not high (that is, the compression ratio of the heap area data is less than the threshold 3), then the additional memory resources requested by the electronic device for performing the decompression action and the CPU resources consumed are not high, and will not affect the system. of normal operation. Then the electronic device can directly execute the lightweight GC, that is, step 407 is executed. When the compression ratio of the heap area data is high (that is, the heap area data compression ratio is equal to or greater than the threshold 3), the electronic device needs to further determine the current CPU load, that is, step 404 is performed.
404、判断CPU负载是否超过阈值4。404. Determine whether the CPU load exceeds threshold 4.
如果CPU负载(也称为CPU的占用率)超过阈值4(例如40%),表明当前CPU负载过重,则不宜立即执行轻量级GC,可以通过上调阈值1的方法延迟执行轻量级GC,即执行步骤405。如果CPU负载没有超过阈值4,则可以直接执行轻量级GC,即执行步骤407。If the CPU load (also called CPU occupancy) exceeds threshold 4 (for example, 40%), it indicates that the current CPU load is too heavy, and it is not appropriate to execute lightweight GC immediately. You can delay the execution of lightweight GC by raising the threshold 1. , that is, step 405 is executed. If the CPU load does not exceed threshold 4, lightweight GC can be performed directly, that is, step 407 is performed.
405、上调阈值1。405. Increase threshold 1.
在一个实施例中,电子设备可以采用步进法上调阈值1的数值。在该实施例中,电子设备可以采用默认设置的步长和预设次数,或者接收用户设置的步长和预设次数。例如,电子设备可以先以步长(例如为2M)为单位上调一次阈值1,然后判断本进程预计占用的堆内存除以目标利用率的数值是否小于上调后的阈值1。如果仍然等于或大于上调后的阈值1,则再次以步长为单位上调阈值1(例如再次上调2M,累积已上调4M)。当电子设备上调次数小于预设次数(例如3次)时,再次判断本进程预计占用的堆内存除以目标利用率的数值是否小于上调后的阈值1,如果仍然等于或大于上调后的阈值1,再次上调阈值1。如果电子设备上调次数等于预设次数时,执行步骤406。需要说明的是,在任一次上调阈值1的过程中,如果检测到本进程预计占用的堆内存除以目标利用率的数值小于调整后的阈值1时,则停止再次上调阈值1,直接分配第一内存,即执行步骤417。In one embodiment, the electronic device may use a stepwise method to increase the value of threshold 1. In this embodiment, the electronic device may adopt a default set step size and a preset number of times, or receive a step size and a preset number of times set by the user. For example, the electronic device can first increase the threshold 1 once in steps (for example, 2M), and then determine whether the value of the heap memory expected to be occupied by the process divided by the target utilization is less than the increased threshold 1. If it is still equal to or greater than the raised threshold 1, raise the threshold 1 again in steps (for example, raise it again by 2M, and the cumulative raise has been 4M). When the number of times the electronic device is raised is less than the preset number (for example, 3 times), it is again judged whether the value of the heap memory expected to be occupied by the process divided by the target utilization is less than the raised threshold 1, and if it is still equal to or greater than the raised threshold 1 , raise the threshold 1 again. If the number of times the electronic device is adjusted up is equal to the preset number, step 406 is executed. It should be noted that during any process of raising the threshold 1, if it is detected that the heap memory expected to be occupied by this process divided by the target utilization is less than the adjusted threshold 1, it will stop raising the threshold 1 again and directly allocate the first memory, that is, perform step 417.
406、判断本进程预计占用的堆内存除以目标利用率的数值是否小于阈值1。如果等于或大于阈值1,则执行步骤407,如果小于阈值1,则执行步骤417。406. Determine whether the value of the heap memory expected to be occupied by this process divided by the target utilization is less than the threshold 1. If it is equal to or greater than the threshold value 1, then step 407 is executed. If it is less than the threshold value 1, then step 417 is executed.
可以理解的是,上调阈值1意味着上调了本进程使用堆内存的上限。在一些场景中,上调阈值1后,能够使得本进程预计占用的堆内存除以目标利用率的数值小于上调后的阈值1,那么能够完成第一内存的分配。在该场景下,电子设备通过上调阈值1 的方法减少了电子设备执行轻量级GC的次数。It can be understood that raising the threshold by 1 means raising the upper limit of heap memory used by this process. In some scenarios, after raising the threshold 1, the value of the heap memory expected to be occupied by this process divided by the target utilization can be smaller than the raised threshold 1, and then the allocation of the first memory can be completed. In this scenario, the electronic device raises the threshold 1 The method reduces the number of times electronic equipment performs lightweight GC.
在其他一些场景中,上调阈值1后,本进程预计占用的堆内存除以目标利用率的数值仍然可能等于或大于上调后的阈值1。例如,本进程的其他任务可能在该时间段也申请分配了堆内存,造成本进程预计占用的堆内存除以目标利用率的数值仍然等于或大于上调后的阈值1。那么,不能完成第一内存的分配,仍然需要执行轻量级GC回收更多的内存。可见在该场景下,电子设备也通过上调阈值1的方法延迟了电子设备执行轻量级GC的时机,避免在当前CPU负载过重时执行轻量级GC,有利于保证系统的流畅性和稳定性。In some other scenarios, after raising the threshold 1, the value of the heap memory expected to be occupied by this process divided by the target utilization may still be equal to or greater than the raised threshold 1. For example, other tasks of this process may also apply for heap memory allocation during this time period, causing the value of the heap memory expected to be occupied by this process divided by the target utilization to still be equal to or greater than the increased threshold of 1. Then, the first memory allocation cannot be completed, and lightweight GC still needs to be executed to reclaim more memory. It can be seen that in this scenario, the electronic device also delays the timing of the electronic device to perform lightweight GC by raising the threshold 1, avoiding the execution of lightweight GC when the current CPU load is too heavy, which is conducive to ensuring the smoothness and stability of the system. sex.
407、执行轻量级GC。407. Execute lightweight GC.
电子设备针对本进程在特定时间段内新申请的堆内存进行扫描,判断堆内存中的数据是否最近被引用过,然后电子设备可以采用例如LRU的策略回收最近最少引用的堆内存。其中,特定时间段是指从电子设备上一次执行GC(包括轻量级GC和全量级GC)后到本次执行轻量级GC前的时间段。The electronic device scans the heap memory newly requested by this process within a specific time period to determine whether the data in the heap memory has been referenced recently. Then the electronic device can use a strategy such as LRU to reclaim the heap memory that has been least recently referenced. The specific time period refers to the time period from the last time the electronic device executed GC (including lightweight GC and full-scale GC) to the time before the current execution of lightweight GC.
可以理解的是,相较于全量级GC,电子设备执行轻量级GC占用的内存资源和CPU资源本身较少。由于电子设备执行轻量级GC时并未扫描全部的堆区数据,那么扫描到的压缩数据也较少,因电子设备压缩行为占用的额外的内存资源和CPU资源也不会太多,有利于保证系统的流畅性和稳定性。It is understandable that, compared to full-weight GC, electronic devices performing lightweight GC occupy less memory resources and CPU resources themselves. Since the electronic device does not scan all the heap area data when executing lightweight GC, the scanned compressed data is also less, and the additional memory resources and CPU resources occupied by the compression behavior of the electronic device will not be too much, which is beneficial to Ensure system fluency and stability.
408、判断本进程预计占用的堆内存除以目标利用率的数值是否小于阈值1。如果等于或大于阈值1,则执行步骤409,如果小于阈值1,则执行步骤417。408. Determine whether the value of the heap memory expected to be occupied by this process divided by the target utilization is less than the threshold 1. If it is equal to or greater than the threshold value 1, then step 409 is executed. If it is less than the threshold value 1, then step 417 is executed.
409、上调阈值1。409. Increase threshold 1.
也就是说,在电子设备执行轻量级GC后,如果电子设备当前的内存情况仍然不能满足需求,即本进程预计占用的堆内存除以目标利用率的数值仍等于或大于阈值1,那么电子设备再次上调阈值1。通过再次上调阈值1的方法,再次减少电子设备执行全量级GC的次数,或者延长电子设备执行全量级GC的时机。That is to say, after the electronic device performs lightweight GC, if the current memory situation of the electronic device still cannot meet the demand, that is, the value of the heap memory expected to be occupied by this process divided by the target utilization is still equal to or greater than the threshold 1, then the electronic device The device increases threshold 1 again. By raising the threshold 1 again, the number of times the electronic device performs full-scale GC is again reduced, or the opportunity for the electronic device to perform full-scale GC is extended.
410、判断本进程预计占用的堆内存除以目标利用率的数值是否小于阈值1。如果等于或大于阈值1,则执行步骤411,如果小于阈值1,则执行步骤417。410. Determine whether the value of the heap memory expected to be occupied by this process divided by the target utilization is less than the threshold 1. If it is equal to or greater than the threshold value 1, then step 411 is executed. If it is less than the threshold value 1, then step 417 is executed.
在一些场景中,上调阈值1后,能够使得本进程预计占用的堆内存除以目标利用率的数值小于上调后的阈值1,那么能够完成第一内存的分配。在该场景下,电子设备通过上调阈值1的方法减少了电子设备执行一次全量级GC。In some scenarios, after raising the threshold 1, the value of the heap memory expected to be occupied by this process divided by the target utilization can be smaller than the raised threshold 1, and then the allocation of the first memory can be completed. In this scenario, the electronic device reduces the need for the electronic device to perform a full-scale GC by raising the threshold value 1.
在其他一些场景中,上调阈值1后,本进程预计占用的堆内存除以目标利用率的数值仍然可能等于或大于上调后的阈值1。例如,本进程中可能有多个任务并行,那么多个任务可能同时或先后申请分配堆内存。那么在本任务执行本次内存分配请求时,通过上调阈值1的方法提高了本进程的堆内存的使用上限,即最大堆内存,但可能其他任务提前分配到所需的堆内存(即本进程已占用的堆内存增大),从而造成此时本进程预计占用的堆内存(本任务申请分配的内存和本进程已经占用的堆内存)除以目标利用率的数值仍然高于阈值1,仍然不能完成本任务的本次内存分配请求。那么,仍然需要执行轻量级GC回收更多的内存。可见在该场景下,电子设备也通过上调阈值1的方法延迟了电子设备执行全量级GC的时机。In some other scenarios, after raising the threshold 1, the value of the heap memory expected to be occupied by this process divided by the target utilization may still be equal to or greater than the raised threshold 1. For example, there may be multiple parallel tasks in this process, and then multiple tasks may apply for heap memory allocation at the same time or one after another. Then when this task executes this memory allocation request, the upper limit of heap memory usage of this process is increased by raising the threshold 1, that is, the maximum heap memory, but other tasks may be allocated the required heap memory in advance (that is, this process The occupied heap memory increases), causing the heap memory expected to be occupied by this process at this time (the memory allocated by this task and the heap memory already occupied by this process) divided by the target utilization to still be higher than the threshold 1, still This memory allocation request for this task cannot be completed. Then, it is still necessary to perform lightweight GC to reclaim more memory. It can be seen that in this scenario, the electronic device also delays the opportunity for the electronic device to perform full-scale GC by raising the threshold 1.
411、检测堆区数据压缩比。 411. Check the data compression ratio of the heap area.
也就是说,此时电子设备执行过一次轻量级GC,并且上调阈值1之后,电子设备的内存情况仍然不能满足需求时,电子设备也不能立即执行全量级GC。这是因为,电子设备执行全量级GC时占用的内存资源和CPU资源较大,并且由于电子设备需要扫描全部的堆区数据。当堆区数据中的压缩数据量较大时,电子设备因解压行为需要额外占用的内存资源和CPU资源会加重系统的内存紧张程度,影响系统运行的稳定性和流畅性,甚至造成系统的崩溃。本申请将根据堆区数据的压缩比情况分情况采用不同的策略延迟电子设备执行全量级GC,即分情况分别执行下述步骤412、步骤413或步骤414a中的任一步骤。That is to say, at this time, the electronic device has performed a lightweight GC, and after raising the threshold to 1, if the memory of the electronic device still cannot meet the demand, the electronic device cannot immediately perform a full-scale GC. This is because the electronic device occupies a large amount of memory resources and CPU resources when executing full-scale GC, and because the electronic device needs to scan all heap area data. When the amount of compressed data in the heap area is large, the additional memory resources and CPU resources required by the electronic device for decompression will increase the memory tension of the system, affect the stability and fluency of system operation, and even cause the system to crash. . This application will use different strategies to delay the electronic device from performing full-scale GC according to the compression ratio of the heap area data, that is, perform any of the following steps 412, 413 or 414a respectively.
其中,检测堆区数据压缩比的具体方法请参考步骤403中相关内容的描述,这里不再赘述。For the specific method of detecting the heap area data compression ratio, please refer to the description of relevant content in step 403, which will not be described again here.
412、当堆区数据压缩比较低时,如堆区数据压缩比小于阈值5时,避开CPU负载的高峰期执行全量级GC。412. When the heap area data compression ratio is low, for example, when the heap area data compression ratio is less than the threshold 5, avoid executing full-scale GC during the peak period of CPU load.
在一些实施例中,当检测到堆区数据压缩比较低(例如位于区间1,区间1例如为小于阈值5,阈值5例如为30%)时,继续检测CPU的负载。当CPU负载过重时,例如CPU负载大于阈值7(可以与步骤404中的阈值4相同,或者大于步骤404中的阈值4),暂停执行全量级GC,以避免在CPU负载的高峰期执行全量级GC,保证系统的流畅度和稳定性。持续检测CPU的负载,当CPU负载低于阈值7时,电子设备可以执行全量级GC。可以理解的,CPU负载的高峰期包括CPU启动APP的时间段,或者CPU执行负载较重的任务(例如人脸解锁任务,图像识别任务、语音语义识别任务、图形渲染任务等)的时间段,或者CPU同时执行大量任务的时间段等。In some embodiments, when it is detected that the heap area data compression ratio is low (for example, in interval 1, interval 1 is, for example, less than threshold 5, and threshold 5 is, for example, 30%), the load of the CPU is continued to be detected. When the CPU load is too heavy, for example, the CPU load is greater than the threshold 7 (which can be the same as the threshold 4 in step 404, or greater than the threshold 4 in step 404), the execution of the full-scale GC is suspended to avoid execution during the peak period of the CPU load. Full-scale GC ensures system fluency and stability. The load of the CPU is continuously detected. When the CPU load is lower than the threshold 7, the electronic device can perform full-scale GC. It is understandable that the peak period of CPU load includes the time period when the CPU starts the APP, or the time period when the CPU performs heavy load tasks (such as face unlocking tasks, image recognition tasks, speech semantic recognition tasks, graphics rendering tasks, etc.). Or the time period when the CPU performs a large number of tasks at the same time, etc.
在另一些实施例中,当检测到堆区数据压缩比较低(例如小于阈值5,阈值5例如为30%)时,电子设备也可以直接延迟执行全量级GC的时间,延迟的时长可以根据电子设备当前运行的前台进程或核心进程进行确定。需要说明的是,本申请实施例对延迟执行全量级GC的具体方案不做具体限定。In other embodiments, when it is detected that the heap area data compression ratio is low (for example, less than the threshold 5, and the threshold 5 is, for example, 30%), the electronic device can also directly delay the time to execute the full-scale GC. The length of the delay can be based on The foreground process or core process currently running on the electronic device is determined. It should be noted that the embodiments of the present application do not specifically limit the specific solution for delaying the execution of full-scale GC.
413、当堆区数据压缩比中等时,如堆区数据压缩比等于或大于阈值5,且小于阈值6时,在CPU空闲时执行全量级GC。413. When the heap area data compression ratio is medium, such as when the heap area data compression ratio is equal to or greater than the threshold 5 and less than the threshold 6, a full-scale GC is executed when the CPU is idle.
当检测到堆区数据压缩比较低(例如位于区间2,例如区间2例如为阈值5和阈值6之间,阈值5例如为30%,阈值6例如为50%)时,继续检测CPU的负载。当CPU负载较重时,例如CPU负载大于阈值8(阈值8小于阈值7),暂停执行全量级GC,电子设备可以在CPU空闲时执行全量级GC,以保证系统的流畅度和稳定性。其中,CPU空闲,是指CPU在发出I/O请求后,等待I/O设备完成I/O请求的时间。When it is detected that the heap area data compression ratio is low (for example, it is located in interval 2, for example, interval 2 is between threshold 5 and threshold 6, threshold 5 is, for example, 30%, and threshold 6 is, for example, 50%), continue to detect the load of the CPU. When the CPU load is heavy, for example, the CPU load is greater than threshold 8 (threshold 8 is less than threshold 7), the execution of full-scale GC is suspended. The electronic device can execute full-scale GC when the CPU is idle to ensure the smoothness and stability of the system. . Among them, CPU idle refers to the time that the CPU waits for the I/O device to complete the I/O request after issuing the I/O request.
414a、当堆区数据压缩比较高时,如堆区数据压缩比等于或大于阈值6时,冻结本进程。414a. When the heap area data compression ratio is high, such as when the heap area data compression ratio is equal to or greater than the threshold 6, freeze the process.
当检测到堆区数据压缩比较高(例如位于区间3,区间3例如为等于或大于阈值6,阈值6例如为50%)时,若电子设备继续执行全量级GC,需要解压的堆区数据量也较大,需要因解压行为占用大量的内存资源和CPU资源,会严重影响系统的流畅度和稳定性,甚至会导致系统崩溃。因此,在该场景下,电子设备直接冻结本进程,从内存分配源头暂停本进程执行全量级GC(当然本进程也无法执行轻量级GC)。在一个具体实现中,电子设备的内核可以在进程列表将本进程的状态设置为停止,并保存本 进程的上下文,以便后续解冻该进程。When it is detected that the heap data compression ratio is high (for example, in interval 3, and interval 3 is, for example, equal to or greater than threshold 6, and threshold 6 is, for example, 50%), if the electronic device continues to perform full-scale GC, the heap area data that needs to be decompressed The amount is also large, and it requires a large amount of memory resources and CPU resources due to the decompression behavior, which will seriously affect the fluency and stability of the system, and even cause the system to crash. Therefore, in this scenario, the electronic device directly freezes this process, suspends this process from the source of memory allocation, and performs full-scale GC (of course, this process cannot perform lightweight GC). In a specific implementation, the kernel of the electronic device can set the status of the process to stopped in the process list and save the process. The context of the process so that it can be unfrozen later.
可以理解的是,通常冻结的是后台进程,那么用户无法感知进程被冻结,也不会影响到用户的使用体验。It is understandable that what is usually frozen is the background process, so the user cannot perceive that the process is frozen, and the user experience will not be affected.
414b、当接收到解冻本进程的指令后,解冻本进程,并执行全量级GC。414b. After receiving the instruction to unfreeze the process, unfreeze the process and execute a full-scale GC.
一些场景中,当电子设备接收到解冻本进程的指令后,解冻本进程,并执行全量级GC。其中,解冻本进程的指令包括但不限于,用户将本进程对应的APP切换到前台运行的指令等。In some scenarios, when the electronic device receives the instruction to unfreeze the process, it unfreezes the process and performs a full-scale GC. Among them, instructions for unfreezing this process include, but are not limited to, instructions for the user to switch the APP corresponding to this process to run in the foreground.
可选的,在步骤412、步骤413以及步骤414b之后,当电子设备执行全量级GC后,电子设备还可以下调阈值1。这是因为,全量级GC扫描到堆区数据更多,通过能够释放更多的堆内存,使得空闲的堆内存增多,因此在执行全量级GC后电子设备可以适时地下调阈值1,保证堆内存的利用率。Optionally, after step 412, step 413, and step 414b, when the electronic device performs a full-level GC, the electronic device can also lower the threshold 1. This is because the full-scale GC scans more data in the heap area and releases more heap memory, which increases the free heap memory. Therefore, after executing the full-scale GC, the electronic device can lower the threshold 1 in a timely manner to ensure Heap memory utilization.
415、判断本进程预计占用的堆内存除以目标利用率的数值是否小于阈值1。如果等于或大于阈值1,则执行步骤416,如果小于阈值1,则执行步骤417。415. Determine whether the value of the heap memory expected to be occupied by this process divided by the target utilization is less than the threshold 1. If it is equal to or greater than the threshold value 1, then step 416 is executed. If it is less than the threshold value 1, then step 417 is executed.
416、触发内存溢出机制。416. Trigger the memory overflow mechanism.
在触发内存溢出机制时,电子设备清理所有申请堆内存的进程,本流程结束。When the memory overflow mechanism is triggered, the electronic device cleans up all processes that have applied for heap memory, and the process ends.
417、分配第一内存。417. Allocate the first memory.
综上,本申请实施例提出一种检测堆区数据压缩比的方法,使得电子设备内核的GC线程可以感知堆区数据的压缩量级(即堆区数据的压缩比),从而便于根据堆区数据的压缩量级,进行高效的GC管控。在堆区数据的压缩量级不同的情况下,结合电子设备当前CPU的负载采用不同策略减少电子设备执行GC(轻量级GC和全量级GC)的次数或者延迟电子设备执行GC(轻量级GC和全量级GC)的时机,避免电子设备在CPU高负载的情况下执行GC。由此,本申请降低了电子设备在执行GC时整机占用的系统资源(包括内存资源和CPU资源),有利于保证系统的流畅性和稳定性。In summary, the embodiments of this application propose a method for detecting the compression ratio of heap area data, so that the GC thread of the electronic device core can perceive the compression level of the heap area data (ie, the compression ratio of the heap area data), thereby facilitating the heap area data compression ratio according to the heap area data compression ratio. Data compression level for efficient GC control. When the compression levels of heap area data are different, different strategies are adopted based on the current CPU load of the electronic device to reduce the number of times the electronic device performs GC (lightweight GC and full-weight GC) or delay the execution of GC (lightweight GC) by the electronic device. level GC and full-scale GC) to prevent electronic equipment from executing GC under high CPU load. Therefore, this application reduces the system resources (including memory resources and CPU resources) occupied by the electronic device when executing GC, which is beneficial to ensuring the smoothness and stability of the system.
还需要说明的是,上述步骤401-步骤417仅为本申请实施例提供的一个实施例,在其他实施例中,上述步骤401-步骤417中部分步骤可以缺省,或者步骤之间的执行顺序可以变化,或者在方案不矛盾的情况下步骤间可以进行其他组合。例如,在一些实施例中,步骤405可以缺省,或者步骤407可以缺省,或者步骤409可以缺省。又例如,在其他一些实施例中,步骤403可以替换为步骤411,并且,电子设备可以根据堆区数据压缩比的情况,采用不同的策略延迟GC。例如,当堆区数据压缩比较低时,可以直接执行轻量级GC。当堆区数据压缩比较高时,采用上调阈值1的方法延迟执行轻量级GC。在本申请揭露的技术范围内的其他变化或替换均涵盖在本申请的保护范围之内,这里不再一一列举。It should also be noted that the above-mentioned steps 401 to 417 are only one example provided by the embodiment of the present application. In other embodiments, some of the above-mentioned steps 401 to 417 may be omitted by default, or the execution order between the steps may be changed. It can be changed, or other combinations of steps can be made if the solutions are not inconsistent. 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 can be replaced by step 411, and the electronic device can use different strategies to delay GC according to the heap area data compression ratio. For example, when the heap area data compression ratio is low, lightweight GC can be performed directly. When the heap area data compression is relatively high, the method of raising the threshold 1 is used to delay the execution of lightweight GC. Other changes or substitutions within the technical scope disclosed in this application are covered by the protection scope of this application, and will not be listed here.
本申请实施例还提供一种芯片系统,如图5所示,该芯片系统包括至少一个处理器1101和至少一个接口电路1102。处理器1101和接口电路1102可通过线路互联。例如,接口电路1102可用于从其它装置(例如电子设备100的存储器)接收信号。又例如,接口电路1102可用于向其它装置(例如处理器1101)发送信号。示例性的,接口电路1102可读取存储器中存储的指令,并将该指令发送给处理器1101。当所述指令被处理器1101执行时,可使得电子设备执行上述实施例中的电子设备100(比如,手机)执行的各个步骤。当然,该芯片系统还可以包含其他分立器件,本申请实施例 对此不作具体限定。An embodiment of the present application also provides a chip system. As shown in FIG. 5 , the chip system includes at least one processor 1101 and at least one interface circuit 1102 . The processor 1101 and the interface circuit 1102 may be interconnected by wires. For example, interface circuitry 1102 may be used to receive signals from other devices, such as the memory of electronic device 100 . As another example, interface circuit 1102 may be used to send signals to other devices (eg, processor 1101). For example, the interface circuit 1102 can read instructions stored in the memory and send the instructions to the processor 1101. When the instructions are executed by the processor 1101, the electronic device can be caused to perform various steps performed by the electronic device 100 (such as a mobile phone) in the above embodiments. Of course, the chip system may also include other discrete devices. The embodiments of this application There is no specific limit on this.
本申请实施例还提供一种装置,该装置包含在电子设备中,该装置具有实现上述实施例中任一方法中电子设备行为的功能。该功能可以通过硬件实现,也可以通过硬件执行相应的软件实现。硬件或软件包括至少一个与上述功能相对应的模块或单元。例如,检测模块或单元、确定模块或单元、以及处理模块或单元等。An embodiment of the present application also provides a device, which is included in an electronic device and has the function of realizing the behavior of the electronic device in any of the methods in the above embodiments. This function can be implemented by hardware, or it can be implemented by hardware executing corresponding software. The hardware or software includes at least one module or unit corresponding to the above functions. For example, a detection module or unit, a determination module or unit, a processing module or unit, etc.
本申请实施例还提供一种计算机存储介质,包括计算机指令,当计算机指令在电子设备上运行时,使得电子设备执行如上述实施例中任一方法。Embodiments of the present application also provide a computer storage medium that includes computer instructions. When the computer instructions are run on an electronic device, the electronic device causes the electronic device to perform any of the methods in the above embodiments.
本申请实施例还提供一种计算机程序产品,当计算机程序产品在计算机上运行时,使得计算机执行如上述实施例中任一方法。Embodiments of the present application also provide a computer program product. When the computer program product is run on a computer, it causes the computer to perform any of the methods in the above embodiments.
可以理解的是,上述电子设备等为了实现上述功能,其包含了执行各个功能相应的硬件结构和/或软件模块。本领域技术人员应该很容易意识到,结合本文中所公开的实施例描述的各示例的单元及算法步骤,本申请实施例能够以硬件或硬件和计算机软件的结合形式来实现。某个功能究竟以硬件还是计算机软件驱动硬件的方式来执行,取决于技术方案的特定应用和设计约束条件。专业技术人员可以对每个特定的应用来使用不同方法来实现所描述的功能,但是这种实现不应认为超出本发明实施例的范围。It can be understood that, in order to implement the above functions, the above-mentioned electronic devices include hardware structures and/or software modules corresponding to each function. Persons skilled in the art should easily realize that, in conjunction with the units and algorithm steps of each example described in the embodiments disclosed herein, the embodiments of the present application can be implemented in the form of hardware or a combination of hardware and computer software. Whether a function is performed by hardware or computer software driving the hardware depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementations should not be considered to be beyond the scope of the embodiments of the present invention.
本申请实施例可以根据上述方法示例对上述电子设备等进行功能模块的划分,例如,可以对应各个功能划分各个功能模块,也可以将两个或两个以上的功能集成在一个处理模块中。上述集成的模块既可以采用硬件的形式实现,也可以采用软件功能模块的形式实现。需要说明的是,本发明实施例中对模块的划分是示意性的,仅仅为一种逻辑功能划分,实际实现时可以有另外的划分方式。Embodiments of the present application can divide the above-mentioned electronic equipment into functional modules according to the above-mentioned method examples. For example, each functional module can be divided corresponding to each function, or two or more functions can be integrated into one processing module. The above integrated modules can be implemented in the form of hardware or software function modules. It should be noted that the division of modules in the embodiment of the present invention is schematic and is only a logical function division. In actual implementation, there may be other division methods.
通过以上的实施方式的描述,所属领域的技术人员可以清楚地了解到,为描述的方便和简洁,仅以上述各功能模块的划分进行举例说明,实际应用中,可以根据需要而将上述功能分配由不同的功能模块完成,即将装置的内部结构划分成不同的功能模块,以完成以上描述的全部或者部分功能。上述描述的系统,装置和单元的具体工作过程,可以参考前述方法实施例中的对应过程,在此不再赘述。Through the above description of the embodiments, those skilled in the art can clearly understand that for the convenience and simplicity of description, only the division of the above functional modules is used as an example. In actual applications, the above functions can be allocated as needed. It is completed by different functional modules, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above. For the specific working processes of the systems, devices and units described above, reference can be made to the corresponding processes in the foregoing method embodiments, which will not be described again here.
在本申请实施例各个实施例中的各功能单元可以集成在一个处理单元中,也可以是各个单元单独物理存在,也可以两个或两个以上单元集成在一个单元中。上述集成的单元既可以采用硬件的形式实现,也可以采用软件功能单元的形式实现。Each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically alone, or two or more units may be integrated into one unit. The above integrated units can be implemented in the form of hardware or software functional units.
所述集成的单元如果以软件功能单元的形式实现并作为独立的产品销售或使用时,可以存储在一个计算机可读取存储介质中。基于这样的理解,本申请实施例的技术方案本质上或者说对现有技术做出贡献的部分或者该技术方案的全部或部分可以以软件产品的形式体现出来,该计算机软件产品存储在一个存储介质中,包括若干指令用以使得一台计算机设备(可以是个人计算机,服务器,或者网络设备等)或处理器执行本申请各个实施例所述方法的全部或部分步骤。而前述的存储介质包括:快闪存储器、移动硬盘、只读存储器、随机存取存储器、磁碟或者光盘等各种可以存储程序代码的介质。If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it may be stored in a computer-readable storage medium. Based on this understanding, the technical solutions of the embodiments of the present application are essentially or contribute to the existing technology, or all or part of the technical solution can be embodied in the form of a software product. The computer software product is stored in a storage device. The medium includes several instructions to cause a computer device (which may be a personal computer, a server, or a network device, etc.) or a processor to execute all or part of the steps of the methods described in various embodiments of this application. The aforementioned storage media include: flash memory, mobile hard disk, read-only memory, random access memory, magnetic disk or optical disk and other media that can store program codes.
以上所述,仅为本申请的具体实施方式,但本申请的保护范围并不局限于此,任何在本申请揭露的技术范围内的变化或替换,都应涵盖在本申请的保护范围之内。因此,本申请的保护范围应以所述权利要求的保护范围为准。 The above are only specific embodiments of the present application, but the protection scope of the present application is not limited thereto. Any changes or substitutions within the technical scope disclosed in the present application shall be covered by the protection scope of the present application. . Therefore, the protection scope of this application should be subject to the protection scope of the claims.

Claims (14)

  1. 一种垃圾回收GC的方法,其特征在于,所述方法包括:A garbage collection GC method, characterized in that the method includes:
    电子设备运行应用APP;Electronic devices run application apps;
    当所述APP的进程请求分配第一内存时,所述电子设备检测所述进程预计占用的堆内存除以目标利用率的数值是否等于或大于第一阈值,其中,所述进程预计占用的堆内存包括所述进程已占用的堆内存和所述第一内存;When the process of the APP requests to allocate the 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 is equal to or greater than the first threshold, wherein the heap memory expected to be occupied by the process is The memory includes the heap memory 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 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 the second threshold;
    当所述进程对应的堆区数据压缩比等于或大于第二阈值,且所述电子设备当前的CPU负载大于第三阈值时,所述电子设备延迟所述进程执行垃圾回收。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 delays the process to perform garbage collection.
  2. 根据权利要求1所述的方法,其特征在于,所述当所述进程对应的堆区数据压缩比等于或大于第二阈值,且所述电子设备当前的CPU负载大于第三阈值时,所述电子设备延迟所述进程执行垃圾回收,包括:The method according to claim 1, characterized in that when the heap area data compression ratio corresponding to the process is equal to or greater than a second threshold, and the current CPU load of the electronic device is greater than a third threshold, the Electronic devices delay the process from performing garbage collection, including:
    当所述进程对应的堆区数据压缩比等于或大于第二阈值,且所述电子设备当前的CPU负载大于第三阈值时,所述电子设备上调所述第一阈值。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 increases the first threshold.
  3. 根据权利要求2所述的方法,其特征在于,在所述电子设备上调所述第一阈值后,所述方法还包括:The method of claim 2, wherein after the electronic device increases the first threshold, the method further includes:
    所述电子设备检测所述进程预计占用的堆内存除以目标利用率的数值是否等于或大于上调后的所述第一阈值;The electronic device detects 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;
    当所述电子设备检测所述进程预计占用的堆内存除以目标利用率的数值等于或大于上调后的所述第一阈值时,所述电子设备执行轻量级GC。When the electronic device detects that 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 raised first threshold, the electronic device executes a lightweight GC.
  4. 根据权利要求3所述的方法,其特征在于,在所述电子设备执行轻量级GC之后,所述方法还包括:The method according to claim 3, characterized in that, after the electronic device performs lightweight GC, the method further includes:
    所述电子设备检测所述进程预计占用的堆内存除以目标利用率的数值是否等于或大于上调后的所述第一阈值;The electronic device detects 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 raised first threshold, the electronic device raises the first threshold again.
  5. 根据权利要求4所述的方法,其特征在于,在所述电子设备再次上调所述第一阈值之后,所述方法还包括:The method of claim 4, wherein after the electronic device raises the first threshold again, the method further includes:
    当所述进程预计占用的堆内存除以目标利用率的数值等于或大于再次上调后的所述第一阈值时,所述电子设备再次检测所述进程对应的堆区数据压缩比;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 first threshold after being raised again, the electronic device detects the heap area data compression ratio corresponding to the process again;
    所述电子设备根据所述进程对应的堆区数据压缩比采用不同的策略,延迟执行全量级垃圾回收GC。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.
  6. 根据权利要求5所述的方法,其特征在于,所述电子设备根据所述进程对应的堆区数据压缩比采用不同的策略,延迟执行全量级垃圾回收GC,包括:The method according to claim 5, characterized in that 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, including:
    当所述进程对应的堆区数据压缩比位于第一区间时,所述电子设备避开CPU负载的高峰期执行全量级GC;When the heap area data compression ratio corresponding to the process is in the first interval, the electronic device avoids the peak period of CPU load and executes full-scale GC;
    当所述进程对应的堆区数据压缩比位于第二区间时,所述电子设备在CPU空闲时执行全量级GC; When the heap area data compression ratio corresponding to the process is in the second interval, the electronic device executes a full-scale GC when the CPU is idle;
    当所述进程对应的堆区数据压缩比位于第三区间时,所述电子设备冻结所述进程,并在接收到解冻所述进程的指令后,解冻所述进程并执行全量级GC。When the heap data compression ratio 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 executes a full-scale GC.
  7. 根据权利要求2所述的方法,其特征在于,在所述电子设备上调所述第一阈值后,所述方法还包括:The method of claim 2, wherein after the electronic device increases 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 is equal to or greater than the raised first threshold, the electronic device detects the heap area data compression ratio corresponding to the process again;
    所述电子设备根据所述进程对应的堆区数据压缩比采用不同的策略,延迟执行全量级垃圾回收GC。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.
  8. 根据权利要求7所述的方法,其特征在于,所述电子设备根据所述进程对应的堆区数据压缩比采用不同的策略,延迟执行全量级垃圾回收GC,包括:The method according to claim 7, characterized in that 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, including:
    当所述进程对应的堆区数据压缩比位于第一区间时,所述电子设备避开CPU负载的高峰期执行全量级GC;When the heap area data compression ratio corresponding to the process is in the first interval, the electronic device avoids the peak period of CPU load and executes full-scale GC;
    当所述进程对应的堆区数据压缩比位于第二区间时,所述电子设备在CPU空闲时执行全量级GC;When the heap area data compression ratio corresponding to the process is in the second interval, the electronic device executes a full-scale GC when the CPU is idle;
    当所述进程对应的堆区数据压缩比位于第三区间时,所述电子设备冻结所述进程,并在接收到解冻所述进程的指令后,解冻所述进程并执行全量级GC。When the heap data compression ratio 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 executes a full-scale GC.
  9. 根据权利要求1-8任一项所述方法,其特征在于,所述方法还包括:The method according to any one of claims 1-8, characterized in that, the method further includes:
    在所述进程申请堆内存时,所述电子设备对所述进程申请的堆内存进行标记。When the process applies for heap memory, the electronic device marks the heap memory applied for by the process.
  10. 根据权利要求9所述的方法,其特征在于,所述方法还包括:The method of claim 9, further comprising:
    在所述电子设备压缩所述进程的堆区数据时,所述电子设备记录压缩的堆区数据的数据量。When the electronic device compresses the heap data of the process, the electronic device records the data amount of the compressed heap data.
  11. 根据权利要求1-10任一项所述的方法,其特征在于,The method according to any one of claims 1-10, characterized in that,
    所述进程的所述堆区数据压缩比,为所述进程中压缩的堆区数据的数据量与本进程中全部堆区数据的数据量的比值。The heap area data compression ratio 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 current process.
  12. 一种电子设备,其特征在于,包括:处理器、存储器和触摸屏,所述存储器、所述触摸屏与所述处理器耦合,所述存储器用于存储计算机程序代码,所述计算机程序代码包括计算机指令,当所述处理器从所述存储器中读取所述计算机指令,以使得所述电子设备执行如权利要求1-11中任一项所述的垃圾回收GC的方法。An electronic device, characterized by comprising: a processor, a memory and a touch screen, the memory, the touch screen are coupled to the processor, the memory is used to store computer program code, the computer program code includes computer instructions , when the processor reads the computer instructions from the memory, so that the electronic device executes the garbage collection GC method according to any one of claims 1-11.
  13. 一种计算机可读存储介质,其特征在于,包括计算机指令,当所述计算机指令在电子设备上运行时,使得所述电子设备执行如权利要求1-11中任一项所述的垃圾回收GC的方法。A computer-readable storage medium, characterized by comprising computer instructions that, when the computer instructions are run on an electronic device, cause the electronic device to perform the garbage collection GC according to any one of claims 1-11 Methods.
  14. 一种芯片系统,其特征在于,包括一个或多个处理器,当所述一个或多个处理器执行指令时,所述一个或多个处理器执行如权利要求1-11中任一项所述的垃圾回收GC的方法。 A chip system, characterized in that it includes one or more processors. When the one or more processors execute instructions, the one or more processors execute the method according to any one of claims 1-11. The garbage collection GC method described above.
PCT/CN2023/087634 2022-04-19 2023-04-11 Garbage recycling method and electronic device WO2023202429A1 (en)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN202210409365.4 2022-04-19
CN202210409365.4A CN114968836A (en) 2022-04-19 2022-04-19 Garbage recycling method and electronic equipment

Publications (1)

Publication Number Publication Date
WO2023202429A1 true WO2023202429A1 (en) 2023-10-26

Family

ID=82976663

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2023/087634 WO2023202429A1 (en) 2022-04-19 2023-04-11 Garbage recycling method and electronic device

Country Status (2)

Country Link
CN (1) CN114968836A (en)
WO (1) WO2023202429A1 (en)

Families Citing this family (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114968836A (en) * 2022-04-19 2022-08-30 华为技术有限公司 Garbage recycling method and electronic equipment
CN118069542A (en) * 2022-11-22 2024-05-24 荣耀终端有限公司 Memory garbage recycling method and electronic equipment

Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101281458A (en) * 2008-05-14 2008-10-08 华为技术有限公司 Apparatus, system and for recycling rubbish
US20180210825A1 (en) * 2017-01-20 2018-07-26 Samsung Electronics Co., Ltd. Tail latency aware foreground garbage collection algorithm
CN110633113A (en) * 2019-09-02 2019-12-31 北京安云世纪科技有限公司 Method and system for starting application program, storage medium and mobile device
CN113626431A (en) * 2021-07-28 2021-11-09 浪潮云信息技术股份公司 LSM tree-based key value separation storage method and system for delaying garbage recovery
WO2022017002A1 (en) * 2020-07-22 2022-01-27 华为技术有限公司 Garbage collection method and device
CN114968836A (en) * 2022-04-19 2022-08-30 华为技术有限公司 Garbage recycling method and electronic equipment

Patent Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101281458A (en) * 2008-05-14 2008-10-08 华为技术有限公司 Apparatus, system and for recycling rubbish
US20180210825A1 (en) * 2017-01-20 2018-07-26 Samsung Electronics Co., Ltd. Tail latency aware foreground garbage collection algorithm
CN110633113A (en) * 2019-09-02 2019-12-31 北京安云世纪科技有限公司 Method and system for starting application program, storage medium and mobile device
WO2022017002A1 (en) * 2020-07-22 2022-01-27 华为技术有限公司 Garbage collection method and device
CN113626431A (en) * 2021-07-28 2021-11-09 浪潮云信息技术股份公司 LSM tree-based key value separation storage method and system for delaying garbage recovery
CN114968836A (en) * 2022-04-19 2022-08-30 华为技术有限公司 Garbage recycling method and electronic equipment

Also Published As

Publication number Publication date
CN114968836A (en) 2022-08-30

Similar Documents

Publication Publication Date Title
WO2023202429A1 (en) Garbage recycling method and electronic device
WO2022257748A1 (en) Virtual memory management method and electronic device
WO2022199509A1 (en) Method for application performing drawing operation, and electronic device
WO2022052897A1 (en) Method and device for adjusting memory configuration parameter
WO2021218370A1 (en) Memory management method and terminal device
WO2021223539A1 (en) Radio frequency resource allocation method and apparatus
CN114461589B (en) Method for reading compressed file, file system and electronic equipment
WO2021169379A1 (en) Permission reuse method, permission reuse-based resource access method, and related device
WO2022083465A1 (en) Electronic device screen projection method, medium thereof, and electronic device
CN111381996B (en) Memory exception handling method and device
CN115794361A (en) Method for managing memory and electronic equipment
WO2024037068A1 (en) Task scheduling method, electronic device and computer-readable storage medium
CN114579954A (en) Method for safely starting verification and electronic equipment
WO2023185684A1 (en) Process killing method for application, and electronic device
CN116126744B (en) Memory recycling method and device and terminal equipment
CN115794413B (en) Memory processing method and related device
CN112783418A (en) Method for storing application program data and mobile terminal
US20110185142A1 (en) Information processing apparatus and data saving acceleration method of the information processing apparatus
CN114253737B (en) Electronic device, memory recovery method thereof and medium
CN117130824A (en) Method for processing exception, electronic equipment and storage medium
CN113760191B (en) Data reading method, data reading apparatus, storage medium, and program product
WO2020147859A1 (en) Decentralized fat lock deflation
CN113760192A (en) Data reading method, data reading apparatus, storage medium, and program product
WO2024032430A1 (en) Memory management method and electronic device
US20240357705A1 (en) Cellular network control method and electronic device

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 23791084

Country of ref document: EP

Kind code of ref document: A1