Memory recovery method, device, storage medium and electronic device
By setting a specific memory recycling threshold for the user interaction event thread of the electronic device, the problem of device lag when running multiple applications is solved, and the memory allocation success rate and user experience are improved.
Patent Information
- Application Number
- CN202010628903.X
- 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 poor user experience.
By setting a separate memory recycling threshold (first preset threshold) for the thread that executes user interaction events and setting a higher threshold (second preset threshold) for other threads, the thread type is determined when a memory allocation request is made and the corresponding memory allocation is performed. Recycling processing ensures that threads of preset types can obtain memory allocation in time, reducing the probability of slow allocation paths.
It improves the success rate of memory allocation for user interaction events, reduces the occurrence of lags, and ensures the smoothness of user interaction scenarios.
Smart Images

Figure CN111831444B_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, determining the amount of free memory in the memory space, and judging whether the thread is a thread of a preset type, wherein the thread of the preset type is a thread that executes a task related to a user interaction event;
[0006] When the thread is a thread of a preset type, determining whether the amount of free memory is less than a first preset threshold, and performing memory recycling processing when the amount of free memory is less than the first preset threshold;
[0007] When the thread is not a thread of a preset type, determining whether the amount of free memory is less than a second preset threshold, wherein the second preset threshold is greater than the first preset threshold;
[0008] If the amount of free memory is less than the second preset threshold, memory recycling is performed.
[0009] In a second aspect, an embodiment of the present application further provides a memory recovery device, including:
[0010] A memory detection module, used to determine the amount of free memory in the memory space when receiving a memory allocation request sent by a thread;
[0011] A thread identification module, used to determine whether the thread is a thread of a preset type, wherein the thread of the preset type is a thread that executes a task related to a user interaction event;
[0012] The memory detection module is further configured to determine whether the amount of free memory is less than a first preset threshold when the thread is a thread of a preset type;
[0013] A memory recycling module, used for performing memory recycling when the amount of free memory is less than the first preset threshold;
[0014] The memory detection module is further configured to, when the thread is not a thread of a preset type, determine whether the amount of free memory is less than a second preset threshold, wherein the second preset threshold is greater than the first preset threshold;
[0015] The memory recycling module is used to perform memory recycling processing if the amount of free memory is less than the second preset threshold.
[0016] 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.
[0017] 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.
[0018] The technical solution provided by the embodiment of the present application regards the thread that executes the relevant tasks in the user interaction event as a thread of a preset type, sets a corresponding first preset threshold for this type of thread, and sets a corresponding second preset threshold for other threads except this type of thread, and the second preset threshold is greater than the first preset threshold. Through this solution, the amount of memory between the first preset threshold and the second preset threshold is only used by threads of the preset type. For threads of non-preset types, if the amount of free memory is already lower than the second preset threshold, the system will enter the slow allocation path due to memory recycling processing. However, for threads that execute relevant tasks in user interaction events, even if the amount of free memory is already lower than the second preset threshold, as long as it is not less than the first preset threshold, the system will still perform normal memory allocation for this type of thread, reducing the probability of this type of thread entering the slow allocation path, so that the memory allocation request of the preset type of thread can be responded to in a timely manner, and the memory allocation success rate of this type of thread is improved, so that the user interaction event can be completed in time, avoiding jamming in user interaction scenarios. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] 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.
[0020] Figure 1A first flow chart of the memory recycling method provided in an embodiment of the present application.
[0021] Figure 2 A schematic diagram of a memory allocation scenario in related technologies.
[0022] Figure 3 A second flow chart of the memory recycling method provided in an embodiment of the present application.
[0023] Figure 4 A schematic diagram of the structure of a memory recovery device provided in an embodiment of the present application.
[0024] Figure 5 A first structural schematic diagram of an electronic device provided in an embodiment of the present application.
[0025] Figure 6 A second structural schematic diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0026] 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.
[0027] 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.
[0028] 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.
[0029] 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:
[0030] In 101, when a memory allocation request sent by a thread is received, the amount of free memory in the memory space is determined, and it is determined whether the thread is a thread of a preset type, wherein the thread of the preset type is a thread that executes related tasks in a user interaction event.
[0031] 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.
[0032] A thread is an execution path of a process, the smallest unit of program execution, and the basic unit of CPU scheduling and dispatching. A process can have multiple threads, but at least one thread. 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 WeChat application process will start a main thread to execute code, and during the execution, it will also start other sub-threads to assist in running the task code of each part.
[0033] 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.
[0034] Without distinguishing threads, the Linux kernel uses three memory watermarks to mark the state of the system's free memory. In order from small to large, they are: 2. 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 not lower than the high watermark. When the system's free memory is lower than the min watermark, the memory allocation thread will simultaneously perform directreclaim memory recovery until the memory allocation demand is met. This mechanism means that when the memory recovery process kswapd recycles memory, the speed of memory consumption will be relatively slow, but memory allocation will also enter the slow allocation path (slowpath), and when the system's free memory is lower than the min watermark, the memory allocation thread will also stop memory allocation because it enters the memory recovery process. It should be noted that the slow allocation path here is an abstract concept, which means that the system's memory allocation efficiency for threads slows down due to the need to recycle memory in the background.
[0035] Under such an allocation mechanism, if the thread that executes related tasks in the user interaction event also enters the slow allocation path and cannot be allocated memory in time, it will cause system lag that can be perceived by users in the interactive scenario.
[0036] For example, see Figure 2 , Figure 2 A schematic diagram of a memory allocation scenario in the related technology. Thread 1 of an application performs data backup in the background. Assuming that thread 1 needs to continuously allocate some memory, if WeChat chat is in progress in the foreground, WeChat thread 2 also needs to allocate memory. If the system free memory is low at this time, for example, the system free memory is lower than the low watermark, both thread 1 and thread 2 enter the slow allocation path at this time, and the system needs to recycle memory in the background. It is very likely that the memory just recycled is allocated by thread 1 in the background, and WeChat, which directly interacts with the user, may cause user-perceived lag due to slow memory allocation, affecting the user experience.
[0037] In the embodiment of the present application, by setting a separate memory recycling waterline ux_low (first preset threshold) for threads of preset types, memory is timely allocated to threads of related tasks in these user interaction events. The low waterline is the second preset threshold corresponding to threads other than threads of preset types, the high waterline is the third preset threshold, and the min waterline is the fourth preset threshold.
[0038] In the embodiment of the present application, whether the thread that executes the relevant tasks in the user interaction event can run smoothly determines whether a 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, threads of a preset type.
[0039] The system architecture of the electronic device includes at least an application framework layer and a kernel layer. The application framework layer or the kernel layer will mark some threads as ux threads. 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. In addition, the kernel layer is also responsible for scheduling and executing these threads.
[0040] 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.
[0041] For example, in one embodiment, "determining a target thread for executing related tasks in a user interaction event from threads of a foreground process, and marking the target thread as a thread of a preset type" includes: identifying a first preset thread for executing related tasks in a user interaction event from threads of a foreground process as the target thread; adding a preset tag to the target thread to mark the thread as a thread of a preset type.
[0042] 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.
[0043] 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.
[0044] 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.
[0045] 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.
[0046] 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.
[0047] In addition, in another embodiment, the method further includes: when it is detected that a second preset thread is created, marking the created second preset thread as a thread of a preset type, wherein the second preset thread is a system-level thread.
[0048] 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.
[0049] 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.
[0050] 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.
[0051] In another embodiment, "determining a target thread for executing relevant tasks in a user interaction event from threads of the foreground process, and marking the target thread as a thread of a preset type" 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 thread of a preset type.
[0052] 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.
[0053] 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.
[0054] 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.
[0055] 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.
[0056] 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.
[0057] Based on this, in some embodiments, the method also includes: detecting the running status of a thread of a preset type; when it is detected that a thread of the preset type has entered a blocked state, determining an associated thread that has a constraint relationship with the thread of the preset type that has entered the blocked state; and adding a preset label to the associated thread to mark the associated thread as a thread of the preset type.
[0058] In some embodiments, after marking the associated thread as a thread of a preset type, the method further includes: when it is detected that the constraint relationship is released, deleting the preset label of the associated thread.
[0059] 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.
[0060] 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.
[0061] In the above manner, all ux threads including static ux threads and dynamic ux threads can be identified, thereby distinguishing threads related to user experience from other common threads in the system.
[0062] When the kernel receives a memory allocation request sent by a thread, it determines whether the thread is a ux thread by determining whether the thread carries a preset tag, for example, a ux tag. If the thread has a preset tag, the thread is determined to be a thread of a preset type; if the thread does not have a preset tag, the thread is determined not to be a thread of a preset type.
[0063] In addition, after receiving the memory allocation request sent by the thread, the kernel also detects the amount of free memory. The free memory refers to the capacity of the remaining memory space in the entire memory space of the system that has not been allocated and is in an idle state. For example, the total memory space of the electronic device is 4G, of which 3G has been allocated to one or more running processes, and the other 1G is in an idle state. The current free memory of the electronic device is 1G. The numbers here are only examples for readers to understand the solution and do not constitute a limitation on this solution.
[0064] In 102, when the thread is a thread of a preset type, it is determined whether the amount of free memory is less than a first preset threshold, and when the amount of free memory is less than the first preset threshold, memory recycling processing is performed.
[0065] After the kernel determines that the thread sending the memory allocation request is a ux thread, it determines whether to enter the memory recovery process according to the first preset threshold corresponding to the ux thread. If the amount of free memory is less than the first preset threshold, memory recovery is performed. Otherwise, if the amount of free memory is not less than the first preset threshold, memory is allocated to the thread.
[0066] In 103, when the thread is not a thread of a preset type, it is determined whether the amount of free memory is less than a second preset threshold, wherein the second preset threshold is greater than the first preset threshold.
[0067] In 104, if the amount of free memory is less than the second preset threshold, memory recycling is performed.
[0068] When the kernel determines that the thread sending the memory allocation request is not a ux thread, it determines whether to enter the memory recovery process according to the second preset threshold corresponding to the non-ux thread. If the amount of free memory is less than the second preset threshold, memory recovery is performed. Otherwise, if the amount of free memory is not less than the second preset threshold, memory is allocated to the thread.
[0069] When performing memory recycling, a background thread dedicated to memory recycling, such as a kswapd thread, can be awakened to perform memory recycling operations. When performing memory recycling, anonymous pages can be recycled first. When the recycled anonymous pages are not enough to obtain a free memory amount not less than a third preset threshold, the file pages can be recycled.
[0070] According to the scheme of the embodiment of the present application, when the amount of free memory is less than the second preset threshold but greater than the first preset threshold, thread 2 of the foreground WeChat application is a ux thread, which can quickly and successfully allocate memory without waiting, while thread 1, which is not a ux thread, will enter the slow allocation path. This scheme allows only the ux thread to allocate memory of the size of the [ux_low waterline, low waterline] area, and other threads cannot allocate it. This makes it highly likely that when the ux thread allocates memory, it does not need to enter the slow path to wait for memory to be recovered before the allocation is successful, reducing latency and improving user experience.
[0071] 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.
[0072] As can be seen from the above, the memory recovery method provided by the embodiment of the present application takes the thread that executes the relevant tasks in the user interaction event as a thread of a preset type, sets a corresponding first preset threshold for this type of thread, and sets a corresponding second preset threshold for other threads except this type of thread, and the second preset threshold is greater than the first preset threshold. Through this scheme, the amount of memory between the first preset threshold and the second preset threshold is only used by threads of the preset type. For threads of non-preset types, if the amount of free memory is already lower than the second preset threshold, the system will enter the slow allocation path due to memory recovery processing. However, for threads that execute relevant tasks in user interaction events, even if the amount of free memory is already lower than the second preset threshold, as long as it is not less than the first preset threshold, the system will still perform normal memory allocation for this type of thread, reducing the probability of this type of thread entering the slow allocation path, so that the memory allocation request of the preset type of thread can be responded to in a timely manner, and the memory allocation success rate of this type of thread is improved, so that the user interaction event can be completed in time, avoiding jamming in user interaction scenarios.
[0073] In some embodiments, memory recycling processing is performed, including: determining target memory pages occupied by threads that are not of a preset type; recycling the target memory pages so that the amount of free memory reaches a third preset threshold, wherein the third preset threshold is greater than the second preset threshold.
[0074] In this embodiment, when performing memory recycling, memory pages occupied by non-ux threads are preferentially selected for recycling, so as to avoid frequent recycling of memory pages occupied by ux threads, improve their performance when they are needed again, and further reduce system jams.
[0075] The method described in the above embodiments is further described in detail below with examples.
[0076] See also Figure 3 Figure 3 A second flow chart of a memory recycling method provided by an embodiment of the present invention. The method comprises:
[0077] In 201, a target thread for executing a task related to a user interaction event is determined.
[0078] In 202 , a preset tag is added to the target thread to mark the target thread as a thread of a preset type.
[0079] In the embodiment of the present application, whether the thread executing the relevant tasks in the user interaction event can run smoothly determines whether the user-perceivable lag will occur in the user interaction event, so these threads closely related to the user experience are recorded as ux (user experience) threads.
[0080] The system architecture of the electronic device includes at least an application framework layer and a kernel layer. The application framework layer or the kernel layer will mark some threads as ux threads. 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. In addition, the kernel layer is also responsible for scheduling and executing these threads.
[0081] Since in the process of executing user interaction events, in addition to the threads of the corresponding process, some system-level threads may also be involved to complete the task, 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.
[0082] In addition, when it is detected that a process has switched to the foreground, the foreground process is first determined. From the threads of the foreground process, a thread used to execute relevant tasks in the user interaction event is determined as the target thread. For example, a first preset thread and some temporarily created task threads used to execute relevant tasks in the user interaction event. For the first preset thread, it can be determined whether the attribute information of the thread of the foreground process matches the preset attribute information to determine the first preset thread, and these first preset threads are marked as ux threads. For temporarily created task threads, it can be determined whether these threads are threads created during the execution of the user interaction event to execute relevant tasks. If so, they are also marked as ux threads.
[0083] There are also some threads that do not directly execute tasks related to user interaction events, but the operation of these threads will also affect the operation of static ux threads, and thus 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, these threads that have a constraint relationship with the static ux threads are also marked as ux threads. Once this constraint relationship ends, the thread will be restored to a non-ux thread.
[0084] In addition, it should be noted that the marking of dynamic ux threads is performed at the kernel layer. From the kernel's perspective, it does not distinguish which process these threads belong to. Therefore, the kernel only marks the dynamic ux threads based on the above constraints.
[0085] In 203, when a memory allocation request sent by a thread is received, the amount of free memory in the memory space is determined, and it is determined whether the thread is a thread of a preset type, wherein the thread of the preset type is a thread that executes related tasks in a user interaction event.
[0086] When the kernel receives a memory allocation request sent by a thread, it determines whether the thread is a ux thread by determining whether the thread carries a preset tag, for example, a ux tag. If the thread has a preset tag, the thread is determined to be a thread of a preset type. If the thread does not have a preset tag, the thread is determined not to be a thread of a preset type. In addition, after receiving a memory allocation request sent by a thread, the kernel also detects the amount of free memory.
[0087] When the thread is a thread of a preset type, execute 204 ; when the thread is not a thread of a preset type, execute 207 .
[0088] In 204, it is determined whether the amount of free memory is less than a first preset threshold.
[0089] When the amount of free memory is less than the first preset threshold, execute 205 ; when the amount of free memory is not less than the first preset threshold, execute 206 .
[0090] In 205, memory recycling processing is performed.
[0091] At 206, the memory allocation request is responded to based on the amount of free memory.
[0092] After the kernel determines that the thread sending the memory allocation request is a ux thread, it determines whether to enter the memory recovery process according to the first preset threshold corresponding to the ux thread. If the amount of free memory is less than the first preset threshold, memory recovery is performed. Otherwise, if the amount of free memory is not less than the first preset threshold, memory is allocated to the thread.
[0093] In 207, it is determined whether the amount of free memory is less than a second preset threshold, wherein the second preset threshold is greater than the first preset threshold.
[0094] When the amount of free memory is less than the second preset threshold, execute 205 ; when the amount of free memory is not less than the second preset threshold, execute 206 .
[0095] When the kernel determines that the thread sending the memory allocation request is not a ux thread, it determines whether to enter the memory recovery process according to the second preset threshold corresponding to the non-ux thread. If the amount of free memory is less than the second preset threshold, memory recovery is performed. Otherwise, if the amount of free memory is not less than the second preset threshold, memory is allocated to the thread.
[0096] From the above, it can be seen that the memory recovery method proposed in the embodiment of the present invention determines whether the thread that executes the relevant tasks in the user interaction event runs smoothly, which determines whether there will be a jamming phenomenon in the interaction scene. Therefore, in this application, the thread that executes the relevant tasks in the user interaction event is marked as a ux thread, and a corresponding first preset threshold is set for this type of thread, and a corresponding second preset threshold is set for other threads except this type of thread, and the second preset threshold is greater than the first preset threshold. For the thread that executes the relevant tasks in the user interaction event, even if the amount of free memory is lower than the second preset threshold, as long as it is not less than the first preset threshold, the system will still perform normal memory allocation for this type of thread, reducing the probability of this type of thread entering the slow allocation path, so that the memory allocation request of the preset type of thread can be responded to in time, and the memory allocation success rate of this type of thread is improved, so that the user interaction event can be completed in time, avoiding jamming in the user interaction scene.
[0097] In one embodiment, a memory recovery device is also provided. Figure 4 , Figure 4The 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:
[0098] The memory detection module 301 is used to determine the amount of free memory in the memory space when receiving a memory allocation request sent by a thread;
[0099] A thread identification module 302, used to determine whether the thread is a thread of a preset type, wherein the thread of the preset type is a thread that executes a task related to a user interaction event;
[0100] The memory detection module 301 is further used to determine whether the amount of free memory is less than a first preset threshold when the thread is a thread of a preset type;
[0101] A memory recycling module 303, configured to perform memory recycling when the amount of free memory is less than the first preset threshold;
[0102] The memory detection module 301 is further used to determine whether the amount of free memory is less than a second preset threshold when the thread is not a thread of a preset type, wherein the second preset threshold is greater than the first preset threshold;
[0103] The memory recycling module 303 is further configured to perform memory recycling processing if the amount of free memory is less than the second preset threshold.
[0104] In some embodiments, the thread identification module 302 is further used to determine whether the thread has a preset tag, wherein if the thread has a preset tag, the thread is determined to be a thread of a preset type; if the thread does not have a preset tag, the thread is determined not to be a thread of a preset type.
[0105] In some embodiments, the memory recovery device also includes a thread marking module, which is used to: determine the target thread of the relevant task in the user interaction event; add the preset label to the target thread to mark the target thread as a thread of a preset type.
[0106] 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.
[0107] In some embodiments, the thread marking module is further used to: if it is detected that a thread of the preset type enters a blocked state, determine an associated thread having a constraint relationship with the thread of the preset type entering the blocked state; and mark the associated thread as a thread of the preset type.
[0108] In some embodiments, the thread marking module is further used to: when it is detected that the constraint relationship is released, delete the preset label of the associated thread.
[0109] In some embodiments, the memory recovery module 303 is also used to: determine the target memory pages occupied by threads that do not belong to the preset type; and recycle the target memory pages so that the amount of free memory reaches a third preset threshold, wherein the third preset threshold is greater than the second preset threshold.
[0110] 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.
[0111] As can be seen from the above, the memory recovery device proposed in the embodiment of the present application takes the thread that executes the relevant tasks in the user interaction event as a thread of a preset type, sets a corresponding first preset threshold for this type of thread, and sets a corresponding second preset threshold for other threads except this type of thread, and the second preset threshold is greater than the first preset threshold. Through this scheme, the amount of memory between the first preset threshold and the second preset threshold is only used by threads of the preset type. For threads of non-preset types, if the amount of free memory is already lower than the second preset threshold, the system will enter the slow allocation path due to memory recovery processing. However, for threads that execute relevant tasks in user interaction events, even if the amount of free memory is already lower than the second preset threshold, as long as it is not less than the first preset threshold, the system will still perform normal memory allocation for this type of thread, reducing the probability of this type of thread entering the slow allocation path, so that the memory allocation request of the preset type of thread can be responded to in a timely manner, and the memory allocation success rate of this type of thread is improved, so that the user interaction event can be completed in time, avoiding jamming in user interaction scenarios.
[0112] The present application also provides an electronic device. The electronic device may be a smart phone, a tablet computer, or other device. Figure 5 , Figure 5 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.
[0113] 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.
[0114] 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.
[0115] 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:
[0116] When receiving a memory allocation request sent by a thread, determining the amount of free memory in the memory space, and judging whether the thread is a thread of a preset type, wherein the thread of the preset type is a thread that executes a task related to a user interaction event;
[0117] When the thread is a thread of a preset type, determining whether the amount of free memory is less than a first preset threshold, and performing memory recycling processing when the amount of free memory is less than the first preset threshold;
[0118] When the thread is not a thread of a preset type, determining whether the amount of free memory is less than a second preset threshold, wherein the second preset threshold is greater than the first preset threshold;
[0119] If the amount of free memory is less than the second preset threshold, memory recycling is performed.
[0120] In some embodiments, see Figure 6 , Figure 6 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.
[0121] 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.
[0122] 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.
[0123] The control circuit 405 is electrically connected to the display screen 404 and is used to control the display screen 404 to display information.
[0124] 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.
[0125] 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.
[0126] 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.
[0127] 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.
[0128] 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.
[0129] 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:
[0130] When receiving a memory allocation request sent by a thread, determining the amount of free memory in the memory space, and judging whether the thread is a thread of a preset type, wherein the thread of the preset type is a thread that executes a task related to a user interaction event;
[0131] When the thread is a thread of a preset type, determining whether the amount of free memory is less than a first preset threshold, and performing memory recycling processing when the amount of free memory is less than the first preset threshold;
[0132] When the thread is not a thread of a preset type, determining whether the amount of free memory is less than a second preset threshold, wherein the second preset threshold is greater than the first preset threshold;
[0133] If the amount of free memory is less than the second preset threshold, memory recycling is performed.
[0134] As can be seen from the above, an embodiment of the present application provides an electronic device, which uses the thread that executes the relevant task in the user interaction event as a thread of a preset type, sets a corresponding first preset threshold for the thread of this type, and sets a corresponding second preset threshold for other threads except the thread of this type, and the second preset threshold is greater than the first preset threshold. Through this scheme, the amount of memory between the first preset threshold and the second preset threshold is only used by threads of the preset type. For threads of non-preset types, if the amount of free memory is already lower than the second preset threshold, the system will enter the slow allocation path due to memory recycling processing. However, for threads that execute relevant tasks in user interaction events, even if the amount of free memory is already lower than the second preset threshold, as long as it is not less than the first preset threshold, the system will still perform normal memory allocation for this type of thread, reducing the probability of this type of thread entering the slow allocation path, so that the memory allocation request of the preset type of thread can be responded to in a timely manner, and the memory allocation success rate of this type of thread is improved, so that the user interaction event can be completed in time, avoiding the jamming in the user interaction scenario.
[0135] 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.
[0136] 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.
[0137] 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.
[0138] 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: When it is detected that a second preset thread is created, the created second preset thread is marked as a thread of a preset type, wherein the second preset thread is a system-level thread, the second preset thread of the system level always has a preset label, the preset label is a ux label, if the system-level thread is used to execute related tasks in a user interaction event, the system-level thread is used as the second preset thread, and the second preset thread of the system level includes a SystemUI thread; When the foreground process switches to the background, a preset tag of a first preset thread corresponding to the process is deleted, where the first preset thread is an application-level thread and includes some threads created when the process is running and used to directly execute tasks related to user interaction events; When receiving a memory allocation request sent by a thread, determining the amount of free memory in the memory space, and determining whether the thread is a thread of a preset type, wherein the thread of the preset type is a thread that executes a related task in a user interaction event, wherein determining whether the thread has a preset tag, wherein if the thread has a preset tag, determining that the thread is a thread of the preset type, and if the thread does not have a preset tag, determining that the thread is not a thread of the preset type; When the thread is a thread of a preset type, determining whether the amount of free memory is less than a first preset threshold, and performing memory recycling processing when the amount of free memory is less than the first preset threshold; When the thread is not a thread of a preset type, determining whether the amount of free memory is less than a second preset threshold, wherein the second preset threshold is greater than the first preset threshold; If the amount of free memory is less than the second preset threshold, memory recycling is performed; wherein, when memory recycling is performed, anonymous pages are recycled first, and when the recycled anonymous pages are not sufficient to obtain a free memory amount that is not less than a third preset threshold, file pages are recycled to determine target memory pages occupied by threads that do not belong to the preset type; recycling is performed on the target memory pages so that the amount of free memory reaches a third preset threshold, wherein the third preset threshold is greater than the second preset threshold.
2. The memory recovery method according to claim 1, characterized in that: The method further comprises: Determine the target thread for executing relevant tasks in the user interaction event; The preset tag is added to the target thread to mark the target thread as a thread of a preset type.
3. The memory recovery method according to claim 2, characterized in that: The step of determining a target thread for a task related to executing 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.
4. The memory recovery method according to claim 3, characterized in that: After adding a preset tag to the target thread to mark the target thread as a thread of a preset type, the method further includes: If it is detected that a thread of the preset type enters a blocked state, determining an associated thread having a constraint relationship with the thread of the preset type entering the blocked state; The associated thread is marked as a thread of a preset type.
5. The memory recovery method according to claim 4, characterized in that: After marking the associated thread as a thread of a preset type, the method further includes: When it is detected that the constraint relationship is released, the preset tag of the associated thread is deleted.
6. A memory recovery device, characterized in that: include: A memory detection module, for determining the amount of free memory in the memory space when receiving a memory allocation request sent by a thread, and when detecting that a second preset thread is created, marking the created second preset thread as a thread of a preset type, wherein the second preset thread is a system-level thread, and the second preset thread at the system level always has a preset label, which is a ux label. If the system-level thread is used to execute related tasks in a user interaction event, the system-level thread is used as the second preset thread, and the second preset thread at the system level includes a SystemUI thread; when a foreground process switches to background execution, the preset label of the first preset thread corresponding to the process is deleted, and the first preset thread is an application-level thread, and the first preset thread includes some threads created when the process is running and used to directly execute related tasks of user interaction events; a thread identification module, configured to determine whether the thread is a thread of a preset type, wherein the thread of the preset type is a thread that executes a related task in a user interaction event, and to determine whether the thread has a preset tag, wherein if the thread has a preset tag, the thread is determined to be a thread of the preset type, and if the thread does not have a preset tag, the thread is determined not to be a thread of the preset type; The memory detection module is further configured to determine whether the amount of free memory is less than a first preset threshold when the thread is a thread of a preset type; A memory recycling module, used for performing memory recycling when the amount of free memory is less than the first preset threshold; The memory detection module is further configured to, when the thread is not a thread of a preset type, determine whether the amount of free memory is less than a second preset threshold, wherein the second preset threshold is greater than the first preset threshold; The memory recovery module is also used to perform memory recovery processing if the amount of free memory is less than the second preset threshold; wherein, when performing memory recovery, anonymous pages are first recovered, and when the recovered anonymous pages are not sufficient to obtain a free memory amount not less than a third preset threshold, file pages are then recovered to determine target memory pages occupied by threads that do not belong to the preset type; the target memory pages are recovered so that the amount of free memory reaches a third preset threshold, wherein the third preset threshold is greater than the second preset threshold.
7. A storage medium having a computer program stored thereon, characterized in that: When the computer program is executed on a computer, the computer is enabled to execute the memory recycling method according to any one of claims 1 to 5.
8. 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 5 by calling the computer program.
