WO2023231900A1 - 一种内存管理方法及相关装置 - Google Patents

一种内存管理方法及相关装置 Download PDF

Info

Publication number
WO2023231900A1
WO2023231900A1 PCT/CN2023/096330 CN2023096330W WO2023231900A1 WO 2023231900 A1 WO2023231900 A1 WO 2023231900A1 CN 2023096330 W CN2023096330 W CN 2023096330W WO 2023231900 A1 WO2023231900 A1 WO 2023231900A1
Authority
WO
WIPO (PCT)
Prior art keywords
memory
type
thread
memory area
program
Prior art date
Application number
PCT/CN2023/096330
Other languages
English (en)
French (fr)
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 WO2023231900A1 publication Critical patent/WO2023231900A1/zh

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]

Definitions

  • the present application relates to the field of terminals, and in particular, to a memory management method and related devices.
  • the present application provides a memory management method and related devices, which implements thread-based business types and allocates a part of memory in the memory area corresponding to the business type to the thread.
  • the electronic device 100 can sequentially reclaim the memory in the memory areas corresponding to different service types based on the priority of the service type and in order from low to high priority. In this way, the electronic device can preferentially recycle data of threads of unimportant (low priority) business types, and recycle data of threads of important (high priority) business types as late as possible to ensure the running of the application program.
  • this application provides a memory management method, which is applied to an electronic device.
  • the electronic device is allocated memory areas of multiple service types.
  • the memory areas of the multiple service types include at least a first service type memory area and a second service type.
  • the memory area of the first business type is used to store the data of the thread whose business type is the first business type
  • the memory area of the second business type is used to store the data of the thread whose business type is the second business type
  • the priority of the second service type is lower than the priority of the first service type
  • the method includes:
  • the memory in the memory area of the second business type is recovered.
  • the method further includes: receiving a memory allocation request from the first thread of the first program;
  • the memory of the memory area of the first business type is allocated to the first thread.
  • the memory area of the first business type is in the memory space of the first program.
  • the memory area of the first business type is used to store the first business type.
  • the service type of the program is the data of the thread of the first service type, and the service type of the first thread is the first service type.
  • the method further includes: receiving a memory allocation request from the first thread of the first program;
  • the memory area of the business type can be further divided, which can reduce the number of memory areas and make management easier.
  • the method further includes: using the first service type to mark the memory area of the first service type.
  • the memory area can be identified based on the business type, making it easier to find the memory area and reclaim the memory in the memory area.
  • the first business type and the second business type are any one of non-critical business types, display business type, interactive business type, stack business type, and push business type;
  • the order of priority of business types from low to high is: non-critical business type, display business type, interactive business type, stack business type, and push business type.
  • the first business type and the second business type are any one of non-critical business types, display business type, interactive business type, stack business type, and push business type;
  • the priority of the business types from low to high is: non-critical business type, heap area business type, push business type, display business type, interactive business type;
  • the priority of the business types from low to high is: non-critical business type, display business type, interactive business type, heap business type, and push business type.
  • the memory space of the first program includes a memory area of the first business type and a memory area of the second business type
  • the memory space of the second program also includes a memory area of the first business type and a memory area of the second business type.
  • the memory area of the first business type of the first program and the first memory area of the second program are recycled.
  • the memory area of the business type is recycled.
  • the memory space of the first program includes a memory area of the first business type
  • the memory space of the second program also includes a memory area of the first business type
  • the priority of the second program is lower than In the case of the priority of the first program, the memory in the memory area of the first business type is recycled, specifically including:
  • the memory of the memory area of the first business type of the second program is recovered.
  • the method before reclaiming the memory of the memory area of the first business type of the second program, the method further includes:
  • the priorities of the second program and the first program are determined based on the frequency of use of the second program and the first program, and the frequency of use of the second program is lower than the frequency of use of the first program.
  • applications can be prioritized based on their frequency of use, and data from applications used more by users can be recycled later.
  • the memory in the memory area of the first service type is reclaimed based on the least recently used LRU algorithm. In this way, when recycling memory in a certain memory area, it is ensured that the most frequently called memory pages are finally recycled, thereby reducing the operating frequency of electronic devices re-reading data into memory.
  • the method also includes:
  • the service type of the first thread is determined based on the identification and/or application scenario of the first thread. In this way, the business type of the thread can be more accurately determined according to the thread's identification and/application scenario.
  • this application provides another memory management method, which is applied to electronic devices, including:
  • the first type memory area is in the memory space of the first program.
  • the first type memory area is used to store threads whose business type is the first type. data, the business type of the first thread is the first business type;
  • the memory in the memory areas corresponding to different business types is recovered in sequence; among them, one type of memory area is used to store the data of threads of one business type, and the memory areas corresponding to different business types are
  • the memory area includes a first type memory area.
  • the memory area is allocated to the thread based on the business type of the thread, so that the data of threads of different business types can be recycled based on the priority of the business type. Based on the priority of the business type, the data of threads with low business type priority will be recycled first, and the data of threads with high (important) business type priority will not be easily recycled, thus avoiding program lag caused by data recycling of important threads.
  • the present application provides an electronic device, including one or more processors and one or more memories.
  • the one or more memories are coupled to one or more processors.
  • the one or more memories are used to store computer program codes.
  • the computer program codes include computer instructions.
  • the electronic device causes the electronic device to execute Memory management method in any possible implementation of any of the above aspects.
  • embodiments of the present application 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 execute the memory management method in any of the possible implementations of any of the above aspects. .
  • embodiments of the present application provide a computer program product.
  • the computer program product When the computer program product is run on a computer, it causes the computer to execute the memory management method in any of the possible implementations of any of the above aspects.
  • Figure 1 is a schematic structural diagram of an electronic device 100 provided by an embodiment of the present application.
  • Figure 2 is a schematic diagram of a virtual address space provided by an embodiment of the present application.
  • Figure 3 is a schematic diagram of thread memory allocation provided by an embodiment of the present application.
  • Figure 4 is a schematic diagram of the corresponding relationship between a virtual address space and an identifier provided by an embodiment of the present application
  • Figure 5 is a schematic diagram of a memory recycling process provided by an embodiment of the present application.
  • Figure 6 is a schematic diagram of a thread memory allocation process provided by an embodiment of the present application.
  • Figure 7 is a schematic diagram of a software architecture provided by an embodiment of the present application.
  • Figure 8 is a schematic flow chart of a memory management method provided by an embodiment of the present application.
  • Figure 9 is a schematic diagram of a thread memory allocation process provided by an embodiment of the present application.
  • Figure 10A is a schematic diagram of a memory recycling sequence provided by an embodiment of the present application.
  • Figure 10B is a schematic diagram of another memory recycling sequence provided by an embodiment of the present application.
  • Figure 10C is a schematic diagram of another memory recycling sequence provided by an embodiment of the present application.
  • Figure 11 is a flow chart of an example of a memory management method provided by an embodiment of the present application.
  • Figure 12A is a flow chart of another example of a memory management method provided by an embodiment of the present application.
  • Figure 12B is a schematic diagram of another memory recycling sequence provided by an embodiment of the present application.
  • Figure 13A is a flow chart of another example of a memory management method provided by an embodiment of the present application.
  • Figure 13B is a schematic diagram of another memory recycling sequence provided by an embodiment of the present application.
  • Figure 14 is a schematic flow chart of another memory management method provided by an embodiment of the present application.
  • Figure 15 is a schematic diagram of another thread memory allocation process provided by an embodiment of the present application.
  • Figure 16 is a flow chart of another example of a memory management method provided by an embodiment of the present application.
  • Figure 17A is a flow chart of another example of a memory management method provided by an embodiment of the present application.
  • Figure 17B is a schematic diagram of another memory recycling sequence provided by an embodiment of the present application.
  • Figure 18 is a schematic flow chart of another memory management method provided by an embodiment of the present application.
  • Figure 19 is a flow chart of another example of a memory management method provided by an embodiment of the present application.
  • Figure 20 is a flow chart of another example of a memory management method provided by an embodiment of the present application.
  • first and second are used for descriptive purposes only and shall not be understood as implying or implying relative importance or implicitly specifying the quantity of indicated technical features. Therefore, the features defined as “first” and “second” may explicitly or implicitly include one or more of the features. In the description of the embodiments of this application, unless otherwise specified, “plurality” The meaning is two or more.
  • FIG. 1 shows a schematic structural diagram of an electronic device 100 .
  • the electronic device 100 may be a mobile phone, a tablet computer, a desktop computer, a laptop computer, a handheld computer, a notebook computer, an ultra-mobile personal computer (UMPC), a netbook, a cellular phone, a personal digital assistant (personal digital assistant) digital assistant (PDA), augmented reality (AR) device, virtual reality (VR) device, artificial intelligence (AI) device, wearable device, vehicle-mounted device, smart home device and/or Smart city equipment, the specific type of electronic equipment in the embodiment of this application There are no special restrictions on the type.
  • PDA personal digital assistant
  • AR augmented reality
  • VR virtual reality
  • AI artificial intelligence
  • 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, 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 (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, speaker 170A, receiver 170B, microphone 170C, headphone interface 170D, sensor module 180, button 190, motor 191, indicator 192, camera 193, display
  • 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 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 .
  • the electronic device 100 may also adopt different interface connection methods in the above embodiments, or a combination of multiple interface connection methods.
  • 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.
  • the power management module 141 receives input from the battery 142 and/or the charging management module 140, and supplies power to the processor 110, the internal memory 121, the display screen 194, the camera 193, the wireless communication module 160, and the like.
  • the power management module 141 can also be used to monitor battery capacity, Battery cycle times, battery health status (leakage, impedance) and other parameters.
  • the power management module 141 may also be provided in the processor 110 .
  • 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 antenna 1 of the electronic device 100 is coupled to the mobile communication module 150, and the antenna 2 is coupled to the wireless communication module 160, so that the electronic device 100 can communicate with the network and other devices through wireless communication technology.
  • the wireless communication technology may include global system for mobile communications (GSM), general packet radio service (GPRS), code division multiple access (CDMA), broadband Code division multiple access (wideband code division multiple access, WCDMA), time-division code division multiple access (TD-SCDMA), long term evolution (long term evolution, LTE), BT, GNSS, WLAN, NFC , FM, and/or IR technology, etc.
  • the GNSS may include global positioning system (GPS), global navigation satellite system (GLONASS), Beidou navigation satellite system (BDS), quasi-zenith satellite system (quasi -zenith satellite system (QZSS) and/or satellite based augmentation systems (SBAS).
  • GPS global positioning system
  • GLONASS global navigation satellite system
  • BDS Beidou navigation satellite system
  • QZSS quasi-zenith satellite system
  • SBAS satellite based augmentation systems
  • 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 (OLED), an active matrix organic light emitting diode or an active matrix organic light emitting diode (active-matrix organic light emitting diode).
  • LCD liquid crystal display
  • OLED organic light-emitting diode
  • AMOLED organic light-emitting diode
  • FLED flexible light-emitting diode
  • Miniled MicroLed, Micro-oLed, quantum dot light emitting diode (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.
  • the object passes through the lens to produce an optical image that is projected onto the photosensitive element.
  • Digital signal processors are used to process digital signals.
  • Video codecs are used to compress or decompress digital video.
  • NPU is a neural network (NN) computing processor. By drawing on the structure of biological neural networks, such as the transmission mode between neurons in the human brain, it can quickly process input information and can continuously learn by itself. Intelligent cognitive applications of the electronic device 100 can be implemented through the NPU, such as image recognition, face recognition, speech recognition, text understanding, etc.
  • the internal memory 121 may include one or more random access memories (RAM) and one or more non-volatile memories (NVM).
  • RAM random access memories
  • NVM non-volatile memories
  • Random access memory can include static random-access memory (SRAM), dynamic random-access memory (DRAM), synchronous dynamic random-access memory (SDRAM), double data rate synchronous Dynamic random access memory (double data rate synchronous dynamic random access memory, DDR SDRAM, such as the fifth generation DDR SDRAM is generally called DDR5SDRAM), etc.;
  • SRAM static random-access memory
  • DRAM dynamic random-access memory
  • SDRAM synchronous dynamic random-access memory
  • DDR SDRAM double data rate synchronous Dynamic random access memory
  • DDR SDRAM double data rate synchronous dynamic random access memory
  • DDR5SDRAM double data rate synchronous dynamic random access memory
  • Non-volatile memory can include disk storage devices and flash memory.
  • Flash memory can be divided according to the operating principle to include NOR FLASH, NAND FLASH, 3D NAND FLASH, etc.
  • the storage unit potential level it can include single-level storage cells (single-level cell, SLC), multi-level storage cells (multi-level cell, MLC), third-level storage unit (triple-level cell, TLC), fourth-level storage unit (quad-level cell, QLC), etc., which can include universal flash storage (English: universal flash storage, UFS) according to storage specifications. , embedded multi media card (embedded multi media Card, eMMC), etc.
  • the random access memory can be directly read and written by the processor 110, can be used to store executable programs (such as machine instructions) of the operating system or other running programs, and can also be used to store user and application data, etc.
  • the non-volatile memory can also store executable programs and user and application program data, etc., and can be loaded into the random access memory in advance for direct reading and writing by the processor 110.
  • the external memory interface 120 can be used to connect an external non-volatile memory to expand the storage capacity of the electronic device 100 .
  • the external non-volatile memory communicates with the processor 110 through the external memory interface 120 to implement the data storage function. For example, save music, video and other files in external non-volatile memory.
  • 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 audio module 170 is used to convert digital audio information into analog audio signal output, and is also used to convert analog audio input into digital audio signals. Audio module 170 may also be used to encode and decode audio signals. In some embodiments, the tone The audio module 170 may be disposed in the processor 110 , or some functional modules of the audio module 170 may be disposed in the processor 110 .
  • Speaker 170A also called “speaker” is used to convert audio electrical signals into sound signals.
  • the electronic device 100 can listen to music through the speaker 170A, or listen to hands-free calls.
  • Receiver 170B also called “earpiece” is used to convert audio electrical signals into sound signals.
  • the electronic device 100 answers a call or a voice message, the voice can be heard by bringing the receiver 170B close to the human ear.
  • Microphone 170C also called “microphone” or “microphone” is used to convert sound signals into electrical signals. When making a call or sending a voice message, the user can speak close to the microphone 170C with the human mouth and input the sound signal to the microphone 170C.
  • the electronic device 100 may be provided with at least one microphone 170C. In other embodiments, the electronic device 100 may be provided with two microphones 170C, which in addition to collecting sound signals, may also implement a noise reduction function. In other embodiments, the electronic device 100 can also be provided with three, four or more microphones 170C to collect sound signals, reduce noise, identify sound sources, and implement directional recording functions, etc.
  • the headphone interface 170D is used to connect wired headphones.
  • the headphone interface 170D may be a USB interface 130, or may be a 3.5mm open mobile terminal platform (OMTP) standard interface, or a Cellular Telecommunications Industry Association of the USA (CTIA) standard interface.
  • OMTP open mobile terminal platform
  • CTIA Cellular Telecommunications Industry Association of the USA
  • the pressure sensor 180A is used to sense pressure signals and can convert the pressure signals into electrical signals.
  • pressure sensor 180A may be disposed on display screen 194 .
  • the gyro sensor 180B may be used to determine the motion posture of the electronic device 100 .
  • Air pressure sensor 180C is used to measure air pressure.
  • Magnetic sensor 180D includes a Hall sensor.
  • the electronic device 100 may utilize the magnetic sensor 180D to detect opening and closing of the flip holster.
  • the acceleration sensor 180E can detect the acceleration of the electronic device 100 in various directions (generally three axes). When the electronic device 100 is stationary, the magnitude and direction of gravity can be detected. It can also be used to identify the posture of electronic devices and be used in horizontal and vertical screen switching, pedometer and other applications.
  • Distance sensor 180F for measuring distance.
  • Electronic device 100 can measure distance via infrared or laser. In some embodiments, when shooting a scene, the electronic device 100 may utilize the distance sensor 180F to measure distance to achieve fast focusing.
  • Proximity light sensor 180G may include, for example, a light emitting diode (LED) and a light detector, such as a photodiode.
  • the light emitting diode may be an infrared light emitting diode.
  • the electronic device 100 emits infrared light outwardly through the light emitting diode.
  • Electronic device 100 uses photodiodes to detect infrared reflected light from nearby objects. When sufficient reflected light is detected, it can be determined that there is an object near the electronic device 100 .
  • the electronic device 100 may determine that there is no object near the electronic device 100 .
  • the electronic device 100 can use the proximity light sensor 180G to detect when the user holds the electronic device 100 close to the ear for talking, so as to automatically turn off the screen to save power.
  • the proximity light sensor 180G can also be used in holster mode, and pocket mode automatically unlocks and locks the screen.
  • the ambient light sensor 180L is used to sense ambient light brightness.
  • Fingerprint sensor 180H is used to collect fingerprints.
  • Temperature sensor 180J is used to detect temperature.
  • Touch sensor 180K also known as "touch device”.
  • the touch sensor 180K can be disposed on the display screen 194.
  • the touch sensor 180K and the display screen 194 form a touch screen, which is also called a "touch screen”.
  • the touch sensor 180K is used to detect a touch operation on or near the touch sensor 180K.
  • the touch sensor can pass the detected touch operation to the application processor to determine the touch event type.
  • Visual output related to the touch operation may be provided through display screen 194 .
  • the touch sensor 180K may also be disposed on the surface of the electronic device 100 at a location different from that of the display screen 194 .
  • Bone conduction sensor 180M can acquire vibration signals.
  • the bone conduction sensor 180M can acquire the vibration signal of the vibrating bone mass of the human body's vocal part.
  • 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 motor 191 can generate vibration prompts.
  • Motor 191 can be used to vibrate for incoming calls or for touch vibration feedback. Feedback.
  • touch operations for different applications can correspond to different vibration feedback effects.
  • the motor 191 can also respond to different vibration feedback effects for touch operations in different areas of the display screen 194 .
  • Different application scenarios such as time reminders, receiving information, alarm clocks, games, etc.
  • the touch vibration feedback effect can also be customized.
  • the indicator 192 may be an indicator light, which may be used to indicate charging status, power changes, or may be used to indicate messages, missed calls, notifications, etc.
  • the SIM card interface 195 is used to connect a SIM card.
  • the SIM card can be connected to or separated from the electronic device 100 by inserting it into the SIM card interface 195 or pulling it out from the SIM card interface 195 .
  • the electronic device 100 can support 1 or N SIM card interfaces, where N is a positive integer greater than 1.
  • SIM card interface 195 can support Nano SIM card, Micro SIM card, SIM card, etc. Multiple cards can be inserted into the same SIM card interface 195 at the same time. The types of the plurality of cards may be the same or different.
  • the SIM card interface 195 is also compatible with different types of SIM cards.
  • the SIM card interface 195 is also compatible with external memory cards.
  • the electronic device 100 interacts with the network through the SIM card to implement functions such as calls and data communications.
  • the electronic device 100 uses an eSIM, that is, an embedded SIM card.
  • the eSIM card can be embedded in the electronic device 100 and cannot be separated from the electronic device 100 .
  • the operating system can allocate a virtual address space to the target application.
  • the operating system allocates a partial virtual memory address of the virtual address space to the thread.
  • the address given in the instruction is a virtual address.
  • the CPU executes the instruction, it must first convert the virtual address into a physical address before fetching instructions and data from the physical memory.
  • the address translation work can be completed by the memory management unit (MMU) in the CPU.
  • the physical address is an address that can be recognized and accessed by specific physical hardware.
  • a virtual address is an address that can be recognized and accessed by applications recognized by the operating system.
  • the target application's virtual address space includes multiple functional areas.
  • the multiple functional areas may include kernel space, stack, file mapping area, heap, bss segment, data segment, code segment and reserved area.
  • the kernel space stores the operating system code, and each process can map the virtual address of the kernel space to the same physical address.
  • the stack is used to store local variables used by the process, function parameter values, return values, etc. It is a data structure that extends from a high virtual address to a low virtual address.
  • the file mapping area is used to store file data read from the hard disk.
  • the heap is used to store memory segments that are dynamically allocated while the process is running. It is a data structure that extends from a low virtual address to a high virtual address.
  • the bss section is used to store uninitialized global variables and static local variables.
  • the data segment is used to store global variables and static local variables that have been initialized in the application.
  • the code segment is used to store the execution code of the application (machine instructions executed by the CPU). None of the virtual addresses in the reserved area are mapped to physical addresses, and the operating system does not allow any user process to access the reserved area.
  • the operating system divides the memory into multiple memory pages according to fixed sizes. Memory pages in the virtual address space and memory pages in the physical address space can be mapped through page tables.
  • the operating system will not load all the data of the target application into physical memory.
  • the operating system will first allocate virtual memory pages to the virtual address space. These virtual memory pages are not mapped to the physical memory pages. relation. Only when the target application accesses the instructions or data in the virtual memory page, the operating system can establish a mapping relationship between the accessed virtual memory page and the physical memory page, and transfer the data indicated by the virtual memory page (for example, local data generated during program running) variables, disk data, etc.), read into physical memory pages.
  • the threads of the target application may include but are not limited to window management thread (user interface thread, UI Thread), image rendering thread (render thread), system service thread (andriod pools), push service thread, etc.
  • the window management thread can be used to manage windows.
  • the window management thread can be used to obtain the display size. Determine whether there is a status bar, lock the screen, capture the screen, etc.
  • the image rendering thread can be used for image rendering.
  • System service threads can be used to call system services.
  • Push service threads can be used to manage communication information. It should be noted that these threads are only examples of threads of the target application, and should not constitute specific limitations on the names, functions, etc. of the threads of the target application.
  • the operating system When the operating system receives memory allocation requests submitted by threads of the target application, it can allocate one or more memory pages to these threads through a memory allocator (for example, jemalloc allocator, scudo allocator, musl allocator, etc.).
  • a memory allocator for example, jemalloc allocator, scudo allocator, musl allocator, etc.
  • One or more memory pages may be collectively referred to as virtual memory areas (VMAs).
  • the thread initiates a memory allocation request to the memory allocator and executes it in user mode.
  • the memory allocator allocates memory pages to threads for execution in kernel mode.
  • the gray squares in Figure 3 can represent the memory pages occupied by the thread, and the white squares can represent the memory pages not occupied by the thread.
  • the memory page allocated to the thread by the memory allocator is a virtual memory page that has established a mapping relationship with the physical memory
  • the one or more memory pages can be divided into file pages (file-backed pages) and anonymous pages (anonymous pages).
  • the file page can be used to store file data. For example, code snippets, accessed file data, etc.
  • the data stored in the file page can be loaded from the disk.
  • the anonymous page is the memory dynamically allocated when the application is running, and is used to store data generated when the application is running.
  • each file page has a corresponding file name.
  • Anonymous pages have no corresponding identifier.
  • a file (for example, a smaps file) including a correspondence between a virtual address space and an identifier is stored in the electronic device 100 .
  • the file records the virtual address range of the file page and the name of the file page in the virtual address range, for example, "filename1", "filename2".
  • Figure 4 also shows the address range of anonymous pages.
  • the names of all anonymous pages are "anon”. That is to say, the operating system cannot distinguish different anonymous pages based on the names of anonymous pages.
  • the operating system can reclaim memory pages based on the least recently used (LRU) algorithm. Specifically, as shown in Figure 5, the operating system reclaims memory as follows: the operating system can add the latest accessed memory page to the head of the active list (active list), and move the memory page of the active list to the end of the list. The memory page at the end of the list is moved to the head of the inactive list (inactive list), the memory page of the inactive list is moved to the end of the list, and the memory page at the end of the inactive list is removed from the inactive list and recycled by the operating system .
  • LRU least recently used
  • the operating system can generate active linked lists and inactive linked lists for file pages, as well as active linked lists and inactive linked lists for anonymous pages. It should be noted that when the operating system recycles a file page, the operating system needs to query whether the file page has been modified by the application. If the file page has not been modified, it will directly release the memory of the file page. If the file page has been modified, Then the contents of the modified file page are written back to the disk, and then the file page is released. In this way, the operating system can release the most recently unused file pages based on the LRU algorithm to relieve memory pressure.
  • the operating system when the operating system recycles anonymous pages, it only recycles them based on the latest access status of the anonymous page. If the data stored in the anonymous page is key data of the application, the data in the anonymous page is generated during the running of the program. Data, there is no data corresponding to the anonymous page in the disk, so once the anonymous page is recycled, it may cause the program to freeze due to the loss of key data.
  • the operating system after the operating system allocates the virtual address space, it can uniformly name the allocated vma.
  • the virtual address space can be named "libc_malloc".
  • the jemalloc memory allocator can call the mmap interface to allocate vma to the thread and map the physical memory to vma.
  • the jemalloc memory allocator can also call the prctl interface to name the allocated vma.
  • the jemalloc memory allocator can uniformly name the allocated vma "anon:libc_malloc", as shown in Figure 6.
  • the electronic device 100 may store the name of the vma and other information in the memory. Specifically, the information may be stored in the smaps file of the memory. In other words, the memory allocator will not name the allocated vma, or, even if it names the allocated vma, it will only give all vma the same name. Because the vma allocated by the memory allocator has no identity, or the identities of all vma are the same At the same time, the operating system cannot distinguish between different VMAs and cannot retain the anonymous pages where the thread's critical data is located.
  • 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.
  • This embodiment of the present invention takes an operating system with a layered architecture as an example to illustrate the software structure of the electronic device 100 .
  • the software architecture of the electronic device 100 includes several layers, and each layer has clear roles and division of labor.
  • the layers communicate through software interfaces.
  • the operating system is divided into four layers, from top to bottom: application layer, system layer, hardware abstraction layer, and kernel layer.
  • the application layer can be the application layer and the application framework layer.
  • the application layer includes a series of application packages.
  • the application package can include camera, gallery, calendar, calling, map, navigation, WLAN, Bluetooth, music, video, SMS and other applications.
  • the application framework layer provides application programming interface (application programming interface, API) and programming framework for applications.
  • the application framework layer includes some predefined functions.
  • the application framework layer can include window managers, content providers, view systems, resource managers, notification managers, etc.
  • the system layer can include system libraries (native) and runtime.
  • the system library 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.
  • Runtime can refer to all code libraries, frameworks, etc. required when the program is running.
  • the runtime includes a series of function libraries required to run C programs.
  • the runtime includes a core library and a virtual machine.
  • the core library can include functional functions that the Java language needs to call.
  • the application layer and application framework layer run in virtual machines.
  • the virtual machine executes the Java files of the application layer and application framework layer as 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 hardware abstraction layer is used to provide applications with access interfaces to different hardware devices.
  • the hardware abstraction layer is used to encapsulate the kernel driver and provide hardware interfaces to the upper layer.
  • the kernel layer is the layer between hardware and software and can be used for process management, memory management, etc.
  • Java threads and C/C++ threads are created.
  • Java threads run on virtual machines and are mainly used to implement basic program functions and user operation logic.
  • the virtual machine can obtain memory from the kernel layer through the mmap interface.
  • the mmap interface is used to map part of the physical memory from the kernel state to the user state.
  • C/C++ threads are mainly used to implement core functions of the program, such as rendering, communication, etc.
  • C/C++ threads can obtain memory through the memory allocator of the native layer (for example, jemalloc allocator, scudo allocator, etc.).
  • the memory allocator also obtains memory from the kernel layer through the mmap interface. It can be understood that any electronic device that obtains anonymous pages through the above-mentioned virtual machine or memory allocator can implement the memory management method provided by this application.
  • Embodiments of the present application provide a memory management method.
  • the electronic device 100 receives a memory allocation request from a thread of an application program, the electronic device 100 can allocate a virtual memory area to the thread.
  • the electronic device 100 may also obtain the name of the thread and use the name of the thread to identify the virtual memory area allocated to the thread.
  • the electronic device 100 may preferentially reclaim the virtual memory area of the thread with the lowest priority based on the priority of each thread of the application program. In this way, the electronic device 100 can preferentially reclaim memory areas of unimportant threads to ensure the running of important threads of the application program.
  • the electronic device 100 may classify threads, with threads of different types having different priorities and threads of the same type having the same priority.
  • the electronic device 100 can determine the business type of the thread and obtain the thread name of the thread.
  • the electronic device 100 can allocate an independent memory area to the thread in the memory space of the program, and use the thread name of the thread to identify the memory area.
  • the memory areas of threads of different business types can be reclaimed in sequence based on the business type.
  • the electronic device 100 recycles the memory area of multiple threads of the same type of the application program, it may preferentially recycle the memory area of the thread that has not been run recently, or it may randomly recycle the memory area of a certain thread of the multiple threads of the same type. Etc., this application does not limit this.
  • the memory management method includes:
  • the identification parameter and type parameter of the thread may be set.
  • the value of the identification parameter may be the name of the thread, and the electronic device 100 may obtain the name of the thread through the interface.
  • the electronic device 100 can obtain the thread name through the prctl function that meets the POSIX interface protocol.
  • the input of prtcl includes the option parameter, which can be used to set the function of prctl function.
  • the option parameter is "PR_GET_NAME”
  • the prctl function is used to obtain the thread name.
  • the option parameter is "PR_SET_VMA”
  • the prctl function is used to set the name of the memory area.
  • the type parameter can be used to represent the business type of the thread.
  • the electronic device 100 may be provided with multiple service types.
  • the electronic device 100 may determine the service type of the thread based on the thread name or the like.
  • the electronic device 100 can be provided with five service types, which are: non-critical service type, display service type, interactive service type, stack service type, and push service type.
  • the thread of the display business type can be used to complete the processing of the display content, for example, perform operations such as compositing layers and rendering images.
  • Interactive business type threads can be used to obtain user input and perform corresponding operations in response to user input. For example, corresponding processing flows are performed when the user clicks on the screen.
  • Threads of the heap area business type are used to manage heap area data, mainly threads related to virtual machine business.
  • Threads of push business type are used to receive message data.
  • Threads of non-critical business types are threads other than those of the above business types.
  • the electronic device 100 can use the following methods to determine the service type of the thread.
  • the electronic device 100 can determine the service type of the thread by determining whether the thread name of the thread includes a specific field. For example, when a specific field includes “view”, “Mali”, “surface”, “canvas”, “GL Track”, “JS”, “UI”, “compose”, “Hwcompose”, “display”, “skia” When waiting for the string related to the display business, it can be determined that the business type of the thread is the display business type. When a specific field includes “input”, “event”, “swing” and other strings related to interactive business, it can be determined that the business type of the thread is an interactive business type.
  • the specific field includes "Java”, “object”, “area” and other strings related to the heap area business
  • the business type of the thread is the heap area business type.
  • a specific field includes "service”, “message”, “push” and other strings related to push services, it can be determined that the service type of the thread is a push service type.
  • the electronic device 100 may determine that the service type of the thread is a non-critical service type.
  • the electronic device 100 can determine the service type of the thread based on the application scenario. For example, the electronic device 100 may determine the type of thread involved in receiving user input as the interactive service type. The electronic device 100 may determine the type of thread involved after receiving the user input as the display service type. The electronic device 100 may determine the type of thread involved after the application is switched to the background as the heap area business type. The electronic device 100 may determine the type of thread involved in the process of receiving message data as the push service type. The electronic device 100 may determine threads in the application program other than the threads of the above business types as non-critical business types.
  • the electronic device 100 can determine the business type of the other one or more threads as the certain thread. The business type of each thread.
  • the electronic device 100 can jointly determine the service type of the thread in combination with the above-mentioned multiple ways of determining the service type of the thread. For example, the electronic device 100 may determine whether the service type of the thread is any one of a display service type, an interactive service type, a heap service type, and a push service type based on the name of the thread. Then for threads whose business types cannot be determined based on the thread name, it is determined based on the application scenario whether the business type of the thread is any one of the display business type, the interactive business type, the heap business type, and the push business type.
  • the business type of the thread For threads whose business types cannot be determined based on thread names and application scenarios, you can determine whether the business type of the thread is a display business type or interaction based on whether it causes other threads with determined business types to switch from running state to blocking state or lock pool state. Any type among business type, heap area business type and push business type. If the electronic device 100 is still unable to determine whether the service type of a certain thread is any of the display service type, interactive service type, heap service type and push service type, the service type of the thread can be determined as a non-critical service. type.
  • the electronic device 100 can determine the service type of the thread using several methods including but not limited to those listed above, which is not limited in this application.
  • the electronic device 100 may store the corresponding relationship between the thread name of the thread and the service type. In this way, when the electronic device 100 executes step S801, it can directly obtain the service type of the thread based on the thread name. It should be noted that the stored information on the correspondence between thread names and service types is not unmodifiable. The electronic device 100 can update the stored correspondence between thread names and service types at regular intervals.
  • the electronic device 100 After the electronic device 100 determines the identification parameter of the thread, it can allocate a memory area to the thread from the virtual address space of the application program, and use the identification parameter of the thread to mark the memory area. It should be noted that a memory area only includes memory pages for one thread. It should also be noted that the memory page allocated here is a virtual memory page that has established a mapping relationship with a physical memory page. When the electronic device 100 recycles the memory pages in the memory area, the memory pages in the memory area can be recycled through a memory recovery algorithm such as LRU.
  • the electronic device 100 can allocate a memory allocation unit to the thread, and the memory allocation unit can obtain a memory area of the application's memory space for the thread. And get the thread name of the thread, use the thread name of the thread to identify the memory area.
  • the memory allocation unit can be used to allocate, manage, and reclaim the memory area of the thread.
  • the thread can request memory allocation by calling methods such as malloc or new.
  • the thread's memory allocation unit can obtain a memory area of the application's memory space by calling the mmap interface.
  • the memory allocation unit can also set the name of the thread's memory area through the prctl function that meets the POSIX interface protocol.
  • the input of prtcl includes the option parameter, which can be used to set the function of prctl function.
  • the option parameter is "PR_SET_VMA”
  • the prctl function is used to set the name of the memory area.
  • the application includes three threads, and thread 1 can obtain the thread 1 memory area through the thread 1 allocation unit. Thread 2 can obtain the thread 2 memory area through the thread 2 allocation unit. Thread 3 can obtain the thread 3 memory area through the thread 3 allocation unit.
  • the electronic device 100 can reclaim memory based on the identification parameters and type parameters of the thread when memory resources are insufficient. Specifically, the electronic device 100 stores the recycling order of threads of different business types. The electronic device 100 can determine the thread of the service type to be recycled according to the thread type parameter. The electronic device 100 can determine the memory area to be reclaimed based on the identification parameter of the thread, that is, the memory in the memory area of the thread is reclaimed.
  • the electronic device 100 can determine whether the memory resources are sufficient based on the free memory capacity. For example, the electronic device 100 may determine that the memory resources are insufficient when the free memory capacity is lower than a first memory capacity value (eg, 3GB). For another example, the electronic device 100 may operate when the ratio of free memory capacity to the total memory capacity is less than the first ratio (for example, 40%). When, it is determined that the memory resources are insufficient. It should be noted that, without being limited to the example of insufficient memory resources, the electronic device 100 may also determine insufficient memory resources through other methods, which is not limited in the embodiments of the present application.
  • a first memory capacity value eg, 3GB
  • the electronic device 100 may operate when the ratio of free memory capacity to the total memory capacity is less than the first ratio (for example, 40%). When, it is determined that the memory resources are insufficient.
  • the electronic device 100 may also determine insufficient memory resources through other methods, which is not limited in the embodiments of the present application.
  • the service types of the electronic device 100 include non-critical service types, display service types, interactive service types, stack service types, and push service types.
  • display The order of business type, interactive business type, heap area business type, and push business type recycles the memory of the thread of the corresponding business type in sequence.
  • the application program includes a non-critical business type thread
  • the electronic device 100 first reclaims the memory of the business type thread.
  • the application program includes threads of non-critical business type and display business type
  • the electronic device 100 first reclaims the memory of the thread of non-critical business type, and then reclaims the memory of the thread of display business type, and so on.
  • the electronic device 100 may randomly reclaim the memory of threads of the same business type of all programs based on the business type. For example, the electronic device 100 may, according to the recycling sequence of business types shown in FIG. 10A , first reclaim the memory of threads of non-critical business types of all applications, and then reclaim the memory of threads of display business types of all applications, and so on. . It can be understood that when the electronic device 100 recycles the memory of a thread of a certain service type, it can randomly select a certain thread among all the threads of the service type and recycle the memory of the memory area of the certain thread. When the electronic device 100 reclaims the memory in the memory area of a certain thread, it can reclaim the memory pages in the memory area through a memory recovery algorithm such as LRU.
  • a memory recovery algorithm such as LRU.
  • the electronic device 100 may reclaim the memory of threads of the same service type of all programs based on the priority and service type of the application program. For example, the electronic device 100 may, according to the recycling sequence of business types shown in FIG. 10A , first reclaim the memory of threads of non-critical business types of all applications, and then reclaim the memory of threads of display business types of all applications, and so on. .
  • the electronic device 100 recycles the memory of a thread of a certain service type, it may preferentially recycle the memory of the thread of the application program with the lowest priority among all the threads of the service type according to the priority of the application program.
  • the electronic device 100 may determine the priority of the application program based on the usage frequency of the application.
  • the usage frequency of an application may be the length of time the user uses the application within a period of time (for example, one day). The longer a user uses the application over a period of time and the more frequently the application is used, the higher the priority of the application.
  • the frequency of application usage can also be the number of times a user opens the application within a period of time (such as a day).
  • the electronic device 100 may also determine the priority of an application program based on how long the application occupies the CPU. For example, the longer the application has occupied the CPU in the recent period (for example, 200ms), the higher the priority.
  • the electronic device 100 may also determine the priority of the application program based on the power consumption of the application within a period of time. For example, the more power an application consumes in the recent period (for example, a day), the higher the priority.
  • the electronic device 100 may also determine the priority of the application based on the latest usage time of the application. For example, the more recently the app received user input or the more recently the app read data, the higher the priority.
  • the electronic device 100 may determine the priority of the application using a combination of the above methods. For example, when the electronic device 100 determines that the priorities of multiple applications are the same based on application usage frequency, the electronic device 100 may determine the priorities of the multiple applications based on the power consumption of the applications, and so on.
  • the electronic device 100 may determine the priority of the application program according to the service type of the application. For example, the electronic device 100 may determine an instant messaging application among multiple applications as the application with the highest priority.
  • the electronic device 100 when the electronic device 100 runs Application 1, Application 2, and Application 3, it is assumed that Application 1 has a lower priority than Application 2, and Application 2 has a lower priority than Application 3.
  • the electronic device 100 recycles the memory of a thread of the same business type, it can first recycle the memory of the memory area of the thread of the business type of Application 1, then recycle the memory of the memory area of the thread of the business type of Application 2, and finally recycle the application. 3.
  • the memory area of the thread of this business type Memory As shown by the arrow in FIG. 10B , the electronic device 100 may first reclaim the memory of the non-critical business type thread of Application 1, and then reclaim the memory of the non-critical business type thread of Application 2, and so on.
  • application 1 does not include a thread of a certain business type, application 1 can be skipped and the memory in the memory area of the thread of a certain business type of application 2 can be recycled. It should be noted that when application 1 includes multiple threads of the same business type, the memory of the multiple threads can be reclaimed randomly or by preferentially reclaiming the least recently used thread.
  • the service types of the electronic device 100 may include, but are not limited to, the non-critical service types, display service types, interactive service types, stack service types and push service types shown in FIG. 10A .
  • the electronic device 100 can operate according to the non-critical service type, heap area service type, push service type, and display service type.
  • the order of interactive business types, and the memory of the threads of the corresponding business types is recovered in turn.
  • the electronic device 100 can perform the non-critical service type, display service type, interactive service type, heap service type, and push service as shown in FIG. 10C In the order of the types, the memory of the threads of the corresponding business types is recycled in turn.
  • the application running in the foreground needs to display the user interface, and the threads of the display business type and interactive business type are responsible for generating the user interface that the application needs to display. Therefore, for applications running in the foreground, threads that display business types and interactive business types are more important and should be recycled last.
  • Applications running in the background do not need to display the user interface, but may need to receive push messages and maintain key data from being lost. Therefore, for applications running in the background, heap business type and push business type threads are more important. should be recycled last.
  • the reading APP when the user switches the reading APP to the background, the reading APP no longer needs to display the business type thread to generate the user interface, but it needs to ensure the key data of the user's reading progress (stored in the memory space corresponding to the business type thread in the heap area ) can not be lost due to memory recycling. Therefore, for this reading APP, threads with display business types can be recycled first, while threads with heap business types should not be recycled as much as possible.
  • the user when the user puts a game APP in the foreground, the user may be playing in the game.
  • the game APP not only needs to display the rendered game screen, but also needs to receive the user's input through the touch screen for interaction. If at this time, Thread recycling of display business types and interactive business types will cause the game to crash and affect the user experience. Therefore, for the game APP that is in the foreground, it should be ensured as much as possible that the threads of the display business type and interactive business type are finally recycled.
  • the electronic device 100 may preferentially reclaim the memory of the application running in the background, and then reclaim the memory of the application running in the foreground. Detailed descriptions of how the electronic device 100 recycles the memory of applications running in the background and the memory of applications running in the foreground can be found in the above embodiments and will not be described again here.
  • the electronic device 100 may classify threads, with threads of different types having different priorities and threads of the same type having the same priority.
  • the thread name of the thread may be obtained.
  • the electronic device 100 can allocate an independent memory area to the thread in the memory space of the program, and use the thread name of the thread to identify the memory area.
  • the electronic device 100 can determine the service type of the thread, and based on the service type, sequentially reclaim the memory areas of threads of different service types.
  • the electronic device 100 when the electronic device 100 recycles the memory area of multiple threads of the same type of the application program, it may preferentially recycle the memory area of the thread that has not been run recently. Or, randomly recycle the memory area of a certain thread among multiple threads of the same type, etc. This application does not limit this.
  • the electronic device 100 may, when receiving memory allocation requests from different threads of a program, allocate different memory areas to different threads and identify the memory areas based on the thread names of the threads. When memory resources are insufficient, the electronic device 100 can reclaim the thread's memory based on the thread's business type, as shown in Figure 11:
  • the electronic device 100 receives memory allocation requests from the first thread and the second thread of the first program, where the first thread belongs to the first service type, the second thread belongs to the second service type, and the priority of the first service type Higher priority than the second business type.
  • the electronic device 100 will preferentially reclaim the memory of threads of lower priority business types and retain the memory of threads of higher priority business types as much as possible.
  • the threads of the display business type and the interactive business type are responsible for generating the user interface that the application needs to display. Therefore, for applications running in the foreground, threads that display business types and interactive business types are more important and should be recycled last. Applications running in the background do not need to display the user interface, but may need to receive push messages and maintain key data from being lost. Therefore, for applications running in the background, heap business type and push business type threads are more important. should be recycled last. Therefore, when the first program is a program running in the background, if the first service type is a heap area service type, the second service type may be a non-critical service type, a display service type or an interactive service type.
  • the second service type may be a non-critical service type, a heap area service type or a push service type.
  • the service type please refer to the embodiment shown in Figure 8, which will not be described again here.
  • the electronic device 100 allocates a first memory area to the first thread from the memory space of the first program, marks the first memory area with the identifier of the first thread, allocates a second memory area to the second thread, and marks the first memory area with the identifier of the first thread.
  • the thread's identification marks the second memory area.
  • the identifier of the thread may be the thread name of the thread, and the electronic device 100 may determine the memory area of the thread based on the thread name of the thread.
  • the detailed description of how the electronic device 100 allocates a memory area to the thread, obtains the thread name, and names the memory area can be found in the embodiment shown in FIG. 8 and will not be described again here.
  • the second memory area corresponding to the second thread of the second business type is first recovered, and then the first memory area corresponding to the first thread of the first business type is recovered. In order, the memory is reclaimed in turn.
  • the priority of the first service type is higher than the priority of the second service type.
  • the higher the priority the later it should be recycled. Therefore, when memory resources are insufficient, the second memory area corresponding to the second thread of the second business type should be recovered first, and then the first memory area corresponding to the first thread of the first business type should be recovered.
  • the electronic device 100 can gradually reclaim the second memory area (or the first memory area) through the LRU algorithm. memory page. For details, reference may be made to the embodiment shown in FIG. 5 , which will not be described again here.
  • the electronic device 100 can randomly reclaim the first memory area corresponding to the first thread and the second memory area corresponding to the second thread, or the electronic device 100 can Priority is given to reclaiming the memory area corresponding to the recently unused thread among the first thread and the second thread (for example, if the second thread has not been used recently, the second memory area is reclaimed first), and so on.
  • the electronic device 100 may, when receiving memory allocation requests from different threads of multiple programs, allocate different memory areas to different threads and identify the memory areas based on the thread names of the threads. When memory resources are insufficient, The electronic device 100 can reclaim memory based on the business type of the thread and the priority of the program, as shown in Figure 12A:
  • the electronic device 100 receives a memory allocation request from the first thread of the first program, the second thread of the first program, and the third thread of the second program, where the first thread and the third thread belong to the first business type,
  • the second thread belongs to the second service type, and the priority of the first service type is higher than the priority of the second service type.
  • the electronic device 100 allocates a first memory area to the first thread from the memory space of the first program, allocates a second memory area to the second thread, and marks the first memory area with the identifier of the first thread, and marks the first memory area with the identifier of the second thread.
  • the second memory area is marked with the identifier
  • the third memory area is allocated to the third thread from the memory space of the second program, and the third memory area is marked with the identifier of the third thread.
  • the service type of the first thread and the third thread is the first service type
  • the service type of the second thread is the second service type
  • the priority of the second service type is priority 2.
  • the priority of the first service type is priority 1.
  • the priority of the second service type is lower than the priority of the first service type. The lower the priority of the business type, the earlier the memory area corresponding to the thread of this business type is recycled. Therefore, when memory resources are insufficient, the memory area corresponding to the thread of the second business type (in this example, the second thread) can be recycled first, and then the thread of the first business type (in this example, the third thread, the third thread) can be recycled A memory area corresponding to one thread).
  • the order of memory recycling can be determined according to the priority of the program.
  • the third thread and the first thread are both threads of the first business type with "priority level 1". Since the priority of the second program corresponding to the third thread is lower than the priority of the first program corresponding to the first thread, the lower the priority of the program, the earlier the memory area corresponding to the thread of the program is recycled. Therefore, in this example, the third memory area corresponding to the third thread can be reclaimed first, and then the first memory area corresponding to the first thread can be reclaimed.
  • the order of reclaiming the memory area is: first reclaim the second memory area corresponding to the second thread, then reclaim the third memory area corresponding to the third thread, and finally reclaim the first memory area corresponding to the first thread.
  • the electronic device 100 allocates memory areas and how to reclaim the memory in each memory area, please refer to the embodiment shown in FIG. 8 (for example, reclaiming the memory in each memory area based on the LRU algorithm), which will not be described again here.
  • the electronic device 100 may, when receiving memory allocation requests from multiple threads of multiple programs, allocate different memory areas to the multiple threads and identify the corresponding memory areas based on the thread names of the threads. When memory resources are insufficient, the electronic device 100 can reclaim memory based on the business type of the thread and the priority of the program, as shown in Figure 13A:
  • the electronic device 100 receives memory allocation requests from the first thread of the first program, the second thread of the first program, the third thread of the second program, and the fourth thread of the second program, where the first thread and the The three threads belong to the first service type, the second thread and the fourth thread belong to the second service type, and the priority of the first service type is higher than the priority of the second service type.
  • the electronic device 100 allocates a first memory area to the first thread from the memory space of the first program, allocates a second memory area to the second thread, and marks the first memory area with the identifier of the first thread, and marks the first memory area with the identifier of the second thread. Mark the second memory area with the identifier of the second program, allocate the third memory area to the third thread from the memory space of the second program, allocate the fourth memory area to the fourth thread, and mark the third memory area with the identifier of the third thread, Mark the fourth memory area with the identifier of the fourth thread.
  • the fourth thread corresponding to the second business type of the second program will be recycled first.
  • Four memory areas then recycle the second memory area corresponding to the second thread of the second business type of the first program, then recycle the third memory area corresponding to the third thread of the first business type of the second program, and finally recycle the first
  • the memory is reclaimed sequentially in the order of the first memory area corresponding to the first thread of the first business type of the program.
  • the service type of the first thread and the third thread is the first service type
  • the service type of the second thread and the fourth thread is the second service type
  • the priority of the second service type is priority 2.
  • the priority of the first service type is priority 1.
  • the priority of the second service type is lower than the priority of the first service type.
  • the lower the priority of the business type the earlier the memory area corresponding to the thread of this business type is recycled. Therefore, when memory resources are insufficient, the memory area corresponding to the thread of the second business type (in this example, the second thread and the fourth thread) can be recycled first, and then the thread of the first business type (in this example, the third thread) can be recycled.
  • the memory area corresponding to the third thread and the first thread can be recycled.
  • the order of memory recycling can be determined according to the priority of the program.
  • the third thread and the first thread are both threads of the first business type with "priority level 1". Since the priority of the second program corresponding to the third thread is lower than the priority of the first program corresponding to the first thread, the lower the priority of the program, the earlier the memory area corresponding to the thread of the program is recycled. Therefore, in this example, the third memory area corresponding to the third thread can be reclaimed first, and then the first memory area corresponding to the first thread can be reclaimed.
  • Both the fourth thread and the second thread are threads of the second business type of "priority 2".
  • the priority of the second program corresponding to the fourth thread is lower than the priority of the first program corresponding to the second thread, the lower the priority of the program, the earlier the memory area corresponding to the thread of the program is recycled. Therefore, in this example, the fourth memory area corresponding to the fourth thread can be reclaimed first, and then the second memory area corresponding to the second thread can be reclaimed.
  • the order of recycling the memory area is: first, reclaim the fourth memory area corresponding to the fourth thread, then reclaim the second memory area corresponding to the second thread, and then reclaim the third memory area corresponding to the third thread, Finally, the first memory area corresponding to the first thread is recycled.
  • the electronic device 100 allocates memory areas and how to reclaim the memory in each memory area, please refer to the embodiment shown in FIG. 8 (for example, reclaiming the memory in each memory area based on the LRU algorithm), which will not be described again here.
  • the electronic device 100 may allocate a corresponding memory area to each thread, and name the memory area using the thread name of the thread.
  • the memory of the application program with the lowest priority can be reclaimed first according to the priority of the application program.
  • the electronic device 100 reclaims the memory of an application program the memory of the thread with the lowest priority can be reclaimed first according to the priority of the thread of the application program.
  • the electronic device 100 reclaims the thread's memory it may reclaim the memory pages of the thread's memory area according to a memory reclaim algorithm such as LRU. In this way, the electronic device 100 can also avoid reclaiming the memory of important threads.
  • the electronic device 100 may determine the priority of the application program based on the usage frequency of the application.
  • the usage frequency of the application may be the time the user uses the application within a period of time (for example, one day). The longer a user uses the application over a period of time and the more frequently the application is used, the higher the priority of the application.
  • the electronic device 100 may also determine the priority of an application program by the time the application occupies the CPU. For example, the longer the application has occupied the CPU in the recent period (for example, 200ms), the higher the priority.
  • the electronic device 100 may also determine the priority of the application based on the latest usage time of the application. For example, the more recently the app received user input or the more recently the app read data, the higher the priority.
  • each thread is allocated a memory area, if the number of threads is large, memory management is complicated.
  • the electronic device 100 may determine the service type of the thread when receiving the memory allocation request of the thread of the application program. Based on the service type of the thread, the electronic device 100 allocates a part of the memory area in the memory space of the application program for storing data of the thread of the service type to the thread. In the memory reclamation process, the electronic device 100 can reclaim memory in different memory areas based on the priority of the service type. In this way, the electronic device 100 can preferentially reclaim the memory area used to store data of threads of unimportant business types to ensure the running of important threads of the application program.
  • the electronic device 100 is preset with M service types.
  • the memory space of each program of the electronic device 100 includes M memory areas, and the M memory areas have a one-to-one correspondence with the M service types.
  • a memory area is used to store the data of all threads of a business type.
  • the electronic device 100 receives the memory allocation request of the thread, it can determine the service type of the thread.
  • the M service types include the service types of the thread, and allocate a part of the memory area corresponding to the service type to the thread.
  • the electronic device 100 can sequentially reclaim the memory of the corresponding memory area based on the priority order of the M service types.
  • the memory management method includes:
  • the electronic device 100 is preset with M service types, M is greater than or equal to 1, and M memory areas are divided into the memory space based on the M service types.
  • the memory in each memory area can be allocated to threads of the corresponding service type.
  • the electronic device 100 When the electronic device 100 allocates a virtual address space to an application program, it can divide M memory areas in the virtual address space, and each of the M memory areas is used to store data of a thread of a business type. In other words, there is a one-to-one correspondence between M business types and M memory areas. It can be understood that a memory area may include memory pages of one or more threads.
  • the electronic device 100 is preset with five service types, which are: non-critical service type, display service type, interactive service type, stack service type, and push service type.
  • service types which are: non-critical service type, display service type, interactive service type, stack service type, and push service type.
  • the virtual address space of each application program of the electronic device 100 includes 5 memory areas, which are respectively a memory area used to store data of non-critical business type threads, a memory area used to store data of display business type threads, The memory area used to store the data of threads of interactive business type, the memory area used to store the data of threads of heap area business type, and the memory area used to store the data of threads of push business type.
  • M equals 5.
  • the electronic device 100 can obtain the memory area through the mmap interface, and the electronic device 100 can also set the name of the memory area, for example, through the prctl function shown in FIG. 8 .
  • the name of the memory area can be a value of the business type. In this way, the memory area can be associated with the business type to facilitate memory allocation and recycling of the memory area.
  • the electronic device 100 receives the thread's memory allocation request and determines the service type of the thread.
  • the electronic device 100 may determine the service type of the thread when receiving the thread's memory allocation request. For details, reference may be made to the embodiment shown in FIG. 8 , which will not be described again here.
  • the electronic device 100 allocates memory in the corresponding memory area to the thread based on the service type of the thread.
  • the electronic device 100 After the electronic device 100 determines the service type of the thread, it can use the virtual address space of the application program for storage. A part of the memory area in which the data of the thread of this business type is placed is allocated to the thread.
  • the electronic device 100 can determine the business type of the thread, and allocate the corresponding memory allocation unit to the thread based on the business type.
  • the memory allocation unit can assign the memory to the thread.
  • the thread allocates a portion of memory in the corresponding memory area.
  • the memory allocation unit can be used to allocate, manage, and reclaim memory areas.
  • the thread can request memory allocation by calling methods such as malloc or new.
  • the thread's memory allocation unit can obtain a part of the memory area by calling the mmap interface.
  • the memory allocation unit can also set the name of the memory area through the prctl function that meets the POSIX interface protocol.
  • the input of prtcl includes the option parameter, which can be used to set the function of prctl function.
  • the option parameter is "PR_SET_VMA”
  • the prctl function is used to set the name of the memory area.
  • the application program includes but is not limited to 7 threads.
  • the business type of thread 1 is a non-critical business type
  • the business type of thread 2 is an interactive business type
  • the business types of threads 3 and 4 are display business allocation units
  • the business type of thread 5 is a display business allocation unit.
  • the business type of thread 6 is push business type
  • the business type of thread 7 is heap area business type.
  • Memory area 1 can be allocated to threads whose business type is non-critical business type
  • memory area 2 can be allocated to threads whose business type is display business allocation unit
  • memory area 3 can be allocated to threads whose business type is interactive business type
  • memory area 4 It can be allocated to threads whose business type is the heap area business type
  • memory area 5 can be allocated to threads whose business type is the push business type.
  • Thread 1 can obtain part of memory in memory area 1 through non-critical business allocation units.
  • Thread 2 can obtain part of the memory in memory area 3 through the interactive service allocation unit.
  • Thread 3 can obtain part of the memory in memory area 2 by displaying the business allocation unit.
  • Thread 4 can also obtain part of the memory in memory area 2 by displaying the business allocation unit.
  • Thread 5 can obtain part of the memory in memory area 5 by pushing the business allocation unit.
  • Thread 6 can also obtain part of the memory in memory area 5 by pushing the business allocation unit.
  • Thread 7 can obtain part of the memory in memory area 4 through the heap area business allocation unit.
  • the electronic device 100 can sequentially reclaim the memory in different memory areas based on the service type.
  • the electronic device 100 can reclaim the memory pages in the memory area through a memory recovery algorithm such as LRU.
  • the electronic device 100 can sequentially reclaim the memory in the memory area used to store data of non-critical business type threads and the memory area used to store data of display business type threads in the order shown in FIG. 10A .
  • Memory the memory in the memory area used to store the data of the interactive business type thread, the memory in the memory area used to store the data of the heap area business type thread, the memory in the memory area used to store the data of the push business type thread .
  • the electronic device 100 may randomly reclaim the memory of the memory areas of the same business type of all programs based on the business type. For example, the electronic device 100 may, according to the recycling sequence of service types shown in FIG. 10A , first reclaim the memory in the memory area of non-critical service types of all applications, then reclaim the memory in the memory area of the display business type, and so on.
  • the electronic device 100 may reclaim the memory of the memory area of the same service type of all programs based on the priority and service type of the application program. For example, the electronic device 100 may, according to the recycling sequence of service types shown in FIG. 10A , first reclaim the memory in the memory area of non-critical service types of all applications, then reclaim the memory in the memory area of the display business type, and so on.
  • the electronic device 100 recycles the memory in the memory area of a certain service type, according to the priority of the application program, the electronic device 100 may preferentially recycle the memory in the memory area of the service type of the application program with the lowest priority.
  • the electronic device 100 may determine the priority of the application based on any of the frequency of use of the application, the most recent time of use of the application, the most recent time the application received user input, etc. For details, see the implementation shown in Figure 8 For example, I won’t go into details here.
  • the electronic device 100 when the electronic device 100 runs application 1, application 2, and application 3, if the priority of application 1 is lower than that of application 2, the priority of application 2 is lower than that of application 3.
  • the electronic device 100 recycles the memory of the memory area of the same service type, it can first recycle the memory of the memory area of the service type of Application 1, then recycle the memory of the memory area of the service type of Application 2, and finally recycle the memory of the memory area of Application 3.
  • the memory area of the business type when the electronic device 100 runs application 1, application 2, and application 3, if the priority of application 1 is lower than that of application 2, the priority of application 2 is lower than that of application 3.
  • the electronic device 100 when receiving memory allocation requests from different threads of a program, can determine the business type of the thread, and allocate a portion of the corresponding memory area to the thread based on the business type. When memory resources are insufficient, the electronic device 100 can reclaim memory in different memory areas based on service types, as shown in Figure 16:
  • the electronic device 100 receives memory allocation requests from the first thread and the second thread of the first program.
  • the memory space of the first program includes a first type of memory area and a second type of memory area.
  • the first type of memory area is used for storage.
  • the second type of memory area is used to store the data of threads of the first business type, and the priority of the first business type is higher than the priority of the second business type.
  • threads of business types with higher priority are more important.
  • the electronic device 100 will first reclaim the memory area used to store thread data of business types with low priority, and then reclaim the memory area used to store thread data of business types with low priority.
  • the memory area for thread data of high-priority business types is more important.
  • the electronic device 100 determines that the service type of the first thread is the first service type and the service type of the second thread is the second service type, allocates the memory in the first type memory area to the first thread and allocates it to the second thread. Memory in the second type of memory area.
  • the electronic device 100 determines the service type of the thread and allocates a memory area to the thread for detailed description. Refer to the embodiment shown in FIG. 14 and will not be described again here.
  • the priority of the first service type is higher than the priority of the second service type. The higher the priority, the later it should be recycled. Therefore, when memory resources are insufficient, the second memory area corresponding to the second service type should be reclaimed first, and then the first memory area corresponding to the first service type should be reclaimed.
  • the electronic device 100 can gradually reclaim the memory pages of the second type memory area (or the first type memory area) through memory recovery algorithms such as LRU. .
  • memory recovery algorithms such as LRU.
  • the electronic device 100 may determine the service type of the thread when receiving memory allocation requests from different threads of multiple programs, and allocate a part of the corresponding memory area to the thread based on the service type. When memory resources are insufficient, the electronic device 100 can reclaim memory in different memory areas based on service types, as shown in Figure 17A:
  • the electronic device 100 receives memory allocation requests from the first thread and the second thread of the first program, and the third thread and the fourth thread of the second program.
  • the memory space of the first program includes the first type of memory area and the second Type memory area
  • the memory space of the second program includes a first type memory area and a second type memory area, where the first type memory area is used to store data of threads of the first business type, and the second type memory area is used to store For the data of the thread of the second business type, the priority of the first business type is higher than the priority of the second business type.
  • first type of memory area of the first program can be allocated to the thread of the first business type of the first program
  • second type of memory area of the first program can be allocated to the thread of the second business type of the first program
  • first type memory area of the second program can be allocated to the thread of the first business type of the second program
  • second type memory area of the second program can be allocated to the thread of the second business type of the second program.
  • threads of business types with higher priority are more important.
  • the electronic device 100 will first reclaim the memory area used to store thread data of business types with low priority, and then reclaim the memory area used to store thread data of business types with low priority.
  • the memory area for thread data of high-priority business types is more important.
  • the electronic device 100 determines that the service type of the first thread is the first service type, the service type of the second thread is the second service type, the service type of the third thread is the first service type, and the service type of the fourth thread is The second business type allocates the memory in the first type memory area of the first program to the first thread, the second thread allocates the memory in the second type memory area of the first program, and the third thread allocates the first type memory of the second program. The fourth thread allocates memory in the second type memory area of the second program.
  • the electronic device 100 determines the service type of the thread and allocates a memory area to the thread for detailed description. Refer to the embodiment shown in FIG. 14 and will not be described again here.
  • the second type memory area of the second program will be reclaimed first, and then the first program will be reclaimed.
  • the second type of memory area is reclaimed, and then the first type of memory area of the second program is reclaimed, and finally the first type of memory area of the first program is reclaimed, and the memory is reclaimed in sequence.
  • Memory pages in the memory area can be gradually reclaimed through memory recycling algorithms such as LRU.
  • LRU memory recycling algorithms
  • the second type memory area of the first program is used to store data of the second business type thread (for example, the second thread) in the first program
  • the first type memory area of the first program is used
  • the second type memory area of the second program is used to store the data of the threads of the first business type (for example, the first thread) in the first program
  • the second type memory area of the second program is used to store the threads of the second business type (for example, the fourth thread) in the second program.
  • the first type memory area of the second program is used to store data of the thread of the first business type (for example, the third thread) in the second program.
  • the priority of the second service type is priority 2.
  • the priority of the first service type is priority 1.
  • the priority of the second service type is lower than the priority of the first service type. The lower the priority of the business type, the earlier the memory area that stores the thread data of this business type is reclaimed. Therefore, when the memory resources are insufficient, the memory area storing the thread data of the second business type can be recycled first (in this example, the second type memory area of the first program and the second type memory area of the second program), and then Reclaim the memory area storing thread data of the first business type (in this example, the first type memory area of the first program and the first type memory area of the second program).
  • the order of recycling the memory areas can be determined according to the priority of the program.
  • the first type memory area of the first program and the first type memory area of the second program are both memory areas that store thread data of the first business type of "priority 1". Since the priority of the second program is lower than the priority of the first program, the lower the priority of the program, the earlier the memory area of the program is reclaimed. Therefore, in this example, the first type memory area of the second program can be reclaimed first, and then the first type memory area of the first program can be reclaimed. Similarly, in this example, the second type memory area of the second program can be reclaimed first, and then the second type memory area of the first program can be reclaimed.
  • the order of reclaiming the memory area is: first reclaim the second type memory area of the second program, then reclaim the second type memory area of the first program, and then reclaim the first type memory area of the second program, The first cycle of final recycling The first type of memory area in sequence.
  • the electronic device 100 allocates memory areas and how to reclaim the memory in each memory area, please refer to the embodiment shown in FIG. 14 (for example, reclaiming the memory in each memory area based on the LRU algorithm), which will not be described again here.
  • the electronic device 100 is preset with M service types.
  • the electronic device 100 receives a memory allocation request from a thread, it can determine the service type of the thread.
  • the M service types include the service type of the thread. If the memory space of the program includes a memory area corresponding to the service type, the electronic device 100 can allocate part of the memory in the corresponding memory area to the thread. If the memory space of the program does not include a memory area corresponding to the service type, the electronic device 100 can divide a memory area corresponding to the service type in the memory space of the program, and then allocate part of the memory in the corresponding memory area to the thread.
  • the electronic device 100 can sequentially reclaim the memory of the corresponding memory areas based on the priority order of the service types corresponding to the memory areas in the memory space of the program. In this way, when the program only includes threads for some of the M business types, there is no need to divide the memory space into memory areas corresponding to some of the business types not included in the M business types, thereby reducing management costs.
  • the memory management method includes:
  • the electronic device 100 is preset with M service types, and M is greater than or equal to 1.
  • the electronic device 100 is preset with five service types, which are: non-critical service type, display service type, interactive service type, stack service type, and push service type.
  • service types which are: non-critical service type, display service type, interactive service type, stack service type, and push service type.
  • non-critical service type display service type
  • interactive service type interactive service type
  • stack service type stack service type
  • push service type push service type
  • the electronic device 100 receives the thread's memory allocation request and determines the service type of the thread.
  • the memory space includes a memory area used to store the data of the thread of this business type, allocate the memory in the corresponding memory area to the thread; if the memory space does not include the memory used to store the data of the thread of this business type Area, allocate a memory area in the memory space to store the data of the thread of this business type, and allocate the memory in this memory area to the thread.
  • the memory of the corresponding memory area can be allocated to the thread through the corresponding memory allocation unit. If the memory space of the program does not include the memory area corresponding to the business type, the corresponding memory allocation unit can divide a memory area in the memory space of the program and use this memory area as the memory area used to store the data of all threads of the business type. , for example, the memory allocation unit can divide the memory area by calling the mmap function. The corresponding memory allocation unit can also name the memory area based on the business type. For example, the memory allocation unit can name the memory area through the prctl function. The memory allocation unit can allocate part of the memory area to threads. If the memory space of the program includes a memory area corresponding to the business type, the corresponding memory allocation unit can allocate part of the corresponding memory area to the thread.
  • a memory area can be allocated to multiple threads of the corresponding business type of the program.
  • the electronic device 100 can reclaim memory in different memory areas based on service types when memory resources are insufficient.
  • the memory area of the service type with the lowest priority may be reclaimed first.
  • the memory area of the application program with the lowest priority may be reclaimed first.
  • the electronic device 100 may reclaim the memory pages in the memory area through a memory reclamation algorithm such as LRU.
  • LRU memory reclamation algorithm
  • the electronic device 100 may determine the business class of the thread when receiving the thread's memory allocation request. type, and based on the business type, allocate a part of the corresponding memory area to the thread. When memory resources are insufficient, the electronic device 100 can reclaim memory in different memory areas based on service types, as shown in Figure 19:
  • the electronic device 100 receives the memory allocation requests of the first thread and the second thread of the first program, determines that the service type of the first thread is the first service type, and the service type of the second thread is the second service type.
  • One service type has a higher priority than the second service type.
  • threads of business types with higher priority are more important.
  • the electronic device 100 will first reclaim the memory area used to store thread data of business types with low priority, and then reclaim the memory area used to store thread data of business types with low priority.
  • the memory area for thread data of high-priority business types is more important.
  • the memory space of the first program includes a first type of memory area and a second type of memory area, allocate part of the memory of the first type of memory area to the first thread, and allocate part of the second type of memory area to the second thread.
  • the first type of memory area is used to store data of threads of the first business type
  • the area within the second type is used to store data of threads of the second business type.
  • the electronic device 100 determines the service type of the thread and allocates a memory area to the thread for detailed description. Refer to the embodiment shown in FIG. 18 and will not be described again here.
  • the electronic device 100 allocates a part of the memory in the first type of memory area to the first thread, and divides the memory space of the first program into the first type of memory area.
  • the second type of memory area allocates a part of the memory in the second type of memory area to the second thread.
  • the electronic device 100 allocates a part of the memory of the second type of memory area to the second thread, and divides the memory space of the first program into the second type of memory area.
  • a type of memory area allocating a part of memory in the first type of memory area to the first thread.
  • the memory space of the first program does not include the first type of memory area and does not include the second type of memory area, divide the second type of memory area into the memory space of the first program and allocate the second type of memory area to the second thread.
  • a part of the memory is divided into a first type memory area in the memory space of the first program, and a part of the memory in the first type memory area is allocated to the first thread.
  • the electronic device 100 may gradually reclaim the memory pages in the memory area through memory recovery algorithms such as LRU.
  • memory recovery algorithms such as LRU.
  • the electronic device 100 will first reclaim the memory area used to store the thread data of the low priority business type, and then reclaim the memory area used to store the priority.
  • Memory area for thread data of high-level business types Since the first business type has a higher priority than the second business type, when the memory space of the first program includes the first type memory area and the second type memory area, the memory of the second type memory area is first reclaimed, and after the recovery is completed After the memory in the second type memory area is recovered, the memory in the first type memory area is reclaimed.
  • the electronic device 100 may determine the business type of the thread, and allocate a portion of the corresponding memory area to the thread based on the business type. When memory resources are insufficient, the electronic device 100 can reclaim memory in different memory areas based on service type and program priority, as shown in Figure 20:
  • the electronic device 100 receives memory allocation requests from the first thread and the second thread of the first program, and the third thread and the fourth thread of the second program, and determines that the service type of the first thread and the third thread is the first thread.
  • the service type, the service type of the second thread and the fourth thread is the second service type, and the first service type has a higher priority than the second service type.
  • the memory space of the first program includes a first type memory area and a second type memory area
  • the memory space of the second program includes a first type memory area and a second type memory area
  • the memory in the first type memory area, the second thread allocates the memory in the second type memory area of the first program, the third thread allocates the memory in the first type memory area of the second program, the fourth thread allocates the second program
  • the memory in the second type memory area wherein the first type memory area is used to store data of threads of the first business type, and the second type inner area is used to store data of threads of the second business type.
  • the electronic device 100 determines the service type of the thread and allocates a memory area to the thread for detailed description. Refer to the embodiment shown in FIG. 18 and will not be described again here.
  • the electronic device 100 allocates a part of the memory in the first type of memory area to the first thread.
  • the memory space is divided into a second type memory area, and a part of the memory in the second type memory area is allocated to the second thread.
  • the electronic device 100 allocates a part of the memory of the second type of memory area to the second thread, and divides the memory space of the first program into the second type of memory area.
  • a type of memory area allocating a part of memory in the first type of memory area to the first thread.
  • the memory space of the first program does not include the first type of memory area and does not include the second type of memory area, divide the second type of memory area into the memory space of the first program and allocate the second type of memory area to the second thread.
  • a part of the memory is divided into a first type memory area in the memory space of the first program, and a part of the memory in the first type memory area is allocated to the first thread.
  • the description of whether the memory space of the second program includes the first type of memory area and/or the second type of memory area can be referred to the above embodiments, and will not be described again here.
  • the electronic device 100 may gradually reclaim the memory pages in the memory area through memory recovery algorithms such as LRU.
  • memory recovery algorithms such as LRU.
  • the priority of the second program is lower than the priority of the first program.
  • the memory space of the first program includes a first type memory area and a second type memory area
  • the memory space of the second program includes a first type memory area and a second type memory area
  • the second type memory area of the first program, the first type memory area of the second program, and the first type memory area of the first program are sequentially recycled.
  • the embodiment shown in Figure 17A please refer to the embodiment shown in Figure 17A, which will not be discussed here. Again.
  • the memory space of the first program includes a first type memory area
  • the memory space of the second program includes a first type memory area and a second type memory area
  • the second type memory area of the second program A type of memory area and a first type of memory area of the first program are sequentially reclaimed.
  • the order of the second type memory area of the first program and the first type memory area of the second program is Reclaim memory, and so on.
  • the electronic device 100 is provided with M service types.
  • the electronic device 100 can divide M memory areas in the memory, and each memory area is used to store data of all threads of a service type.
  • the electronic device 100 receives the thread's memory allocation request, it can determine the thread's service type and allocate a part of the memory area corresponding to the service type to the thread.
  • the electronic device 100 can sequentially reclaim the memory of the corresponding memory area based on the priority order of the M service types.
  • the priorities of the M service types can be set, as shown in FIG. 10A , for example. It should be noted that a memory area can store data of threads of the same business type of all applications.
  • the electronic device 100 may reallocate the memory area when receiving a memory allocation request from a thread. That is to say, when the electronic device 100 receives the memory allocation request of the thread, it first determines the service type of the thread. If the memory includes a memory area corresponding to the thread's business type, allocate the memory in this memory area to the thread. If the memory does not include the memory area corresponding to the thread's business type, first divide a memory area in the memory to store the data of all threads of the thread's business type, and then allocate the memory in this memory area to the thread.
  • the electronic device 100 may allocate M memory areas for applications running in the foreground and allocate M memory areas for applications running in the background.
  • the memory recycling of applications running in the foreground and the memory recycling of applications running in the background do not interfere with each other.
  • the electronic device 100 may preferentially reclaim the memory of applications running in the background.

Landscapes

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

Abstract

本申请公开了一种内存管理方法。电子设备100可以在接收到应用程序的线程的内存分配请求时,确定线程的业务类型。电子设备100基于线程的业务类型,将应用程序的内存空间中用于存储该业务类型的线程的数据的内存区域中的一部分内存划分给该线程。在内存回收流程中,电子设备100可以基于业务类型的优先级,回收不同内存区域的内存。这样,电子设备100可以优先回收用于存储不重要业务类型的线程的数据的内存区域,保证应用程序的重要线程的运行。

Description

一种内存管理方法及相关装置
本申请要求于2022年05月28日提交中国专利局、申请号为202210592530.4、申请名称为“一种内存管理方法及相关装置”的中国专利申请的优先权,其全部内容通过引用结合在本申请中。
技术领域
本申请涉及终端领域,尤其涉及一种内存管理方法及相关装置。
背景技术
电子设备运行多个应用时,由于电子设备内存空间有限,若应用运行时占用大量内存资源,可能会导致部分应用运行卡顿。因此,如何管理内存成为亟待解决的问题。
发明内容
本申请提供了一种内存管理方法及相关装置,实现了基于线程的业务类型,给线程分配该业务类型对应的内存区域的一部分内存。电子设备100可以在回收内存的过程中,基于业务类型的优先级,按照优先级从低到高的顺序,依次回收不同业务类型对应的内存区域的内存。这样,电子设备可以优先回收不重要(优先级低)的业务类型的线程的数据,尽可能晚地回收重要(优先级高)的业务类型的线程的数据,保证应用程序的运行。
第一方面,本申请提供了一种内存管理方法,应用于电子设备,电子设备分配有多种业务类型的内存区域,多种业务类型的内存区域至少包括第一业务类型的内存区域和第二业务类型的内存区域;
第一业务类型的内存区域用于存放业务类型为第一业务类型的线程的数据,第二业务类型的内存区域用于存放业务类型为第二业务类型的线程的数据;
第二业务类型的优先级低于第一业务类型的优先级;
该方法包括:
在进行内存回收时,按照内存区域对应的业务类型优先级的高低顺序,先回收第二业务类型的内存区域的内存;
在回收完第二业务类型的内存区域的内存之后,回收第一业务类型的内存区域的内存。
这样,基于业务类型的优先级,优先回收业务类型优先级低的线程的数据,业务类型优先级高(重要)的线程的数据不易被回收。解决了只根据匿名页的最新访问情况来回收匿名页,引起的程序卡顿问题。
可以理解,本申请中将“优先级低”定义为会被优先回收,在其他一些实现方式中,也可以将“优先级高”定义为会被优先回收。
在一种可能的实现方式中,该方法还包括:接收到第一程序的第一线程的内存分配请求;
基于第一线程的业务类型,给第一线程分配第一业务类型的内存区域的内存,第一业务类型的内存区域在第一程序的内存空间,第一业务类型的内存区域用于存放第一程序的业务类型为第一业务类型的线程的数据,第一线程的业务类型为第一业务类型。
这样,由于基于线程的业务类型给线程分配内存区域,便于基于业务类型的优先级,回收不同业务类型的线程的数据。
在一种可能的实现方式中,方法还包括:接收到第一程序的第一线程的内存分配请求;
基于第一线程的业务类型,在第一程序的内存空间不包括第一业务类型的内存区域的情 况下,在第一程序的内存空间划分出第一业务类型的内存区域;
将第一业务类型的内存区域的内存分配给第一线程。
这样,在收到某个业务类型的线程的内存分配请求时,再划分该业务类型的内存区域,可以减少内存区域的数量,更加便于管理。
在一种可能的实现方式中,在第一程序的内存空间划分出第一业务类型的内存区域时,方法还包括:使用第一业务类型,标记第一业务类型的内存区域。
这样,可以基于业务类型标识内存区域,便于查找内存区域,回收内存区域的内存。
在一种可能的实现方式中,第一业务类型、第二业务类型为非关键业务类型,显示业务类型,交互业务类型,堆区业务类型,推送业务类型中的任一个;
业务类型的优先级从低到高的顺序依次为:非关键业务类型,显示业务类型,交互业务类型,堆区业务类型,推送业务类型。
这样,可以按照示出的业务类型优先级回收不同业务类型的线程的数据。
在一种可能的实现方式中,第一业务类型、第二业务类型为非关键业务类型,显示业务类型,交互业务类型,堆区业务类型,推送业务类型中的任一个;
若第一程序为前台运行的应用程序,则业务类型的优先级从低到高的顺序依次为:非关键业务类型,堆区业务类型,推送业务类型,显示业务类型,交互业务类型;
若第一程序为后台运行的应用程序,则业务类型的优先级从低到高的顺序依次为:非关键业务类型,显示业务类型,交互业务类型,堆区业务类型,推送业务类型。
这样,由于前台运行的应用程序需要显示用户界面,显示业务类型、交互业务类型的线程负责生成应用程序需要显示的用户界面。因此,对前台运行的应用程序来说,显示业务类型、交互业务类型的线程更为重要,应当最后被回收。而后台运行的应用程序不需要显示用户界面,但可能需要接收推送消息、维持关键数据不丢失,因此,对于后台运行的应用程序来说,堆区业务类型、推送业务类型的线程更为重要,应当最后被回收。
在一种可能的实现方式中,在第一程序的内存空间包括第一业务类型的内存区域和第二业务类型的内存区域,第二程序的内存空间也包括第一业务类型的内存区域和第二业务类型的内存区域的情况下,
在回收完第二业务类型的内存区域的内存之后,回收第一业务类型的内存区域的内存,具体包括:
在回收完第一程序的第二业务类型的内存区域和第二程序的第二业务类型的内存区域的内存后,再回收第一程序的第一业务类型的内存区域和第二程序的第一业务类型的内存区域的内存。
这样,可以保证所有程序中业务类型优先级最低的线程的数据都被回收后,才会回收业务类型优先级第二低的线程的数据。
在一种可能的实现方式中,在第一程序的内存空间包括第一业务类型的内存区域,第二程序的内存空间也包括第一业务类型的内存区域,且第二程序的优先级低于第一程序的优先级的情况下,回收第一业务类型的内存区域的内存,具体包括:
先回收第二程序的第一业务类型的内存区域的内存;
在回收完第二程序的第一业务类型的内存区域的内存之后,再回收第一程序的第一业务类型的内存区域的内存。
这样,可以按照应用程序的优先级,回收相同业务类型的线程的数据。保证优先级高的应用程序的数据后回收,可以维持优先级高的应用程序的运行。
在一种可能的实现方式中,在回收第二程序的第一业务类型的内存区域的内存之前,方法还包括:
基于第二程序和第一程序的使用频度确定第二程序和第一程序的优先级,第二程序的使用频度低于第一程序的使用频度。
这样,可以根据应用的使用频度来确定应用程序的优先级,靠后回收用户更多使用的应用程序的数据。
在一种可能的实现方式中,基于最近最少使用LRU算法回收第一业务类型的内存区域的内存。这样,可以在回收某一内存区域的内存时,保证调用最频繁的内存页,最后被回收,降低电子设备重新读取数据到内存的操作频率。
在一种可能的实现方式中,方法还包括:
在基于第一线程的业务类型,给第一线程分配第一业务类型的内存区域的内存之前,基于第一线程的标识和/或应用场景,确定第一线程的业务类型。这样,按照线程的标识和/应用场景,可以更准确地确定线程的业务类型。
第二方面,本申请提供另了一种内存管理方法,应用于电子设备,包括:
接收到第一程序的第一线程的内存分配请求;
基于第一线程的业务类型,给第一线程分配第一类型内存区域的内存,第一类型内存区域在第一程序的内存空间,第一类型内存区域用于存放业务类型为第一类型的线程的数据,第一线程的业务类型为第一业务类型;
在进行内存回收时,基于业务类型的优先级,依次回收不同业务类型对应的内存区域的内存;其中,一种类型的内存区域用于存放一种业务类型的线程的数据,不同业务类型对应的内存区域包括第一类型内存区域。
这样,基于线程的业务类型给线程分配内存区域,便于基于业务类型的优先级,回收不同业务类型的线程的数据。基于业务类型的优先级,优先回收业务类型优先级低的线程的数据,业务类型优先级高(重要)的线程的数据不易被回收,避免出现重要线程的数据被回收引起的程序卡顿问题。
第三方面,本申请提供了一种电子设备,包括一个或多个处理器和一个或多个存储器。该一个或多个存储器与一个或多个处理器耦合,一个或多个存储器用于存储计算机程序代码,计算机程序代码包括计算机指令,当一个或多个处理器执行计算机指令时,使得电子设备执行上述任一方面任一项可能的实现方式中的内存管理方法。
第四方面,本申请实施例提供了一种计算机存储介质,包括计算机指令,当计算机指令在电子设备上运行时,使得电子设备执行上述任一方面任一项可能的实现方式中的内存管理方法。
第五方面,本申请实施例提供了一种计算机程序产品,当计算机程序产品在计算机上运行时,使得计算机执行上述任一方面任一项可能的实现方式中的内存管理方法。
附图说明
图1为本申请实施例提供的一种电子设备100的结构示意图;
图2为本申请实施例提供的一种虚拟地址空间示意图;
图3为本申请实施例提供的一种线程内存分配示意图;
图4为本申请实施例提供的一种虚拟地址空间与标识的对应关系示意图;
图5为本申请实施例提供的一种内存回收流程示意图;
图6为本申请实施例提供的一种线程内存分配流程示意图;
图7为本申请实施例提供的一种软件架构示意图;
图8为本申请实施例提供的一种内存管理方法的流程示意图;
图9为本申请实施例提供的一种线程内存分配流程示意图;
图10A为本申请实施例提供的一种内存回收顺序示意图;
图10B为本申请实施例提供的另一种内存回收顺序示意图;
图10C为本申请实施例提供的另一种内存回收顺序示意图;
图11为本申请实施例提供的一种内存管理方法示例的流程图;
图12A为本申请实施例提供的另一种内存管理方法示例的流程图;
图12B为本申请实施例提供的另一种内存回收顺序示意图;
图13A为本申请实施例提供的另一种内存管理方法示例的流程图;
图13B为本申请实施例提供的另一种内存回收顺序示意图;
图14为本申请实施例提供的另一种内存管理方法的流程示意图;
图15为本申请实施例提供的另一种线程内存分配流程示意图;
图16为本申请实施例提供的另一种内存管理方法示例的流程图;
图17A为本申请实施例提供的另一种内存管理方法示例的流程图;
图17B为本申请实施例提供的另一种内存回收顺序示意图;
图18为本申请实施例提供的另一种内存管理方法的流程示意图;
图19为本申请实施例提供的另一种内存管理方法示例的流程图;
图20为本申请实施例提供的另一种内存管理方法示例的流程图。
具体实施方式
下面将结合附图对本申请实施例中的技术方案进行清楚、详尽地描述。其中,在本申请实施例的描述中,除非另有说明,“/”表示或的意思,例如,A/B可以表示A或B;文本中的“和/或”仅仅是一种描述关联对象的关联关系,表示可以存在三种关系,例如,A和/或B,可以表示:单独存在A,同时存在A和B,单独存在B这三种情况。
以下,术语“第一”、“第二”仅用于描述目的,而不能理解为暗示或暗示相对重要性或者隐含指明所指示的技术特征的数量。由此,限定有“第一”、“第二”的特征可以明示或者隐含地包括一个或者更多个该特征,在本申请实施例的描述中,除非另有说明,“多个”的含义是两个或两个以上。
图1示出了电子设备100的结构示意图。
电子设备100可以是手机、平板电脑、桌面型计算机、膝上型计算机、手持计算机、笔记本电脑、超级移动个人计算机(ultra-mobile personal computer,UMPC)、上网本,以及蜂窝电话、个人数字助理(personal digital assistant,PDA)、增强现实(augmented reality,AR)设备、虚拟现实(virtual reality,VR)设备、人工智能(artificial intelligence,AI)设备、可穿戴式设备、车载设备、智能家居设备和/或智慧城市设备,本申请实施例对该电子设备的具体类 型不作特殊限制。
电子设备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等。
可以理解的是,本发明实施例示意的结构并不构成对电子设备100的具体限定。在本申请另一些实施例中,电子设备100可以包括比图示更多或更少的部件,或者组合某些部件,或者拆分某些部件,或者不同的部件布置。图示的部件可以以硬件,软件或软件和硬件的组合实现。
处理器110可以包括一个或多个处理单元,例如:处理器110可以包括应用处理器(application processor,AP),调制解调处理器,图形处理器(graphics processing unit,GPU),图像信号处理器(image signal processor,ISP),控制器,视频编解码器,数字信号处理器(digital signal processor,DSP),基带处理器,和/或神经网络处理器(neural-network processing unit,NPU)等。其中,不同的处理单元可以是独立的器件,也可以集成在一个或多个处理器中。
控制器可以根据指令操作码和时序信号,产生操作控制信号,完成取指令和执行指令的控制。
处理器110中还可以设置存储器,用于存储指令和数据。在一些实施例中,处理器110中的存储器为高速缓冲存储器。该存储器可以保存处理器110刚用过或循环使用的指令或数据。如果处理器110需要再次使用该指令或数据,可从所述存储器中直接调用。避免了重复存取,减少了处理器110的等待时间,因而提高了系统的效率。
在一些实施例中,处理器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也可以采用上述实施例中不同的接口连接方式,或多种接口连接方式的组合。
充电管理模块140用于从充电器接收充电输入。其中,充电器可以是无线充电器,也可以是有线充电器。在一些有线充电的实施例中,充电管理模块140可以通过USB接口130接收有线充电器的充电输入。在一些无线充电的实施例中,充电管理模块140可以通过电子设备100的无线充电线圈接收无线充电输入。充电管理模块140为电池142充电的同时,还可以通过电源管理模块141为电子设备供电。
电源管理模块141用于连接电池142,充电管理模块140与处理器110。电源管理模块141接收电池142和/或充电管理模块140的输入,为处理器110,内部存储器121,显示屏194,摄像头193,和无线通信模块160等供电。电源管理模块141还可以用于监测电池容量, 电池循环次数,电池健康状态(漏电,阻抗)等参数。在其他一些实施例中,电源管理模块141也可以设置于处理器110中。在另一些实施例中,电源管理模块141和充电管理模块140也可以设置于同一个器件中。
电子设备100的无线通信功能可以通过天线1,天线2,移动通信模块150,无线通信模块160,调制解调处理器以及基带处理器等实现。
天线1和天线2用于发射和接收电磁波信号。电子设备100中的每个天线可用于覆盖单个或多个通信频带。不同的天线还可以复用,以提高天线的利用率。例如:可以将天线1复用为无线局域网的分集天线。在另外一些实施例中,天线可以和调谐开关结合使用。
移动通信模块150可以提供应用在电子设备100上的包括2G/3G/4G/5G等无线通信的解决方案。移动通信模块150可以包括至少一个滤波器,开关,功率放大器,低噪声放大器(low noise amplifier,LNA)等。移动通信模块150可以由天线1接收电磁波,并对接收的电磁波进行滤波,放大等处理,传送至调制解调处理器进行解调。移动通信模块150还可以对经调制解调处理器调制后的信号放大,经天线1转为电磁波辐射出去。在一些实施例中,移动通信模块150的至少部分功能模块可以被设置于处理器110中。在一些实施例中,移动通信模块150的至少部分功能模块可以与处理器110的至少部分模块被设置在同一个器件中。
调制解调处理器可以包括调制器和解调器。其中,调制器用于将待发送的低频基带信号调制成中高频信号。解调器用于将接收的电磁波信号解调为低频基带信号。随后解调器将解调得到的低频基带信号传送至基带处理器处理。低频基带信号经基带处理器处理后,被传递给应用处理器。应用处理器通过音频设备(不限于扬声器170A,受话器170B等)输出声音信号,或通过显示屏194显示图像或视频。在一些实施例中,调制解调处理器可以是独立的器件。在另一些实施例中,调制解调处理器可以独立于处理器110,与移动通信模块150或其他功能模块设置在同一个器件中。
无线通信模块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转为电磁波辐射出去。
在一些实施例中,电子设备100的天线1和移动通信模块150耦合,天线2和无线通信模块160耦合,使得电子设备100可以通过无线通信技术与网络以及其他设备通信。所述无线通信技术可以包括全球移动通讯系统(global system for mobile communications,GSM),通用分组无线服务(general packet radio service,GPRS),码分多址接入(code division multiple access,CDMA),宽带码分多址(wideband code division multiple access,WCDMA),时分码分多址(time-division code division multiple access,TD-SCDMA),长期演进(long term evolution,LTE),BT,GNSS,WLAN,NFC,FM,和/或IR技术等。所述GNSS可以包括全球卫星定位系统(global positioning system,GPS),全球导航卫星系统(global navigation satellite system,GLONASS),北斗卫星导航系统(beidou navigation satellite system,BDS),准天顶卫星系统(quasi-zenith satellite system,QZSS)和/或星基增强系统(satellite based augmentation systems,SBAS)。
电子设备100通过GPU,显示屏194,以及应用处理器等实现显示功能。GPU为图像处理的微处理器,连接显示屏194和应用处理器。GPU用于执行数学和几何计算,用于图形渲染。处理器110可包括一个或多个GPU,其执行程序指令以生成或改变显示信息。
显示屏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的正整数。
电子设备100可以通过ISP,摄像头193,视频编解码器,GPU,显示屏194以及应用处理器等实现拍摄功能。ISP用于处理摄像头193反馈的数据。摄像头193用于捕获静态图像或视频。物体通过镜头生成光学图像投射到感光元件。数字信号处理器用于处理数字信号。视频编解码器用于对数字视频压缩或解压缩。NPU为神经网络(neural-network,NN)计算处理器,通过借鉴生物神经网络结构,例如借鉴人脑神经元之间传递模式,对输入信息快速处理,还可以不断的自学习。通过NPU可以实现电子设备100的智能认知等应用,例如:图像识别,人脸识别,语音识别,文本理解等。
内部存储器121可以包括一个或多个随机存取存储器(random access memory,RAM)和一个或多个非易失性存储器(non-volatile memory,NVM)。
随机存取存储器可以包括静态随机存储器(static random-access memory,SRAM)、动态随机存储器(dynamic random access memory,DRAM)、同步动态随机存储器(synchronous dynamic random access memory,SDRAM)、双倍资料率同步动态随机存取存储器(double data rate synchronous dynamic random access memory,DDR SDRAM,例如第五代DDR SDRAM一般称为DDR5SDRAM)等;
非易失性存储器可以包括磁盘存储器件、快闪存储器(flash memory)。
快闪存储器按照运作原理划分可以包括NOR FLASH、NAND FLASH、3D NAND FLASH等,按照存储单元电位阶数划分可以包括单阶存储单元(single-level cell,SLC)、多阶存储单元(multi-level cell,MLC)、三阶储存单元(triple-level cell,TLC)、四阶储存单元(quad-level cell,QLC)等,按照存储规范划分可以包括通用闪存存储(英文:universal flash storage,UFS)、嵌入式多媒体存储卡(embedded multi media Card,eMMC)等。
随机存取存储器可以由处理器110直接进行读写,可以用于存储操作系统或其他正在运行中的程序的可执行程序(例如机器指令),还可以用于存储用户及应用程序的数据等。
非易失性存储器也可以存储可执行程序和存储用户及应用程序的数据等,可以提前加载到随机存取存储器中,用于处理器110直接进行读写。
外部存储器接口120可以用于连接外部的非易失性存储器,实现扩展电子设备100的存储能力。外部的非易失性存储器通过外部存储器接口120与处理器110通信,实现数据存储功能。例如将音乐,视频等文件保存在外部的非易失性存储器中。
电子设备100可以通过音频模块170,扬声器170A,受话器170B,麦克风170C,耳机接口170D,以及应用处理器等实现音频功能。例如音乐播放,录音等。
音频模块170用于将数字音频信息转换成模拟音频信号输出,也用于将模拟音频输入转换为数字音频信号。音频模块170还可以用于对音频信号编码和解码。在一些实施例中,音 频模块170可以设置于处理器110中,或将音频模块170的部分功能模块设置于处理器110中。
扬声器170A,也称“喇叭”,用于将音频电信号转换为声音信号。电子设备100可以通过扬声器170A收听音乐,或收听免提通话。
受话器170B,也称“听筒”,用于将音频电信号转换成声音信号。当电子设备100接听电话或语音信息时,可以通过将受话器170B靠近人耳接听语音。
麦克风170C,也称“话筒”,“传声器”,用于将声音信号转换为电信号。当拨打电话或发送语音信息时,用户可以通过人嘴靠近麦克风170C发声,将声音信号输入到麦克风170C。电子设备100可以设置至少一个麦克风170C。在另一些实施例中,电子设备100可以设置两个麦克风170C,除了采集声音信号,还可以实现降噪功能。在另一些实施例中,电子设备100还可以设置三个,四个或更多麦克风170C,实现采集声音信号,降噪,还可以识别声音来源,实现定向录音功能等。
耳机接口170D用于连接有线耳机。耳机接口170D可以是USB接口130,也可以是3.5mm的开放移动电子设备平台(open mobile terminal platform,OMTP)标准接口,美国蜂窝电信工业协会(cellular telecommunications industry association of the USA,CTIA)标准接口。
压力传感器180A用于感受压力信号,可以将压力信号转换成电信号。在一些实施例中,压力传感器180A可以设置于显示屏194。陀螺仪传感器180B可以用于确定电子设备100的运动姿态。气压传感器180C用于测量气压。磁传感器180D包括霍尔传感器。电子设备100可以利用磁传感器180D检测翻盖皮套的开合。加速度传感器180E可检测电子设备100在各个方向上(一般为三轴)加速度的大小。当电子设备100静止时可检测出重力的大小及方向。还可以用于识别电子设备姿态,应用于横竖屏切换,计步器等应用。距离传感器180F,用于测量距离。电子设备100可以通过红外或激光测量距离。在一些实施例中,拍摄场景,电子设备100可以利用距离传感器180F测距以实现快速对焦。接近光传感器180G可以包括例如发光二极管(LED)和光检测器,例如光电二极管。发光二极管可以是红外发光二极管。电子设备100通过发光二极管向外发射红外光。电子设备100使用光电二极管检测来自附近物体的红外反射光。当检测到充分的反射光时,可以确定电子设备100附近有物体。当检测到不充分的反射光时,电子设备100可以确定电子设备100附近没有物体。电子设备100可以利用接近光传感器180G检测用户手持电子设备100贴近耳朵通话,以便自动熄灭屏幕达到省电的目的。接近光传感器180G也可用于皮套模式,口袋模式自动解锁与锁屏。环境光传感器180L用于感知环境光亮度。指纹传感器180H用于采集指纹。温度传感器180J用于检测温度。触摸传感器180K,也称“触控器件”。触摸传感器180K可以设置于显示屏194,由触摸传感器180K与显示屏194组成触摸屏,也称“触控屏”。触摸传感器180K用于检测作用于其上或附近的触摸操作。触摸传感器可以将检测到的触摸操作传递给应用处理器,以确定触摸事件类型。可以通过显示屏194提供与触摸操作相关的视觉输出。在另一些实施例中,触摸传感器180K也可以设置于电子设备100的表面,与显示屏194所处的位置不同。骨传导传感器180M可以获取振动信号。在一些实施例中,骨传导传感器180M可以获取人体声部振动骨块的振动信号。
按键190包括开机键,音量键等。按键190可以是机械按键。也可以是触摸式按键。电子设备100可以接收按键输入,产生与电子设备100的用户设置以及功能控制有关的键信号输入。
马达191可以产生振动提示。马达191可以用于来电振动提示,也可以用于触摸振动反 馈。例如,作用于不同应用(例如拍照,音频播放等)的触摸操作,可以对应不同的振动反馈效果。作用于显示屏194不同区域的触摸操作,马达191也可对应不同的振动反馈效果。不同的应用场景(例如:时间提醒,接收信息,闹钟,游戏等)也可以对应不同的振动反馈效果。触摸振动反馈效果还可以支持自定义。
指示器192可以是指示灯,可以用于指示充电状态,电量变化,也可以用于指示消息,未接来电,通知等。
SIM卡接口195用于连接SIM卡。SIM卡可以通过插入SIM卡接口195,或从SIM卡接口195拔出,实现和电子设备100的接触和分离。电子设备100可以支持1个或N个SIM卡接口,N为大于1的正整数。SIM卡接口195可以支持Nano SIM卡,Micro SIM卡,SIM卡等。同一个SIM卡接口195可以同时插入多张卡。所述多张卡的类型可以相同,也可以不同。SIM卡接口195也可以兼容不同类型的SIM卡。SIM卡接口195也可以兼容外部存储卡。电子设备100通过SIM卡和网络交互,实现通话以及数据通信等功能。在一些实施例中,电子设备100采用eSIM,即:嵌入式SIM卡。eSIM卡可以嵌在电子设备100中,不能和电子设备100分离。
目标应用程序的可执行文件运行时,操作系统可以给目标应用程序分配一个虚拟地址空间。在目标应用程序的运行过程中,目标应用程序创建线程时,操作系统会为该线程分配虚拟地址空间的部分虚拟内存地址。目标应用程序的线程执行读写操作时,指令中给出的地址是虚拟地址,CPU执行指令时,首先要将虚拟地址转换为物理地址,才能到物理内存取指令和数据。其中,地址转换(address translation)工作可以由CPU中的内存管理单元(memory management unit,MMU)完成。其中,物理地址为具体物理硬件可识别并访问的地址。虚拟地址为操作系统认可的应用程序内可识别并访问的地址。
如图2所示,目标应用程序的虚拟地址空间包括多个功能区域。该多个功能区域可以包括内核空间、栈、文件映射区、堆、bss段、数据段、代码段以及保留区。
其中,内核空间存储有操作系统的代码,每个进程都可以将内核空间的虚拟地址映射到相同的物理地址。栈用于存放进程使用的局部变量,函数的参数值、返回值等,是一种从高虚拟地址向低虚拟地址扩展的数据结构。文件映射区用于存放从硬盘读取的文件数据。堆用于存放进程运行中被动态分配的内存段,是一种从低虚拟地址向高虚拟地址扩展的数据结构。bss段用于存放未初始化的全局变量和静态局部变量。数据段用于存放应用程序中已经初始化的全局变量和静态局部变量。代码段用于存放应用程序的执行代码(CPU执行的机器指令)。保留区的虚拟地址都未映射物理地址,操作系统不允许任何用户进程访问保留区。
在一些实施例中,操作系统将内存按照固定的尺寸分成多个内存页。虚拟地址空间的内存页和物理地址空间的内存页可以通过页表映射。在目标应用程序运行过程中,操作系统不会将目标应用程序的所有数据都加载到物理内存,操作系统首先只会给虚拟地址空间分配虚拟内存页,这些虚拟内存页没有和物理内存页建立映射关系。只有目标应用程序访问虚拟内存页里的指令或数据时,操作系统可以建立被访问的虚拟内存页和物理内存页的映射关系,将虚拟内存页指示的数据(例如,程序运行过程中产生的局部变量,磁盘数据等等),读取到物理内存页中。
如图3所示,目标应用程序的线程可以包括但不限于窗口管理线程(user interface thread,UI Thread),图像渲染线程(render thread),系统服务线程(andriod pools),推送服务线程等等。其中,窗口管理线程可以用于管理窗口,例如,窗口管理线程可以用于获取显示屏大小, 判断是否有状态栏,锁定屏幕,截取屏幕等。图像渲染线程可以用于图像渲染。系统服务线程可以用于调用系统服务。推送服务线程可以用于管理通信信息。需要说明的是,这些线程仅为目标应用程序的线程示例,不应对目标应用程序的线程的名称、功能等构成具体限定。当操作系统接收到目标应用程序的线程提交的内存分配请求时,可以通过内存分配器(例如,jemalloc分配器,scudo分配器,musl分配器等)给这些线程分配一个或多个内存页,该一个或多个内存页可以统称为虚拟内存区域(virtual memory areas,VMA)。其中,线程向内存分配器发起内存分配请求在用户态执行。内存分配器给线程分配内存页在内核态执行。其中,图3中的灰色方块可以表示线程已占用的内存页,白色方块可以表示线程未占用的内存页。其中,内存分配器分配给线程的内存页为已经和物理内存页建立映射关系的虚拟内存页。
其中,该一个或多个内存页可以分为文件页(file-backed page)和匿名页(anonymous page)。其中,文件页可以用于存放文件类数据。例如,代码段、访问的文件数据等等。文件页中存放的数据可以是从磁盘中加载进来的。其中,匿名页为应用程序运行在过程中动态分配的内存,用于存储应用程序运行时产生的数据。
需要说明的是,文件页都有对应的文件名。匿名页没有对应的标识。电子设备100中存储有包括虚拟地址空间和标识的对应关系的文件(例如,smaps文件)。如图4所示,该文件中记录了文件页的虚拟地址区间,以及该虚拟地址区间的文件页的名称,例如,“filename1”,“filename2”。图4还示出了匿名页的地址区间,但是,所有匿名页的名称都为“anon”,也就是说,操作系统无法基于匿名页的名称区分不同的匿名页。
当内存资源紧张时,操作系统会执行内存回收操作。示例性的,操作系统可以基于最近最少使用(least recently used,LRU)算法回收内存页。具体的,如图5所示,操作系统回收内存过程如下:操作系统可以将最新访问的内存页添加在活跃链表(active list)的表头,活跃链表的内存页向表尾移动,活跃链表的表尾的内存页移动至非活跃链表(inactive list)的表头,非活跃链表的内存页向表尾移动,非活跃链表的表尾的内存页从非活跃链表中移除,被操作系统回收。由于文件页和内存页不同,操作系统可以生成针对文件页的活跃链表、非活跃链表,以及,针对匿名页的活跃链表和非活跃链表。需要说明的是,在操作系统回收文件页时,操作系统需要查询该文件页是否被应用程序修改,若该文件页未被修改,则直接释放该文件页的内存,若该文件页被修改,则将修改后的文件页的内容写回磁盘,再释放该文件页。这样,操作系统可以基于LRU算法,释放最近最久没有使用的文件页,缓解内存压力。
其中,由于操作系统在回收匿名页时,只根据匿名页的最新访问情况来回收,若该匿名页中存储的数据为应用程序的关键数据,由于匿名页中的数据为程序运行过程中产生的数据,磁盘中不存在匿名页对应的数据,因此匿名页一旦被回收,可能会造成因为关键数据丢失,引起的程序卡顿问题。
在一些实施例中,操作系统在分配虚拟地址空间后,可以对分配出的vma统一命名。示例性的,操作系统可以通过jamalloc内存分配器给应用程序分配虚拟地址空间时,可以将该虚拟地址空间命名为“libc_malloc”。当应用程序的线程调用malloc函数或new函数,请求分配内存时,jemalloc内存分配器可以调用mmap接口给线程分配vma,并将物理内存映射到vma。jemalloc内存分配器还可以调用prctl接口给分配的vma命名。例如,jemalloc内存分配器可以给分配的vma统一命名为“anon:libc_malloc”,如图6所示。其中,电子设备100可以将vma的名称等信息存储在存储器中,具体的,可以存储在存储器的smaps文件中。也就是说,内存分配器不会给分配的vma命名,或者,即使给分配的vma命名,也只会赋予所有的vma相同的名称。由于内存分配器分配的vma没有标识,或者,所有vma的标识都相 同,操作系统不能区分不同的vma,无法保留线程的关键数据所在的匿名页。
接下来介绍本申请实施例提供的一种软件架构示意图。
电子设备100的软件系统可以采用分层架构,事件驱动架构,微核架构,微服务架构,或云架构。本发明实施例以分层架构的操作系统为例,示例性说明电子设备100的软件结构。
示例性的,如图7所示,电子设备100的软件架构包括若干个层,每一层都有清晰的角色和分工。层与层之间通过软件接口通信。在一些实施例中,将操作系统分为四层,从上至下分别为应用层,系统层,硬件抽象层,以及内核层。
应用层可以应用程序层和应用程序框架层。其中,应用程序层包括一系列应用程序包。例如,应用程序包可以包括相机,图库,日历,通话,地图,导航,WLAN,蓝牙,音乐,视频,短信息等应用程序。其中,应用程序框架层为应用程序提供应用编程接口(application programming interface,API)和编程框架。应用程序框架层包括一些预先定义的函数。例如,应用程序框架层可以包括窗口管理器,内容提供器,视图系统,资源管理器,通知管理器等。
系统层可以包括系统库(native)和运行时。其中,系统库可以包括多个功能模块。例如:表面管理器(surface manager),媒体库(Media Libraries),三维图形处理库(例如:OpenGL ES),2D图形引擎(例如:SGL)等。
运行时可以指程序运行时所需的一切代码库、框架等。例如,对于C语言来说,运行时包括一系列C程序运行所需的函数库。对于Java语言来说,运行时包括核心库和虚拟机,核心库可包括Java语言需要调用的功能函数。应用程序层和应用程序框架层运行在虚拟机中。虚拟机将应用程序层和应用程序框架层的Java文件执行为二进制文件。虚拟机用于执行对象生命周期的管理,堆栈管理,线程管理,安全和异常的管理,以及垃圾回收等功能。硬件抽象层用于给应用程序提供不同硬件设备的访问接口。
硬件抽象层用于封装内核驱动程序,给上层提供硬件接口。
内核层是硬件和软件之间的层,可以用于进程管理,内存管理等。
在一些应用场景中,应用程序运行时,创建有Java线程和C/C++线程。其中,Java线程运行在虚拟机上,主要用于实现程序基础功能和用户操作逻辑。虚拟机可以通过mmap接口从内核层获取内存,mmap接口用于将一部分物理内存从内核态映射到用户态。其中,C/C++线程主要用于实现程序核心功能,例如,渲染、通信等。C/C++线程可以通过native层的内存分配器(例如,jemalloc分配器、scudo分配器等)获取内存。需要说明的是,内存分配器也是通过mmap接口从内核层获取内存。可以理解的是,凡是通过上述虚拟机或内存分配器获取匿名页的电子设备,都可以实现本申请提供的内存管理方法。
本申请实施例提供了一种内存管理方法,电子设备100可以在接收到应用程序的线程的内存分配请求时,给该线程分配虚拟内存区域。电子设备100还可以获取该线程的名称,使用该线程的名称标识分配给该线程的虚拟内存区域。在内存回收流程中,电子设备100可以基于应用程序的各个线程的优先级,优先回收优先级最低的线程的虚拟内存区域。这样,电子设备100可以优先回收不重要线程的内存区域,保证应用程序的重要线程的运行。
在一些实施例中,电子设备100可以将线程分类,不同类型的线程的优先级不同,相同类型的线程的优先级相同。当电子设备100接收到线程的内存分配请求时,可以确定出线程的业务类型,并获取线程的线程名称。电子设备100可以在程序的内存空间给线程分配一块独立的内存区域,并使用线程的线程名称标识该内存区域。在内存回收流程中,电子设备100 可以基于业务类型,依次回收不同业务类型的线程的内存区域。其中,电子设备100回收应用程序的相同类型的多个线程的内存区域时,可以优先回收最近未运行的线程的内存区域,或者,随机回收相同类型的多个线程的某个线程的内存区域,等等,本申请对此不作限定。
接下来介绍本申请实施例提供的一种内存管理方法的流程图示例。
如图8所示,该内存管理方法包括:
S801.接收到线程的内存分配请求,设置该线程的标识参数和类型参数。
当电子设备100接收到线程的内存分配请求时,可以设置该线程的标识参数和类型参数。其中,标识参数的值可以为该线程的名称,电子设备100可以通过接口获取线程的名称。例如,电子设备100可以通过满足posix接口协议的prctl函数获取线程名。其中,prtcl的输入包括option参数,该参数option可以用于设置prctl函数的功能。其中,当option参数为“PR_GET_NAME”时,prctl函数用于获取线程名。当option参数为“PR_SET_VMA”时,prctl函数用于设置内存区域的名称。
其中,类型参数可以用于表示线程的业务类型。其中,电子设备100可以设置有多个业务类型。电子设备100可以基于线程名称等确定线程的业务类型。
在一些示例中,电子设备100可以设置有5个业务类型,分别为:非关键业务类型,显示业务类型,交互业务类型,堆区业务类型,推送业务类型。其中,显示业务类型的线程可以用于完成显示内容的处理,例如,执行合成图层、渲染图像等操作。交互业务类型的线程可以用于获取用户输入,响应于用户输入执行对应的操作,例如相应用户点击屏幕的操作执行对应的处理流程。堆区业务类型的线程用于管理堆区数据,主要是虚拟机业务相关的线程。推送业务类型的线程用于接收消息数据。非关键业务类型的线程为除了以上业务类型的线程以外的线程。
其中,电子设备100可以采用以下几种方法判断线程的业务类型。
(1)电子设备100可以通过判断线程的线程名称是否包括特定字段,以判断线程的业务类型。例如,当特定字段包括“view”,“Mali”,“surface”,“canvas”,“GL Track”,“JS”,“UI”,“compose”,“Hwcompose”,“display”,“skia”等与显示业务相关的字符串时,可以确定出该线程的业务类型为显示业务类型。当特定字段包括“input”,“event”,“swing”等与交互业务相关的字符串时,可以确定出该线程的业务类型为交互业务类型。当特定字段包括“Java”,“object”,“area”等与堆区业务相关的字符串时,可以确定出该线程的业务类型为堆区业务类型。当特定字段包括“service”,“message”,“push”等与推送业务相关的字符串时,可以确定出该线程的业务类型为推送业务类型。
在一些示例中,若线程的线程名称中不包括显示业务类型、交互业务类型、堆区业务类型以及推送业务类型的特定字段,电子设备100可以确定该线程的业务类型为非关键业务类型。
(2)电子设备100可以基于应用场景,判断线程的业务类型。例如,电子设备100可以将接收用户输入过程中涉及的线程的类型确定为交互业务类型。电子设备100可以将接收到用户输入后涉及的线程的类型确定为显示业务类型。电子设备100可以将应用程序切换到后台之后涉及的线程的类型确定为堆区业务类型。电子设备100可以将接收消息数据过程中涉及的线程的类型确定为推送业务类型。电子设备100可以将应用程序中除了以上业务类型的线程以外的线程确定为非关键业务类型。
在一些示例中,若某个线程由于另外的一个或多个线程,从运行态切换为阻塞态或锁池态时,电子设备100可以将另外的一个或多个线程的业务类型确定为该某个线程的业务类型。
在一些示例中,电子设备100可以结合上述多种确定线程的业务类型的方式,共同判断线程的业务类型。例如,电子设备100可以基于线程的名称确定线程的业务类型是否为显示业务类型、交互业务类型、堆区业务类型以及推送业务类型中的任一类型。再针对无法基于线程名称确定业务类型的线程,基于应用场景判断线程的业务类型是否为显示业务类型、交互业务类型、堆区业务类型以及推送业务类型中的任一类型。针对无法基于线程名称、应用场景确定业务类型的线程,可以基于是否导致其他已经确定业务类型的线程从运行态切换为阻塞态或锁池态,判断该线程的业务类型是否为显示业务类型、交互业务类型、堆区业务类型以及推送业务类型中的任一类型。若电子设备100至此都无法确定某个线程的业务类型是否为显示业务类型、交互业务类型、堆区业务类型以及推送业务类型中的任一类型,可以将该线程的业务类型确定为非关键业务类型。
当然,电子设备100可以使用包括但不限于以上列举的几种方法确定线程的业务类型,本申请对此不作限定。
在一些实施例中,电子设备100可以在确定业务类型后,存储线程的线程名称和业务类型的对应关系。这样,电子设备100在执行步骤S801时,可以直接基于线程名称,获取线程的业务类型。需要说明的是,存储的线程名称和业务类型的对应关系的信息并不是不可修改的,电子设备100可以每隔一段时间,更新存储的线程名称和业务类型的对应关系。
S802.给该线程分配内存区域,基于线程的标识参数标记该内存区域。
电子设备100确定出线程的标识参数后,可以从应用程序的虚拟地址空间给线程分配一块内存区域,并使用该线程的标识参数标记该内存区域。需要说明的是,一块内存区域只包括有一个线程的内存页。还需要说明的是,此处分配的内存页为已经和物理内存页建立映射关系的虚拟内存页。其中,当电子设备100回收该内存区域的内存页时,可以通过LRU等内存回收算法,回收该内存区域的内存页。
示例性的,如图9所示,当线程向电子设备100请求分配内存时,电子设备100可以给线程分配一个内存分配单元,内存分配单元可以给线程获取应用程序的内存空间的一块内存区域,并获取线程的线程名称,使用线程的线程名称标识该内存区域。其中,内存分配单元可以用于分配、管理、回收线程的内存区域。其中,线程可以通过调用malloc或new等方法,请求分配内存。线程的内存分配单元可以通过调用mmap接口获取应用程序的内存空间的一块内存区域。该内存分配单元还可以通过满足posix接口协议的prctl函数设置线程的内存区域的名称。其中,prtcl的输入包括option参数,该参数option可以用于设置prctl函数的功能。其中,当option参数为“PR_SET_VMA”时,prctl函数用于设置内存区域的名称。在此,应用程序包括三个线程,线程1可以通过线程1分配单元,获取线程1内存区域。线程2可以通过线程2分配单元,获取线程2内存区域。线程3可以通过线程3分配单元,获取线程3内存区域。
S803.当内存资源不足时,基于线程的标识参数和类型参数,执行内存回收操作。
电子设备100可以在内存资源不足时,基于线程的标识参数和类型参数,回收内存。具体的,电子设备100存储有不同业务类型的线程的回收顺序。电子设备100可以按照线程的类型参数,确定出待回收的业务类型的线程。电子设备100可以基于线程的标识参数,确定出待回收的内存区域,即,回收线程的内存区域的内存。
其中,电子设备100可以基于空闲的内存容量,判断内存资源是否充足。例如,电子设备100可以在空闲的内存容量低于第一内存容量值(例如,3GB)时,确定内存资源不足。再例如,电子设备100可以在空闲的内存容量占总内存容量的比例小于第一比例(例如,40%) 时,确定内存资源不足。需要说明的是,不限于提及的内存资源不足的示例,电子设备100也可以通过其他方法确定出内存资源不足,本申请实施例对此不作限定。
其中,如图10A所示,电子设备100的业务类型包括非关键业务类型,显示业务类型,交互业务类型,堆区业务类型,以及推送业务类型时,电子设备100可以按照非关键业务类型、显示业务类型、交互业务类型、堆区业务类型、推送业务类型的顺序,依次回收对应的业务类型的线程的内存。具体的,当应用程序包括非关键业务类型的线程时,电子设备100首先回收该业务类型的线程的内存。当应用程序包括非关键业务类型、显示业务类型的线程时,电子设备100首先回收非关键业务类型的线程的内存,再回收显示业务类型的线程的内存,以此类推。
在一些示例中,当电子设备100回收多个程序的线程的内存时,电子设备100可以基于业务类型,随机回收所有程序的相同业务类型的线程的内存。例如,电子设备100可以按照图10A示出的业务类型的回收顺序,首先回收所有应用程序的非关键业务类型的线程的内存,再回收所有应用程序的显示业务类型的线程的内存,以此类推。可以理解的是,电子设备100在回收某个业务类型的线程的内存时,可以随机选取所有该业务类型的线程中的某一个线程,回收该某一个线程的内存区域的内存。其中,电子设备100回收某一个线程的内存区域的内存时,可以通过LRU等内存回收算法回收该内存区域的内存页。
在另一些示例中,电子设备100可以基于应用程序的优先级和业务类型,回收所有程序的相同业务类型的线程的内存。例如,电子设备100可以按照图10A示出的业务类型的回收顺序,首先回收所有应用程序的非关键业务类型的线程的内存,再回收所有应用程序的显示业务类型的线程的内存,以此类推。其中,电子设备100在回收某个业务类型的线程的内存时,可以按照应用程序的优先级,优先回收所有该业务类型的线程中的优先级最低的应用程序的线程的内存。其中,电子设备100可以基于应用的使用频度确定应用程序的优先级。其中,应用的使用频度可以为用户在一段时间内(例如一天)使用该应用程序的时长。用户在一段时间内使用该应用程序的时长越长,应用的使用频度越高,应用程序的优先级越高。应用的使用频度也可以为用户在一段时间内(例如一天)打开该应用程序的次数。
在一些示例中,电子设备100也可以通过应用占用CPU的时长确定应用程序的优先级。例如,最近一段时间(例如,200ms)内应用程序占用CPU的时长的越长,优先级越高。
在一些示例中,电子设备100也可以通过一段时间内应用的耗电量确定应用程序的优先级。例如,最近一段时间(例如,一天)内应用程序耗电量越多,优先级越高。
在一些示例中,电子设备100也可以通过应用的最近使用时间,确定应用程序的优先级。例如,应用最近接收用户输入的时间越近或应用最近读取数据的时间越近,优先级越高。
在一些示例中,为了避免多个应用程序优先级相同的情形,电子设备100可以结合上述多种方式判断应用程序的优先级。例如,电子设备100可以在基于应用使用频度确定出多个应用程序的优先级相同时,基于应用的耗电量多少判断该多个应用程序的优先级,以此类推。
可选的,当多个应用程序的优先级相同时,电子设备100可以按照应用的业务类型确定应用程序的优先级。例如,电子设备100可以将多个应用中的即时通讯应用认定为优先级最高的应用。
示例性的,如图10B所示,当电子设备100运行有应用1、应用2、应用3时,假设应用1的优先级低于应用2,应用2的优先级低于应用3。当电子设备100回收同一业务类型的线程的内存时,可以首先回收应用1的该业务类型的线程的内存区域的内存,再回收应用2的该业务类型的线程的内存区域的内存,最后回收应用3的该业务类型的线程的内存区域的 内存。如图10B中的箭头所示,电子设备100可以先回收应用1的非关键业务类型的线程的内存,再回收应用2的非关键业务类型的线程的内存,以此类推。可以理解的是,若应用1不包括某个业务类型的线程时,可以跳过应用1,回收应用2的该某个业务类型的线程的内存区域的内存。需要说明的是,当应用1包括多个相同的业务类型的线程时,可以随机,或,优先回收最近最少使用的线程等,回收该多个线程的内存。
在一种可能的实现方式中,由于前台运行的应用程序和后台运行的应用程序的线程重要性不同,前台运行的应用程序和后台运行的应用程序的业务类型的优先级不同。例如,电子设备100的业务类型可以包括但不限于图10A所示的非关键业务类型、显示业务类型、交互业务类型、堆区业务类型以及推送业务类型。针对前台运行的应用程序,由于前台运行的应用程序的交互业务和显示业务更加重要,如图10C所示,电子设备100可以按照非关键业务类型、堆区业务类型、推送业务类型、显示业务类型、交互业务类型的顺序,依次回收对应的业务类型的线程的内存。针对后台运行的应用程序,由于后台运行的应用程序的推送业务更加重要,电子设备100可以按照如图10C所示的非关键业务类型、显示业务类型、交互业务类型、堆区业务类型、推送业务类型的顺序,依次回收对应的业务类型的线程的内存。
可以理解的是,前台运行的应用程序需要显示用户界面,显示业务类型、交互业务类型的线程负责生成应用程序需要显示的用户界面。因此,对前台运行的应用程序来说,显示业务类型、交互业务类型的线程更为重要,应当最后被回收。而后台运行的应用程序不需要显示用户界面,但可能需要接收推送消息、维持关键数据不丢失,因此,对于后台运行的应用程序来说,堆区业务类型、推送业务类型的线程更为重要,应当最后被回收。
例如,当用户将阅读APP切换到后台,该阅读APP不再需要显示业务类型的线程来生成用户界面,但是需要确保用户阅读进度这一关键数据(存储在堆区业务类型的线程对应的内存空间)能够不因内存回收而丢失。因此,对于该阅读APP来说,显示业务类型的线程可以优先被回收,而堆区业务类型的线程应尽可能不被回收。
又例如,当用户将微信APP切换到后台,用户仍希望能够持续收到微信APP中联系人发送的消息,并希望手机能够实时推送该消息。因此,对于微信APP来说,推送业务类型的线程应尽可能不被回收。
再例如,当用户将某个游戏APP置于前台时,用户可能正在游戏中对战,该游戏APP既需要显示渲染出来的游戏画面,又需要接收用户通过触摸屏输入的操作进行交互,如果此时将显示业务类型、交互业务类型的线程回收,则会造成游戏崩溃,及其影响用户体验。因此,对于处于前台的游戏APP来说,应尽可能确保显示业务类型、交互业务类型的线程最后被回收。
需要说明的是,由于前台运行的应用程序和后台运行的应用程序的业务类型的优先级不同,电子设备100可以优先回收后台运行的应用程序的内存,再回收前台运行的应用程序的内存。其中,电子设备100回收后台运行的应用程序的内存,以及回收前台运行的应用程序的内存的详细描述可以参见上述实施例,在此不再赘述。
在另一些实施例中,电子设备100可以将线程分类,不同类型的线程的优先级不同,相同类型的线程的优先级相同。当电子设备100接收到线程的内存分配请求时,可以获取线程的线程名称。电子设备100可以在程序的内存空间给线程分配一块独立的内存区域,并使用线程的线程名称标识该内存区域。在内存回收流程中,电子设备100可以确定线程的业务类型,并基于业务类型,依次回收不同业务类型的线程的内存区域。其中,电子设备100回收应用程序的相同类型的多个线程的内存区域时,可以优先回收最近未运行的线程的内存区域, 或者,随机回收相同类型的多个线程的某个线程的内存区域,等等,本申请对此不作限定。
在一些示例中,电子设备100可以在接收到一个程序的不同线程的内存分配请求时,给不同线程分配不同的内存区域,并基于线程的线程名称标识该内存区域。当内存资源不足时,电子设备100可以基于线程的业务类型,回收线程的内存,如图11所示:
S1101.电子设备100接收到第一程序的第一线程、第二线程的内存分配请求,其中,第一线程属于第一业务类型,第二线程属于第二业务类型,第一业务类型的优先级高于第二业务类型的优先级。
其中,由于优先级越高的业务类型的线程越重要,电子设备100将优先回收优先级更低的业务类型的线程的内存,尽可能保留优先级更高的业务类型的线程的内存。
其中,由于前台运行的应用程序需要显示用户界面,显示业务类型、交互业务类型的线程负责生成应用程序需要显示的用户界面。因此,对前台运行的应用程序来说,显示业务类型、交互业务类型的线程更为重要,应当最后被回收。而后台运行的应用程序不需要显示用户界面,但可能需要接收推送消息、维持关键数据不丢失,因此,对于后台运行的应用程序来说,堆区业务类型、推送业务类型的线程更为重要,应当最后被回收。因此,在第一程序为后台运行的程序的情况下,若第一业务类型为堆区业务类型,第二业务类型可以为非关键业务类型、显示业务类型或交互业务类型。若第一程序为前台运行的程序,若第一业务类型为显示业务类型,第二业务类型可以为非关键业务类型、堆区业务类型或推送业务类型。具体的,关于业务类型的描述可以参见图8所示实施例,在此不再赘述。
S1102.电子设备100从第一程序的内存空间给第一线程分配第一内存区域,并以第一线程的标识标记该第一内存区域,给第二线程分配第二内存区域,并以第二线程的标识标记该第二内存区域。
其中,线程的标识可以为线程的线程名称,电子设备100可以基于线程的线程名称,确定线程的内存区域。其中,电子设备100给线程分配内存区域,获取线程名称,给内存区域命名的详细描述可以参见图8所示实施例,在此不再赘述。
S1103.当内存资源不足时,基于业务类型的优先级,按照先回收第二业务类型的第二线程对应的第二内存区域、再回收第一业务类型的第一线程对应的第一内存区域的顺序,依次回收内存。
如前所述,第一业务类型的优先级高于第二业务类型的优先级,优先级越高,则应越后被回收。因此,当内存资源不足时,应当先回收第二业务类型的第二线程对应的第二内存区域,再回收第一业务类型的第一线程对应的第一内存区域。
其中,电子设备100在回收第二线程对应的第二内存区域(或第一线程对应的第一内存区域)的内存时,可以通过LRU算法,逐步回收第二内存区域(或第一内存区域)的内存页。具体的,可以参见图5所示实施例,在此不再赘述。
在一些示例中,若第一线程和第二线程的业务类型相同,电子设备100可以随机回收第一线程对应的第一内存区域和第二线程对应的第二内存区域,或者,电子设备100可以优先回收第一线程和第二线程中最近未使用的线程对应的内存区域(比如,最近未使用第二线程,则优先回收第二内存区域),等等。
在一些示例中,电子设备100可以在接收到多个程序的不同线程的内存分配请求时,给不同线程分配不同的内存区域,并基于线程的线程名称标识该内存区域。当内存资源不足时, 电子设备100可以基于线程的业务类型以及程序的优先级,回收内存,如图12A所示:
S1201.电子设备100接收到第一程序的第一线程、第一程序的第二线程、第二程序的第三线程的内存分配请求,其中,第一线程和第三线程属于第一业务类型,第二线程属于第二业务类型,第一业务类型的优先级高于第二业务类型的优先级。
需要说明的是,由于优先级越高的业务类型的线程越重要。当内存资源不足时,应当先回收优先级低的业务类型的线程对应的内存区域,后回收优先级高的业务类型的线程对应的内存区域。
S1202.电子设备100从第一程序的内存空间给第一线程分配第一内存区域,给第二线程分配第二内存区域,并以第一线程的标识标记该第一内存区域,以第二线程的标识标记该第二内存区域,从第二程序的内存空间给第三线程分配第三内存区域,并以第三线程的标识标记该第三内存区域。
S1203.当内存资源不足时,若第一程序比第二程序优先级高,基于业务类型的优先级与程序的优先级,按照先回收第一程序的第二业务类型的第二线程对应的第二内存区域、再回收第二程序的第一业务类型的第三线程对应的第三内存区域、最后回收第一程序的第一业务类型的第一线程对应的第一内存区域的顺序,依次回收内存。
其中,如图12B所示,第一线程和第三线程的业务类型为第一业务类型,第二线程的业务类型为第二业务类型。
针对业务类型的优先级,第二业务类型的优先级为优先级2。第一业务类型的优先级为优先级1。第二业务类型的优先级低于第一业务类型的优先级。业务类型优先级越低则该业务类型的线程对应的内存区域越先被回收。因此当内存资源不足时,可以优先回收第二业务类型的线程(此示例中,为第二线程)对应的内存区域,再回收第一业务类型的线程(此示例中,为第三线程、第一线程)对应的内存区域。
针对相同业务类型的线程,则可以按照程序的优先级,确定内存回收的顺序。此示例中,第三线程、第一线程均为“优先级1”的第一业务类型的线程。由于第三线程对应的第二程序优先级低于第一线程对应的第一程序的优先级,程序优先级越低则该程序的线程对应的内存区域越先被回收。因此,此示例中,可以先回收第三线程对应的第三内存区域,再回收第一线程对应的第一内存区域。
从而,在此示例中,内存区域的回收顺序为:先回收第二线程对应的第二内存区域,再回收第三线程对应的第三内存区域,最后回收第一线程对应的第一内存区域。
其中,电子设备100如何分配内存区域、如何回收每个内存区域内的内存,可以参见图8所示实施例(例如基于LRU算法回收每个内存区域内的内存),在此不再赘述。
在一些示例中,电子设备100可以在接收到多个程序的多个线程的内存分配请求时,给多个线程分配不同的内存区域,并基于线程的线程名称标识对应的内存区域。当内存资源不足时,电子设备100可以基于线程的业务类型以及程序的优先级,回收内存,如图13A所示:
S1301.电子设备100接收到第一程序的第一线程、第一程序的第二线程、第二程序的第三线程以及第二程序的第四线程的内存分配请求,其中,第一线程和第三线程属于第一业务类型,第二线程和第四线程属于第二业务类型,第一业务类型的优先级高于第二业务类型的优先级。
需要说明的是,由于优先级越高的业务类型的线程越重要。当内存资源不足时,应当先回收优先级低的业务类型的线程对应的内存区域,后回收优先级高的业务类型的线程对应的 内存区域。
S1302.电子设备100从第一程序的内存空间给第一线程分配第一内存区域,给第二线程分配第二内存区域,并以第一线程的标识标记该第一内存区域,以第二线程的标识标记该第二内存区域,从第二程序的内存空间给第三线程分配第三内存区域,给第四线程分配第四内存区域,并以第三线程的标识标记该第三内存区域,以第四线程的标识标记该第四内存区域。
S1303.当内存资源不足时,若第一程序比第二程序优先级高,基于业务类型的优先级与程序的优先级,按照先回收第二程序的第二业务类型的第四线程对应的第四内存区域,再回收第一程序的第二业务类型的第二线程对应的第二内存区域,再回收第二程序的第一业务类型的第三线程对应的第三内存区域,最后回收第一程序的第一业务类型的第一线程对应的第一内存区域的顺序,依次回收内存。
其中,如图13B所示,第一线程和第三线程的业务类型为第一业务类型,第二线程和第四线程的业务类型为第二业务类型。
针对业务类型的优先级,第二业务类型的优先级为优先级2。第一业务类型的优先级为优先级1。第二业务类型的优先级低于第一业务类型的优先级。业务类型优先级越低则该业务类型的线程对应的内存区域越先被回收。因此当内存资源不足时,可以优先回收第二业务类型的线程(此示例中,为第二线程、第四线程)对应的内存区域,再回收第一业务类型的线程(此示例中,为第三线程、第一线程)对应的内存区域。
针对相同业务类型的线程,则可以按照程序的优先级,确定内存回收的顺序。此示例中,第三线程、第一线程均为“优先级1”的第一业务类型的线程。由于第三线程对应的第二程序优先级低于第一线程对应的第一程序的优先级,程序优先级越低则该程序的线程对应的内存区域越先被回收。因此,此示例中,可以先回收第三线程对应的第三内存区域,再回收第一线程对应的第一内存区域。第四线程、第二线程均为“优先级2”的第二业务类型的线程。由于第四线程对应的第二程序优先级低于第二线程对应的第一程序的优先级,程序优先级越低则该程序的线程对应的内存区域越先被回收。因此,此示例中,可以先回收第四线程对应的第四内存区域,再回收第二线程对应的第二内存区域。
从而,在此示例中,内存区域的回收顺序为:先回收第四线程对应的第四内存区域,再回收第二线程对应的第二内存区域,再回收第三线程对应的第三内存区域,最后回收第一线程对应的第一内存区域。
其中,电子设备100如何分配内存区域、如何回收每个内存区域内的内存,可以参见图8所示实施例(例如基于LRU算法回收每个内存区域内的内存),在此不再赘述。
在另一些实施例中,电子设备100可以给每个线程分配对应的内存区域,并使用线程的线程名称给内存区域命名。电子设备100回收内存时,可以按照应用程序的优先级,优先回收优先级最低的应用程序的内存。电子设备100回收应用程序的内存时,可以按照应用程序的线程的优先级,优先回收优先级最低的线程的内存。电子设备100回收线程的内存时,可以按照LRU等内存回收算法,回收线程的内存区域的内存页。这样,电子设备100也可以避免回收重要线程的内存。其中,电子设备100可以基于应用的使用频度确定应用程序的优先级。其中,应用的使用频度可以为用户在一段时间内(例如一天)使用该应用程序的时间。用户在一段时间内使用该应用程序的时间越长,应用的使用频度越高,应用程序的优先级越高。
在一些示例中,电子设备100也可以通过应用占用CPU的时间确定应用程序的优先级。 例如,最近一段时间(例如,200ms)内应用程序占用CPU的时间的越长,优先级越高。
在一些示例中,电子设备100也可以通过应用的最近使用时间,确定应用程序的优先级。例如,应用最近接收用户输入的时间越近或应用最近读取数据的时间越近,优先级越高。
但是,由于上述实现方式中,每个线程都分配有一块内存区域,若线程数量较多,内存管理复杂。
本申请提供了另一种内存管理方法。电子设备100可以在接收到应用程序的线程的内存分配请求时,确定线程的业务类型。电子设备100基于线程的业务类型,将应用程序的内存空间中用于存储该业务类型的线程的数据的内存区域中的一部分内存划分给该线程。在内存回收流程中,电子设备100可以基于业务类型的优先级,回收不同内存区域的内存。这样,电子设备100可以优先回收用于存储不重要业务类型的线程的数据的内存区域,保证应用程序的重要线程的运行。
在一些实施例中,电子设备100预置M种业务类型,电子设备100的每一个程序的内存空间包括M个内存区域,该M个内存区域和M种业务类型为一一对应的关系。一个内存区域用于存放一种业务类型的所有线程的数据。当电子设备100接收到线程的内存分配请求时,可以确定出线程的业务类型,M种业务类型包括该线程的业务类型,并将该业务类型对应的内存区域的一部分内存分配给该线程。当内存资源不足时,电子设备100可以基于M种业务类型的优先级顺序,依次回收对应的内存区域的内存。
接下来介绍本申请实施例提供的另一种内存管理方法的流程图示例。
如图14所示,该内存管理方法包括:
S1401.电子设备100预置M个业务类型,M大于等于1,基于M个业务类型,在内存空间划分出M个内存区域,每一个内存区域的内存可以分配给对应的业务类型的线程。
电子设备100在给应用程序分配虚拟地址空间时,可以在该虚拟地址空间中划分出M个内存区域,该M个内存区域中的每一个内存区域用于存放一个业务类型的线程的数据。也就是说,M个业务类型和M个内存区域为一一对应的关系。可以理解的是,一个内存区域内可以包括有一个或多个线程的内存页。
在一些示例中,电子设备100预置的有5个业务类型,分别为:非关键业务类型,显示业务类型,交互业务类型,堆区业务类型,推送业务类型。具体的,可以参见图8所示实施例,在此不再赘述。电子设备100的每一个应用程序的虚拟地址空间中包括5个内存区域,分别为用于存储非关键业务类型的线程的数据的内存区域,用于存储显示业务类型的线程的数据的内存区域,用于存储交互业务类型的线程的数据的内存区域,用于存储堆区业务类型的线程的数据的内存区域,用于存储推送业务类型的线程的数据的内存区域。在此,M等于5。
在一些示例中,电子设备100可以通过mmap接口获取内存区域,电子设备100还可以设置内存区域的名称,例如,通过图8所示的prctl函数。其中,内存区域的名称可以为业务类型的值。这样,可以通过业务类型关联内存区域,便于内存区域的内存分配及回收。
S1402.电子设备100接收到线程的内存分配请求,确定该线程的业务类型。
电子设备100可以在接收到线程的内存分配请求时,判断线程的业务类型。具体的,可以参见图8所示实施例,在此不再赘述。
S1403.电子设备100基于线程的业务类型,给该线程分配对应的内存区域中的内存。
当电子设备100确定出线程的业务类型后,可以将应用程序的虚拟地址空间中的用于存 放该业务类型的线程的数据的内存区域中的一部分内存分配给该线程。
示例性的,如图15所示,当线程向电子设备100请求分配内存时,电子设备100可以确定出线程的业务类型,基于业务类型,给线程分配对应的内存分配单元,内存分配单元可以给线程分配对应的内存区域的一部分内存。其中,内存分配单元可以用于分配、管理、回收内存区域。其中,线程可以通过调用malloc或new等方法,请求分配内存。线程的内存分配单元可以通过调用mmap接口获取内存区域的一部分内存。该内存分配单元还可以通过满足posix接口协议的prctl函数设置该内存区域的名称。其中,prtcl的输入包括option参数,该参数option可以用于设置prctl函数的功能。其中,当option参数为“PR_SET_VMA”时,prctl函数用于设置内存区域的名称。
在此,应用程序包括但不限于7个线程,线程1的业务类型为非关键业务类型,线程2的业务类型为交互业务类型,线程3和线程4的业务类型为显示业务分配单元,线程5和线程6的业务类型为推送业务类型,线程7的业务类型为堆区业务类型。内存区域1可以分配给业务类型为非关键业务类型的线程,内存区域2可以分配给业务类型为显示业务分配单元的线程,内存区域3可以分配给业务类型为交互业务类型的线程,内存区域4可以分配给业务类型为堆区业务类型的线程,内存区域5可以分配给业务类型为推送业务类型的线程。
线程1可以通过非关键业务分配单元,获取内存区域1的一部分内存。线程2可以通过交互业务分配单元,获取内存区域3的一部分内存。线程3可以通过显示业务分配单元,获取内存区域2的一部分内存。线程4也可以通过显示业务分配单元,获取内存区域2的一部分内存。线程5可以通过推送业务分配单元,获取内存区域5的一部分内存。线程6也可以通过推送业务分配单元,获取内存区域5的一部分内存。线程7可以通过堆区业务分配单元,获取内存区域4的一部分内存。
S1404.当内存资源不足时,基于业务类型,执行内存回收操作。
电子设备100可以在内存资源不足时,基于业务类型,依次回收不同内存区域的内存。其中,电子设备100回收某一个内存区域的内存时,可以通过LRU等内存回收算法回收该内存区域的内存页。
在一些示例中,电子设备100可以如图10A所示的顺序,依次回收用于存放非关键业务类型的线程的数据的内存区域的内存,用于存放显示业务类型的线程的数据的内存区域的内存,用于存放交互业务类型的线程的数据的内存区域的内存,用于存放堆区业务类型的线程的数据的内存区域的内存,用于存放推送业务类型的线程的数据的内存区域的内存。
在一些示例中,当电子设备100回收多个程序的内存区域的内存时,电子设备100可以基于业务类型,随机回收所有程序的相同业务类型的内存区域的内存。例如,电子设备100可以按照图10A示出的业务类型的回收顺序,首先回收所有应用程序的非关键业务类型的内存区域的内存,再回收显示业务类型的内存区域的内存,以此类推。
在另一些示例中,电子设备100可以基于应用程序的优先级和业务类型,回收所有程序的相同业务类型的内存区域的内存。例如,电子设备100可以按照图10A示出的业务类型的回收顺序,首先回收所有应用程序的非关键业务类型的内存区域的内存,再回收显示业务类型的内存区域的内存,以此类推。其中,电子设备100在回收某个业务类型的内存区域的内存时,可以按照应用程序的优先级,优先回收优先级最低的应用程序的该业务类型的内存区域的内存。其中,电子设备100可以基于应用的使用频度,应用的最近使用时间,应用最近接收用户输入的时间等中的任一项来确定应用程序的优先级,具体的,可以参见图8所示实施例,在此不再赘述。
示例性的,如图10B所示,当电子设备100运行有应用1、应用2、应用3时,若应用1的优先级低于应用2,应用2的优先级低于应用3。当电子设备100回收同一业务类型的内存区域的内存时,可以首先回收应用1的该业务类型的内存区域的内存,再回收应用2的该业务类型的内存区域的内存,最后回收应用3的该业务类型的内存区域的内存。
在一种可能的实现方式中,由于前台运行的应用程序和后台运行的应用程序的线程重要性不同,前台运行的应用程序和后台运行的应用程序的业务类型的优先级不同。具体的,可以参见图8所示实施例,在此不再赘述。
在一些示例中,电子设备100可以在接收到一个程序的不同线程的内存分配请求时,确定线程的业务类型,并基于业务类型,给线程分配对应的内存区域的一部分内存。当内存资源不足时,电子设备100可以基于业务类型,回收不同内存区域的内存,如图16所示:
S1601.电子设备100接收到第一程序的第一线程和第二线程的内存分配请求,第一程序的内存空间包括第一类型内存区域和第二类型内存区域,第一类型内存区域用于存放第一业务类型的线程的数据,第二类型内存区域用于存放第二业务类型的线程的数据,第一业务类型的优先级高于第二业务类型的优先级。
需要说明的是,优先级越高的业务类型的线程越重要,在内存回收过程中,电子设备100将先回收用于存放优先级低的业务类型的线程数据的内存区域,后回收用于存放优先级高的业务类型的线程数据的内存区域。
S1602.电子设备100确定出第一线程的业务类型为第一业务类型,第二线程的业务类型为第二业务类型,给第一线程分配第一类型内存区域中的内存,给第二线程分配第二类型内存区域中的内存。
其中,电子设备100确定线程的业务类型,给线程分配内存区域的详细描述可以参见图14所示实施例,在此不再赘述。
S1603.当内存资源不足时,基于业务类型的优先级,按照先回收用于存放第二业务类型的线程数据的第二类型内存区域,再回收用于存放第一业务类型的线程数据的第一类型内存区域的顺序,依次回收内存。
如前所述,第一业务类型的优先级高于第二业务类型的优先级,优先级越高,则应越后被回收。因此,当内存资源不足时,应当先回收第二业务类型对应的第二内存区域,再回收第一业务类型对应的第一内存区域。
其中,电子设备100在回收第二类型内存区域(或第一类型内存区域)的内存时,可以通过LRU等内存回收算法,逐步回收第二类型内存区域(或第一类型内存区域)的内存页。具体的,可以参见图5所示实施例,在此不再赘述。
在一些示例中,电子设备100可以在接收到多个程序的不同线程的内存分配请求时,确定线程的业务类型,并基于业务类型,给线程分配对应的内存区域的一部分内存。当内存资源不足时,电子设备100可以基于业务类型,回收不同内存区域的内存,如图17A所示:
S1701.电子设备100接收到第一程序的第一线程和第二线程、第二程序的第三线程和第四线程的内存分配请求,第一程序的内存空间包括第一类型内存区域和第二类型内存区域,第二程序的内存空间包括第一类型内存区域和第二类型内存区域,其中,第一类型内存区域用于存放第一业务类型的线程的数据,第二类型内存区域用于存放第二业务类型的线程的数据,第一业务类型的优先级高于第二业务类型的优先级。
可以理解的是,第一程序的第一类型内存区域可以分配给第一程序的第一业务类型的线程,第一程序的第二类型内存区域可以分配给第一程序的第二业务类型的线程,第二程序的第一类型内存区域可以分配给第二程序的第一业务类型的线程,第二程序的第二类型内存区域可以分配给第二程序的第二业务类型的线程。
需要说明的是,优先级越高的业务类型的线程越重要,在内存回收过程中,电子设备100将先回收用于存放优先级低的业务类型的线程数据的内存区域,后回收用于存放优先级高的业务类型的线程数据的内存区域。
S1702.电子设备100确定出第一线程的业务类型为第一业务类型,第二线程的业务类型为第二业务类型,第三线程的业务类型为第一业务类型,第四线程的业务类型为第二业务类型,给第一线程分配第一程序的第一类型内存区域中的内存,第二线程分配第一程序的第二类型内存区域中的内存,第三线程分配第二程序的第一类型内存区域中的内存,第四线程分配第二程序的第二类型内存区域中的内存。
其中,电子设备100给确定线程的业务类型,给线程分配内存区域的详细描述可以参见图14所示实施例,在此不再赘述。
S1703.当内存资源不足时,若第一程序比第二程序优先级高,基于业务类型的优先级与程序优先级,按照先回收第二程序的第二类型内存区域,再回收第一程序的第二类型内存区域,再回收第二程序的第一类型内存区域,最后回收第一程序的第一类型内存区域的顺序,依次回收内存。
其中,电子设备100在回收第二程序的第二类型内存区域、第二程序的第一类型内存区域、第一程序的第二类型内存区域或第一程序的第一类型内存区域的内存时,可以通过LRU等内存回收算法,逐步回收内存区域的内存页。具体的,可以参见图5所示实施例,在此不再赘述。
其中,如图17B所示,第一程序的第二类型内存区域用于存放第一程序中第二业务类型的线程(例如,第二线程)的数据,第一程序的第一类型内存区域用于存放第一程序中第一业务类型的线程(例如,第一线程)的数据,第二程序的第二类型内存区域用于存放第二程序中第二业务类型的线程(例如,第四线程)的数据,第二程序的第一类型内存区域用于存放第二程序中第一业务类型的线程(例如,第三线程)的数据。
针对业务类型的优先级,第二业务类型的优先级为优先级2。第一业务类型的优先级为优先级1。第二业务类型的优先级低于第一业务类型的优先级。业务类型优先级越低则存放该业务类型的线程数据的内存区域越先被回收。因此当内存资源不足时,可以优先回收存放有第二业务类型的线程数据的内存区域(此示例中,为第一程序的第二类型内存区域、第二程序的第二类型内存区域),再回收存放有第一业务类型的线程数据的内存区域(此示例中,为第一程序的第一类型内存区域、第二程序的第一类型内存区域)。
针对相同业务类型的内存区域,则可以按照程序的优先级,确定内存区域的回收顺序。此示例中,第一程序的第一类型内存区域、第二程序的第一类型内存区域均为存放“优先级1”的第一业务类型的线程数据的内存区域。由于第二程序优先级低于第一程序的优先级,程序优先级越低则该程序的内存区域越先被回收。因此,此示例中,可以先回收第二程序的第一类型内存区域,再回收第一程序的第一类型内存区域。同理,在此示例中,可以先回收第二程序的第二类型内存区域,再回收第一程序的第二类型内存区域。
从而,在此示例中,内存区域的回收顺序为:先回收第二程序的第二类型内存区域,再回收第一程序的第二类型内存区域,再回收第二程序的第一类型内存区域,最后回收第一程 序的第一类型内存区域。
其中,电子设备100如何分配内存区域、如何回收每个内存区域内的内存,可以参见图14所示实施例(例如基于LRU算法回收每个内存区域内的内存),在此不再赘述。
在另一些实施例中,电子设备100预置有M种业务类型,当电子设备100接收到线程的内存分配请求时,可以确定出线程的业务类型,M种业务类型包括该线程的业务类型。若程序的内存空间包括该业务类型对应的内存区域,电子设备100可以将对应的内存区域中的一部分内存分配给该线程。若程序的内存空间不包括该业务类型对应的内存区域,电子设备100可以在程序的内存空间划分出一块该业务类型对应的内存区域,再将对应的内存区域中的一部分内存分配给该线程。当内存资源不足时,电子设备100可以基于程序的内存空间中的内存区域对应的业务类型的优先级顺序,依次回收对应的内存区域的内存。这样,当程序只包括M种业务类型中的部分业务类型的线程时,不需要在内存空间中划分M种业务类型中不包括的部分业务类型对应的内存区域,降低管理成本。
接下来介绍本申请实施例提供的另一种内存管理方法的流程图示例。
如图18所示,该内存管理方法包括:
S1801.电子设备100预置M个业务类型,M大于等于1。
在一些示例中,电子设备100预置的有5个业务类型,分别为:非关键业务类型,显示业务类型,交互业务类型,堆区业务类型,推送业务类型。具体的,可以参见图8所示实施例,在此不再赘述。
S1802.电子设备100接收到线程的内存分配请求,确定该线程的业务类型。
具体的,可以参见图8所示实施例,在此不再赘述。
S1803.若内存空间包括用于存放该业务类型的线程的数据的内存区域,将对应的内存区域中的内存分配给该线程;若内存空间不包括用于存放该业务类型的线程的数据的内存区域,在内存空间中分配用于存放该业务类型的线程的数据的内存区域,并将该内存区域中的内存分配给该线程。
在一些示例中,电子设备100确定出线程的业务类型后,可以通过对应的内存分配单元,将对应的内存区域的内存分配给该线程。若程序的内存空间不包括业务类型对应的内存区域,对应的内存分配单元可以在程序的内存空间划分一块内存区域,并将该内存区域作为用于存放该业务类型的所有线程的数据的内存区域,例如,内存分配单元可以通过调用mmap函数划分内存区域。对应的内存分配单元还可以基于业务类型给内存区域命名,例如,内存分配单元可以通过prctl函数给内存区域命名。内存分配单元可以将内存区域的一部分内存分配给线程使用。若程序的内存空间包括业务类型对应的内存区域,对应的内存分配单元可以对应的内存区域的一部分内存分配给线程。
可以理解的是,一个内存区域可以分配给程序的对应的业务类型的多个线程使用。
S1804.当内存资源不足时,基于业务类型,执行内存回收操作。
电子设备100可以在内存资源不足时,基于业务类型,回收不同内存区域的内存。电子设备100回收内存资源时,可以首先回收优先级最低的业务类型的内存区域。电子设备100回收某一个业务类型对应的所有内存区域时,可以首先回收优先级最低的应用程序的内存区域。电子设备100回收某一个内存区域的内存时,可以通过LRU等内存回收算法回收该内存区域的内存页。具体的,可以参见图14所示实施例,在此不再赘述。
在一些示例中,电子设备100可以在接收到线程的内存分配请求时,确定线程的业务类 型,并基于业务类型,给线程分配对应的内存区域的一部分内存。当内存资源不足时,电子设备100可以基于业务类型,回收不同内存区域的内存,如图19所示:
S1901.电子设备100接收到第一程序的第一线程和第二线程的内存分配请求,确定出第一线程的业务类型为第一业务类型,第二线程的业务类型为第二业务类型,第一业务类型的优先级高于第二业务类型。
需要说明的是,优先级越高的业务类型的线程越重要,在内存回收过程中,电子设备100将先回收用于存放优先级低的业务类型的线程数据的内存区域,后回收用于存放优先级高的业务类型的线程数据的内存区域。
S1902.若第一程序的内存空间包括第一类型内存区域和第二类型内存区域,给第一线程分配第一类型内存区域中的一部分内存,给第二线程分配第二类型内存区域中的一部分内存,第一类型内存区域用于存放第一业务类型的线程的数据,第二类型内区域用于存放第二业务类型的线程的数据。
其中,电子设备100确定线程的业务类型,给线程分配内存区域的详细描述可以参见图18所示实施例,在此不再赘述。
若第一程序的内存空间包括第一类型内存区域,不包括第二类型内存区域,电子设备100给第一线程分配第一类型内存区域中的一部分内存,在第一程序的内存空间划分出第二类型内存区域,给第二线程分配第二类型内存区域中的一部分内存。
若第一程序的内存空间不包括第一类型内存区域,包括第二类型内存区域,电子设备100给第二线程分配第二类型内存区域中的一部分内存,在第一程序的内存空间划分出第一类型内存区域,给第一线程分配第一类型内存区域中的一部分内存。
若第一程序的内存空间不包括第一类型内存区域,且不包括第二类型内存区域,在第一程序的内存空间划分出第二类型内存区域,给第二线程分配第二类型内存区域中的一部分内存,在第一程序的内存空间划分出第一类型内存区域,给第一线程分配第一类型内存区域中的一部分内存。
S1903.当内存资源不足时,基于业务类型的优先级,针对内存区域执行内存回收操作。
其中,电子设备100在回收第二类型内存区域和第一类型内存区域的内存时,可以通过LRU等内存回收算法,逐步回收内存区域的内存页。具体的,可以参见图5所示实施例,在此不再赘述。
其中,优先级越高的业务类型的线程的数据越重要,在内存回收过程中,电子设备100将先回收用于存放优先级低的业务类型的线程数据的内存区域,后回收用于存放优先级高的业务类型的线程数据的内存区域。由于第一业务类型的优先级高于第二业务类型,当第一程序的内存空间包括第一类型内存区域和第二类型内存区域时,首先回收第二类型内存区域的内存,并在回收完第二类型内存区域的内存后,再回收第一类型内存区域的内存。
在一些示例中,电子设备100可以在接收到多个程序的多个线程的内存分配请求时,确定线程的业务类型,并基于业务类型,给线程分配对应的内存区域的一部分内存。当内存资源不足时,电子设备100可以基于业务类型和程序优先级,回收不同内存区域的内存,如图20所示:
S2001.电子设备100接收到第一程序的第一线程和第二线程、第二程序的第三线程和第四线程的内存分配请求,确定出第一线程和第三线程的业务类型为第一业务类型,第二线程和第四线程的业务类型为第二业务类型,第一业务类型的优先级高于第二业务类型。
S2002.若第一程序的内存空间包括第一类型内存区域和第二类型内存区域,第二程序的内存空间包括第一类型内存区域和第二类型内存区域,给第一线程分配第一程序的第一类型内存区域中的内存,第二线程分配第一程序的第二类型内存区域中的内存,第三线程分配第二程序的第一类型内存区域中的内存,第四线程分配第二程序的第二类型内存区域中的内存,其中,第一类型内存区域用于存放第一业务类型的线程的数据,第二类型内区域用于存放第二业务类型的线程的数据。
其中,电子设备100确定线程的业务类型,给线程分配内存区域的详细描述可以参见图18所示实施例,在此不再赘述。
需要说明的是,若第一程序的内存空间包括第一类型内存区域,不包括第二类型内存区域,电子设备100给第一线程分配第一类型内存区域中的一部分内存,在第一程序的内存空间划分出第二类型内存区域,给第二线程分配第二类型内存区域中的一部分内存。若第一程序的内存空间不包括第一类型内存区域,包括第二类型内存区域,电子设备100给第二线程分配第二类型内存区域中的一部分内存,在第一程序的内存空间划分出第一类型内存区域,给第一线程分配第一类型内存区域中的一部分内存。若第一程序的内存空间不包括第一类型内存区域,且不包括第二类型内存区域,在第一程序的内存空间划分出第二类型内存区域,给第二线程分配第二类型内存区域中的一部分内存,在第一程序的内存空间划分出第一类型内存区域,给第一线程分配第一类型内存区域中的一部分内存。同理,第二程序的内存空间是否包括第一类型内存区域和/或第二类型内存区域的描述可以参见上述实施例,在此不再赘述。
S2003.当内存资源不足时,基于业务类型的优先级与程序的优先级,针对内存区域执行内存回收操作。
其中,电子设备100在回收第二类型内存区域和第一类型内存区域的内存时,可以通过LRU等内存回收算法,逐步回收内存区域的内存页。具体的,可以参见图5所示实施例,在此不再赘述。
例如,若第二程序的优先级低于第一程序的优先级。当第一程序的内存空间包括第一类型内存区域和第二类型内存区域,第二程序的内存空间包括第一类型内存区域和第二类型内存区域时,按照第二程序的第二类型内存区域、第一程序的第二类型内存区域、第二程序的第一类型内存区域、第一程序的第一类型内存区域的顺序回收内存,具体的,可以参见图17A所示实施例,在此不再赘述。当第一程序的内存空间包括第一类型内存区域,第二程序的内存空间包括第一类型内存区域和第二类型内存区域时,按照第二程序的第二类型内存区域、第二程序的第一类型内存区域、第一程序的第一类型内存区域的顺序回收内存。当第一程序的内存空间包括第二类型内存区域,第二程序的内存空间包括第一类型内存区域时,按照第一程序的第二类型内存区域、第二程序的第一类型内存区域的顺序回收内存,以此类推。
在一种可能的实现方式中,电子设备100设置有M种业务类型,电子设备100可以在内存中划分M个内存区域,每一个内存区域用于存放一种业务类型的所有线程的数据。当电子设备100接收到线程的内存分配请求时,可以确定出线程的业务类型,并将该业务类型对应的内存区域的一部分内存分配给该线程。当内存资源不足时,电子设备100可以基于M种业务类型的优先级顺序,依次回收对应的内存区域的内存。其中,M种业务类型的优先级可以为设置的,示例性的,如图10A所示。需要说明的是,一个内存区域中可以存放所有应用程序的相同业务类型的线程的数据。
在一些实施例中,电子设备100可以在接收到线程的内存分配请求时,再分配内存区域。 也就是说,当电子设备100接收到线程的内存分配请求时,首先确定线程的业务类型。若内存中包括该线程的业务类型对应的内存区域,将该内存区域的内存分配给线程。若内存中不包括该线程的业务类型对应的内存区域,先在内存中划分一块用于存放该线程的业务类型的所有线程的数据的内存区域,再将该内存区域的内存分配给线程。
在一些实施例中,电子设备100可以为前台运行的应用程序分配M个内存区域,为后台运行的应用程序也分配M个内存区域。前台运行的应用程序的内存回收和后台运行的应用程序的内存回收互不干扰。电子设备100可以优先回收后台运行的应用程序的内存。
以上所述,以上实施例仅用以说明本申请的技术方案,而非对其限制;尽管参照前述实施例对本申请进行了详细的说明,本领域的普通技术人员应当理解:其依然可以对前述各实施例所记载的技术方案进行修改,或者对其中部分技术特征进行等同替换;而这些修改或者替换,并不使相应技术方案的本质脱离本申请各实施例技术方案的范围。

Claims (14)

  1. 一种内存管理方法,应用于电子设备,其特征在于,
    所述电子设备分配有多种业务类型的内存区域,所述多种业务类型的内存区域至少包括第一业务类型的内存区域和第二业务类型的内存区域;
    所述第一业务类型的内存区域用于存放业务类型为所述第一业务类型的线程的数据,所述第二业务类型的内存区域用于存放业务类型为所述第二业务类型的线程的数据;
    所述第二业务类型的优先级低于所述第一业务类型的优先级;
    所述方法包括:
    在进行内存回收时,按照内存区域对应的业务类型优先级的高低顺序,先回收所述第二业务类型的内存区域的内存;
    在回收完所述第二业务类型的内存区域的内存之后,回收所述第一业务类型的内存区域的内存。
  2. 根据权利要求1所述的方法,其特征在于,所述方法还包括:
    接收到第一程序的第一线程的内存分配请求;
    基于所述第一线程的业务类型,给所述第一线程分配第一业务类型的内存区域的内存,所述第一业务类型的内存区域在所述第一程序的内存空间,所述第一业务类型的内存区域用于存放所述第一程序的业务类型为第一业务类型的线程的数据,所述第一线程的业务类型为所述第一业务类型。
  3. 根据权利要求1所述的方法,其特征在于,所述方法还包括:
    接收到第一程序的第一线程的内存分配请求;
    基于所述第一线程的业务类型,在所述第一程序的内存空间不包括所述第一业务类型的内存区域的情况下,在所述第一程序的内存空间划分出所述第一业务类型的内存区域;
    将所述第一业务类型的内存区域的内存分配给所述第一线程。
  4. 根据权利要求3所述的方法,其特征在于,在所述第一程序的内存空间划分出所述第一业务类型的内存区域时,所述方法还包括:
    使用所述第一业务类型,标记所述第一业务类型的内存区域。
  5. 根据权利要求1-4中任一项所述的方法,其特征在于,所述第一业务类型、所述第二业务类型为非关键业务类型,显示业务类型,交互业务类型,堆区业务类型,推送业务类型中的任一个;
    所述业务类型的优先级从低到高的顺序依次为:非关键业务类型,显示业务类型,交互业务类型,堆区业务类型,推送业务类型。
  6. 根据权利要求1-4中任一项所述的方法,其特征在于,所述第一业务类型、所述第二业务类型为非关键业务类型,显示业务类型,交互业务类型,堆区业务类型,推送业务类型中的任一个;
    若第一程序为前台运行的应用程序,则所述业务类型的优先级从低到高的顺序依次为:非关键业务类型,堆区业务类型,推送业务类型,显示业务类型,交互业务类型;
    若第一程序为后台运行的应用程序,则所述业务类型的优先级从低到高的顺序依次为:非关键业务类型,显示业务类型,交互业务类型,堆区业务类型,推送业务类型。
  7. 根据权利要求1-6中任一项所述的方法,其特征在于,
    在第一程序的内存空间包括第一业务类型的内存区域和第二业务类型的内存区域,第二程序的内存空间也包括第一业务类型的内存区域和第二业务类型的内存区域的情况下,
    所述在回收完所述第二业务类型的内存区域的内存之后,回收所述第一业务类型的内存区域的内存,具体包括:
    在回收完所述第一程序的所述第二业务类型的内存区域和所述第二程序的所述第二业务类型的内存区域的内存后,再回收所述第一程序的所述第一业务类型的内存区域和所述第二程序的所述第一业务类型的内存区域的内存。
  8. 根据权利要求1-7中任一项所述的方法,其特征在于,
    在第一程序的内存空间包括第一业务类型的内存区域,第二程序的内存空间也包括第一业务类型的内存区域,且所述第二程序的优先级低于所述第一程序的优先级的情况下,
    所述回收第一业务类型的内存区域的内存,具体包括:
    先回收所述第二程序的所述第一业务类型的内存区域的内存;
    在回收完所述第二程序的所述第一业务类型的内存区域的内存之后,再回收所述第一程序的所述第一业务类型的内存区域的内存。
  9. 根据权利要求8所述的方法,其特征在于,在所述回收所述第二程序的所述第一业务类型的内存区域的内存之前,所述方法还包括:
    基于所述第二程序和所述第一程序的使用频度确定所述第二程序和所述第一程序的优先级,所述第二程序的使用频度低于所述第一程序的使用频度。
  10. 根据权利要求1-9中任一项所述的方法,其特征在于,基于最近最少使用LRU算法回收所述第一业务类型的内存区域的内存。
  11. 根据权利要求1-10中任一项所述的方法,其特征在于,所述方法还包括:
    在基于第一线程的业务类型,给所述第一线程分配第一业务类型的内存区域的内存之前,基于所述第一线程的标识和/或应用场景,确定所述第一线程的业务类型。
  12. 一种内存管理方法,应用于电子设备,其特征在于,包括:
    接收到第一程序的第一线程的内存分配请求;
    基于所述第一线程的业务类型,给所述第一线程分配第一类型内存区域的内存,所述第一类型内存区域在所述第一程序的内存空间,所述第一类型内存区域用于存放业务类型为第一类型的线程的数据,所述第一线程的业务类型为所述第一业务类型;
    在进行内存回收时,基于所述业务类型的优先级,依次回收不同业务类型对应的内存区域的内存;其中,一种类型的内存区域用于存放一种业务类型的线程的数据,所述不同业务类型对应的内存区域包括所述第一类型内存区域。
  13. 一种电子设备,其特征在于,包括:一个或多个处理器、一个或多个存储器;其中,所述一个或多个存储器与所述一个或多个处理器耦合,所述一个或多个存储器用于存储计算机程序代码,所述计算机程序代码包括计算机指令,当所述一个或多个处理器在执行所述计算机指令时,使得所述电子设备执行如权利要求1至11中任一项所述的方法。
  14. 一种计算机可读存储介质,包括指令,其特征在于,当所述指令在电子设备上运行时,使得所述电子设备执行如权利要求1至11中任一项所述的方法。
PCT/CN2023/096330 2022-05-28 2023-05-25 一种内存管理方法及相关装置 WO2023231900A1 (zh)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN202210592530.4 2022-05-28
CN202210592530.4A CN117170856A (zh) 2022-05-28 2022-05-28 一种内存管理方法及相关装置

Publications (1)

Publication Number Publication Date
WO2023231900A1 true WO2023231900A1 (zh) 2023-12-07

Family

ID=88945596

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2023/096330 WO2023231900A1 (zh) 2022-05-28 2023-05-25 一种内存管理方法及相关装置

Country Status (2)

Country Link
CN (1) CN117170856A (zh)
WO (1) WO2023231900A1 (zh)

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111831440A (zh) * 2020-07-01 2020-10-27 Oppo广东移动通信有限公司 内存回收方法、装置、存储介质及电子设备
CN111831441A (zh) * 2020-07-01 2020-10-27 Oppo广东移动通信有限公司 内存回收方法、装置、存储介质及电子设备
US20210064371A1 (en) * 2019-08-26 2021-03-04 Arm Limited Method and Apparatus for Application Thread Prioritization

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20210064371A1 (en) * 2019-08-26 2021-03-04 Arm Limited Method and Apparatus for Application Thread Prioritization
CN111831440A (zh) * 2020-07-01 2020-10-27 Oppo广东移动通信有限公司 内存回收方法、装置、存储介质及电子设备
CN111831441A (zh) * 2020-07-01 2020-10-27 Oppo广东移动通信有限公司 内存回收方法、装置、存储介质及电子设备

Also Published As

Publication number Publication date
CN117170856A (zh) 2023-12-05

Similar Documents

Publication Publication Date Title
CN114443277A (zh) 内存管理方法、装置、电子设备以及计算机可读存储介质
WO2022257748A1 (zh) 虚拟内存管理方法和电子设备
CN113553130B (zh) 应用执行绘制操作的方法及电子设备
CN112817736B (zh) 一种内存的管理方法及电子设备
US20230385112A1 (en) Memory Management Method, Electronic Device, and Computer-Readable Storage Medium
CN116048933B (zh) 一种流畅度检测方法
CN117130541B (zh) 存储空间配置方法及相关设备
WO2023231900A1 (zh) 一种内存管理方法及相关装置
CN114489471B (zh) 一种输入输出处理方法和电子设备
CN115729684B (zh) 输入输出请求处理方法和电子设备
CN112783418B (zh) 一种存储应用程序数据的方法及移动终端
CN114443109A (zh) 补丁修复方法、电子设备及存储介质
CN114461589A (zh) 读取压缩文件的方法、文件系统及电子设备
CN116795476B (zh) 一种删除壁纸的方法及电子设备
CN116627855B (zh) 内存处理方法及相关装置
WO2024045841A1 (zh) 存储的方法、装置和电子设备
WO2024007944A1 (zh) 扩展内存隔离域的方法和电子设备
WO2022143891A1 (zh) 焦点同步方法及电子设备
CN114816028A (zh) 屏幕刷新方法、电子设备和计算机可读存储介质
CN117806745A (zh) 界面生成方法及电子设备
CN116860429A (zh) 内存管理方法及电子设备
CN117687770A (zh) 内存申请方法及相关装置
CN115543326A (zh) 一种应用程序的运行方法和装置
CN117931465A (zh) 一种数据传输方法及相关装置
CN114385282A (zh) 主题色变换方法、电子设备以及计算机可读存储介质

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: 23815083

Country of ref document: EP

Kind code of ref document: A1