Memory recovery method, device, storage medium and electronic device
By prioritizing threads in electronic devices and recycling memory according to type, the lag problem in interactive scenarios is solved and the user experience and memory allocation efficiency are improved.
Patent Information
- Application Number
- CN202010628802.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-07-01
- Publication Date
- 2024-10-01
- Estimated Expiration
- 2040-07-01
AI Technical Summary
Electronic devices are prone to lagging in interactive scenarios when running multiple applications, resulting in a degraded user experience.
By determining the current amount of free memory when receiving a memory allocation request sent by a thread, if it is less than the preset threshold, the thread is divided into multiple types of threads with different priorities, especially the thread that executes user interaction events as the highest priority thread. Different memory recycling ratios are used for memory recycling according to thread types, and file pages are recycled first to improve memory allocation efficiency.
It effectively avoids lagging caused by insufficient memory, improves user experience, and improves the memory allocation efficiency of the first type of thread by optimizing the memory recycling mechanism.
Smart Images

Figure CN111831440B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of electronic devices, and in particular to a memory recovery method, device, storage medium and electronic device. Background Art
[0002] With the development of technology, more and more applications are installed in electronic devices, such as video applications, game applications, and instant messaging applications, etc. This requires electronic devices to often run many applications in the foreground and background, which easily causes freezes in user interaction scenarios. Summary of the invention
[0003] Embodiments of the present application provide a memory recovery method, device, storage medium, and electronic device, which can reduce the jamming phenomenon in interactive scenarios.
[0004] In a first aspect, an embodiment of the present application provides a memory recovery method, comprising:
[0005] When receiving a memory allocation request sent by a thread, determine the current amount of free memory;
[0006] If the amount of free memory is less than a preset threshold, determining the type of the thread, wherein the threads are divided into a plurality of types of threads with different priorities, wherein a first type of thread has a highest priority, and the first type of thread is a thread that executes a task related to a user interaction event;
[0007] Obtain a memory recovery ratio corresponding to the type of the thread, and recycle memory pages in the allocated memory space according to the memory recovery ratio, wherein the memory recovery ratio is used to characterize the ratio between multiple memory recovery objects, and different types of memory recovery objects have different recovery efficiencies. The higher the priority of the thread, the greater the proportion of memory recovery objects with high recovery efficiency in the corresponding memory recovery ratio.
[0008] In a second aspect, an embodiment of the present application further provides a memory recovery device, including:
[0009] A memory detection module is used to determine the current amount of free memory when receiving a memory allocation request sent by a thread;
[0010] a thread identification module, configured to determine the type of the thread if the amount of free memory is less than a preset threshold, wherein the threads are divided into a plurality of classes of threads with different priorities, wherein a first class of threads has the highest priority, and the first class of threads are threads that execute related tasks in a user interaction event;
[0011] A memory recovery module is used to obtain a memory recovery ratio corresponding to the type of the thread, and to recycle memory pages in the allocated memory space according to the memory recovery ratio, wherein the memory recovery ratio is used to characterize the ratio between multiple memory recovery objects, and different types of memory recovery objects have different recovery efficiencies. The higher the priority of the thread, the greater the proportion of memory recovery objects with high recovery efficiency in the corresponding memory recovery ratio.
[0012] In a third aspect, an embodiment of the present application further provides a storage medium on which a computer program is stored. When the computer program is run on a computer, the computer executes a memory recovery method provided in any embodiment of the present application.
[0013] In a fourth aspect, an embodiment of the present application further provides an electronic device, comprising a processor and a memory, wherein the memory has a computer program, and the processor executes a memory recovery method as provided in any embodiment of the present application by calling the computer program.
[0014] The technical solution provided by the embodiment of the present application, when receiving a memory allocation request sent by a thread, first determines the current amount of free memory. If the amount of free memory is less than a preset threshold, memory recovery must be performed first, and then the type of thread is identified. When memory is recovered, different memory recovery ratios are used for memory recovery according to the type of thread requesting memory. The memory recovery ratio is used to characterize the ratio between multiple memory recovery objects. Since the recovery efficiency of different types of memory recovery objects is different, if the memory recovery ratio is different, the overall recovery efficiency is also different. Among them, the first type of thread is the thread that executes the relevant tasks in the user interaction event, and has the highest priority. That is to say, the proportion of memory recovery objects with high recovery efficiency in the memory recovery ratio corresponding to the first type of thread is the largest proportion among all types of threads. Therefore, for the first type of thread, the memory recovery mechanism with the highest efficiency among all threads can complete memory recovery as soon as possible, improve the memory allocation efficiency of the first type of thread, avoid the jamming phenomenon in the interactive scene caused by the failure of the first type of thread to obtain memory in time, and improve the user experience. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.
[0016] Figure 1 A first flow chart of the memory recycling method provided in an embodiment of the present application.
[0017] Figure 2 A second flow chart of the memory recycling method provided in an embodiment of the present application.
[0018] Figure 3 A schematic diagram of the structure of a memory recovery device provided in an embodiment of the present application.
[0019] Figure 4 A first structural schematic diagram of an electronic device provided in an embodiment of the present application.
[0020] Figure 5 A second structural schematic diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0021] The technical solutions in the embodiments of the present application will be described clearly and completely below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, rather than all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present application.
[0022] Reference to "embodiments" herein means that a particular feature, structure, or characteristic described in conjunction with the embodiments may be included in at least one embodiment of the present application. The appearance of the phrase in various locations in the specification does not necessarily refer to the same embodiment, nor is it an independent or alternative embodiment that is mutually exclusive with other embodiments. It is explicitly and implicitly understood by those skilled in the art that the embodiments described herein may be combined with other embodiments.
[0023] The embodiment of the present application provides a memory recovery method, and the execution subject of the memory recovery method can be a memory recovery device provided in the embodiment of the present application, or an electronic device integrated with the memory recovery device, wherein the memory recovery device can be implemented in hardware or software. The electronic device can be a smart phone, a tablet computer, a PDA, a laptop computer, or a desktop computer.
[0024] See also Figure 1 , Figure 1 This is a schematic diagram of the first process of the memory recovery method provided in the embodiment of the present application. The specific process of the memory recovery method provided in the embodiment of the present application can be as follows:
[0025] In 101, when a memory allocation request sent by a thread is received, the current amount of free memory is determined.
[0026] In the embodiment of the present application, the operating system of the electronic device may be a system based on the Linux kernel, such as an Android operating system. The operation of the system process and the application process of the electronic device requires the system kernel to allocate memory space for them. And as the system runs, the kernel continuously recycles and allocates memory.
[0027] A thread is an execution path of a process, the smallest unit of program execution, and the basic unit of memory allocation. A process can have multiple threads, but at least one thread.
[0028] For the kernel, when scheduling resources, such as CPU scheduling, it is specific to a certain thread. There is a main thread in the process, and it will also create many sub-threads to assist in the work. For example, a process of a content interactive application will create a main thread to execute code, and during the execution, it will also create other sub-threads to assist in running the task code of each part.
[0029] In the embodiment of the present application, when a process has a task to execute, a new thread will be created to execute the task, and the thread requires the kernel to allocate a certain amount of memory space for it to run. Therefore, before executing the task, the thread will send a memory allocation request to the kernel.
[0030] When the kernel receives a memory allocation request, it first determines the current amount of free memory.
[0031] Among them, the Linux kernel uses three memory watermarks to mark the state of the system's free memory, in order from small to large, respectively: high watermark, low watermark and min watermark. When the system's free memory is lower than the low watermark, the system will wake up the memory recovery process kswapd to recycle memory until the free memory is no lower than the high watermark. When the memory recovery process kswapd recycles memory, the speed of memory consumption will be relatively slow, but the memory allocation will also enter the slow allocation path (slowpath), which means that the memory allocation efficiency will also be slower. It should be noted that the slow allocation path here is an abstract concept, which means that the system slows down the memory allocation efficiency of the thread due to the need to recycle memory in the background. Among them, the above-mentioned low watermark is a preset threshold in the embodiment of the present application.
[0032] In addition, the kernel mainly recycles memory by recycling anonymous pages and file pages. The strategies for recycling anonymous pages and file pages are different. Recycling anonymous pages generally compresses them and stores them in RAM. Since this process requires the CPU to compress them and then decompress them when they are used, the recycling process consumes a certain amount of CPU time and is relatively slow. There are two ways to recycle file pages. If the file content has not been modified, the file page content is directly discarded; if the file content has been modified, the content needs to be written back to the disk and the file page is discarded. When it is needed again, it needs to be read from the disk again. Generally speaking, many file pages have not been modified, and the recycling process is relatively fast. Conventional recycling methods generally tend to recycle anonymous pages and retain file pages to reduce the cost of re-reading disk data IO. However, under this recycling mechanism, in some scenarios, slow memory recycling may cause freezes and affect user experience.
[0033] In order to improve this defect, in an embodiment of the present application, threads related to user experience are marked out from all threads. For these threads related to user experience, an efficient memory recovery method is adopted. For example, all memory recovery is performed by recycling memory pages, thereby improving the memory recovery speed, recovering the required memory as soon as possible, and shortening the time of the slow path.
[0034] In 102, if the amount of free memory is less than a preset threshold, the type of thread is determined, wherein the threads are divided into multiple types of threads with different priorities, wherein the first type of thread has the highest priority, and the first type of thread is a thread that executes related tasks in the user interaction event.
[0035] Whether the thread that executes the relevant tasks in the user interaction event can run smoothly determines whether the user-perceivable lag will occur in the user interaction event. Therefore, in the embodiment of the present application, the thread that executes the relevant tasks in the user interaction event is determined, and these threads that are closely related to the user experience are recorded as ux (user experience) threads, that is, first-class threads. In the embodiment of the present application, threads other than ux threads are recorded as second-class threads. The operation of the second-class threads generally does not affect the user experience, or has little impact on the user experience.
[0036] Among them, the system architecture of the electronic device includes at least an application framework layer and a kernel layer. In an embodiment of the present application, ux threads are identified and marked from the perspective of the application framework layer and the kernel layer. For example, the application framework layer adds preset labels to some threads that directly execute related tasks in user interaction events to mark these threads as static ux threads, and the kernel layer marks some threads that indirectly affect the execution of related tasks in user interaction events as dynamic ux threads.
[0037] The processes in the embodiments of the present application include system-level processes and application-level processes. Since the scenarios that cause user-perceivable interface freezes are mostly relative to the process running in the foreground. Therefore, in the scheme of the embodiments of the present application, "determining the thread that executes the relevant tasks in the user interaction event" includes: when it is detected that a process switches to the foreground, determining the foreground process; determining the thread used to execute the relevant tasks in the user interaction event from the thread of the foreground process as the target thread.
[0038] For example, in one embodiment, "determining a target thread for executing relevant tasks in a user interaction event from the threads of the foreground process, and marking the target thread as a first-class thread" includes: identifying a first preset thread for executing relevant tasks in a user interaction event from the threads of the foreground process as the target thread; adding a preset label to the target thread to mark the thread as a first-class thread.
[0039] For example, the first preset thread includes some threads created when the process is running and used to directly execute tasks related to user interaction events, such as UI (user interface) thread, Render thread, GL thread, user input event distribution thread, user input event detection thread, etc. Whether these threads can run smoothly determines whether user-perceivable lag will occur in the interaction interface between the user and the process.
[0040] For example, a user uses a chat software to chat with a friend. The user enters text in the dialog box, and the electronic device sends the text entered by the user to the friend's electronic device through the server. In this interaction event, the UI thread, Render thread, user input event distribution thread, user input event detection thread and other threads need to work together to complete this user interaction event. The operation of each thread requires the system to allocate resources for it. Therefore, when the chat software is detected to be running in the foreground, these threads are identified and marked as ux threads.
[0041] The first preset thread is generally an application-level thread, which can be determined by analyzing actual jamming scenarios. For example, in a test, if an application jam occurs in a certain user interaction scenario, and the analysis of the scenario finds that the jam is caused by a thread processing tasks too slowly, then the thread can be considered to be used to execute related tasks in the user interaction event, and the operation of the thread is closely related to the user experience, so the thread can be used as the first preset thread.
[0042] Based on this, various possible jamming scenarios can be tested to record the threads that cause jamming. The electronic device stores the relevant information of the first preset threads, and when the process is switched to the foreground, all threads belonging to the pre-recorded first preset threads under the process are marked as ux threads.
[0043] It is understandable that, for the electronic device, the stored relevant information of the first preset thread is not unmodifiable, and when the system is upgraded, the relevant information of the first preset thread can be updated.
[0044] Furthermore, in another embodiment, the method further comprises: when it is detected that a second preset thread is created, marking the created second preset thread as a first-class thread, wherein the second preset thread is a system-level thread.
[0045] Since in the process of executing user interaction events, in addition to application-level threads, some system-level threads may be involved to complete tasks, the system framework layer also needs to mark these system-level threads as ux threads. Generally, these threads are created when the system starts, so when the system starts, these threads can be identified and marked, such as Surfaceflinger threads, system animation threads, etc. Alternatively, during the operation of the system, if it is detected that a new system process thread is created and used to perform related tasks in the user interaction event, the system framework layer also marks these threads as ux threads. For example, SystemUI threads. Among them, the second preset thread can also be determined by analyzing the actual jamming scene. For example, in the test, if an application jam occurs in a certain user interaction scene, and the jamming phenomenon is found to be caused by a system-level thread processing task too slowly through the analysis of the scene, it can be considered that the system-level thread is used to perform related tasks in the user interaction event, and the operation of the system-level thread is closely related to the user experience, and the system-level thread can be used as the second preset thread. The electronic device stores the relevant information of these second preset threads, and if it is detected that the system has created these threads, they are marked as ux threads.
[0046] Among them, the preset tag can be a ux tag, which is added as follows: Linux uses the task_struct structure to describe and record threads, and each thread has a corresponding task_struct structure. The task_struct records the name, identifier, state, priority, memory pointer, context data and other attribute information of the thread. Therefore, the application framework layer can add a corresponding ux flag member to the task_struct structure to mark the UI thread, Render thread, GL thread and other threads of the foreground process that perform related tasks in the user interaction event with the ux flag bit, so that the kernel layer can identify the task attributes of the thread.
[0047] It should be noted that the above-mentioned static ux threads are only examples and are not limited thereto. As long as it is a thread that directly executes tasks related to user interaction events and its operation directly affects the user experience, it can be marked as a static ux thread. For the application framework layer, when it is detected that the newly created thread is used to execute user interaction events, or when it is detected that some resident system-level threads are used to process user interaction events, these threads are added with ux tags to mark them as static ux threads.
[0048] In another embodiment, "determining a target thread for executing relevant tasks in a user interaction event from threads of a foreground process, and marking the target thread as a first-class thread" also includes: during the running of the foreground process, when it is detected that a new thread is created, determining whether the newly created thread is used to execute relevant tasks in the user interaction event; when the newly created thread is used to execute relevant tasks in the user interaction event, marking the newly created thread as a first-class thread.
[0049] When a foreground process is running, if a user interaction event occurs, in addition to the first preset thread at the application level and the second preset thread at the system level, there may be some temporarily created task threads. The operation of these task threads will also directly affect whether there will be user-perceivable lag in the user's interaction interface with the process. Therefore, the application framework layer will also mark these threads as ux threads.
[0050] Among them, the electronic device determines the user interaction event that occurs based on the detected user instruction. A user interaction event generally refers to a situation where after a user triggers a certain instruction, the electronic device needs to respond to the instruction immediately, perform some processing and display the processing result on the interface. For example, users use electronic devices to watch videos, edit text messages, use chat software, use game software, control the switching of electronic device interfaces, use browsing to browse web pages, etc., all of which are user interaction events. For example, a user uses chat software to chat with a friend, the user enters text in the dialog box, and the electronic device sends the text entered by the user to the friend's electronic device through the server. In this process, the electronic device needs to schedule multiple threads to complete this user interaction event. From the start to the completion of the user interaction event, the threads created by the process to complete this user interaction event can be considered as threads related to user experience.
[0051] In another embodiment, after adding a preset tag to the target thread, the method further includes: if the foreground process is an application process, when it is detected that the foreground process switches to background operation, deleting the preset tag of the first preset thread. When the foreground process switches to background operation, the operation of the process is no longer related to the user experience, and the importance of its thread is also reduced. Therefore, the ux tag of the first preset thread corresponding to the process can be deleted, and these ux threads can be restored to ordinary threads.
[0052] In addition, for those task threads temporarily created in user interaction events, they will be destroyed after completing the corresponding tasks, and they will naturally lose the ux tag. For the second preset threads at the system level, even if the process switches between the foreground and the background, these threads are always related to the user experience, so they always retain the ux tag.
[0053] Through the above embodiments, the framework layer identifies threads that directly affect the user experience and adds tags to them. The operation of threads requires the kernel to allocate system resources for them. Therefore, before executing a task, the thread will request resources from the kernel. When the kernel allocates resources to it based on the request, it can first determine whether the thread is a ux thread, and use different resource allocation methods for ux threads and non-ux threads.
[0054] It should be noted that the "first category" and "second category" in the first category of threads and the second category of threads here are only to distinguish whether the threads have ux tags, rather than to divide the threads in the system into only these two categories. The resource allocation optimization scheme of this application is based on whether the thread has a ux tag. If the thread also has other attributes, then when allocating resources, after considering whether it has a ux tag, other attributes will still be considered.
[0055] The above embodiment introduces the identification of static ux threads. Although some threads do not directly execute tasks related to user interaction events, the operation of these threads will also affect the operation of static ux threads, and then indirectly affect the execution of tasks related to user interaction events. In other words, these threads are not always related to user experience, but these threads may be associated with static ux threads through resource constraints during a certain period of time during the execution process. Therefore, in some embodiments, in order to further reduce the jamming phenomenon in the interactive scenario, the kernel layer also marks these threads with constraints between static ux threads as ux threads. Once this constraint relationship ends, the thread will be restored to a non-ux thread. In the embodiment of the present application, such threads are defined as dynamic ux threads. Among them, the specific constraint relationship includes but is not limited to inter-process communication, inter-thread communication, or holding critical resources. For example, ordinary threads requested by static ux threads through inter-process communication, ordinary threads requested by static ux threads through some inter-thread communication method, ordinary threads holding critical resources such as waiting semaphores, read-write semaphores, and mutexes required by static ux threads, etc., in the embodiment of the present application, such threads are marked as dynamic ux threads.
[0056] Based on this, in some embodiments, the method also includes: detecting the running status of the first-class thread; when it is detected that the first-class thread has entered a blocked state, determining an associated thread that has a constraint relationship with the first-class thread that has entered the blocked state; and adding a preset label to the associated thread to mark the associated thread as a first-class thread.
[0057] In some embodiments, after marking the associated thread as a first-category thread, the method further includes: when it is detected that the constraint relationship is released, deleting the preset label of the associated thread.
[0058] Among them, regarding the blocking state of a thread, it is generally divided into D state (Uninterruptable sleep state) and S state (interruptable sleep state) at the kernel level. For example, if a thread initiates an IO request but is not satisfied, it enters the D state; if a thread initiates a synchronous Binder request, it enters the S state. Threads generally enter these states because they need to actively or passively give up CPU resources due to some reasons or logic during the execution of the thread task.
[0059] In this embodiment, the kernel layer detects the state of the static ux thread. When it is detected that the ux thread enters a blocked state, the kernel layer determines the associated threads that have a constraint relationship with the ux thread that enters the blocked state. If these associated threads are not allocated resources in time, such as IO resources, resulting in operation obstruction, the slow operation of the associated threads will cause the ux thread to be in a blocked state for a long time. Therefore, in order to avoid the ux thread being in a blocked state for a long time, the kernel layer will also mark the identified associated threads as ux threads to improve their IO processing efficiency and ensure their timely execution, thereby quickly releasing the blocked state of the ux thread.
[0060] The above describes the way in which the application framework layer and the kernel layer identify and mark ux threads. For the kernel, a different memory recovery ratio is set for each thread. In the present application, the memory recovery objects include anonymous pages and file pages, and the efficiency of recovering file pages is higher than the efficiency of recovering anonymous pages. The memory recovery ratio is the ratio of file pages to anonymous pages. Since the importance of ux threads is greater than that of non-ux threads for user experience, the priority of ux threads is set to be greater than that of non-ux threads, and the proportion of file pages in the memory recovery ratio of ux threads is greater than the proportion of file pages in non-ux threads. For example, in one embodiment, the memory recovery ratio of ux threads is 200:0, and the memory recovery ratio of non-ux threads is 60:140. In other words, when the current thread is a ux thread, all the recovery objects can be file pages. When the current thread is a non-ux thread, most of the recovery objects are anonymous pages. It should be noted that the above values are for example purposes and do not constitute a limitation on this solution, as long as the proportion of file pages in the memory recovery ratio of ux threads is greater than the proportion of file pages in the memory recovery ratio of non-ux threads.
[0061] In 103, a memory recovery ratio corresponding to the thread type is obtained, and memory pages in the allocated memory space are recovered according to the memory recovery ratio, wherein the memory recovery ratio is used to characterize the ratio between multiple memory recovery objects. Different types of memory recovery objects have different recovery efficiencies. The higher the thread priority, the greater the proportion of memory recovery objects with high recovery efficiency in the corresponding memory recovery ratio.
[0062] After receiving a memory allocation request, the kernel can first determine the type of the thread based on the thread's label. And reclaim the memory pages in the allocated memory space according to the memory reclaim ratio corresponding to the thread. Still take the memory reclaim ratio of ux thread as 200:0 and the memory reclaim ratio of non-ux thread as 60:140 as an example. For example, to reclaim 200M of memory, when the current thread is a ux thread, all 200M of memory is obtained by reclaiming file pages. When the current thread is a non-ux thread, 60M of memory is obtained by reclaiming file pages, and the other 140M of memory is obtained by reclaiming anonymous pages. Among them, the allocated memory space refers to the memory space composed of memory pages that have been allocated to the thread and are in an occupied state.
[0063] In specific implementation, the present application is not limited by the execution order of the various steps described. If no conflict occurs, some steps can be performed in other orders or simultaneously.
[0064] As can be seen from the above, the memory recovery method provided in the embodiment of the present application, when receiving the memory allocation request sent by the thread, first determines the current amount of free memory. If the amount of free memory is less than the preset threshold, memory recovery must be performed first, and then the type of thread is identified. When memory is recovered, different memory recovery ratios are used for memory recovery according to the type of thread requesting memory. The memory recovery ratio is used to characterize the ratio between multiple memory recovery objects. Since the recovery efficiency of different types of memory recovery objects is different, if the memory recovery ratio is different, the overall recovery efficiency is also different. Among them, the first type of thread is the thread that executes the relevant tasks in the user interaction event, and has the highest priority. That is to say, the proportion of memory recovery objects with high recovery efficiency in the memory recovery ratio corresponding to the first type of thread is the largest proportion among all types of threads. Therefore, for the first type of thread, the memory recovery mechanism with the highest efficiency among all threads can complete memory recovery as soon as possible, improve the memory allocation efficiency of the first type of thread, avoid the jamming phenomenon in the interactive scene caused by the failure of the first type of thread to obtain memory in time, and improve the user experience.
[0065] The method described in the above embodiments is further described in detail below with examples.
[0066] See also Figure 2 , Figure 2 A second flow chart of the memory recycling method provided in an embodiment of the present invention.
[0067] Methods include:
[0068] In 201, when a memory allocation request sent by a thread is received, the current amount of free memory is determined.
[0069] In some of the above embodiments, threads are divided into two categories: ux threads and non-ux threads for processing. In this embodiment, threads can be divided into three categories for processing. According to the degree of influence of the running status of the threads on the user experience, the threads in the system are divided into ux threads, foreground threads and background threads with high to low priorities. Among them, the identification and marking method of ux threads can refer to the above embodiments, which will not be repeated here.
[0070] The ux thread is recorded as a first-class thread. When it is detected that a thread is switched to the foreground, it is determined whether the thread switched to the foreground is a first-class thread; if not, a second preset label is added to the thread to mark the thread as a second-class thread; when it is detected that a thread is switched to the background, it is determined whether the thread switched to the background is a first-class thread; if not, a third preset label is added to the thread to mark the thread as a third-class thread.
[0071] In this way, threads running in the foreground among threads other than the ux thread are regarded as second-category threads, recorded as FG threads, and threads running in the background among threads other than the ux thread are regarded as third-category threads, recorded as BG threads.
[0072] Among them, for user experience, the importance of ux thread is greater than that of FG thread, and the importance of FG thread is greater than that of BG thread. Therefore, the priority of ux thread is set higher than that of FG thread, and the priority of FG thread is set higher than that of BG thread. The proportion of file pages in the memory recovery ratio of ux thread is greater than that of FG thread; the proportion of file pages in the memory recovery ratio of FG thread is greater than that of BG thread.
[0073] In 202, if the amount of free memory is less than a preset threshold, a tag carried by the thread is obtained, and the type of the thread is determined according to the tag.
[0074] After receiving the memory allocation request, the kernel may first determine the type of the thread according to the tag of the thread. If the tag is a first preset tag, the thread is determined to be a first type of thread; if the tag is a second preset tag, the thread is determined to be a second type of thread; if the tag is a third preset tag, the thread is determined to be a third type of thread.
[0075] In 203, when the thread is a first type thread, memory pages in the allocated memory space are reclaimed according to a first memory reclaim ratio, wherein the first type thread is a thread that executes tasks related to the user interaction event.
[0076] In 204, when the thread is a second type of thread, memory pages in the allocated memory space are reclaimed according to the second memory reclaim ratio, wherein the proportion of memory reclaim objects with high reclaim efficiency in the first memory reclaim ratio is higher than the proportion of memory reclaim objects with high reclaim efficiency in the second memory reclaim ratio.
[0077] In 205, when the thread is a third type of thread, memory pages in the allocated memory space are reclaimed according to the third memory reclaim ratio, wherein the proportion of memory reclaim objects with high reclaim efficiency in the second memory reclaim ratio is higher than the proportion of memory reclaim objects with high reclaim efficiency in the third memory reclaim ratio.
[0078] After determining the type of the thread, reclaim the memory pages in the allocated memory space according to the memory reclaim ratio corresponding to the thread. Taking the memory reclaim ratio of the ux thread as 200:0 and the memory reclaim ratio of the FG thread as 140:60 as an example, the memory reclaim ratio of the BG thread is 0:200. When the current thread is a ux thread, the required memory is reclaimed at the fastest speed, and the 200M memory is all obtained by reclaiming file pages. When the current thread is a FG thread, the memory needs to be reclaimed quickly, of which 140M of memory is obtained by reclaiming file pages, and the other 60M of memory is obtained by reclaiming anonymous pages. When the current thread is a BG thread, the 200M memory is all obtained by reclaiming anonymous pages, and the file pages are protected as much as possible. The above parameters are only for example. In actual use, the parameters can be adjusted according to the actual test results.
[0079] As can be seen from the above, the memory recovery method proposed in the embodiment of the present invention divides the threads in the system into ux threads, foreground threads and background threads with priorities from high to low according to the degree of influence of the running conditions of the threads on the user experience. When performing memory recovery, different memory recovery ratios are used to recover the memory according to the different thread types. The higher the priority of the thread, the greater the proportion of file pages in the corresponding memory recovery ratio. Therefore, the recovery speed when the current thread is a ux thread is greater than the recovery speed when it is a foreground thread, the recovery speed when the current thread is a foreground thread is greater than the recovery speed when it is a background thread, and when the current thread is a background thread, the file pages can be protected from being recovered as much as possible. This avoids the jamming phenomenon in the interactive scenario caused by the first type of thread not being able to obtain memory in time, improves the user experience, and reduces the cost of re-reading disk data IO again.
[0080] In one embodiment, a memory recovery device is also provided. Figure 3 , Figure 3The schematic diagram of the structure of the memory recovery device 300 provided in the embodiment of the present application. The memory recovery device 300 is applied to an electronic device, and the memory recovery device 300 includes a memory detection module 301, a thread identification module 302 and a memory recovery module 303, as follows:
[0081] The memory detection module 301 is used to determine the current amount of free memory when receiving a memory allocation request sent by a thread;
[0082] A thread identification module 302, for determining the type of thread if the amount of free memory is less than a preset threshold, wherein the threads are divided into multiple types of threads with different priorities, wherein the first type of thread has the highest priority, and the first type of thread is a thread that executes related tasks in the user interaction event;
[0083] The memory recovery module 303 is used to obtain the memory recovery ratio corresponding to the type of thread, and to recycle the memory pages in the allocated memory space according to the memory recovery ratio, wherein the memory recovery ratio is used to characterize the ratio between multiple memory recovery objects, and different types of memory recovery objects have different recovery efficiencies. The higher the priority of the thread, the greater the proportion of memory recovery objects with high recovery efficiency in the corresponding memory recovery ratio.
[0084] In some embodiments, the memory recycling module 303 is further used to: when the thread is a first type of thread, reclaim memory pages in the allocated memory space according to a first memory recycling ratio;
[0085] When the thread is a second type of thread, memory pages in the allocated memory space are reclaimed according to the second memory reclaim ratio, wherein the proportion of memory reclaim objects with high reclaim efficiency in the first memory reclaim ratio is higher than the proportion of memory reclaim objects with high reclaim efficiency in the second memory reclaim ratio.
[0086] In some embodiments, threads are divided into three categories of threads with different priorities; the memory recovery module 303 is also used to: when the thread is a third category thread, reclaim memory pages in the allocated memory space according to a third memory recovery ratio, wherein the proportion of memory recovery objects with high recovery efficiency in the second memory recovery ratio is higher than the proportion of memory recovery objects with high recovery efficiency in the third memory recovery ratio.
[0087] In some embodiments, the thread identification module 302 is further used to: obtain a tag carried by the thread;
[0088] If the tag is the first preset tag, the thread is determined to be a first-category thread;
[0089] If the tag is the second preset tag, the thread is determined to be a second type of thread;
[0090] If the tag is the third preset tag, the thread is determined to be a third type of thread.
[0091] In some embodiments, the memory recovery device 300 further includes a thread marking module, which is used to: determine a target thread that executes a related task in a user interaction event; and add the preset label to the target thread to mark the target thread as a first-category thread.
[0092] In some embodiments, the thread marking module is also used to: determine the foreground process when detecting that a process has switched to the foreground; identify a first preset thread for executing related tasks in the user interaction event from the threads of the foreground process as the target thread.
[0093] In some embodiments, the thread marking module is also used to: detect the running status of the first type of thread; when it is detected that the first type of thread has entered a blocked state, determine the associated threads that have a constraint relationship with the first type of thread that has entered the blocked state; add a preset label to the associated thread to mark the associated thread as a first type of thread.
[0094] In some embodiments, the thread marking module is further used to: when detecting that a thread is switched to the foreground for execution, determine whether the thread switched to the foreground for execution is a first-category thread;
[0095] If not, adding a second preset label to the thread to mark the thread as a second type of thread;
[0096] When it is detected that a thread is switched to the background, it is determined whether the thread switched to the background is a first-class thread;
[0097] If not, a third preset tag is added to the thread to mark the thread as a third type of thread.
[0098] It should be noted that the memory recovery device provided in the embodiment of the present application belongs to the same concept as the memory recovery method in the above embodiment. Any method provided in the memory recovery method embodiment can be implemented through the memory recovery device. The specific implementation process is detailed in the memory recovery method embodiment, which will not be repeated here.
[0099] As can be seen from the above, the memory recovery device proposed in the embodiment of the present application, when receiving the memory allocation request sent by the thread, first determines the current amount of free memory. If the amount of free memory is less than the preset threshold, memory recovery must be performed first, and then the type of thread is identified. When memory is recovered, different memory recovery ratios are used for memory recovery according to the type of thread requesting memory. The memory recovery ratio is used to characterize the ratio between multiple memory recovery objects. Since the recovery efficiency of different types of memory recovery objects is different, if the memory recovery ratio is different, the overall recovery efficiency is also different. Among them, the first type of thread is the thread that executes the relevant tasks in the user interaction event, and has the highest priority. That is to say, the proportion of memory recovery objects with high recovery efficiency in the memory recovery ratio corresponding to the first type of thread is the largest proportion among all types of threads. Therefore, for the first type of thread, the memory recovery mechanism with the highest efficiency among all threads can complete memory recovery as soon as possible, improve the memory allocation efficiency of the first type of thread, avoid the jamming phenomenon in the interactive scene caused by the failure of the first type of thread to obtain memory in time, and improve the user experience.
[0100] The present application also provides an electronic device. The electronic device may be a smart phone, a tablet computer, or other device. Figure 4 , Figure 4 The first structural diagram of the electronic device provided in the embodiment of the present application is as follows: The electronic device 400 includes a processor 401 and a memory 402. The processor 401 is electrically connected to the memory 402.
[0101] The processor 401 is the control center of the electronic device 400. It uses various interfaces and lines to connect various parts of the entire electronic device. It executes various functions of the electronic device and processes data by running or calling computer programs stored in the memory 402, and calling data stored in the memory 402, thereby monitoring the electronic device as a whole.
[0102] The memory 402 may be used to store computer programs and data. The computer programs stored in the memory 402 include instructions that can be executed in the processor. The computer programs may constitute various functional modules. The processor 401 executes various functional applications and data processing by calling the computer programs stored in the memory 402.
[0103] In this embodiment, the processor 401 in the electronic device 400 loads instructions corresponding to the processes of one or more computer programs into the memory 402 according to the following steps, and the processor 401 runs the computer programs stored in the memory 402 to implement various functions:
[0104] When receiving a memory allocation request sent by a thread, determine the current amount of free memory;
[0105] If the amount of free memory is less than a preset threshold, determining the type of the thread, wherein the threads are divided into a plurality of types of threads with different priorities, wherein a first type of thread has a highest priority, and the first type of thread is a thread that executes a task related to a user interaction event;
[0106] Obtain a memory recovery ratio corresponding to the type of the thread, and recycle memory pages in the allocated memory space according to the memory recovery ratio, wherein the memory recovery ratio is used to characterize the ratio between multiple memory recovery objects, and different types of memory recovery objects have different recovery efficiencies. The higher the priority of the thread, the greater the proportion of memory recovery objects with high recovery efficiency in the corresponding memory recovery ratio.
[0107] In some embodiments, see Figure 5 , Figure 5 A second structural diagram of an electronic device provided in an embodiment of the present application. The electronic device 400 further includes: a radio frequency circuit 403, a display screen 404, a control circuit 405, an input unit 406, an audio circuit 407, a sensor 408, and a power supply 409. The processor 401 is electrically connected to the radio frequency circuit 403, the display screen 404, the control circuit 405, the input unit 406, the audio circuit 407, the sensor 408, and the power supply 409, respectively.
[0108] The radio frequency circuit 403 is used to send and receive radio frequency signals to communicate with network devices or other electronic devices through wireless communication.
[0109] The display screen 404 may be used to display information input by a user or information provided to a user and various graphical user interfaces of the electronic device. These graphical user interfaces may be composed of images, texts, icons, videos and any combination thereof.
[0110] The control circuit 405 is electrically connected to the display screen 404 and is used to control the display screen 404 to display information.
[0111] The input unit 406 can be used to receive input numbers, character information or user feature information (such as fingerprints), and generate keyboard, mouse, joystick, optical or trackball signal input related to user settings and function control. The input unit 406 can include a fingerprint recognition module.
[0112] The audio circuit 407 can provide an audio interface between the user and the electronic device through a speaker and a microphone. The audio circuit 407 includes a microphone. The microphone is electrically connected to the processor 401. The microphone is used to receive voice information input by the user.
[0113] The sensor 408 is used to collect external environment information. The sensor 408 may include one or more sensors such as an environment brightness sensor, an acceleration sensor, and a gyroscope.
[0114] The power supply 409 is used to supply power to various components of the electronic device 400. In some embodiments, the power supply 409 may be logically connected to the processor 401 through a power management system, so that the power management system can manage charging, discharging, power consumption, and other functions.
[0115] Although not shown in the figure, the electronic device 400 may also include a camera, a Bluetooth module, etc., which will not be described in detail here.
[0116] In this embodiment, the processor 401 in the electronic device 400 loads instructions corresponding to the processes of one or more computer programs into the memory 402 according to the following steps, and the processor 401 runs the computer programs stored in the memory 402 to implement various functions:
[0117] When receiving a memory allocation request sent by a thread, determine the current amount of free memory;
[0118] If the amount of free memory is less than a preset threshold, determining the type of the thread, wherein the threads are divided into a plurality of types of threads with different priorities, wherein a first type of thread has a highest priority, and the first type of thread is a thread that executes a task related to a user interaction event;
[0119] Obtain a memory recovery ratio corresponding to the type of the thread, and recycle memory pages in the allocated memory space according to the memory recovery ratio, wherein the memory recovery ratio is used to characterize the ratio between multiple memory recovery objects, and different types of memory recovery objects have different recovery efficiencies. The higher the priority of the thread, the greater the proportion of memory recovery objects with high recovery efficiency in the corresponding memory recovery ratio.
[0120] As can be seen from the above, an embodiment of the present application provides an electronic device, which, when receiving a memory allocation request sent by a thread, first determines the current amount of free memory. If the amount of free memory is less than a preset threshold, memory recovery is performed first, and then the type of thread is identified. When memory is recovered, different memory recovery ratios are used for memory recovery according to the type of thread requesting memory. The memory recovery ratio is used to characterize the ratio between multiple memory recovery objects. Since the recovery efficiency of different types of memory recovery objects is different, if the memory recovery ratio is different, the overall recovery efficiency is also different. Among them, the first type of thread is a thread that executes related tasks in user interaction events and has the highest priority. That is to say, the proportion of memory recovery objects with high recovery efficiency in the memory recovery ratio corresponding to the first type of thread is the largest proportion among all types of threads. Therefore, for the first type of thread, the memory recovery mechanism with the highest efficiency among all threads can complete memory recovery as soon as possible, improve the memory allocation efficiency of the first type of thread, avoid the jamming phenomenon in the interactive scene caused by the failure of the first type of thread to obtain memory in time, and improve the user experience.
[0121] An embodiment of the present application further provides a storage medium, in which a computer program is stored. When the computer program runs on a computer, the computer executes the memory recovery method described in any of the above embodiments.
[0122] It should be noted that, a person skilled in the art can understand that all or part of the steps in the various methods of the above embodiments can be completed by instructing related hardware through a computer program, and the computer program can be stored in a computer-readable storage medium, and the storage medium can include but is not limited to: read-only memory (ROM), random access memory (RAM), disk or optical disk, etc.
[0123] In addition, the terms "first", "second", and "third" in this application are used to distinguish different objects, rather than to describe a specific order. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product, or device that includes a series of steps or modules is not limited to the listed steps or modules, but some embodiments also include steps or modules that are not listed, or some embodiments also include other steps or modules inherent to these processes, methods, products, or devices.
[0124] The above is a detailed introduction to the memory recovery method, device, storage medium and electronic device provided in the embodiments of the present application. This article uses specific examples to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method and core ideas of the present application; at the same time, for technicians in this field, according to the ideas of the present application, there will be changes in the specific implementation methods and application scopes. In summary, the content of this specification should not be understood as a limitation on the present application.
Claims
1. A memory recovery method, characterized in that: include: Determine the target thread that performs the tasks related to the user interaction event; Adding a first preset tag to the target thread to mark the target thread as a first type of thread; Detecting the running status of the first type of threads; When it is detected that a first-class thread enters a blocked state, an associated thread having a constraint relationship with the first-class thread entering the blocked state is determined; adding a first preset tag to the associated thread to mark the associated thread as the first type of thread; When receiving a memory allocation request sent by a thread, determine the current amount of free memory; If the amount of free memory is less than a preset threshold, determining the type of the thread, wherein the threads are divided into multiple types of threads with different priorities, wherein a first type of thread has the highest priority, the first type of thread is a thread that executes related tasks in a user interaction event, and the same process can generate multiple types of threads; Obtain a memory recovery ratio corresponding to the type of the thread, and recycle memory pages in the allocated memory space according to the memory recovery ratio, wherein the memory recovery ratio is used to characterize the ratio between multiple memory recovery objects, and different types of memory recovery objects have different recovery efficiencies. The higher the priority of the thread, the greater the proportion of memory recovery objects with high recovery efficiency in the corresponding memory recovery ratio.
2. The memory recovery method according to claim 1, characterized in that: The acquiring of a memory recovery ratio corresponding to the type of the thread, and reclaiming memory pages in the allocated memory space according to the memory recovery ratio, includes: When the thread is a first type of thread, memory pages in the allocated memory space are recycled according to a first memory recycling ratio; When the thread is a second type of thread, memory pages in the allocated memory space are reclaimed according to a second memory reclaim ratio, wherein the proportion of memory reclaim objects with high reclaim efficiency in the first memory reclaim ratio is higher than the proportion of memory reclaim objects with high reclaim efficiency in the second memory reclaim ratio.
3. The memory recovery method according to claim 2, characterized in that: The threads are divided into three types of threads with different priorities; the memory recovery ratio corresponding to the thread type is obtained, and the memory pages in the allocated memory space are recovered according to the memory recovery ratio, and the method further includes: When the thread is a third type of thread, memory pages in the allocated memory space are reclaimed according to a third memory reclaim ratio, wherein the proportion of memory reclaim objects with high reclaim efficiency in the second memory reclaim ratio is higher than the proportion of memory reclaim objects with high reclaim efficiency in the third memory reclaim ratio.
4. The memory recovery method according to claim 3, characterized in that: The determining the type of the thread comprises: Obtaining a label carried by the thread; If the tag is a first preset tag, determining that the thread is a first type of thread; If the tag is a second preset tag, determining that the thread is a second type of thread; If the tag is the third preset tag, the thread is determined to be a third type of thread.
5. The memory recovery method according to claim 1, characterized in that: The step of determining a target thread for executing a task related to a user interaction event includes: When it is detected that a process is switched to the foreground, the foreground process is determined; A first preset thread for executing relevant tasks in the user interaction event is identified from the threads of the foreground process as the target thread.
6. The memory recovery method according to claim 1, characterized in that: The method further comprises: When it is detected that a thread is switched to the foreground for execution, it is determined whether the thread switched to the foreground for execution is a first-class thread; If not, adding a second preset tag to the thread to mark the thread as a second type of thread; When it is detected that a thread is switched to the background, it is determined whether the thread switched to the background is a first-class thread; If not, a third preset tag is added to the thread to mark the thread as a third type of thread.
7. A memory recovery device, characterized in that: include: A thread marking module is used to determine the target thread for executing the relevant tasks in the user interaction event; Adding a first preset tag to the target thread to mark the target thread as a first-class thread; detecting the running state of the first-class thread; when detecting that the first-class thread enters a blocked state, determining an associated thread having a constraint relationship with the first-class thread entering the blocked state; adding a first preset tag to the associated thread to mark the associated thread as the first-class thread; A memory detection module is used to determine the current amount of free memory when receiving a memory allocation request sent by a thread; a thread identification module, for determining the type of the thread if the amount of free memory is less than a preset threshold, wherein the threads are divided into multiple types of threads with different priorities, wherein the first type of thread has the highest priority, and the first type of thread is a thread that executes related tasks in a user interaction event; the same process can generate multiple types of threads; A memory recovery module is used to obtain a memory recovery ratio corresponding to the type of the thread, and to recycle memory pages in the allocated memory space according to the memory recovery ratio, wherein the memory recovery ratio is used to characterize the ratio between multiple memory recovery objects, and different types of memory recovery objects have different recovery efficiencies. The higher the priority of the thread, the greater the proportion of memory recovery objects with high recovery efficiency in the corresponding memory recovery ratio.
8. A storage medium having a computer program stored thereon, characterized in that: When the computer program is executed on a computer, the computer is caused to execute the memory recycling method according to any one of claims 1 to 6.
9. An electronic device comprising a processor and a memory, wherein the memory stores a computer program, characterized in that: The processor is used to execute the memory recovery method as described in any one of claims 1 to 6 by calling the computer program.
