Memory recovery method, device, storage medium and electronic device

By marking and distinguishing memory pages, priority is given to recycling memory pages of non-user interaction event threads, which solves the problem of lag in user interaction scenarios and improves thread operation efficiency and user experience.

CN111831441BActive Publication Date: 2024-12-06GUANGDONG OPPO MOBILE TELECOMMUNICATIONS CORP LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202010628803.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-07-01
Publication Date
2024-12-06
Estimated Expiration
2040-07-01

AI Technical Summary

Technical Problem

Electronic devices are prone to lag in user interaction scenarios, mainly because the memory pages occupied by threads that execute user interaction events are frequently recycled during memory recycling, resulting in inefficient thread operation.

Method used

By marking the target thread that executes user interaction events as the first type of thread, and allocates memory pages to it when memory allocation is allocated, other allocated memory pages are used as the second type of memory pages. When the system enters the memory recycling process, priority will be given to the recycling of the second type of memory page. Class II memory pages to avoid frequent recycling of memory pages of Class I threads.

Benefits of technology

It improves the thread operation efficiency of executing user interaction events, reduces lag in interactive scenarios, and improves user experience.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN111831441B_ABST
    Figure CN111831441B_ABST
Patent Text Reader

Abstract

The embodiment of the present application discloses a memory recovery method, device, storage medium and electronic device, wherein the embodiment of the present application determines a target thread for executing relevant tasks in a user interaction event, and marks the target thread as a first-class thread; when a memory allocation request sent by the first-class thread is received, memory pages are allocated to the first-class thread from the free memory space, wherein the memory pages allocated to the first-class thread are used as first-class memory pages, and the allocated memory pages other than the first-class memory pages in the allocated memory space are used as second-class memory pages; when it is detected that the system enters a memory recovery process, the second-class memory pages in the allocated memory space are recovered. This solution can avoid the frequent recovery of memory pages occupied by threads that execute relevant tasks in user interaction events, improve the operating efficiency of such threads, reduce the jamming phenomenon in interactive scenarios, and enhance user experience.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of electronic equipment technology, 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] Determine a target thread for executing related tasks in the user interaction event, and mark the target thread as a first-category thread;

[0006] When receiving a memory allocation request sent by the first class of threads, allocating memory pages to the first class of threads from the free memory space, wherein the memory pages allocated to the first class of threads are used as first class memory pages, and the allocated memory pages in the allocated memory space other than the first class of memory pages are used as second class memory pages;

[0007] When it is detected that the system enters a memory recycling process, the second type of memory pages in the allocated memory space are recycled.

[0008] In a second aspect, an embodiment of the present application further provides a memory recovery device, including:

[0009] A thread marking module, used to determine a target thread for executing a related task in a user interaction event, and mark the target thread as a first-category thread;

[0010] a memory allocation module, configured to allocate memory pages to the first-class threads from free memory space when receiving a memory allocation request sent by the first-class threads, wherein the memory pages allocated to the first-class threads are used as first-class memory pages, and the allocated memory pages in the allocated memory space other than the first-class memory pages are used as second-class memory pages;

[0011] The memory recycling module is used to recycle the second type of memory pages in the allocated memory space when it is detected that the system enters the memory recycling process.

[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 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 determines the target thread for executing the relevant tasks in the user interaction event, and marks these target threads as the first class of threads. When the system allocates memory for such threads, the memory pages allocated to the first class of threads are used as the first class of memory pages, and the allocated memory pages other than the first class of memory pages in the allocated memory space are used as the second class of memory pages. Since the running status of the threads that execute the relevant tasks in the user interaction event affects whether the jamming phenomenon will occur in the user interaction event, if the memory pages occupied by these threads are frequently recycled during the memory recycling process, it is necessary to re-read and write or perform decompression operations, which leads to low thread operation efficiency. Through the solution of the present application, the memory pages allocated to the threads that execute the relevant tasks in the user interaction event in the allocated memory space are marked. When memory recycling is performed, other memory pages except this part of the memory pages can be recycled to avoid the memory pages occupied by the threads that execute the relevant tasks in the user interaction event from being frequently recycled, thereby improving the running efficiency of such threads, reducing jamming phenomena in interactive scenarios, and improving 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, a target thread for executing a related task in a user interaction event is determined, and the target thread is marked as a first-category thread.

[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. System processes and application processes are running on the electronic device. A thread is an execution path of a process and is the smallest unit of program execution. A process can have multiple threads, but at least one thread.

[0027] 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.

[0028] In the embodiment of the present application, from the perspective of whether the running status of the thread will affect the user experience, the threads that directly or indirectly affect the user experience are marked.

[0029] 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.

[0030] 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 a first preset label 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.

[0031] 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.

[0032] 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 first preset label to the target thread to mark the thread as a first-class thread.

[0033] 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.

[0034] 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.

[0035] 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.

[0036] 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.

[0037] 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.

[0038] 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.

[0039] 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.

[0040] Among them, the first 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.

[0041] 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.

[0042] 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.

[0043] 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.

[0044] 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.

[0045] In another embodiment, after adding the first preset tag to the target thread, the method further includes: if the foreground process is an application process, then when it is detected that the foreground process switches to background operation, deleting the first 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 has also decreased. 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.

[0046] 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.

[0047] 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.

[0048] 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.

[0049] 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.

[0050] Based on this, in some embodiments, the method also includes: detecting the running status of the first type of thread; when it is detected that the first type of thread has entered a blocked state, determining an associated thread that has a constraint relationship with the first type of thread that has entered the blocked state; and adding a first preset label to the associated thread to mark the associated thread as a first type of thread.

[0051] In some embodiments, after marking the associated thread as a first type of thread, the method further includes: when it is detected that the constraint relationship is released, deleting the first preset label of the associated thread.

[0052] 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.

[0053] 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.

[0054] The above describes the way in which the application framework layer and the kernel layer identify and mark ux threads. Through any one or more of the above multiple embodiments, threads that affect the user experience are identified and marked as ux threads.

[0055] In 102, when a memory allocation request sent by a first class thread is received, memory pages are allocated to the first class thread from the free memory space, wherein the memory pages allocated to the first class thread are used as first class memory pages, and the allocated memory pages in the allocated memory space except the first class memory pages are used as second class memory pages.

[0056] Through the above embodiments, the framework layer and the kernel layer identify those threads that are directly applied to the user experience and add tags to them. The threads require the kernel to allocate system resources for them to run. Therefore, before executing the task, the thread will send a system resource allocation request to the kernel. When the kernel receives the resource allocation request, it can first determine whether the thread is a ux thread, wherein it can determine whether it is a first-class thread based on the label carried by the thread. For example, if the thread carries a ux label, the thread is determined to be a ux thread, and if the thread does not have a ux label, the thread is determined to be a normal thread.

[0057] If the thread is a ux thread, after matching memory pages for the thread, add a second preset tag, for example, UX_PAGE, to all memory pages requested by the thread, including anonymous pages and file pages, to mark these memory pages as first-class memory pages. The allocated memory pages in the allocated memory space except the first-class memory pages are treated as second-class memory pages. No tag needs to be added to the second-class memory pages, and the kernel can determine whether the allocated memory page belongs to the first-class memory page or the second-class memory page based on whether the allocated memory page has the second preset tag.

[0058] In 103, when it is detected that the system enters the memory recycling process, the second type of memory pages in the allocated memory space are recycled.

[0059] When the kernel receives a memory allocation request, it first determines the current amount of free memory. When the amount of free memory is insufficient, memory reclaiming will be performed first. The kernel uses three memory watermarks to mark the state of the system's free memory, which are, in order from small to large, the high watermark, the low watermark, and the min watermark. When the system's free memory is lower than the low watermark, the system wakes up the memory reclaiming process kswapd to reclaim memory until the amount of free memory is no lower than the high watermark. When the memory reclaiming process kswapd reclaims memory, the speed of memory consumption will be relatively slow, but 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 memory allocation efficiency of the thread is slowed down because the system needs to reclaim memory in the background.

[0060] The kernel mainly implements memory recycling 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 types of recycling 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 is read from the disk again.

[0061] For the ux thread, if the memory page occupied by the ux thread is likely to be used again shortly after being recycled, if this happens frequently, the running efficiency of the ux thread may be affected due to the need to re-decompress (anonymous page) or re-occur IO (file page) the recycled memory page, resulting in inefficient user interaction event processing and jamming.

[0062] In order to improve this defect, in an embodiment of the present application, threads related to user experience are marked, and when memory is recycled, memory pages occupied by threads other than the ux thread are preferentially recycled, while memory pages occupied by the ux thread are retained as much as possible to avoid frequent recycling of memory pages occupied by the ux thread.

[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 determines the target thread for executing the relevant tasks in the user interaction event, and marks these target threads as the first class of threads. When the system allocates memory for such threads, the memory pages allocated to the first class of threads are used as the first class of memory pages, and the allocated memory pages other than the first class of memory pages in the allocated memory space are used as the second class of memory pages. Since the running status of the threads that execute the relevant tasks in the user interaction event affects whether the jamming phenomenon will occur in the user interaction event, if the memory pages occupied by these threads are frequently recycled during the memory recycling process, it is necessary to re-read and write or perform decompression operations, which leads to low thread operation efficiency. Through the scheme of the present application, the memory pages allocated to the threads that execute the relevant tasks in the user interaction event in the allocated memory space are marked. When performing memory recycling, other memory pages except this part of the memory pages can be recycled to avoid the memory pages occupied by the threads that execute the relevant tasks in the user interaction event from being frequently recycled, thereby improving the running efficiency of such threads, reducing jamming phenomena in interactive scenarios, and improving user experience.

[0065] In one embodiment, when it is detected that the system enters a memory recovery process, the second type of memory pages in the allocated memory space are recovered, including: when it is detected that the system enters a memory recovery process, a second memory page linked list corresponding to the second type of memory pages is obtained, wherein the second memory page linked list is a least recently used linked list; anonymous pages and file pages occupied by second type threads are recovered according to the second memory page linked list, wherein threads other than the first type of threads are recorded as second type threads.

[0066] In this embodiment, when reclaiming the memory pages occupied by non-ux threads, the occupied anonymous pages and file pages can be reclaimed according to the preset ratio of anonymous pages and file pages. There are corresponding LRU (Least Recently Used) linked lists for the occupied anonymous pages and file pages. When reclaiming, the LRU linked lists are used to reclaim the least recently used memory pages first.

[0067] Alternatively, in some other embodiments, anonymous pages may be recycled first, and when anonymous pages do not meet recycling requirements, file pages may be recycled. For example, the anonymous pages and file pages occupied by the second type of threads are recycled according to the second memory page linked list, including: recycling the anonymous pages occupied by the second type of threads according to the second anonymous page linked list until the amount of free memory is not less than a first preset threshold; if the amount of free memory is less than the first preset threshold after the anonymous pages occupied by the second type of threads are recycled, recycling the file pages occupied by the second type of threads according to the second file page linked list until the amount of free memory is not less than the first preset threshold.

[0068] The method described in the above embodiments is further described in detail below with examples.

[0069] See also Figure 2 , Figure 2 A second flow chart of the memory recycling method provided in an embodiment of the present invention.

[0070] Methods include:

[0071] In 201 , a target thread for executing a task related to a user interaction event is determined, and the target thread is marked as a first-category thread.

[0072] In 202, when a memory allocation request sent by a first-category thread is received, a memory page is allocated to the first-category thread from a free memory space.

[0073] In 203, the memory pages allocated to the first-category threads are used as first-category memory pages, and the allocated memory pages in the allocated memory space other than the first-category memory pages are used as second-category memory pages.

[0074] Among them, regarding the method of identifying and marking the ux thread from the system thread, and the method of distinguishing the first type of memory page from the second type of memory page, please refer to the above embodiment, and no further details will be given here.

[0075] In 204, when it is detected that the system enters a memory reclaiming process, anonymous pages occupied by the second type of threads are reclaimed according to the second anonymous page linked list until the amount of free memory is not less than a first preset threshold.

[0076] In 205, the file pages occupied by the second type of threads are reclaimed according to the second file page linked list until the amount of free memory is not less than a first preset threshold.

[0077] If the amount of free memory is less than the second preset threshold after the file pages occupied by the second type of threads are reclaimed, execute 206 .

[0078] In 206, a first anonymous page linked list corresponding to the first type of memory pages is obtained, and anonymous pages occupied by the first type of threads are reclaimed according to the first anonymous page linked list until the amount of free memory is not less than a second preset threshold.

[0079] If the amount of free memory is less than the second preset threshold after the anonymous pages occupied by the first type of threads are reclaimed, execute 207 .

[0080] In 207, the file pages occupied by the first type of threads are reclaimed according to the first file page linked list until the amount of free memory is not less than a second preset threshold.

[0081] In addition to the low watermark and high watermark originally set by the kernel, this application adds a ux_high watermark for the ux thread, where the high watermark is the first preset threshold, the ux_high watermark is the second preset threshold, and the ux_high watermark is greater than the low watermark but less than the high watermark.

[0082] When the kernel receives a memory allocation request, it first determines the current amount of free memory. When the amount of free memory is insufficient, memory recovery will be performed first. After the kernel enters the memory recovery process, it first recovers the anonymous pages occupied by non-ux threads. If the amount of free memory is not less than the first preset threshold by recovering the anonymous pages occupied by non-ux threads, the memory recovery process is stopped. On the contrary, if the amount of free memory is less than the first preset threshold after the anonymous pages occupied by the second type of threads are recovered, the file pages occupied by non-ux threads are recovered. If the amount of free memory is not less than the first preset threshold by recovering the file pages occupied by non-ux threads, the memory recovery process is stopped. On the contrary, if the amount of free memory does not reach the ux_high watermark after the file pages occupied by the second type of threads are recovered, the memory occupied by the ux threads is recovered until the amount of free memory is not less than the ux_high watermark. Among them, for the memory pages occupied by the ux threads, the anonymous pages can be recovered first, and then the file pages can be recovered, or both memory pages can be recovered according to the preset ratio between anonymous pages and files.

[0083] In one embodiment, the difference between the ux_high watermark and the high watermark is smaller than the difference between the ux_high watermark and the low watermark, so that when reclaiming memory, more memory pages occupied by non-ux threads are reclaimed, and as few memory pages occupied by ux threads as possible are reclaimed.

[0084] When it is detected that the free memory amount is not less than the second preset threshold and is less than the first preset threshold, 208 is executed.

[0085] In 208, it is detected whether there are memory pages occupied by the second type of threads that can be reclaimed.

[0086] In 209, when it is detected that the memory pages occupied by the second type of threads are reclaimable, the memory pages occupied by the second type of threads are reclaimed until the amount of free memory is not less than a first preset threshold.

[0087] During the memory recovery process, once the amount of free memory recovered is not less than the second preset threshold but less than the first preset threshold, the memory pages occupied by the ux thread will no longer be recovered, but instead a check will be made as to whether there are recyclable memory pages occupied by the second type of threads. When it is detected that there are recyclable memory pages occupied by the second type of threads, the memory pages occupied by the second type of threads will be recovered until the amount of free memory is not less than the first preset threshold.

[0088] As can be seen from the above, the memory recycling method proposed in the embodiment of the present invention marks the threads related to the user experience, and when recycling the memory, the memory pages occupied by other threads except the ux thread are recycled first, and the memory pages occupied by the ux thread are reserved as much as possible, so as to avoid the frequent recycling of the memory pages occupied by the ux thread. In addition, the problem of low thread operation efficiency due to the need for re-reading and writing or decompression operations is avoided, the operation efficiency of threads related to the user experience is improved, the jamming phenomenon in the interactive scenario is reduced, and the user experience is improved.

[0089] In one embodiment, a memory recovery device is also provided. Figure 3 , Figure 3 The 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 thread marking module 301, a memory allocation module 302 and a memory recovery module 303, as follows:

[0090] A thread marking module 301 is used to determine a target thread for executing a related task in a user interaction event, and mark the target thread as a first-category thread;

[0091] a memory allocation module 302, configured to allocate memory pages to the first-class threads from the free memory space when receiving a memory allocation request sent by the first-class threads, wherein the memory pages allocated to the first-class threads are used as first-class memory pages, and the allocated memory pages in the allocated memory space other than the first-class memory pages are used as second-class memory pages;

[0092] The memory recycling module 303 is used to recycle the second type of memory pages in the allocated memory space when it is detected that the system enters the memory recycling process.

[0093] In some embodiments, the memory recycling module 303 is further used to: when it is detected that the system enters the memory recycling process, obtain a second memory page linked list corresponding to the second type of memory page, wherein the second memory page linked list is a least recently used linked list;

[0094] The anonymous pages and file pages occupied by the second type of threads are reclaimed according to the second memory page linked list, wherein threads other than the first type of threads are recorded as the second type of threads.

[0095] In some embodiments, the memory recycling module 303 is further used to: recycle the anonymous pages occupied by the second type of threads according to the second anonymous page linked list until the amount of free memory is not less than the first preset threshold;

[0096] If the amount of free memory is less than the first preset threshold after the anonymous pages occupied by the second type of threads are reclaimed, the file pages occupied by the second type of threads are reclaimed according to the second file page linked list until the amount of free memory is not less than the first preset threshold.

[0097] In some embodiments, the memory recycling module 303 is further used to: if the amount of free memory is less than the second preset threshold after the file pages occupied by the second type of threads are recycled, obtain a first memory page linked list corresponding to the first type of memory pages, wherein the first memory page linked list is a least recently used linked list, and the second preset threshold is less than the first preset threshold;

[0098] The anonymous pages and / or file pages occupied by the first type of threads are reclaimed according to the first memory page linked list until the amount of free memory is no less than the second preset threshold.

[0099] In some embodiments, the memory recycling module 303 is further used to: recycle the anonymous pages occupied by the first type of threads according to the first anonymous page linked list until the amount of free memory is not less than the second preset threshold;

[0100] If the amount of free memory is less than the second preset threshold after the anonymous pages occupied by the first type of threads are reclaimed, the file pages occupied by the first type of threads are reclaimed according to the first file page linked list until the amount of free memory is not less than the second preset threshold.

[0101] In some embodiments, the memory recycling module 303 is further used to: when it is detected that the amount of free memory is not less than the second preset threshold and less than the first preset threshold, detect whether there are memory pages occupied by the second type of threads that can be recycled;

[0102] When it is detected that the memory pages occupied by the second type of threads are reclaimable, the memory pages occupied by the second type of threads are reclaimed until the amount of free memory is not less than the first preset threshold.

[0103] In some embodiments, the thread marking module 301 is further used to: determine the foreground process when detecting that a process is switched to the foreground;

[0104] A target thread for executing relevant tasks in the user interaction event is determined from the threads of the foreground process, and the target thread is marked as a first-category thread.

[0105] In some embodiments, the thread marking module 301 is further used to: during the operation of the foreground process, when it is detected that a new thread is created, determine whether the newly created thread is used to execute a related task in the user interaction event;

[0106] When the newly created thread is used to perform related tasks in the user interaction event, the newly created thread is marked as a first-class thread.

[0107] In some embodiments, the thread marking module 301 is further used to: when detecting that the first type of thread enters a blocked state, determine an associated thread having a constraint relationship with the first type of thread entering the blocked state;

[0108] The first preset tag is added to the associated thread to mark the associated thread as a first-category thread.

[0109] 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.

[0110] As can be seen from the above, the memory recovery device proposed in the embodiment of the present application determines the target thread for executing the relevant tasks in the user interaction event, and marks these target threads as the first class of threads. When the system allocates memory for such threads, the memory pages allocated to the first class of threads are used as the first class of memory pages, and the allocated memory pages other than the first class of memory pages in the allocated memory space are used as the second class of memory pages. Since the running status of the threads that execute the relevant tasks in the user interaction event affects whether the jamming phenomenon will occur in the user interaction event, if the memory pages occupied by these threads are frequently recycled during the memory recycling process, it is necessary to re-read and write or perform decompression operations, which leads to low thread operation efficiency. Through the scheme of the present application, the memory pages allocated to the threads that execute the relevant tasks in the user interaction event in the allocated memory space are marked. When performing memory recycling, other memory pages except this part of the memory pages can be recycled to avoid the memory pages occupied by the threads that execute the relevant tasks in the user interaction event from being frequently recycled, thereby improving the running efficiency of such threads, reducing jamming phenomena in interactive scenarios, and improving user experience.

[0111] 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.

[0112] 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.

[0113] 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.

[0114] 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:

[0115] Determine a target thread for executing related tasks in the user interaction event, and mark the target thread as a first-category thread;

[0116] When receiving a memory allocation request sent by the first class of threads, allocating memory pages to the first class of threads from the free memory space, wherein the memory pages allocated to the first class of threads are used as first class memory pages, and the allocated memory pages in the allocated memory space other than the first class of memory pages are used as second class memory pages;

[0117] When it is detected that the system enters a memory recycling process, the second type of memory pages in the allocated memory space are recycled.

[0118] In some embodiments, see Figure 5 , Figure 5A 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.

[0119] 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.

[0120] 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.

[0121] The control circuit 405 is electrically connected to the display screen 404 and is used to control the display screen 404 to display information.

[0122] 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.

[0123] 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.

[0124] 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.

[0125] 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.

[0126] 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.

[0127] 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:

[0128] Determine a target thread for executing related tasks in the user interaction event, and mark the target thread as a first-category thread;

[0129] When receiving a memory allocation request sent by the first class of threads, allocating memory pages to the first class of threads from the free memory space, wherein the memory pages allocated to the first class of threads are used as first class memory pages, and the allocated memory pages in the allocated memory space other than the first class of memory pages are used as second class memory pages;

[0130] When it is detected that the system enters a memory recycling process, the second type of memory pages in the allocated memory space are recycled.

[0131] As can be seen from the above, an embodiment of the present application provides an electronic device, which determines the target thread for executing the relevant task in the user interaction event, and marks these target threads as first-class threads. When the system allocates memory for such threads, the memory page allocated to the first-class thread is used as the first-class memory page, and the allocated memory page other than the first-class memory page in the allocated memory space is used as the second-class memory page. Since the running status of the thread executing the relevant task in the user interaction event affects whether the jamming phenomenon will occur in the user interaction event, if the memory pages occupied by these threads are frequently recycled during the memory recycling process, it is necessary to re-read and write or perform decompression operations, which leads to low thread operation efficiency. Through the scheme of the present application, the memory page allocated to the thread executing the relevant task in the user interaction event in the allocated memory space is marked, and when memory recycling is performed, other memory pages except this part of the memory page can be recycled to avoid the memory pages occupied by the thread executing the relevant task in the user interaction event from being frequently recycled, thereby improving the running efficiency of such threads, reducing the jamming phenomenon in the interactive scene, and improving the user experience.

[0132] 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.

[0133] 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.

[0134] 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.

[0135] 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 a target thread for executing related tasks in the user interaction event, and mark the target thread as a first-category thread; The running state of the first-class thread is detected, and 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; a first preset label is added to the associated thread to mark the associated thread as a first-class thread; when it is detected that the constraint relationship is released, the first preset label of the associated thread is deleted; wherein, in a user interaction scenario, application freeze occurs, and the application-level thread and system-level thread corresponding to the freeze are determined through scenario analysis, and when the process switches to the foreground to run, the application-level thread is marked as a first-class thread; when it is detected that the system has created the system-level thread, the system-level thread is marked as a first-class thread; when the foreground process is an application process, when it is detected that the foreground process switches to the background, the first-class thread corresponding to the application process is restored to a common thread, and even if the foreground and background switching of the process occurs, the system-level thread always remains as a first-class thread; When receiving a memory allocation request sent by the first class of threads, allocating memory pages for the first class of threads from the free memory space includes: adding a second preset tag to all memory pages including anonymous pages and file pages applied for by the first class of threads to mark the memory pages as first class memory pages, so that the kernel determines whether the allocated memory pages belong to the first class of memory pages or the second class of memory pages according to whether the allocated memory pages have the second preset tag; wherein the memory pages allocated to the first class of threads are taken as first class memory pages, and the allocated memory pages in the allocated memory space except the first class of memory pages are taken as second class memory pages; When it is detected that the system enters a memory recovery process, the second-class memory pages in the allocated memory space are recovered, including: when it is detected that the system enters a memory recovery process, the anonymous pages occupied by the second-class threads are recovered according to the second anonymous page linked list until the amount of free memory is not less than a first preset threshold; if the amount of free memory is less than the first preset threshold after the anonymous pages occupied by the second-class threads are recovered, the file pages occupied by the second-class threads are recovered according to the second file page linked list until the amount of free memory is not less than the first preset threshold; if the amount of free memory is less than the second preset threshold after the file pages occupied by the second-class threads are recovered, the anonymous pages occupied by the first-class threads are recovered according to the first anonymous page linked list until the amount of free memory is not less than the second preset threshold; if the amount of free memory is less than the second preset threshold after the anonymous pages occupied by the first-class threads are recovered, the file pages occupied by the first-class threads are recovered according to the first file page linked list until the amount of free memory is not less than the second preset threshold.

2. The memory recovery method according to claim 1, characterized in that: Before the step of reclaiming the anonymous pages occupied by the second type of threads according to the second anonymous page linked list, the method further includes: obtaining a second memory page linked list corresponding to the second type of memory pages, wherein the second memory page linked list is a least recently used linked list; Among them, other threads except the first-category threads are recorded as the second-category threads.

3. The memory recovery method according to claim 2, characterized in that: The second memory page linked list includes a second anonymous page linked list and a second file page linked list.

4. The memory recovery method according to claim 3, characterized in that: Before the step of reclaiming the anonymous pages occupied by the first type of threads according to the first anonymous page linked list, the method further includes: A first memory page linked list corresponding to the first type of memory page is obtained, wherein the first memory page linked list is a least recently used linked list, and the second preset threshold is less than the first preset threshold.

5. The memory recovery method according to claim 4, characterized in that: The first memory page linked list includes a first anonymous page linked list and a first file page linked list.

6. The memory recovery method according to claim 5, characterized in that: After the step of reclaiming the file pages occupied by the first type of threads according to the first file page linked list until the amount of free memory is not less than the second preset threshold, the method further includes: When it is detected that the amount of free memory is not less than the second preset threshold and less than the first preset threshold, detecting whether there are memory pages occupied by the second type of threads that can be reclaimed; When it is detected that the memory pages occupied by the second type of threads are reclaimable, the memory pages occupied by the second type of threads are reclaimed until the amount of free memory is not less than the first preset threshold.

7. 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 and marking the target thread as a first-category thread includes: When it is detected that a process is switched to the foreground, the foreground process is determined; A target thread for executing relevant tasks in the user interaction event is determined from the threads of the foreground process, and the target thread is marked as a first-category thread.

8. The memory recovery method according to claim 7, characterized in that: The method further comprises: During the operation of the foreground process, when it is detected that a new thread is created, determining whether the newly created thread is used to execute a related task in the user interaction event; When the newly created thread is used to perform related tasks in the user interaction event, the newly created thread is marked as a first-class thread.

9. A memory recovery device, characterized in that: include: A thread marking module, used to determine a target thread for executing a related task in a user interaction event, and mark the target thread as a first-category thread; The running state of the first-class thread is detected, and 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; a first preset label is added to the associated thread to mark the associated thread as a first-class thread; when it is detected that the constraint relationship is released, the first preset label of the associated thread is deleted; wherein, in a user interaction scenario, application freeze occurs, and the application-level thread and system-level thread corresponding to the freeze are determined through scenario analysis, and when the process switches to the foreground to run, the application-level thread is marked as a first-class thread; when it is detected that the system has created the system-level thread, the system-level thread is marked as a first-class thread; when the foreground process is an application process, when it is detected that the foreground process switches to the background, the first-class thread corresponding to the application process is restored to a common thread, and even if the foreground and background switching of the process occurs, the system-level thread always remains as a first-class thread; A memory allocation module, configured to allocate memory pages to the first-class threads from free memory space upon receiving a memory allocation request sent by the first-class threads, including: adding a second preset tag to all memory pages including anonymous pages and file pages applied for by the first-class threads to mark the memory pages as first-class memory pages, so that the kernel determines whether the allocated memory pages belong to the first-class memory pages or the second-class memory pages according to whether the allocated memory pages have the second preset tag; wherein the memory pages allocated to the first-class threads are taken as first-class memory pages, and the allocated memory pages in the allocated memory space except the first-class memory pages are taken as second-class memory pages; A memory recovery module, used for recovering the second-category memory pages in the allocated memory space when it is detected that the system enters a memory recovery process, including: when it is detected that the system enters a memory recovery process, recovering the anonymous pages occupied by the second-category threads according to the second anonymous page linked list until the amount of free memory is not less than a first preset threshold; if the amount of free memory is less than the first preset threshold after the anonymous pages occupied by the second-category threads are recovered, recovering the file pages occupied by the second-category threads according to the second file page linked list until the amount of free memory is not less than the first preset threshold; if the amount of free memory is less than the second preset threshold after the file pages occupied by the second-category threads are recovered, recovering the anonymous pages occupied by the first-category threads according to the first anonymous page linked list until the amount of free memory is not less than the second preset threshold; if the amount of free memory is less than the second preset threshold after the anonymous pages occupied by the first-category threads are recovered, recovering the file pages occupied by the first-category threads according to the first file page linked list until the amount of free memory is not less than the second preset threshold.

10. 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 8.

11. 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 recycling method as described in any one of claims 1 to 8 by calling the computer program.