Method of managing memory and electronic device
By dynamically managing thread-local cache and free memory pages based on process attributes, the problem of low memory allocation efficiency in electronic devices is solved, improving memory utilization and user experience.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-08-09
- Publication Date
- 2026-03-20
AI Technical Summary
In the prior art, when electronic devices allocate memory for application threads, they fail to reclaim free memory in a timely manner, which affects the running speed of threads that request memory later and results in low memory utilization.
Based on the foreground and background attributes of the process, the system dynamically manages the allocation of thread-local caching and free memory pages, ensuring that physical memory is preferentially allocated to critical threads in the foreground process and that free memory in the background process is reclaimed, thereby reducing idle memory usage.
It improves memory utilization and allocation efficiency, thereby enhancing the operating speed of electronic devices and the user experience.
Smart Images

Figure CN117632446B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the field of information technology, in particular, to a memory management method and an electronic device. BACKGROUND
[0002] An operating system usually divides physical memory into fixed-size memory page frames (such as 4K or 16K), when a user-mode process accesses memory that is not actually allocated, a page fault interrupt is generated, and then the operating system allocates an actual physical memory page frame for the process; when a user-mode process does not need a block of memory (the size of which is an integer multiple of a memory page frame), the system call can be used to return the block of memory to the operating system.
[0003] When an electronic device allocates memory to threads of an application program, the memory page frames in an idle state are not recycled in time. Invalid occupation of memory by threads that apply for memory earlier makes it more difficult for threads of an application program that apply for memory later to be allocated sufficient memory, and thus the running speed of the threads of the application program that apply for memory later is affected. How to improve the method of the electronic device for allocating memory to improve the utilization rate of memory is a problem to be solved. SUMMARY
[0004] The present application provides a memory management method and an electronic device, which determines whether to enable a thread local cache function for a thread included in a process according to foreground and background attributes of the process, which is beneficial to allocating physical memory to actually active threads to improve the running speed of the threads, which is beneficial to improving the utilization rate of memory, reducing the situation of empty occupation of memory, and improving the use experience of users of the electronic device.
[0005] In a first aspect, a memory management method is provided, including: an electronic device obtaining a first attribute of a target process, the first attribute being used to indicate that the target process is a foreground process or a background process, the target process including a target thread; and the electronic device determining to enable or disable a thread local cache function of the target thread according to the first attribute.
[0006] In a possible implementation, when the first attribute indicates that the target process is a foreground process, the electronic device determines to enable the thread local cache function of the target thread, and when the first attribute indicates that the target process is a background process, the electronic device determines to disable the thread local cache function of the target thread.
[0007] It should be noted that when the thread local cache (tcache) function of the target thread is in an open state, the electronic device no longer repeatedly opens the tcache function in the case of determining to open the tcache function for the target thread. Similarly, when the tcache function of the target thread is in an open state, the electronic device no longer repeatedly closes the tcache function in the case of determining to close the tcache function for the target thread.
[0008] In the technical solution, the electronic device determines whether to open the tcache function for the thread contained by the process according to the foreground and background attributes of the process. Compared with the memory allocation method in which the tcache function is opened by default for all threads, the memory allocation method provided by the technical solution first distinguishes different threads, which is beneficial to allocate limited physical memory to specific threads, to a certain extent, is beneficial to improve the utilization rate of memory, to a certain extent, is beneficial to reduce the empty occupation of physical memory, and is beneficial to improve the use experience of the user of the electronic device.
[0009] In combination with the first aspect, in some implementations of the first aspect, in the case where the first attribute indicates that the target process is a foreground process, the electronic device determines to open the thread local cache function of the target thread in combination with a second attribute of the target thread, the second attribute being used to indicate that the target thread is a critical thread, and the critical thread being a thread related to user interaction; and the electronic device opens the thread local cache function of the target thread.
[0010] In the technical solution, the electronic device only opens the tcache function for the thread related to user interaction in the foreground process, more physical memory can be used by these threads related to user interaction, which is beneficial to improve the operation speed of these threads, and less physical memory will be empty occupied. The implementation of the technical solution is beneficial to directly improve the use experience of the user of the electronic device.
[0011] In combination with the first aspect, in some implementations of the first aspect, the critical thread includes one or more of the following: a thread receiving a user input event, a user interface thread for drawing an interface, and a rendering thread for rendering a picture.
[0012] For the thread related to user input, the thread related to user interface drawing, and the thread for rendering a picture in the foreground process, the tcache function can be opened. In the technical solution, the electronic device can only open the tcache function for these threads of the determined type, which can to a certain extent realize the directional priority allocation of physical memory, and is beneficial to realize the customization of the electronic device. For example, for an electronic device running a game, by the technical solution, the memory consumption of other threads and the memory resource competition with the critical thread can be reduced on the premise of guaranteeing the operation speed of the game-related thread, which is beneficial to improve the use experience of the user of the electronic device in this scenario.
[0013] In a possible implementation, the electronic device allocates memory for the target thread from a thread local cache of the target thread.
[0014] In a possible implementation, in a case where the electronic device is unable to allocate memory for the target thread from a thread local cache of the target thread, the electronic device allocates memory for the target thread from a global memory.
[0015] With reference to the first aspect, in some implementations of the first aspect, before the electronic device determines, according to the first attribute, whether to turn on or turn off the thread local cache function of the target thread, in response to a first operation of the user, the electronic device sets the first attribute to indicate that the target process is a foreground process.
[0016] In the technical solution, the electronic device can determine the types of different processes running on the electronic device in response to an operation of the user, and determine which processes are foreground processes, so that the electronic device can dynamically turn on the tcache function for different threads and dynamically allocate physical memory for different threads in response to an operation of the user, which realizes more reasonable memory allocation for different threads and is beneficial to improving the overall use experience of the user on the electronic device.
[0017] With reference to the first aspect, in some implementations of the first aspect, in response to a second operation of the user, the electronic device sets the first attribute to indicate that the target process is a background process.
[0018] In the technical solution, the electronic device can set a foreground process to a background process in response to an operation of the user, so that the electronic device can further determine a method of allocating memory for threads contained in a target process after changing the foreground and background attributes according to an operation of the user, which is beneficial to improving the response efficiency of the electronic device to the operation of the user and is beneficial to improving the use experience of the user on the electronic device.
[0019] With reference to the first aspect, in some implementations of the first aspect, in a case where the first attribute indicates that the target process is a background process, the electronic device determines, according to the first attribute, to turn off the thread local cache function of the target thread, and the electronic device turns off the thread local cache function of the target thread.
[0020] In the technical solution, in the case that the first attribute indicates that the target process is a background process, the electronic device closes the tcache function of the target thread contained in the target process according to the first attribute, so that the target thread no longer applies for memory from the global memory through the tcache, which is beneficial to the electronic device to allocate the physical memory to threads other than the threads contained in the background process, beneficial to improve the utilization rate of the physical memory by different threads on the electronic device, improve the allocation efficiency of the physical memory by the electronic device, and improve the use experience of the user of the electronic device.
[0021] In combination with the first aspect, in some implementations of the first aspect, the electronic device recycles thread local cache of the target thread.
[0022] In the technical solution, for the target thread under the background process, the electronic device can recycle the tcache used by the target thread, which is beneficial to reduce the empty occupation of the physical memory by the target thread, beneficial to allocate more physical memory to threads other than the threads contained in the background process, beneficial to improve the utilization rate of the physical memory by different threads on the electronic device, improve the allocation efficiency of the physical memory by the electronic device, and improve the use experience of the user of the electronic device.
[0023] In combination with the first aspect, in some implementations of the first aspect, the electronic device recycles the idle memory page of the slice unit of the target process.
[0024] In the technical solution, the electronic device can further recycle the idle memory page of the slice unit used by the background process. Compared with the method of recycling all idle memory pages in the slice cache, the technical solution can realize the timely recycling of the idle memory page, so that the recycled physical memory can be allocated to other threads for use, which is beneficial to reduce the empty occupation of the physical memory, beneficial to improve the utilization rate of the physical memory, also beneficial to improve the allocation efficiency of the physical memory by the electronic device, and improve the use experience of the user of the electronic device.
[0025] Secondly, a method for managing memory is provided. An electronic device obtains a first attribute of a target process, the first attribute being used to indicate whether the target process is a foreground process or a background process. The electronic device determines whether to recycle an idle memory page of a slice unit of the target process according to the first attribute.
[0026] In the technical solution, the electronic device can determine whether to recycle the idle memory page used by the process according to the foreground and background attributes of the process. Compared with the method of recycling all idle memory pages in the slice unit, the technical solution can improve the recycling efficiency of the electronic device for the idle memory page, beneficial to allocate the recycled physical memory to other threads for use, beneficial to reduce the empty occupation of the physical memory, beneficial to improve the utilization rate of the physical memory, also beneficial to improve the allocation efficiency of the physical memory by the electronic device, and improve the use experience of the user of the electronic device.
[0027] In some implementations of the second aspect, in a case where the first attribute indicates that the target process is a background process, the electronic device determines to reclaim idle memory pages of a slice unit of the target process, and the electronic device reclaims the idle memory pages of the slice unit of the target process.
[0028] In the technical solution, the idle memory pages of the slice unit used by the background process are reclaimed, and the reclaimed physical memory can be allocated to the foreground process, which helps to improve the running speed of the foreground process, reduces the occupation of the idle memory pages by the background process, improves the use rate of the physical memory, and improves the allocation efficiency of the electronic device for the physical memory and the user experience.
[0029] In some implementations of the second aspect, the target process includes a target thread, and the electronic device closes a thread local cache function of the target thread.
[0030] In the technical solution, the thread local cache (tcache) function of the thread of the background process is closed, so that the target thread no longer applies for memory from the global memory through the tcache, which helps to allocate the physical memory to threads other than the threads included in the background process, improves the utilization rate of the physical memory by different threads on the electronic device, improves the allocation efficiency of the electronic device for the physical memory, and improves the user experience.
[0031] In some implementations of the second aspect, the electronic device reclaims a thread local cache of the target thread.
[0032] In the technical solution, the tcache used by the target thread of the background process is reclaimed, which helps to reduce the occupation of the physical memory by the target thread, allocate more physical memory to threads other than the threads included in the background process, improve the utilization rate of the physical memory by different threads on the electronic device, improve the allocation efficiency of the electronic device for the physical memory, and improve the user experience.
[0033] In a third aspect, an electronic device is provided, including a processor and a memory, the memory storing one or more computer programs including instructions, when the instructions are executed by the processor, the processor is configured to: obtain a first attribute of a target process, the first attribute being used to indicate that the target process is a foreground process or a background process, the target process including a target thread; and the processor is further configured to: determine to open or close a thread local cache function of the target thread according to the first attribute.
[0034] In some implementations of the third aspect, in a case where the first attribute indicates that the target process is a foreground process, the processor is further configured to determine, in combination with a second attribute of the target thread, to enable the thread-local cache function of the target thread, the second attribute being used to indicate that the target thread is a critical thread, the critical thread being a thread related to user interaction; and the processor is further configured to enable the thread-local cache function of the target thread.
[0035] In some implementations of the third aspect, the critical thread includes one or more of: a thread that receives a user input event, a user interface thread that draws an interface, and a rendering thread that renders a frame.
[0036] In some implementations of the third aspect, before the processor determines, according to the first attribute, to enable or disable the thread-local cache function of the target thread, the processor is further configured to, in response to a first operation of a user, set the first attribute to indicate that the target process is a foreground process.
[0037] In some implementations of the third aspect, in a case where the first attribute indicates that the target process is a background process, the processor is further configured to, in response to a second operation of a user, set the first attribute to indicate that the target process is a background process.
[0038] In some implementations of the third aspect, the processor is further configured to determine, according to the first attribute, to disable the thread-local cache function of the target thread, and the processor is further configured to disable the thread-local cache function of the target thread.
[0039] In some implementations of the third aspect, the processor is further configured to reclaim a tcache of the target thread.
[0040] In some implementations of the third aspect, the processor is further configured to reclaim an idle memory page of a tile cache of the target process.
[0041] A fourth aspect provides an electronic device, including a processor and a memory, the memory storing one or more computer programs including instructions, when the instructions are executed by the processor, the processor is configured to: obtain a first attribute of a target process, the first attribute being used to indicate that the target process is a foreground process or a background process; and determine, according to the first attribute, whether to reclaim an idle memory page of a tile unit of the target process.
[0042] In conjunction with the fourth aspect, in some implementations of the fourth aspect, when the first attribute indicates that the target process is a background process, the processor is further configured to: determine the free memory pages of the slice unit of the target process to be reclaimed, and the processor is further configured to: reclaim the free memory pages of the slice unit of the target process.
[0043] In conjunction with the fourth aspect, in some implementations of the fourth aspect, the processor is also used to: disable the thread-local cache function of the target thread.
[0044] In conjunction with the fourth aspect, in some implementations of the fourth aspect, the processor is also used to: reclaim the thread-local cache of the target thread.
[0045] Fifthly, an apparatus for managing memory is provided, including an acquisition unit and a processing unit. The acquisition unit is configured to: acquire a first attribute of a target process, the first attribute indicating whether the target process is a foreground process or a background process, the target process including a target thread; the processing unit is configured to: determine whether to enable or disable the thread-local caching function of the target thread based on the first attribute.
[0046] In conjunction with the fifth aspect, in some implementations of the fifth aspect, when the first attribute indicates that the target process is a foreground process, the processing unit is further configured to: determine, based on the second attribute of the target thread, enable the thread-local caching function of the target thread, the second attribute being used to indicate that the target thread is a critical thread, the critical thread being a thread related to user interaction; the processing unit is further configured to: enable the thread-local caching function of the target thread.
[0047] In conjunction with the fifth aspect, in some implementations of the fifth aspect, the key thread includes one or more of the following: a thread for receiving user input events, a user interface thread for drawing the interface, and a rendering thread for rendering the screen.
[0048] In conjunction with the fifth aspect, in some implementations of the fifth aspect, before the electronic device determines whether to enable or disable the thread-local caching function of the target thread based on the first attribute, the processing unit is further configured to: in response to a first operation by the user, set the first attribute to indicate that the target process is a foreground process.
[0049] In conjunction with the fifth aspect, in some implementations of the fifth aspect, the processing unit is further configured to: in response to a second user operation, set the first attribute to indicate that the target process is a background process.
[0050] In conjunction with the fifth aspect, in some implementations of the fifth aspect, when the first attribute indicates that the target process is a background process, the processing unit is further configured to: determine, based on the first attribute, to disable the thread-local cache function of the target thread; the processing unit is further configured to: disable the thread-local cache function of the target thread.
[0051] With reference to the fifth aspect, in some implementations of the fifth aspect, the processing unit is further configured to reclaim thread local cache of the target thread.
[0052] With reference to the fifth aspect, in some implementations of the fifth aspect, the processing unit is further configured to reclaim free memory pages of the tile unit of the target process.
[0053] A sixth aspect provides an apparatus for managing memory, comprising an obtaining unit and a processing unit, the obtaining unit is configured to obtain a first attribute of a target process, the first attribute is used to indicate whether the target process is a foreground process or a background process, and the processing unit is configured to determine whether to reclaim free memory pages of a tile unit of the target process according to the first attribute.
[0054] With reference to the sixth aspect, in some implementations of the sixth aspect, in a case where the first attribute indicates that the target process is a background process, the processing unit is further configured to determine to reclaim the free memory pages of the tile unit of the target process, and the processing unit is further configured to reclaim the free memory pages of the tile unit of the target process.
[0055] With reference to the sixth aspect, in some implementations of the sixth aspect, the target process comprises a target thread, and the processing unit is further configured to close a thread local cache function of the target thread.
[0056] With reference to the sixth aspect, in some implementations of the sixth aspect, the processing unit is further configured to reclaim thread local cache of the target thread.
[0057] A seventh aspect provides a computer program product comprising computer program code which, when run on a computer, causes the method of the first aspect or any possible implementation thereof to be performed.
[0058] An eighth aspect provides a computer program product comprising computer program code which, when run on a computer, causes the method of the second aspect or any possible implementation thereof to be performed.
[0059] A ninth aspect provides a computer-readable storage medium having computer instructions stored therein, which, when run on a computer, causes the method of the first aspect or any possible implementation thereof to be performed.
[0060] A tenth aspect provides a computer-readable storage medium having computer instructions stored therein, which, when run on a computer, causes the method of the second aspect or any possible implementation thereof to be performed.
[0061] In an eleventh aspect, a chip is provided, comprising a processor configured to read instructions stored in a memory, and when the processor executes the instructions, the chip is caused to implement the method in the first aspect or any possible implementation manner thereof.
[0062] In a twelfth aspect, a chip is provided, comprising a processor configured to read instructions stored in a memory, and when the processor executes the instructions, the chip is caused to implement the method in the second aspect or any possible implementation manner thereof. BRIEF DESCRIPTION OF DRAWINGS
[0063] Figure 1 Fig. 1 is a hardware architecture schematic diagram of an electronic device provided by an embodiment of the present application.
[0064] Figure 2 Fig. 2 is a software architecture schematic diagram of an electronic device provided by an embodiment of the present application.
[0065] Figure 3 Fig. 3 is an applicable scenario of a memory management method provided by an embodiment of the present application.
[0066] Figure 4 Fig. 4 is a memory management method schematic diagram provided by an embodiment of the present application.
[0067] Figure 5 Fig. 5 is another memory management method schematic diagram provided by an embodiment of the present application.
[0068] Figure 6 Fig. 6 is yet another memory management method schematic diagram provided by an embodiment of the present application.
[0069] Figure 7 Fig. 7 is yet another memory management method schematic diagram provided by an embodiment of the present application.
[0070] Figure 8 Fig. 8 is yet another memory management method schematic diagram provided by an embodiment of the present application.
[0071] Figure 9 Fig. 9 is an electronic device schematic diagram provided by an embodiment of the present application.
[0072] Figure 10 Fig. 10 is a memory management device schematic diagram provided by an embodiment of the present application. DETAILED DESCRIPTION
[0073] The technical solutions in the present application will be described below with reference to the accompanying drawings.
[0074] The terminology used in the following description merely for the purpose of describing particular embodiments and is not intended to limit the application. As used in this description and the accompanying claims, the singular forms "a," "an" and "the" are intended to include the plural forms as well, unless the context clearly indicates otherwise. It will also be understood that the term "and / or" as used herein refers to and encompasses any and all possible combinations of one or more of the associated listed items. The term "or" as used herein is used to denote exclusive, at least, or inclusive, at least, senses, depending on the context in which it is used. The term "based on" is used to denote the established cause and effect relationship between the two events and does not necessarily indicate a direct causal relationship unless explicitly stated to be a direct causal relationship. The term "coupled" as used herein refers to any direct or indirect communication between two or more elements, and means they can be coupled through some interface, device, or intermediate element, which can induce some loss of original information. The term "connected" as used herein refers to a situation where two or more elements are coupled through some interface, device, or intermediate element, which does not induce any loss of original information.
[0075] Reference throughout this specification to "one embodiment" or "an embodiment" means that a particular feature, structure, or characteristic described in connection with the embodiment is included in at least one embodiment of the application. Thus, the appearances of the phrases "in one embodiment" or "in an embodiment" or "in other embodiments" or "in still other embodiments" or "in yet other embodiments" in various places throughout this specification are not necessarily all referring to the same embodiment, unless otherwise are specifically stated to be so. The terms "including," "containing," "having," and "including" and variations thereof do not exclude the presence of additional items in addition to those listed in the specific description.
[0076] The method provided by the embodiments of the present application can be applied to electronic devices such as mobile phones, tablet computers, wearable devices, vehicle-mounted devices, augmented reality (AR) / virtual reality (VR) devices, notebook computers, ultra-mobile personal computers (UMPCs), netbooks, personal digital assistants (PDAs), and the like. The method provided by the embodiments of the present application can also be applied to network devices such as servers in communication with the above electronic devices. The embodiments of the present application do not make any limitation on the specific type of electronic devices.
[0077] Exemplary, Figure 1A structural diagram of the electronic device 100 is shown. The electronic device 100 can include a processor 110, an external memory interface 120, an internal memory 121, a universal serial bus (USB) interface 130, a charging management module 140, a power management module 141, a battery 142, an antenna 1, an antenna 2, a mobile communication module 150, a wireless communication module 160, an audio module 170, a speaker 170A, a receiver 170B, a microphone 170C, a headset interface 170D, a sensor module 180, a key 190, a motor 191, an indicator 192, a camera 193, a display screen 194, and a subscriber identification module (SIM) card interface 195, etc. The sensor module 180 can include a pressure sensor 180A, a gyroscope sensor 180B, a barometric 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, an ambient light sensor 180L, a bone conduction sensor 180M, etc.
[0078] It can be understood that the structure shown in the embodiments of the present application does not constitute a specific limitation on the electronic device 100. In other embodiments of the present application, the electronic device 100 can include more or fewer components than shown, or combine certain components, or split certain components, or different component arrangements. The components shown can be implemented in hardware, software, or a combination of software and hardware.
[0079] The processor 110 can include one or more processing units, for example: the processor 110 can include an application processor (AP), a modem processor, a graphics processing unit (GPU), an image signal processor (ISP), a controller, a memory, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural-network processing unit (NPU), etc. Different processing units can be independent devices, or can be integrated into one or more processors.
[0080] The controller can be the nerve center and command center of the electronic device 100. The controller can generate operation control signals according to instruction operation codes and timing signals, and complete the control of fetching instructions and executing instructions.
[0081] The processor 110 can also have a memory that stores instructions and data. In some embodiments, the memory in the processor 110 is a cache memory. The memory can hold instructions or data that the processor 110 has just used or is using repeatedly. If the processor 110 needs to use the instructions or data again, it can call them directly from the memory. This avoids repeated access and reduces the latency of the processor 110, thus improving the efficiency of the system.
[0082] In some embodiments, the processor 110 can include one or more interfaces. The interfaces can include an inter-integrated circuit (I2C) interface, an inter-integrated circuit sound (I2S) interface, a pulse code modulation (PCM) interface, a universal asynchronous receiver / transmitter (UART) interface, a mobile industry processor interface (MIPI), a general-purpose input / output (GPIO) interface, a subscriber identity module (SIM) interface, and / or a universal serial bus (USB) interface, etc.
[0083] The I2C interface is a bidirectional synchronous serial bus that includes a serial data line (SDA) and a serial clock line (SCL). In some embodiments, the processor 110 can include multiple sets of I2C buses. The processor 110 can be coupled to the touch sensor 180K, the charger, the flash, the camera 193, etc. through different I2C bus interfaces. For example, the processor 110 can be coupled to the touch sensor 180K through an I2C interface, so that the processor 110 and the touch sensor 180K communicate through the I2C bus interface to realize the touch function of the electronic device 100.
[0084] The I2S interface can be used for audio communication. In some embodiments, the processor 110 can include multiple sets of I2S buses. The processor 110 can be coupled with the audio module 170 through the I2S buses to enable communication between the processor 110 and the audio module 170. In some embodiments, the audio module 170 can deliver audio signals to the wireless communication module 160 through the I2S interface to enable the function of answering a phone call through a Bluetooth earphone.
[0085] The PCM interface can also be used for audio communication to sample, quantize, and encode analog signals. In some embodiments, the audio module 170 can be coupled with the wireless communication module 160 through a PCM bus interface. In some embodiments, the audio module 170 can also deliver audio signals to the wireless communication module 160 through the PCM interface to enable the function of playing music through a Bluetooth earphone. Both the I2S interface and the PCM interface can be used for audio communication.
[0086] The UART interface is a universal serial bus for asynchronous communication. The bus can be a bidirectional communication bus. It converts data to be transmitted between serial communication and parallel communication. In some embodiments, the UART interface is usually used to connect the processor 110 and the wireless communication module 160. For example, the processor 110 communicates with the Bluetooth module in the wireless communication module 160 through the UART interface to enable Bluetooth functionality. In some embodiments, the audio module 170 can deliver audio signals to the wireless communication module 160 through the UART interface to enable the function of playing music through a Bluetooth earphone.
[0087] The MIPI interface can be used to connect the processor 110 and peripheral devices such as the display screen 194 and the camera 193. The MIPI interface includes a camera serial interface (CSI), a display serial interface (DSI), and the like. In some embodiments, the processor 110 and the camera 193 communicate through the CSI interface to enable the camera function of the electronic device 100. The processor 110 and the display screen 194 communicate through the DSI interface to enable the display function of the electronic device 100.
[0088] The GPIO interface can be configured through software. The GPIO interface can be configured as a control signal or as a data signal. In some embodiments, the GPIO interface can be used to connect the processor 110 and the camera 193, the display screen 194, the wireless communication module 160, the audio module 170, the sensor module 180, and the like. The GPIO interface can also be configured as an I2C interface, an I2S interface, a UART interface, a MIPI interface, and the like.
[0089] The USB interface 130 is an interface conforming to the USB standard specification, and can be a Mini USB interface, a Micro USB interface, a USB Type C interface, etc. The USB interface 130 can be used to connect a charger to charge the electronic device 100, and can also be used to transmit data between the electronic device 100 and a peripheral device. It can also be used to connect a headset to play audio through the headset. The interface can also be used to connect other electronic devices, such as AR devices, etc.
[0090] It can be understood that the interface connection relationship between the modules shown in the embodiments of the present application is only illustrative and does not constitute a structural limitation on the electronic device 100. In some other embodiments of the present application, the electronic device 100 can also use different interface connection modes or combinations of multiple interface connection modes in the above embodiments.
[0091] The charging management module 140 is used to receive charging input from a charger. The charger can be a wireless charger or a wired charger. In some wired charging embodiments, the charging management module 140 can receive charging input from a wired charger through the USB interface 130. In some wireless charging embodiments, the charging management module 140 can receive wireless charging input through the wireless charging coil of the electronic device 100. The charging management module 140 can charge the battery 142 while also supplying power to the electronic device through the power management module 141.
[0092] 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 to supply power to the processor 110, the internal memory 121, the external memory, the display screen 194, the camera 193, and the wireless communication module 160, etc. The power management module 141 can also be used to monitor parameters such as battery capacity, battery cycle count, battery health status (leakage, impedance), etc. In some other embodiments, the power management module 141 can also be disposed in the processor 110. In some other embodiments, the power management module 141 and the charging management module 140 can also be disposed in the same device.
[0093] The wireless communication function of the electronic device 100 can be realized through the antenna 1, the antenna 2, the mobile communication module 150, the wireless communication module 160, the modem processor, and the baseband processor, etc.
[0094] The antenna 1 and the antenna 2 are used to transmit and receive electromagnetic wave signals. Each antenna in the electronic device 100 can be used to cover a single or multiple communication frequency bands. Different antennas can also be multiplexed to improve the utilization rate of the antennas. For example, the antenna 1 can be multiplexed as a diversity antenna for a wireless local area network. In some other embodiments, the antennas can be used in combination with a tuning switch.
[0095] The mobile communication module 150 can provide a solution for wireless communication including 2G / 3G / 4G / 5G, etc. applied to the electronic device 100. The mobile communication module 150 can include at least one filter, a switch, a power amplifier, a low noise amplifier (LNA), etc. The mobile communication module 150 can receive electromagnetic waves by the antenna 1, and perform filtering, amplification, etc. on the received electromagnetic waves, and transfer the same to the modem processor for demodulation. The mobile communication module 150 can also amplify signals modulated by the modem processor, and radiate the same as electromagnetic waves through the antenna 1. In some embodiments, at least part of the functional modules of the mobile communication module 150 can be disposed in the processor 110. In some embodiments, at least part of the functional modules of the mobile communication module 150 can be disposed in the same device as at least part of the modules of the processor 110.
[0096] The modem processor can include a modulator and a demodulator. The modulator is configured to modulate a low-frequency baseband signal to be transmitted into a medium-high frequency signal. The demodulator is configured to demodulate a received electromagnetic wave signal into a low-frequency baseband signal. The demodulator then transmits the demodulated low-frequency baseband signal to the baseband processor for processing. The low-frequency baseband signal processed by the baseband processor is transmitted to the application processor. The application processor outputs a sound signal through an audio device (not limited to the speaker 170A, the microphone 170B, etc.), or displays an image or a video through the display screen 194. In some embodiments, the modem processor can be a separate device. In other embodiments, the modem processor can be independent of the processor 110, and disposed in the same device as the mobile communication module 150 or other functional modules.
[0097] The wireless communication module 160 can provide a solution for wireless communication including wireless local area networks (WLAN) (e.g., wireless fidelity (Wi-Fi) network), Bluetooth (BT), global navigation satellite system (GNSS), frequency modulation (FM), near field communication (NFC), infrared (IR) technology, etc. applied to the electronic device 100. The wireless communication module 160 can be one or more devices that integrate at least one communication processing module. The wireless communication module 160 receives an electromagnetic wave via the antenna 2, frequency-modulates and filters the electromagnetic wave signal, and transmits the processed signal to the processor 110. The wireless communication module 160 can also receive a signal to be transmitted from the processor 110, frequency-modulate it, amplify it, and radiate it as an electromagnetic wave via the antenna 2.
[0098] In some embodiments, the antenna 1 and the mobile communication module 150 of the electronic device 100 are coupled, and the antenna 2 and the wireless communication module 160 are coupled, so that the electronic device 100 can communicate with a network and other devices through wireless communication technology. The wireless communication technology can include global system for mobile communications (GSM), general packet radio service (GPRS), code division multiple access (CDMA), wideband code division multiple access (WCDMA), time-division code division multiple access (TD-SCDMA), long term evolution (LTE), BT, GNSS, WLAN, NFC, FM, and / or IR technology, etc. The GNSS can include a global positioning system (GPS), a global navigation satellite system (GLONASS), a beidu navigation satellite system (BDS), a quasi-zenith satellite system (QZSS), and / or a satellite based augmentation systems (SBAS).
[0099] The electronic device 100 implements a display function through a GPU, a display screen 194, and an application processor, etc. The GPU is a microprocessor for image processing, which is connected to the display screen 194 and the application processor. The GPU is used to perform mathematical and geometric calculations for graphics rendering. The processor 110 can include one or more GPUs, which execute program instructions to generate or change display information.
[0100] The display screen 194 is configured to display images, videos, and the like. The display screen 194 includes a display panel. The display panel can be a liquid crystal display (LCD), an organic light-emitting diode (OLED), an active-matrix organic light-emitting diode (AMOLED), a flex light-emitting diode (FLED), a Miniled, a MicroLed, a Micro-oLed, a quantum dot light emitting diodes (QLED), or the like. In some embodiments, the electronic device 100 can include one or N display screens 194, where N is a positive integer greater than 1.
[0101] The electronic device 100 can implement the photographing function through the ISP, the camera 193, the video codec, the GPU, the display screen 194, and the application processor.
[0102] The ISP is configured to process the data fed back by the camera 193. For example, when taking a photo, the shutter is opened, the light is transmitted to the camera photosensitive element through the lens, the light signal is converted into an electrical signal, and the camera photosensitive element transmits the electrical signal to the ISP for processing to convert it into an image visible to the naked eye. The ISP can also optimize the noise, brightness, and skin color of the image. The ISP can also optimize the exposure, color temperature, and other parameters of the shooting scene. In some embodiments, the ISP can be disposed in the camera 193.
[0103] The camera 193 is configured to capture still images or videos. An object generates an optical image through a lens and projects it onto a photosensitive element. The photosensitive element can be a charge coupled device (CCD) or a complementary metal-oxide-semiconductor (CMOS) phototransistor. The photosensitive element converts the light signal into an electrical signal, which is then transmitted to the ISP to convert it into a digital image signal. The ISP outputs the digital image signal to the DSP for processing. The DSP converts the digital image signal into an image signal in a standard RGB, YUV, or the like format. In some embodiments, the electronic device 100 can include one or N cameras 193, where N is a positive integer greater than 1.
[0104] The digital signal processor is used to process digital signals, in addition to being able to process digital image signals, it can also process other digital signals. For example, when the electronic device 100 selects a frequency point, the digital signal processor is used to perform Fourier transform on the frequency point energy, etc.
[0105] The video codec is used to compress or decompress digital video. The electronic device 100 can support one or more video codecs. In this way, the electronic device 100 can play or record videos in multiple encoding formats, such as: moving picture experts group (MPEG) 1, MPEG 2, MPEG 3, MPEG 4, etc.
[0106] The NPU is a neural-network (NN) calculation processor, which can quickly process input information by drawing on the structure of a biological neural network, such as drawing on the transmission mode between human brain neurons, and can also constantly self-learn. Through the NPU, the electronic device 100 can realize intelligent cognition applications such as image recognition, face recognition, voice recognition, text understanding, etc.
[0107] The external memory interface 120 can be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the electronic device 100. The external memory card communicates with the processor 110 through the external memory interface 120 to realize data storage functions. For example, music, video, etc. Files are saved in the external memory card.
[0108] The internal memory 121 can be used to store computer executable program codes, which include instructions. The processor 110 executes various function applications and data processing of the electronic device 100 by running the instructions stored in the internal memory 121. The internal memory 121 can include a program storage area and a data storage area. The program storage area can store an operating system, at least one application program required by a function (such as a sound playing function, an image playing function, etc.), etc. The data storage area can store data created during the use of the electronic device 100 (such as audio data, a phonebook, etc.), etc. In addition, the internal memory 121 can include a high-speed random access memory, and can also include a non-volatile memory, such as at least one magnetic disk storage device, a flash memory device, a universal flash storage (UFS), etc.
[0109] The electronic device 100 can realize audio functions through the audio module 170, the speaker 170A, the receiver 170B, the microphone 170C, the earphone interface 170D, and the application processor, etc. For example, music playing, recording, etc.
[0110] The audio module 170 is configured to convert digital audio information into an analog audio signal output, and to convert an analog audio input into a digital audio signal. The audio module 170 can also be configured to encode and decode audio signals. In some embodiments, the audio module 170 can be disposed in the processor 110, or some of the functions of the audio module 170 can be disposed in the processor 110.
[0111] The speaker 170A, also referred to as a "loudspeaker", is configured to convert an audio electrical signal into a sound signal. The electronic device 100 can listen to music or listen to a hands-free call through the speaker 170A.
[0112] The receiver 170B, also referred to as a "earpiece", is configured to convert an audio electrical signal into a sound signal. When the electronic device 100 receives a call or a voice message, the user can listen to the voice through the receiver 170B close to the ear.
[0113] The microphone 170C, also referred to as a "microphone", "sound collector", is configured to convert a sound signal into an electrical signal. When making a call or sending a voice message, the user can make a sound through the mouth close to the microphone 170C, and input the sound signal into the microphone 170C. The electronic device 100 can be provided with at least one microphone 170C. In other embodiments, the electronic device 100 can be provided with two microphones 170C, in addition to collecting sound signals, noise reduction functions can also be realized. In other embodiments, the electronic device 100 can also be provided with three, four or more microphones 170C, in addition to collecting sound signals, noise reduction, and can also identify the source of the sound, realize directional recording function, etc.
[0114] The earphone interface 170D is configured to connect a wired earphone. The earphone interface 170D can be a USB interface 130, or a 3.5mm open mobile terminal platform (OMTP) standard interface, a cellular telecommunications industry association of the USA (CTIA) standard interface.
[0115] The keys 190 include a power key, a volume key, etc. The keys 190 can be mechanical keys. They can also be touch keys. The electronic device 100 can receive key inputs and generate key signal inputs related to user settings and function control of the electronic device 100.
[0116] The motor 191 can generate a vibration prompt. The motor 191 can be used for incoming call vibration prompt, and can also be used for touch vibration feedback. For example, touch operations acting on different applications (such as taking pictures, playing audio, etc.) can correspond to different vibration feedback effects. The motor 191 can also correspond to different vibration feedback effects for touch operations acting on different regions of the display screen 194. Different application scenarios (such as time reminders, received messages, alarms, games, etc.) can also correspond to different vibration feedback effects. The touch vibration feedback effect can also support customization.
[0117] The indicator 192 can be an indicator light, which can be used to indicate a charging state, a power change, and can also be used to indicate messages, missed calls, notifications, etc.
[0118] The SIM card interface 195 is used to connect a SIM card. The SIM card can be inserted into or pulled out of the SIM card interface 195 to realize contact and separation with the electronic device 100. The electronic device 100 can support one or N SIM card interfaces, and N is a positive integer greater than 1. The SIM card interface 195 can support Nano SIM cards, Micro SIM cards, SIM cards, etc. Multiple cards can be inserted into the same SIM card interface 195 at the same time. The types of the multiple cards can be the same or different. The SIM card interface 195 can also be compatible with different types of SIM cards. The SIM card interface 195 can also be compatible with external storage cards. The electronic device 100 interacts with the network through the SIM card to realize functions such as calling and data communication. In some embodiments, the electronic device 100 uses an embedded SIM (eSIM) card, 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.
[0119] It should be understood that the phone card in the embodiments of the present application includes but is not limited to a SIM card, an eSIM card, a universal subscriber identity module (USIM), a universal integrated circuit card (UICC), etc.
[0120] The software system of the electronic device 100 can adopt a layered architecture, an event-driven architecture, a microkernel architecture, a microservice architecture, or a cloud architecture. The embodiments of the present application take an Android system with a layered architecture as an example to exemplarily illustrate the software structure of the electronic device 100.
[0121] Figure 2Figure 1 is a software structure block diagram of an electronic device 100 according to an embodiment of the present application. The layered architecture divides the software into several layers, each of which has a clear role and division of labor. Layers communicate with each other through software interfaces. In some embodiments, the Android system is divided into four layers, from top to bottom, the application layer, the application framework layer, the Android runtime and system library, and the kernel layer. The application layer can include a series of application packages.
[0122] As shown in Figure 2, the application packages can include camera, gallery, calendar, call, map, navigation, WLAN, Bluetooth, music, video, short message, etc. Figure 2
[0123] The application framework layer provides the application layer with application programming interfaces (APIs) and programming frameworks for the applications. The application framework layer includes some pre-defined functions.
[0124] As shown in Figure 3, the application framework layer can include window manager, content provider, view system, phone manager, resource manager, notification manager, etc. Figure 2
[0125] The window manager is used to manage the window program. The window manager can obtain the size of the display screen, determine whether there is a status bar, lock the screen, and take a screenshot, etc.
[0126] The content provider is used to store and obtain data, and make the data accessible to the application. The data can include video, image, audio, dialed and received calls, browsing history and bookmarks, phonebook, etc.
[0127] The view system includes visual controls, such as controls that display text, controls that display pictures, etc. The view system can be used to build an application. A display interface can be composed of one or more views. For example, a display interface that includes a short message notification icon can include a view that displays text and a view that displays a picture.
[0128] The phone manager is used to provide the communication function of the electronic device 100. For example, the management of the call state (including call connection, call hangup, etc.).
[0129] The resource manager provides various resources for the application, such as localized strings, icons, pictures, layout files, video files, etc.
[0130] The notification manager enables an application to display notification information in the status bar, which can be used to convey a message of the informing type, and can automatically disappear after a short stay without user interaction. For example, the notification manager is used to inform the completion of downloading, message reminders, etc. The notification manager can also be a notification in the form of a chart or a scroll bar text appearing in the system top status bar, such as a notification of a background running application, and can also be a notification in the form of a dialogue window appearing on the screen. For example, the status bar prompts text information, emits a prompt sound, the electronic device vibrates, the indicator light flashes, etc.
[0131] The Android runtime includes a core library and a virtual machine. The Android runtime is responsible for scheduling and managing the Android system.
[0132] The core library includes two parts: one part is the function function that the java language needs to call, and the other part is the core library of Android.
[0133] The application layer and the application framework layer run in the virtual machine. The virtual machine executes the java file of the application layer and the application framework layer into a binary file. The virtual machine is used to perform the management of the object life cycle, the management of the stack, the management of the thread, the management of the security and the exception, and the garbage collection, etc.
[0134] The system library can include multiple functional modules. For example: a surface manager, media libraries, a three-dimensional graphics processing library (for example: OpenGL ES), a 2D graphics engine (for example: SGL), etc.
[0135] The surface manager is used to manage the display subsystem, and provides a fusion of 2D and 3D layers for multiple applications.
[0136] The media library supports multiple commonly used audio, video format playback and recording, and static image files, etc. The media library can support multiple audio and video encoding formats, such as: MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, etc.
[0137] The three-dimensional graphics processing library is used to realize three-dimensional graphics drawing, image rendering, synthesis, and layer processing, etc.
[0138] The 2D graphics engine is a drawing engine for 2D drawing.
[0139] The kernel layer is a layer between hardware and software. The kernel layer at least includes a display driver, a camera driver, an audio driver, and a sensor driver.
[0140] It should be understood that the technical solutions in the embodiments of the present application can be used in Android, IOS, Harmony, etc.
[0141] Due to limited hardware and software resources of the electronic device, in order to improve the efficiency of the operation of the electronic device and improve the user experience, the hardware and software resources of the electronic device need to be reasonably allocated and scheduled, and memory management is a technology for allocating and using memory resources of the electronic device, and the main purpose is how to efficiently and quickly allocate and release and recycle memory resources at appropriate times.
[0142] The electronic device can include various memory allocators, and the following will take Jason Evans memory allocator (Jemalloc) as an example to illustrate the memory management method of the electronic device provided by the present application. It should be noted that the memory management method provided by the embodiments of the present application can also be applicable to other types of memory allocators, and the following embodiments taking Jemalloc as an example should not constitute a limitation to the present application.
[0143] Jemalloc is a memory allocator, which is between the system layer and the application layer, and it can respond to the allocation request of the user, apply for memory from the system kernel, and then return the obtained memory to the user program.
[0144] In order to improve the performance of multi-thread concurrent memory allocation and release of the application program, Jemalloc divides multiple categories of bin managers for memory applications of different sizes, and when multiple threads apply for memory of different sizes, they only need to obtain memory from the corresponding bin manager. In order to reduce the internal fragmentation of the memory page, the bin manager also increases the slab unit inside, each slab unit is composed of multiple regions of the same size, and each slab unit has a corresponding bitmap table to describe whether the internal region is idle. When memory is allocated, an idle region is found from the unused slab unit and allocated to the memory applicant, and the state of the allocated region changes to non-idle state. When the memory is released, the state of the region changes from non-idle state to idle state. The size of the slab unit should be an integer multiple of the size of the memory page (or memory page frame), and also an integer multiple of the corresponding memory application size of the bin manager, and each slab unit can be composed of 1-7 memory page frames.
[0145] Jemalloc also provides thread local cache (tcache), that is, the memory released by each thread will not be immediately returned to the Jemalloc global memory allocator, but will be cached in the thread local first, and when the thread needs to apply for memory, it can directly obtain and use it from the tcache, which can solve the lock competition problem of memory management among multiple threads, thereby further improving the concurrent performance of multi-thread memory allocation and release.
[0146] Before formally introducing the embodiments of the present application, the following definitions of terms that can be used in the embodiments are first explained:
[0147] 1. Foreground process: a process running in the foreground of an operating system, which can generally include a process that needs to interact with a user.
[0148] 2. Background process: a process running in the background of an operating system, which can be used to periodically perform a certain task or wait for processing of certain events that occur. A background process generally does not need to interact with a user.
[0149] 3. Base address: the operating system kernel manages memory in units of memory pages, usually 4K or 16K, and the memory page base address is the first address of the memory page, aligned with the size of the memory page.
[0150] Figure 3 An application scenario of the method for managing memory provided by the present application is exemplarily provided.
[0151] The electronic device displays an application program desktop, and the process for displaying the application program desktop can be referred to as a desktop process. The desktop process can include a thread (thread A) for drawing icons, a thread (thread B) for drawing folders, and other threads, etc. In the case where the electronic device displays the desktop, the desktop process is a foreground process, and the threads related to displaying a user interface (UI), such as the threads A and B described above, can be regarded as critical threads. In the case where the electronic device determines that the threads A and B are critical threads of the foreground process, the electronic device can start the tcache function for the threads A and B, respectively. Further, when the threads A and / or B apply for memory from the electronic device, the Jemalloc memory allocator can preferentially allocate memory to the threads A and / or B from the tcache.
[0152] When the user clicks on the icon of "Calculator" on the desktop, the electronic device displays the calculator interface in response to the operation of the user.
[0153] The process for drawing the calculator interface can be referred to as a calculator process, which can include a thread (thread C) for the user to draw symbols on the calculator interface, a thread (thread D) for obtaining user input information, and other threads. When the electronic device displays the calculator interface, the calculator process is a foreground process, and the relevant threads for displaying the calculator interface, such as the above-mentioned thread C and thread D, can be regarded as critical threads. The electronic device can start the tcache function for the thread C and thread D respectively when it determines that the thread C and thread D are critical threads of the foreground process. Correspondingly, when the thread C and / or thread D apply for memory from the electronic device, the Jemalloc memory allocator can preferentially allocate memory from the tcache to the thread C and / or thread D.
[0154] In the process in which the electronic device switches from the user interface of the application program desktop to the user interface of the calculator, Figure 3 In the process in which the electronic device switches from the user interface of the application program desktop to the user interface of the calculator, the foreground and background properties of the processes running on the electronic device change, that is, the above-mentioned desktop process is switched from the foreground process to the background process, and the calculator process becomes the foreground process. Since the desktop process becomes the background process, the thread A and thread B contained in the desktop process are also switched to run in the background. In this case, the electronic device can close the tcache function of the thread A and thread B, and recycle the tcache used by the thread A and thread B. The idle memory pages in the slab unit under the desktop process can also be recycled, so as to be allocated to the foreground process or other more important processes, so as to make the utilization rate of the memory pages contained in the tcache and slab unit higher, which is beneficial to improve the user experience of the electronic device.
[0155] Conversely, when the user exits from the calculator interface in Figure 3 When the user exits from the calculator interface in the application program desktop, the foreground and background properties of the processes running on the electronic device also change, that is, the above-mentioned calculator process becomes the background process, and the desktop process becomes the foreground process. When the electronic device detects the change event of the foreground and background processes, it can re-detect the current foreground process and the critical threads of the foreground process, start the tcache function for the critical threads of the foreground process, and allocate memory to the threads in response to the memory application of the threads. The electronic device can also re-detect the current background process, and close the tcache function of the threads contained in the background process, and can also recycle the idle memory pages in the slab unit occupied by the process.
[0156] It should be noted that, Figure 3 It should be noted that,
[0157] Figure 4 Fig. 1 shows a schematic diagram of a method for managing memory according to an embodiment of the present application.
[0158] In S101, the electronic device determines that the target process is a foreground process.
[0159] When the target process or the target thread applies for memory from the operating system, in response to the request for applying for memory, the electronic device can determine to allocate memory for the target thread in an optional manner according to the attribute of the target process and / or the attribute of the target thread.
[0160] In some embodiments, the target thread sends a memory application request to the electronic device, and in response to the request, the electronic device obtains a first attribute, which is used to indicate whether the process (target process) to which the target thread belongs is a foreground process or a background process.
[0161] Specifically, the Jemalloc memory allocator of the electronic device can obtain the first attribute from the operating system.
[0162] Optionally, after obtaining the first attribute, the electronic device can add identification information for identifying the first attribute to the Jemalloc instance of the target process. When the first attribute of the target process changes, the Jemalloc memory allocator can obtain the value of the first attribute from the operating system and synchronize it to the identification information, so as to be consulted in subsequent memory allocation operations.
[0163] The electronic device obtains the first attribute, which can be obtained from the operating system or from the identification information of the first attribute.
[0164] In some embodiments, the electronic device can determine whether the target process is a foreground process by obtaining the first attribute. Further, the electronic device determines whether to enable the tcache function for the target thread under the target process according to whether the target process is a foreground process. In the case where the target process is a foreground process, the electronic device enables the tcache function for the target thread contained in the target process, and in the case where the target process is not a foreground process, the electronic device does not enable the tcache function for the target thread under the target process.
[0165] In some embodiments, the electronic device can preset a first process set, and the first process set can include at least one process. The electronic device can determine whether to enable the tcache function for a target thread under a target process according to whether the target process belongs to the processes in the first process set. Specifically, in the case that the target process belongs to the first process set, the electronic device enables the tcache function for the target thread included in the target process, and in the case that the target process does not belong to the first process set, the electronic device does not enable the tcache function for the target thread included in the target process. The target process can be provided with attribute information for identifying the process, and the attribute information can indicate whether the target process belongs to the first process set. By obtaining the attribute information, the electronic device can determine whether the target process belongs to the first process set.
[0166] In some embodiments, the electronic device can preset a first process set, and the first process set can include at least one process. The electronic device can determine whether to enable the tcache function for a target thread under a target process according to whether the target process belongs to the processes in the first process set. Specifically, in the case that the target process belongs to the first process set, the electronic device enables the tcache function for the target thread included in the target process, and in the case that the target process does not belong to the first process set, the electronic device does not enable the tcache function for the target thread included in the target process. The target process can be provided with attribute information for identifying the process, and the attribute information can indicate whether the target process belongs to the first process set. By obtaining the attribute information, the electronic device can determine whether the target process belongs to the first process set.
[0167] By enabling the tcache function for the threads under the specific process, the electronic device can reduce the probability of empty occupation of the tcache caused by unnecessary user threads, thereby improving the utilization rate of the tcache by application threads, and to some extent, can realize the reasonable allocation of memory resources in the electronic device and improve the user experience of the electronic device.
[0168] Optionally, the electronic device can set the target process as a foreground process or a background process in response to the operation of the user.
[0169] S102, the electronic device determines that the target thread is a critical thread.
[0170] For the threads included in the target process, the electronic device can also determine whether to enable the tcache function for the target thread according to the attribute of the target thread. The attribute of the target thread can be referred to as a second attribute.
[0171] In some embodiments, the target thread sends a request for applying for memory to the electronic device, and in response to the request, the electronic device obtains the second attribute of the target thread.
[0172] In some embodiments, the second attribute is used to indicate whether the target thread is a critical thread, which can include one or more of the following threads: a thread receiving a user input event, a UI thread drawing an interface, a render thread rendering a picture, and the like. In a case where the target thread is a critical thread, the electronic device enables the tcache function for the target thread; in a case where the target thread is not a critical thread, the electronic device does not enable the tcache function for the target thread.
[0173] It should be noted that different processes can each include one or more of the above-mentioned threads receiving a user input event, a UI thread drawing an interface, or a render thread rendering a picture, and threads with the same function can be included in multiple different processes at the same time. The above-mentioned types of critical threads are only used to distinguish the functions of the threads, and are not used to limit a specific thread.
[0174] In some embodiments, the second attribute is used to indicate whether the target thread belongs to a thread in a first thread set, which can be a set of at least one thread preset by the electronic device. Specifically, in a case where the electronic device determines according to the second attribute that the target thread belongs to a thread in the first thread set, the electronic device enables the tcache function for the target thread; in a case where the electronic device determines according to the second attribute that the target thread does not belong to a thread in the first thread set, the electronic device does not enable the tcache function for the target thread.
[0175] In some embodiments, the second attribute is used to indicate whether the target thread belongs to a thread in a second thread set, which can be a set of at least one thread preset by the electronic device. Specifically, in a case where the electronic device determines according to the second attribute that the target thread does not belong to a thread in the second thread set, the electronic device enables the tcache function for the target thread; in a case where the electronic device determines according to the second attribute that the target thread belongs to a thread in the second thread set, the electronic device enables the tcache function for the target thread.
[0176] Enabling the tcache function only for specific threads in the foreground process is beneficial for further rational allocation of memory resources in the electronic device. In particular, for threads that receive user input, threads that draw user interfaces, and threads that render pictures, and the like, which are more strongly perceived by users, enabling the tcache function only for these types of threads can significantly improve the user experience.
[0177] S103, the electronic device enables the tcache function of the target thread.
[0178] The electronic device can configure the tcache function of the target thread to be closed by default, or the electronic device can also close the tcache function of the target thread after the end of the last run on the target thread, or the electronic device can also close the tcache function of the target thread in the case where the target process corresponding to the target thread is converted from foreground running to background running.
[0179] In some embodiments, before S103, the tcache function of the target thread has been opened, and the electronic device detects that the tcache function of the target thread has been opened in the case where it is determined to open the tcache function for the target thread, and then the electronic device keeps the tcache function of the target thread opened and does not repeat the action of opening.
[0180] In other embodiments, before S103, the tcache function of the target thread is in a closed state, and the electronic device opens the tcache function of the target thread in the case where it is determined to open the tcache function for the target thread.
[0181] Optionally, the electronic device can immediately open the tcache function of the target thread in the case where it is determined to open the tcache function for the target thread, or the electronic device can also open the tcache function for the target thread after a preset time interval, or the electronic device can also open the tcache function for the target thread when a preset condition is met (for example, the target thread applies for memory from the electronic device again, and the tcache function is directly opened for the target thread).
[0182] S104, the electronic device allocates memory for the target thread.
[0183] After the electronic device opens the tcache function for the target thread, the electronic device can allocate memory for the target thread.
[0184] In some embodiments, after the target thread opens the tcache function, the electronic device allocates a tcache of a certain space for the target thread, and the electronic device can preferentially allocate memory for the target thread from the allocated tcache. In the case where the memory cannot be allocated for the electronic device from the tcache (for example, the memory space corresponding to the tcache does not meet the demand of the memory size applied by the target thread), the electronic device allocates memory for the target thread from the global memory.
[0185] In the embodiments of the present application, when performing memory management, the electronic device determines whether the target process is a foreground process and whether the thread under the target process is a critical thread, to start the tcache function for the target thread, thereby facilitating the allocation of limited physical memory to specific threads, improving the utilization of memory to a certain extent, reducing the empty occupation of physical memory, and improving the user experience of the electronic device to a certain extent.
[0186] Figure 5 Another method for managing memory provided by the embodiments of the present application is shown.
[0187] S201, the electronic device determines that the target process is a background process.
[0188] For example, as shown in the figure, when the user opens the calculator software, the desktop process described above is switched from the foreground process to the background process, or when the user exits the calculator software, the calculator process is switched from the foreground process to the background process. Figure 3
[0189] In some embodiments, when the electronic device detects the event of switching the foreground and background of the above process, the electronic device can determine whether the target process is a background process.
[0190] In other embodiments, for some newly created background processes, the electronic device can also detect whether these processes belong to the background process.
[0191] The target process can contain attribute information (first attribute), which can be used to indicate whether the target process is a background process. The electronic device can detect the first attribute of the target process to determine whether the target process is a background process.
[0192] Specifically, the Jemalloc memory allocator of the electronic device can obtain the first attribute from the operating system.
[0193] Optionally, after obtaining the first attribute, the electronic device can add identification information for identifying the first attribute to the Jemalloc instance of the target process. When the first attribute of the target process changes, the Jemalloc memory allocator can obtain the value of the first attribute from the operating system and synchronize it to the above identification information, so as to be consulted when performing subsequent memory allocation operations.
[0194] The electronic device obtains the first attribute, which can be obtained from the operating system or from the identification information of the first attribute.
[0195] The electronic device can also preset a third process set, and the attribute information included in the target process can be used to indicate whether the target process is a process in the third process set. In a case where the electronic device obtains the attribute information of the target process, the electronic device can determine whether the target process belongs to the third process set. For the target process belonging to the third process set, the electronic device can perform the steps as in S202 and the following steps, and for the target process not belonging to the third process set, the electronic device can not perform the steps as in S202 and the following steps.
[0196] Alternatively, the electronic device can also preset a fourth process set, and the attribute included in the target process can be used to indicate whether the target process is a process in the fourth process set. In a case where the electronic device obtains the attribute information of the target process, the electronic device can determine whether the target process belongs to the fourth process set. For the target process belonging to the fourth process set, the electronic device can not perform the steps as in S202 and the following steps, and for the target process not belonging to the fourth process set, the electronic device can perform the steps as in S202 and the following steps.
[0197] Optionally, the electronic device can set the target process as a foreground process or a background process in response to a user operation.
[0198] S202, the electronic device closes the tcache function of the target thread.
[0199] In some embodiments, in a case where the electronic device determines that the target process belongs to a background process, the electronic device can close the tcache function of all threads included in the target process. In a case where the electronic device determines that the target process does not belong to a background process, the electronic device can not close the tcache function of all threads included in the target process.
[0200] In another embodiment, in a case where the electronic device determines that the target process belongs to a process in the third process set, the electronic device can close the tcache function of all threads included in the target process. In a case where the electronic device determines that the target process does not belong to a process in the third process set, the electronic device can not close the tcache function of all threads included in the target process.
[0201] In yet another embodiment, in a case where the electronic device determines that the target process belongs to a process in the fourth process set, the electronic device does not close the tcache function of all threads included in the target process. In a case where the electronic device determines that the target process does not belong to a process in the fourth process set, the electronic device closes the tcache function of all threads included in the target process.
[0202] Optionally, the electronic device can further preset a third thread set or a fourth thread set, and the electronic device can detect an attribute of the target thread, the attribute being used to indicate whether the target thread belongs to the third thread set or whether the target thread belongs to the fourth thread set. In a case where the electronic device determines that the target thread belongs to the third thread set, or in a case where the electronic device determines that the target thread does not belong to the fourth thread set, the electronic device can close the tcache function of the target thread. In a case where the electronic device determines that the target thread does not belong to the third thread set, or in a case where the electronic device determines that the target thread belongs to the fourth thread set, the electronic device can not close the tcache function of the target thread.
[0203] The thread whose tcache function is closed will no longer apply for memory from the global memory through the tcache, which is beneficial to the electronic device to allocate physical memory to other threads, is beneficial to improve the utilization of physical memory by different threads on the electronic device, improves the efficiency of the electronic device in allocating physical memory, and improves the user experience of the electronic device.
[0204] In S203, the electronic device recycles the tcache of the target thread.
[0205] The method used by the electronic device to determine whether to recycle the tcache of the target thread is similar to the method used in S202 to determine whether to close the tcache function of the target thread.
[0206] In some embodiments, the electronic device can recycle the tcache of the target thread while closing the tcache function of the target thread. In this case, the method and standard used by the electronic device to determine whether to recycle the tcache of the target thread are consistent with the method and standard used to determine whether to close the tcache function of the target thread.
[0207] For example, in a case where the electronic device determines that the target process is a background process, the electronic device closes the tcache function of all threads under the target process and recycles the tcache of these threads.
[0208] In other embodiments, the electronic device can also only recycle the tcache of the target thread. In this case, the method and standard used by the electronic device to determine whether to recycle the tcache of the target thread can be inconsistent with the method used to determine whether to close the tcache function of the target thread.
[0209] For example, the electronic device presets a thread set A and a thread set B, for a thread in the thread set A, the electronic device recycles the tcache of the thread, and for a thread in the thread set B, the electronic device closes the tcache function of the thread.
[0210] The electronic device can recycle the tcache used by the thread, so as to reduce the empty occupation of the tcache or the physical memory by the target thread, facilitate allocation of more physical memory to other threads, improve the utilization rate of physical memory by different threads on the electronic device, and improve the user experience of the electronic device.
[0211] In S204, the electronic device can optionally recycle the idle memory pages of the slab unit of the target process.
[0212] The electronic device can recycle the idle memory pages of the slab unit of the target process independently of the electronic device recycling the tcache of the target thread and the electronic device recycling the tcache of the target thread, that is, the electronic device can recycle the idle memory pages of the slab unit of the target process without recycling the tcache of the target thread. In other words, the electronic device can only recycle the idle memory pages of the slab unit of the target process, the electronic device can only recycle the tcache of the target thread, and the electronic device can only close the tcache function of the target thread.
[0213] The electronic device can also recycle the idle memory pages of the slab unit of the target process and the tcache of the target thread at the same time. For example, in some embodiments, when it is determined that the target process is a background process, the electronic device recycles the tcache of the target thread and the idle memory pages of the slab unit of the target process at the same time.
[0214] The electronic device can recycle the idle memory pages of the slab cache used by the thread contained in the background process. Compared with the method of recycling all idle memory pages in the slab unit, the timely recycled idle memory pages can be used to allocate to other threads, which facilitates reduction of the empty occupation of physical memory, improves the utilization rate of physical memory, and improves the user experience of the electronic device.
[0215] Figure 6 An exemplary process diagram of recycling the idle memory pages of the slab unit of the target process by the electronic device is given.
[0216] It should be noted that, Figure 6 The method of recycling the idle memory pages of the slab unit of the target process by the electronic device is only illustrative, and other methods of recycling idle memory pages can be induced or deduced by those skilled in the art based on the disclosure of the embodiments of the present application, which should also fall within the scope of the present application.
[0217] The target thread can include bin managers for managing the slab units. For example, in this embodiment, the target thread includes five bin managers, which are denoted as b1, b2, b3, b4, and b5. Each bin manager can manage a plurality of slab units. For example, in this embodiment, the b1 manager manages five slab units, which are denoted as s1, s2, s3, s4, and s5. Each slab unit can be divided into a plurality of regions. For example, in this embodiment, the s1 unit includes four regions, which are denoted as r01, r02, r03, and r04, and the s5 unit includes four regions, which are denoted as r11, r12, r13, and r14.
[0218] For each slab unit, an identification table can be set for identifying whether the plurality of regions in the slab unit are in an idle state. The identification table can be referred to as a bitmap. Each identifier in the identification table is used to identify whether each region in the slab unit corresponding to the identification table is in an idle state. For example, for the r01 block in the s1 unit, the corresponding identifier is 0, which is used to identify that the r01 block is in an idle state. For the r12 block in the s5 unit, the corresponding identifier is 1, which is used to identify that the r12 block is in a non-idle state.
[0219] The size of a region is usually smaller than the size of a single memory page, and the size of a small number of regions can be larger than the size of a single memory page. In a slab unit in which the size of a single region exceeds the size of a single memory page, the free memory page contained in the region can be recycled only when the region is in an idle state. In a slab unit in which the size of a single region is smaller than the size of a single memory page, a plurality of idle regions need to be continuous to form a free memory page, and the free memory page can be recycled.
[0220] The electronic device can traverse all the regions of all the slab units managed by all the bin managers in the target thread in a certain order. The electronic device can search for idle regions according to a certain algorithm, and the free memory page can be returned to the operating system.
[0221] For example, the electronic device can traverse all the slab units in the b1 manager in the order of s1, s2, s3, s4, and s5, and can traverse all the bin managers in the target thread in the order of b1, b2, b3, b4, and b5.
[0222] The following is combined with Figure 7 and Figure 8This application describes a method for finding free memory pages in a slab unit, as provided in its embodiments. For ease of understanding, the following embodiments use the case where the size of a single region is smaller than the size of a single memory page as an example.
[0223] S301, find the first available region.
[0224] Combination Figure 8 , Figure 8 As shown in (a), the slab unit consists of 3 memory pages, each containing 4 regions, for a total of 12 regions. Regions 1-4, 9, and 11 are in a free state, while regions 10 and 12 are in a non-free state. The electronic device can traverse from region 1 to region 12 to find free memory pages that can be reclaimed.
[0225] During the first round of traversal, region 1, which is free, is taken as the first free region. In subsequent traversals, region 9 can be taken as the first free region.
[0226] User-mode memory allocators typically return one or more contiguous free memory pages to the operating system kernel. A memory page to be reclaimed can only be reclaimed if all regions within that page are in a free state.
[0227] S302, use the base address A as the page base address that is greater than or equal to the base address of the first free region.
[0228] Specifically, if the first free region is the first region of the memory page it belongs to, then base address A is the base address of the memory page containing the first free region; otherwise, base address A is the base address of the next memory page containing the first free region.
[0229] During the first round of traversal, since region 1 is a free region and the first region of its memory page, the base address of the memory page containing region 1 can be used as base address A. Alternatively, a value greater than the base address of region 1 can be used as base address A. For example, if the base address of the memory page is 4K, then 4K, 8K, or 12K can be used as base address A.
[0230] Similarly, during subsequent traversals, the base address of region9 can be used as the base address A'.
[0231] S303, find the first non-idle region after the first idle region.
[0232] In the first round of traversal, region 5 is the first non-idle region after region 1, and in the subsequent traversal, region 10 is the first non-idle region after region 9.
[0233] S304, taking the page base address of the first non-idle region as the base address B.
[0234] According to the search result in S303, the page base address of region 5 can be taken as the base address B, and the page base address of region 10 can be taken as the base address B'.
[0235] S305, determining that there is at least one idle memory page between the base address B and the base address A.
[0236] In the first round of traversal, there is one memory page between the base address B and the base address A, and all regions in the memory are in the idle state, so the memory page is an idle memory page; in the subsequent traversal, there is no memory page between the base address B' and the base address A', so there is no idle memory page to be recycled.
[0237] When there is less than one memory page between the base address B and the base address A, the electronic device can perform the next round of traversal process to re-determine the base address A and the base address B.
[0238] For the idle memory page between the base address B and the base address A, the electronic device can return it to the operating system, that is, recycle the idle memory page. As shown in the example, Figure 8 The memory pages where regions 1 to 4 are located are recycled and returned to the operating system, and the memory page where region 9 is located is not returned to the operating system.
[0239] In the embodiment of the application, by traversing the plurality of regions in the slab unit in a certain order and recycling the idle memory pages contained in the plurality of idle regions, the plurality of regions not containing idle memory pages are not recycled. On the one hand, compared with the overall recycling scheme, the idle memory page occupancy rate in the slab unit can be effectively reduced, and on the other hand, the adverse effects of recycling idle memory on running processes or threads are also reduced.
[0240] It should be understood that in the above method embodiment, the size of the serial number of each process does not mean the order of execution, and the execution order of each process should be determined according to its function and internal logic, and should not constitute any limitation on the implementation process of the embodiment of the application.
[0241] Based on the same inventive concept, as Figure 9As shown in the figure, this application embodiment also provides an electronic device 900, which includes a processor 910 and a memory 920. The processor is used to perform, for example, Figures 3 to 8 In the embodiments shown, the electronic device performs processing operations, such as determining whether to enable the tcache function of the target thread based on whether the process is a foreground process. The memory stores one or more computer programs, which include instructions that, when executed by one or more processors, cause any of the memory management methods described above to be executed.
[0242] like Figure 10 As shown in the illustration, this application also provides a memory management device 1000, which includes an acquisition unit 1010 and a processing unit 1020. The acquisition unit is used to acquire first attributes of a target process, second attributes of a target thread, etc., and the processing unit is used to execute, for example,... Figures 3 to 8 The processing operations performed by the electronic device in the illustrated embodiment include determining whether to enable the tcache function of the target thread based on whether the process is a foreground process.
[0243] This application also provides a computer program product, which includes computer program code. When the computer program code is run on a computer, it causes the computer to perform the following functions: Figures 3 to 8 The method in the illustrated embodiment.
[0244] This application also provides a computer-readable storage medium storing computer instructions that, when executed on a computer, cause the computer to perform the following actions: Figures 3 to 8 The method in the illustrated embodiment.
[0245] This application embodiment also provides a chip, including a processor, for reading instructions stored in a memory, and when the processor executes the instructions, causing the chip to perform the following: Figures 3 to 8 The method in the illustrated embodiment.
[0246] Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0247] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the system, device and unit described above can refer to the corresponding process in the foregoing method embodiments, which will not be repeated here.
[0248] In several embodiments provided in the present application, it should be understood that the disclosed system, device and method can be implemented in other ways. For example, the device embodiments described above are only schematic, for example, the division of the units is only a logical function division, and actual implementation can have another division manner, for example, a plurality of units or components can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the coupling or direct coupling or communication connection between the units shown or discussed can be indirect coupling or communication connection through some interface, device or unit, and can be electrical, mechanical or other forms.
[0249] The units described as separate components can or can not be physically separate, and the components shown as units can or can not be physical units, that is, they can be located in one place, or can be distributed on a plurality of network units. Part or all of the units can be selected according to actual needs to achieve the purpose of the embodiment scheme.
[0250] In addition, the functional units in each embodiment of the present application can be integrated in one processing unit, or each unit can be physically present separately, or two or more units can be integrated in one unit.
[0251] If the functions are realized in the form of software function units and sold or used as independent products, they can be stored in a computer readable storage medium. Based on this understanding, the technical solutions of the present application or the essential part or part of the technical solutions that make contributions to the prior art can be embodied in the form of a software product. The computer software product is stored in a storage medium, and includes a plurality of instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present application. The foregoing storage medium includes: a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, and various program code storage media.
[0252] The above merely provides the specific implementation of the present application, but the protection scope of the present application is not limited to this. Any person skilled in the art can easily think of the changes or replacements within the technical range disclosed by the present application, which should be covered in the protection scope of the present application. Therefore, the protection scope of the present application should be subject to the protection scope of the claims.
Claims
1. A method for managing memory, characterized in that, The method is applied to an electronic device, and the method includes: The electronic device acquires a first attribute of the target process, the first attribute being used to indicate whether the target process is a foreground process or a background process, the target process including a target thread; If the first attribute indicates that the target process is a foreground process, the electronic device determines to enable the thread-local caching function of the target thread; If the first attribute indicates that the target process is a background process, the electronic device determines to disable the thread-local caching function of the target thread.
2. The method according to claim 1, characterized in that, When the first attribute indicates that the target process is a foreground process, the electronic device determines to enable the thread-local caching function of the target thread, including: The electronic device determines to enable the thread-local caching function of the target thread based on the second attribute of the target thread. The second attribute is used to indicate that the target thread is a critical thread, and the critical thread is a thread related to user interaction. The method further includes: The electronic device enables the thread-local caching function of the target thread.
3. The method according to claim 2, characterized in that, The key threads include one or more of the following: a thread for receiving user input events, a user interface thread for drawing the interface, and a rendering thread for rendering the screen.
4. The method according to claim 2 or 3, characterized in that, Before the electronic device determines whether to enable or disable the thread-local caching function of the target thread based on the first attribute, the method further includes: In response to the user's first action, the electronic device sets the first attribute to indicate that the target process is a foreground process.
5. The method according to any one of claims 1 to 3, characterized in that, The method further includes: In response to a second user action, the electronic device sets the first attribute to indicate that the target process is a background process.
6. The method according to claim 5, characterized in that, The method further includes: The electronic device disables the thread-local caching function of the target thread.
7. The method according to claim 5, characterized in that, The method further includes: The electronic device reclaims the thread-local cache of the target thread.
8. The method according to claim 5, characterized in that, The method further includes: The electronic device reclaims free memory pages from the slice units of the target process.
9. An electronic device, characterized in that, It includes a processor and a memory, the memory being used to store program instructions, and the processor being used to invoke the program instructions to perform the method of any one of claims 1 to 8.
10. A device for managing memory, characterized in that, Includes modules for implementing the method of any one of claims 1 to 8.
11. A computer-readable storage medium, characterized in that, It stores a computer program thereon, which, when executed by a computer, enables the implementation of the method according to any one of claims 1 to 8.
12. A chip product, characterized in that, include: A processor for reading instructions stored in memory, which, when executed, cause the chip to perform the method of any one of claims 1 to 8.
Citation Information
Patent Citations
Memory recovery method and device, electronic equipment and storage medium
CN111984413A