Memory recovery method and device, electronic equipment and storage medium
By using a memory reclamation thread and an asynchronous thread to reclaim different types of memory pages in parallel, the problem of memory page reclamation blocking in drivers is solved, improving memory reclamation speed and user experience.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- GUANGDONG OPPO MOBILE TELECOMMUNICATIONS CORP LTD
- Filing Date
- 2022-09-29
- Publication Date
- 2026-07-14
AI Technical Summary
In existing technologies, during memory reclamation, the blocking of memory page reclamation by the driver can lead to untimely or overall memory reclamation, affecting system stability and user experience.
Different memory pages, including anonymous pages and memory pages corresponding to drivers, are reclaimed asynchronously and in parallel by a memory reclamation thread and a first asynchronous thread, avoiding blocking caused by serial operations.
It improves memory reclamation speed, enhances user performance, and resolves the issue of memory page reclamation blocking in drivers.
Smart Images

Figure CN115587049B_ABST
Abstract
Description
Technical Field
[0001] This application belongs to the field of memory reclamation technology, specifically relating to a memory reclamation method, apparatus, electronic device, and storage medium. Background Technology
[0002] Memory reclamation is one of the core technologies of a system, playing a crucial role in improving user experience and system stability. When memory is scarce, the system may experience a series of problems such as lag, errors, and crashes. Among related technologies, the speed of memory reclamation still needs improvement. Summary of the Invention
[0003] In view of the above problems, this application proposes a memory reclamation method, apparatus, electronic device, and storage medium to improve the above problems.
[0004] In a first aspect, embodiments of this application provide a memory reclamation method, the method comprising: during the memory reclamation process, reclaiming a first memory page through a memory reclamation thread; and reclaiming a second memory page through a first asynchronous thread, wherein the memory reclamation thread is different from the first asynchronous thread, the first memory page is different from the second memory page, and the second memory page is a memory page corresponding to a driver program.
[0005] Secondly, embodiments of this application provide a memory reclamation device, the device comprising: a first reclamation unit, configured to reclaim a first memory page through a memory reclamation thread during the memory reclamation process; and a second reclamation unit, configured to reclaim a second memory page through a first asynchronous thread, wherein the memory reclamation thread is different from the first asynchronous thread, the first memory page is different from the second memory page, and the second memory page is a memory page corresponding to a driver program.
[0006] Thirdly, embodiments of this application provide an electronic device, including one or more processors and a memory; one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, and the one or more programs are configured to perform the methods described above.
[0007] Fourthly, embodiments of this application provide a computer-readable storage medium storing program code, wherein the above-described method is executed when the program code is run.
[0008] This application provides a memory reclamation method, apparatus, electronic device, and storage medium. During memory reclamation, a first memory page is reclaimed using a memory reclamation thread, and a second memory page is reclaimed using a first asynchronous thread. The memory reclamation thread and the first asynchronous thread are different, and the first memory page and the second memory page are different; the second memory page corresponds to a driver program's memory page. Through this method, different memory pages are reclaimed asynchronously using the memory reclamation thread and the first asynchronous thread, allowing for parallel reclamation of different memory pages. This effectively improves the speed of memory reclamation and enhances the user's performance experience. Attached Figure Description
[0009] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0010] Figure 1 This illustration shows an application scenario diagram of a memory reclamation method proposed in an embodiment of this application;
[0011] Figure 2 This illustration shows an application scenario diagram of a memory reclamation method proposed in an embodiment of this application;
[0012] Figure 3 A flowchart of a memory reclamation method according to an embodiment of this application is shown;
[0013] Figure 4 A flowchart of a memory reclamation method according to another embodiment of this application is shown;
[0014] Figure 5 A flowchart of a memory reclamation method according to another embodiment of this application is shown;
[0015] Figure 6 A flowchart of a memory reclamation method according to another embodiment of this application is shown;
[0016] Figure 7 A structural block diagram of a memory reclamation device according to an embodiment of this application is shown;
[0017] Figure 8 A structural block diagram of a memory reclamation device according to an embodiment of this application is shown;
[0018] Figure 9This invention illustrates a structural block diagram of an electronic device or server used to perform a memory reclamation method according to an embodiment of this invention.
[0019] Figure 10 This application shows a storage unit for storing or carrying program code that implements the memory reclamation method according to an embodiment of this application. Detailed Implementation
[0020] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of this application.
[0021] With the rapid development of mobile terminal technology, consumers' demands for mobile terminals are no longer limited to simple functions, but rather to the smooth use of multiple functions. The smoothness of mobile terminals has become a necessary factor for users' purchasing decisions. Memory performance is even more crucial to smoothness because current terminal devices have increasingly higher memory requirements for applications. For example, a single large mobile game may occupy 2GB of memory space. Therefore, the key to better managing memory performance lies in memory reclamation technology.
[0022] In the Linux kernel system, the memory reclamation process mainly includes two parts: direct reclaim and background reclaim. There are two main reasons why the kernel performs memory reclamation: (1) The kernel needs to provide enough memory for any sudden memory requests so that the use of cache and other related memory does not leave the system's remaining memory in a very low state for a long time; (2) When a request for more than the free memory arrives, it will trigger forced memory reclamation.
[0023] To address the first reason mentioned above, the Linux system is designed with the kswapd background memory reclamation thread. When the kernel allocates physical pages, if the system is short of memory and cannot allocate memory under low watermark conditions, the kswapd background memory reclamation thread will be woken up to reclaim memory, so as to ensure that memory allocation is not delayed.
[0024] In response to the second reason mentioned above, the Linux system will trigger direct reclaim. When the kernel calls the page allocation function to allocate physical pages, if the system memory is insufficient to meet the allocation request, the kernel will directly trigger the page reclamation mechanism, and the user thread will enter the memory reclamation process to ensure memory usage.
[0025] The inventors discovered in their research on related memory reclamation methods that, during memory reclamation using either the kswapd background memory reclamation thread or a user thread, the `shrink_Iruvec` and `shrink_slab` operations are executed sequentially. The `shrink_Iruvec` operation is used to reclaim anonymous and file pages, while the `shrink_slab` operation is used to reclaim memory pages corresponding to various drivers. The sequential execution of `shrink_Iruvec` and `shrink_slab` means that `shrink_Iruvec` is executed first, followed by `shrink_slab`.
[0026] When performing the shrink_slab operation, due to the varying quality of drivers, the memory pages of various drivers are reclaimed sequentially. If the reclamation of memory pages of one driver is blocked, it will cause background memory reclamation or direct memory reclamation to be blocked as a whole, resulting in untimely memory reclamation or overall memory reclamation blockage.
[0027] Therefore, the inventors have proposed the memory reclamation method, apparatus, electronic device, and storage medium of this application. During memory reclamation, a first memory page is reclaimed via a memory reclamation thread, and a second memory page is reclaimed via a first asynchronous thread. The memory reclamation thread is different from the first asynchronous thread, and the first memory page is different from the second memory page; the second memory page corresponds to the memory page of the driver program. Through this method, different memory pages are reclaimed asynchronously via the memory reclamation thread and the first asynchronous thread, allowing the reclamation of different memory pages to proceed in parallel. This effectively improves the speed of memory reclamation and enhances the user's performance experience.
[0028] In this embodiment of the application, the provided memory reclamation method can be executed by an electronic device. In this mode of execution by an electronic device, all steps in the memory reclamation method provided in this embodiment of the application can be performed by the electronic device. For example, as... Figure 1As shown, during the memory reclamation process executed by the processor of electronic device 100, a first memory page is reclaimed through a memory reclamation thread; a second memory page is reclaimed through a first asynchronous thread. The memory reclamation thread is different from the first asynchronous thread, and the first memory page is different from the second memory page. The second memory page is a memory page corresponding to the driver program.
[0029] Furthermore, the memory reclamation method provided in this application embodiment can also be executed by a server (cloud). Correspondingly, in this server-executed method, the server can reclaim the first memory page through a memory reclamation thread and reclaim the second memory page through a first asynchronous thread during the memory reclamation process. The memory reclamation thread is different from the first asynchronous thread, and the first memory page is different from the second memory page, with the second memory page corresponding to the driver program.
[0030] Alternatively, the process can be performed collaboratively by an electronic device and a server. In this collaborative manner, some steps of the memory reclamation method provided in this application embodiment are performed by the electronic device, while other steps are performed by the server.
[0031] For example, such as Figure 2 As shown, the electronic device 100 can perform a memory reclamation method including: during the memory reclamation process, a first memory page is reclaimed through a memory reclamation thread, and then a second memory page is reclaimed through a first asynchronous thread by a server 200. The memory reclamation thread is different from the first asynchronous thread, the first memory page is different from the second memory page, and the second memory page is a memory page corresponding to the driver.
[0032] It should be noted that in this method where electronic devices and servers work together, the steps performed by the electronic devices and servers are not limited to those described in the examples above. In practical applications, the steps performed by the electronic devices and servers can be dynamically adjusted according to the actual situation.
[0033] It should be noted that, in addition to being able to... Figure 1 and Figure 2 In addition to smartphones, the devices shown can also be in-vehicle systems, wearable devices, tablets, laptops, smart speakers, etc. Server 120 can be a standalone physical server, or a server cluster or distributed system composed of multiple physical servers.
[0034] The embodiments of this application will now be described in detail with reference to the accompanying drawings.
[0035] Please see Figure 3 The memory reclamation method provided in this application embodiment is applied to, for example, Figure 1 or Figure 2 The method includes the following: (illustrated electronic device or server)
[0036] Step S110: During the memory reclamation process, the first memory page is reclaimed through the memory reclamation thread.
[0037] In this embodiment, the memory reclamation thread is the original thread that performs the memory reclamation operation. Different memory reclamation operations correspond to different memory reclamation threads; for example, a background memory reclamation operation corresponds to the kswapd background memory reclamation thread, while a direct memory reclamation operation corresponds to a user thread. The first memory page may include anonymous pages and file pages.
[0038] As one approach, in response to a memory reclamation command, the shrink_Iruvec function is called by the memory reclamation thread to reclaim anonymous pages and file pages. The memory reclamation command can be triggered by detecting the startup of a specified application or by detecting when free memory falls below the memory threshold; no specific limitation is made here.
[0039] As one approach, if the memory reclamation instruction is triggered by the startup of a specified application, then the specified application can be one that requires the kernel to allocate a large amount of memory, such as a game application. When the startup of an application that requires the kernel to allocate a large amount of memory is detected, the shrink_Iruvec function is called by the memory reclamation thread to reclaim anonymous pages and file pages.
[0040] Optionally, the memory reclamation command can also be sent by other external devices that have established a communication connection with the electronic device. In this case, the other external devices can monitor the free memory of the electronic device in real time. When the other external devices detect that the free memory of the electronic device is lower than the preset memory, the other external devices send a memory reclamation command to the electronic device. When the electronic device receives the memory reclamation execution, in response to the memory reclamation command, it starts to reclaim anonymous pages and file pages by calling the shrink_Iruvec function through the memory reclamation thread.
[0041] Before reclaiming anonymous and file pages using the above method, the size of the anonymous and file pages to be reclaimed can be determined first. After determining the size, the memory reclamation thread calls the `shrink_Iruvec` function to reclaim the specified size of anonymous and file pages. The reclaimed anonymous and file pages can be infrequently used. Optionally, infrequently used anonymous and file pages are those that have not been used for a preset time. The preset time is any pre-set time value; specifically, if it is determined that anonymous and file page reclamation needs to be performed frequently, the preset time can be set smaller; if it is determined that anonymous and file page reclamation does not need to be performed frequently, the preset time can be set larger.
[0042] Step S120: Reclaim the second memory page through the first asynchronous thread, wherein the memory reclamation thread is different from the first asynchronous thread, the first memory page is different from the second memory page, and the second memory page is the memory page corresponding to the driver.
[0043] In this embodiment, the first asynchronous thread is a newly created thread used to asynchronously reclaim the memory pages corresponding to the driver. This first asynchronous thread can be either a user thread or a kernel thread. A user thread refers to a thread implemented in a user program that does not require kernel support; it does not depend on the operating system kernel, and the application process uses functions provided by the thread library to create, synchronize, schedule, and manage threads to control user threads. A kernel thread, on the other hand, requires kernel involvement; the kernel handles thread scheduling, and it depends on the operating system kernel. Its creation and destruction are determined by the kernel's internal needs. The kernel thread table is located in the kernel and includes a thread control block (TCB). If a thread is blocked, the kernel will select a new thread from the current process or another process to ensure program execution.
[0044] Optionally, in this embodiment, the second memory page is the memory page corresponding to the driver, which is a memory page dynamically allocated to the driver by the kernel during the driver's operation. The memory page dynamically allocated to the driver by the kernel can include non-pageable memory and pageable memory.
[0045] In device drivers, memory management is crucial. Memory is divided into paging and non-paging memory. By default, the kernel loader loads all code and global data into non-paging memory. A device driver is a special program that enables communication between a computer and a device; it can be considered the hardware interface. The operating system can only control the hardware device through this interface. If a device driver is not installed correctly, the device will not function properly. Therefore, device drivers are often referred to as the "soul of hardware," the "master of hardware," and the "bridge between hardware and the system." Specifically, device drivers can include GPU drivers, USB drivers, SCSI drivers, etc., without further specific limitations.
[0046] As one approach, the `shrink_slab` function is called via a first asynchronous thread to reclaim the memory pages corresponding to the driver program. In this embodiment, the reclamation of the first memory page via the memory reclamation thread and the reclamation of the second memory page via the first asynchronous thread are executed in parallel. That is, the reclamation of the first memory page and the reclamation of the second memory page are performed simultaneously, thereby improving the speed of memory reclamation.
[0047] This application provides a memory reclamation method in which a first memory page is reclaimed through a memory reclamation thread, and a second memory page is reclaimed through a first asynchronous thread. The memory reclamation thread and the first asynchronous thread are different, and the first memory page and the second memory page are different; the second memory page corresponds to the memory page of a driver program. Through this method, different memory pages are reclaimed asynchronously by the memory reclamation thread and the first asynchronous thread, allowing the reclamation of different memory pages to proceed in parallel. This effectively improves the speed of memory reclamation and enhances the user's performance experience.
[0048] Please see Figure 4 The memory reclamation method provided in this application embodiment is applied to, for example, Figure 1 or Figure 2 The method includes the following: (illustrated electronic device or server)
[0049] Step S210: If the current free memory is lower than the preset memory, wake up the first asynchronous thread.
[0050] In this embodiment, the preset memory is a pre-set threshold of free memory that needs to be reclaimed. The preset memory can be set to the memory threshold corresponding to the memory waterline, or it can be set to a memory threshold greater than the memory waterline. No specific limitation is made here.
[0051] Understandably, the operation of electronic device systems and application processes requires the system kernel to allocate memory space. Furthermore, as the system runs, the kernel continuously performs memory reclamation and allocation. For example, the kernel can monitor memory usage in real time and check whether the remaining free memory space is less than the preset memory limit.
[0052] Normally, the system kernel sets a memory watermark when managing memory. When allocating memory pages, the system checks if the remaining free memory space is less than this watermark. If it is, memory reclamation is required. When the system detects that the current free memory is below the watermark, it wakes up the first asynchronous thread to perform the memory reclamation operation.
[0053] However, generally, when the remaining free memory space of the system is detected to be lower than this memory threshold, the system is already in a low memory state. Furthermore, memory compression operations are usually time-consuming. If memory compression is only performed when the system is already in a low memory state, it will cause the system to remain in a low memory state for a long time, failing to complete memory reclamation in a timely manner, resulting in low memory reclamation efficiency and affecting the normal operation of the system and applications.
[0054] Therefore, to avoid the system being in a low memory state for a long time, memory can be reclaimed in advance. So the preset memory can be set to a value greater than the memory threshold. When the free memory space is detected to be less than the preset memory, memory reclamation will begin.
[0055] One approach is to wake up the first asynchronous thread when the system's current free memory is detected to be lower than a preset memory level. After waking up the first asynchronous thread, its state can be set to "RUNNING". Optionally, to wake up a specific first asynchronous thread, a specific thread identifier can be set for it. Then, when the corresponding wake-up condition is met, the corresponding first asynchronous thread can be found through this specified thread identifier and thus woke up. Optionally, when the specified wake-up condition is met, the first asynchronous thread can be woken up using a preset function. The preset function can be a pre-defined function used to wake up the first asynchronous thread; the specified wake-up condition can be the aforementioned current free memory being lower than the preset memory level, and is not specifically limited here.
[0056] Step S220: During the memory reclamation process, the first memory page is reclaimed through the memory reclamation thread.
[0057] As one approach, step S220 may specifically include: during background memory reclamation, reclaiming the first memory page through a background reclamation thread; or, during direct memory reclamation, reclaiming the first memory page through a user thread.
[0058] In this embodiment, the memory reclamation process may include background memory reclamation and direct memory reclamation. Background memory reclamation is performed by the kswapd background memory reclamation thread on the first memory page; direct memory reclamation is performed by a user thread on the first memory page.
[0059] Step S230: The second memory pages corresponding to each of the multiple drivers are reclaimed serially through the first asynchronous thread.
[0060] In the embodiments of this application, the driver may include multiple drivers, and each driver corresponds to a corresponding second memory page.
[0061] As one approach, after creating the first asynchronous thread, the memory page reclamation operations corresponding to multiple drivers are transferred to the first asynchronous thread for execution, thereby resolving the blocking of the first memory page reclamation caused by the memory page reclamation of the drivers.
[0062] In this embodiment of the application, since there are multiple drivers, the first asynchronous thread performs the reclamation of the second memory pages corresponding to multiple drivers serially. That is, after the reclamation of the second memory page corresponding to one driver is completed, the reclamation of the second memory page corresponding to the next driver is then performed.
[0063] When serially reclaiming the second memory pages corresponding to multiple drivers, the reclamation can be performed according to the drivers' priorities. One approach is to reclaim the second memory pages of the drivers in descending or ascending order of priority. For example, if priorities include first, second, third, and fourth priorities, where first priority is higher than second priority, second priority is higher than third priority, and third priority is higher than fourth priority, then when reclaiming the second memory pages of the drivers in descending order of priority, the second memory pages corresponding to the drivers with the highest priority are reclaimed first, followed by those with the second priority, then those with the third priority, and finally those with the fourth priority. The priorities of the multiple drivers can be preset, and when setting the priorities of multiple drivers, they can be set according to the importance of the drivers; the higher the importance of the driver, the higher its priority, and vice versa.
[0064] As another approach, the second memory pages corresponding to multiple drivers can be reclaimed in descending or ascending order based on the number of pages corresponding to each driver. For example, if the multiple drivers include a first driver, a second driver, a third driver, and a fourth driver, where the number of second memory pages corresponding to the first driver is A, the number of second memory pages corresponding to the second driver is B, the number of second memory pages corresponding to the third driver is C, and the number of second memory pages corresponding to the fourth driver is D, with A>B>C>D, then when reclaiming the second memory pages corresponding to each driver in descending order of the number of pages, the second memory pages corresponding to the first driver are reclaimed first, then the second memory pages corresponding to the second driver, then the second memory pages corresponding to the third driver, and finally the second memory pages corresponding to the fourth driver.
[0065] This application provides a memory reclamation method. If the current free memory is lower than a preset memory level, a first asynchronous thread is awakened. During the memory reclamation process, the memory reclamation thread reclaims the first memory page, and the first asynchronous thread serially reclaims the second memory pages corresponding to multiple drivers. Through this method, different memory pages are reclaimed asynchronously by the memory reclamation thread and the first asynchronous thread, allowing the reclamation of different memory pages to proceed in parallel. This effectively improves the speed of memory reclamation and enhances the user's performance experience.
[0066] Please see Figure 5 The memory reclamation method provided in this application embodiment is applied to, for example, Figure 1 or Figure 2 The method includes the following: (illustrated electronic device or server)
[0067] Step S310: During the memory reclamation process, the first memory page is reclaimed through the memory reclamation thread.
[0068] Step S320: The second memory pages corresponding to each of the multiple drivers are reclaimed serially through the first asynchronous thread.
[0069] Step S330: During the process of serially reclaiming the second memory pages corresponding to each of the multiple drivers through the first asynchronous thread, the memory reclamation time corresponding to the first driver is obtained, wherein the first driver is the driver currently performing second memory page reclamation.
[0070] In this embodiment of the application, the memory reclamation time corresponding to the first driver is the duration of reclamation of the second memory page corresponding to the first driver, that is, the time period from the start of reclamation of the second memory page corresponding to the first driver to the acquisition of the memory reclamation time.
[0071] When serially reclaiming the second memory pages corresponding to multiple drivers via a first asynchronous thread, if the memory reclamation time for the second memory page corresponding to one driver is too long, it will cause the reclamation of the second memory pages corresponding to the remaining drivers to be blocked. Therefore, to avoid the blocking of the reclamation of the second memory pages corresponding to the remaining drivers, the memory reclamation time of the second memory pages corresponding to each driver can be monitored in real time.
[0072] Step S340: If the memory reclamation time corresponding to the first driver exceeds a preset time threshold, and the reclamation of the second memory page corresponding to the first driver has not yet been completed, then create a second asynchronous thread.
[0073] In this embodiment, the preset time threshold is a pre-set maximum time for the reclamation of the second memory page corresponding to the first driver. The second asynchronous memory is a newly created kernel thread used to asynchronously reclaim the second memory page.
[0074] If the memory reclamation time of the first driver exceeds the preset time threshold, and the reclamation of the second memory page corresponding to the first driver has not yet been completed, it is determined that the memory reclamation time of the second memory page corresponding to the first driver is too long. Then, a new kernel thread is created to reclaim the second memory pages corresponding to other drivers that have not yet been reclaimed.
[0075] Step S350: The second memory page corresponding to the second driver is reclaimed through the second asynchronous thread, wherein the second driver is the driver that performs the reclamation of the second memory page after the first driver.
[0076] In this embodiment, after creating a second asynchronous thread, the second memory pages corresponding to drivers that have not yet been reclaimed are reclaimed serially through the second asynchronous thread. While reclaiming the second memory pages corresponding to drivers that have not yet been reclaimed serially through the second asynchronous thread, the memory reclamation time of the second memory pages corresponding to each driver is also monitored. If the memory reclamation time of the second memory page corresponding to the next driver exceeds a preset time threshold, a new kernel thread can be created to reclaim the second memory pages corresponding to subsequent drivers that have not yet been reclaimed.
[0077] This application provides a memory reclamation method. First, during memory reclamation, a memory reclamation thread reclaims a first memory page. Then, a first asynchronous thread serially reclaims the second memory pages corresponding to multiple drivers. During this serial reclamation, the memory reclamation time for the driver currently reclaiming its second memory pages is obtained. If this time exceeds a preset threshold and the reclamation of the second memory pages for the driver currently reclaiming its second memory pages is not yet complete, a second asynchronous thread is created. This second asynchronous thread then reclaims the second memory pages for drivers reclaiming their second memory pages after the first driver. This method employs a two-step asynchronous approach to reclaiming the second memory pages, effectively improving memory reclamation speed and resolving performance bottlenecks caused by thread blocking, thus enhancing the user experience.
[0078] Please see Figure 6The memory reclamation method provided in this application embodiment is applied to, for example, Figure 1 or Figure 2 The method includes the following: (illustrated electronic device or server)
[0079] Step S410: During the memory reclamation process, the first memory page is reclaimed through the memory reclamation thread.
[0080] Step S420: Start the timer.
[0081] In this embodiment of the application, a timer is started when it is detected that the second memory page corresponding to the driver has begun to be reclaimed through the first asynchronous thread.
[0082] Step S430: Monitor the memory reclamation time corresponding to the first driver through the timer.
[0083] In this embodiment of the application, in order to avoid blocking the reclamation of the second memory page corresponding to the remaining driver, a timer can be started to monitor the memory reclamation time corresponding to the driver.
[0084] Step S440: During the process of serially reclaiming the second memory pages corresponding to each of the multiple drivers through the first asynchronous thread, the memory reclamation time corresponding to the first driver is obtained, wherein the first driver is the driver currently performing second memory page reclamation.
[0085] Step S450: If the memory reclamation time corresponding to the first driver exceeds a preset time threshold, and the reclamation of the second memory page corresponding to the first driver has not yet been completed, obtain the number of established second asynchronous threads.
[0086] In this embodiment, to avoid excessive CPU load, the number of created second asynchronous threads cannot exceed a certain thread threshold. Therefore, the number of created second asynchronous threads can be monitored. Each time it is determined that a new second asynchronous thread needs to be created, the number of already created second asynchronous threads can be obtained to determine whether a new second asynchronous thread can be created. Specifically, the number of created second asynchronous threads can be incremented by 1 each time a second asynchronous thread is created.
[0087] Step S460: If the number of second asynchronous threads already established is less than a preset threshold, create the second asynchronous thread.
[0088] In this embodiment of the application, the preset quantity threshold is the maximum number of second asynchronous threads that can be created in advance.
[0089] After determining that the memory reclamation time corresponding to the first driver exceeds a preset time threshold, and the reclamation of the second memory page corresponding to the first driver has not yet been completed, the number of created second asynchronous threads is obtained, and the obtained number of created second asynchronous threads is compared with a preset number threshold. If it is determined that the number of created second asynchronous threads is less than the preset number threshold, the creation of new second asynchronous threads is allowed.
[0090] Step S470: The second memory page corresponding to the second driver is reclaimed through the second asynchronous thread, wherein the second driver is the driver that performs the reclamation of the second memory page after the first driver.
[0091] This application provides a memory reclamation method. First, during memory reclamation, a memory reclamation thread reclaims the first memory page. Then, a timer is started to monitor the memory reclamation time corresponding to a first driver. While a first asynchronous thread serially reclaims the second memory pages corresponding to multiple drivers, the memory reclamation time corresponding to the first driver is obtained. If the memory reclamation time for the first driver exceeds a preset time threshold, and the reclamation of the second memory pages corresponding to the first driver is not yet complete, the number of established second asynchronous threads is obtained. If the number of established second asynchronous threads is less than a preset threshold, a new second asynchronous thread is created, and this new thread reclaims the second memory pages corresponding to the second driver. Through this method, different memory pages are reclaimed asynchronously by the memory reclamation thread and the first asynchronous thread, allowing the reclamation of different memory pages to proceed in parallel, thereby effectively improving the speed of memory reclamation and enhancing user performance. Furthermore, during the reclamation of the second memory pages corresponding to multiple drivers through the second asynchronous thread, new second threads can be continuously created to reclaim the second memory pages of the second driver, preventing the reclamation of the second memory pages of the second driver from being blocked.
[0092] Please see Figure 7 This application provides a memory reclamation device 500, which includes:
[0093] The first reclamation unit 510 is used to reclaim the first memory page through the memory reclamation thread during the memory reclamation process.
[0094] In one manner, the first recycling unit 510 is specifically used to reclaim the first memory page through a background recycling thread during the background memory reclamation process; or, during the direct memory reclamation process, to reclaim the first memory page through a user thread.
[0095] The second recycling unit 520 is used to reclaim the second memory page through a first asynchronous thread, wherein the memory recycling thread is different from the first asynchronous thread, the first memory page is different from the second memory page, and the second memory page is a memory page corresponding to the driver.
[0096] In one manner, the second recycling unit 520 is specifically used to serially reclaim the second memory pages corresponding to each of the multiple drivers through the first asynchronous thread.
[0097] Alternatively, the second recycling unit 520 is specifically used to obtain the memory recycling time corresponding to the first driver during the process of serially recycling the second memory pages corresponding to multiple drivers through the first asynchronous thread, wherein the first driver is the driver currently recycling the second memory pages; if the memory recycling time corresponding to the first driver exceeds a preset time threshold and the recycling of the second memory pages corresponding to the first driver has not yet been completed, then a second asynchronous thread is created; the second asynchronous thread is used to recycle the second memory pages corresponding to the second driver, wherein the second driver is the driver that performs the second memory page recycling after the first driver.
[0098] Optionally, the second recycling unit 520 is also used to start a timer; the timer is used to monitor the memory recycling time corresponding to the first driver.
[0099] Optionally, the second recycling unit 520 is also used to obtain the number of second asynchronous threads that have been established; if the number of second asynchronous threads that have been established is less than a preset threshold, the second asynchronous thread is created.
[0100] Please see Figure 8 The device 500 further includes:
[0101] The wake-up unit 530 is used to wake up the first asynchronous thread if the current free memory is lower than the preset memory.
[0102] It should be noted that the device embodiments in this application correspond to the aforementioned method embodiments. The specific principles in the device embodiments can be found in the content of the aforementioned method embodiments, and will not be repeated here.
[0103] The following will combine Figure 9 This application describes an electronic device or server provided.
[0104] Please see Figure 9Based on the aforementioned memory reclamation method and apparatus, this application embodiment also provides another electronic device or server 800 capable of executing the aforementioned memory reclamation method. The electronic device or server 800 includes one or more (only one shown in the figure) processors 802, a memory 804, and a network module 806 coupled together. The memory 804 stores programs capable of executing the contents of the aforementioned embodiments, and the processor 802 can execute the programs stored in the memory 804.
[0105] The processor 802 may include one or more processing cores. The processor 802 connects to various parts of the electronic device or server 800 using various interfaces and lines. It executes various functions and processes data by running or executing instructions, programs, code sets, or instruction sets stored in the memory 804, and by calling data stored in the memory 804. Optionally, the processor 802 may be implemented using at least one hardware form of Digital Signal Processing (DSP), Field-Programmable Gate Array (FPGA), or Programmable Logic Array (PLA). The processor 802 may integrate one or more of the following: Central Processing Unit (CPU), Graphics Processing Unit (GPU), and modem. The CPU primarily handles the operating system, user interface, and applications; the GPU is responsible for rendering and drawing the displayed content; and the modem handles wireless communication. It is understood that the modem may also not be integrated into the processor 802 and may be implemented separately using a communication chip.
[0106] The memory 804 may include random access memory (RAM) or read-only memory (ROM). The memory 804 can be used to store instructions, programs, code, code sets, or instruction sets. The memory 804 may include a program storage area and a data storage area. The program storage area may store instructions for implementing an operating system, instructions for implementing at least one function (such as touch functionality, sound playback functionality, image playback functionality, etc.), and instructions for implementing the various method embodiments described below. The data storage area may also store data created during the use of the electronic device or server 800 (such as phonebooks, audio and video data, chat log data, etc.).
[0107] The network module 806 is used to receive and transmit electromagnetic waves, realizing the mutual conversion between electromagnetic waves and electrical signals, thereby communicating with communication networks or other devices, such as audio playback devices. The network module 806 may include various existing circuit elements for performing these functions, such as antennas, radio frequency transceivers, digital signal processors, encryption / decryption chips, user identity modules (SIM cards), memory, etc. The network module 806 can communicate with various networks such as the Internet, corporate intranets, and wireless networks, or communicate with other devices through wireless networks. The aforementioned wireless networks may include cellular telephone networks, wireless local area networks (WLANs), or metropolitan area networks (MANs). For example, the network module 806 can interact with base stations.
[0108] Please refer to Figure 10 This diagram illustrates a structural block diagram of a computer-readable storage medium provided in an embodiment of this application. The computer-readable storage medium 900 stores program code that can be called by a processor to execute the methods described in the above method embodiments.
[0109] The computer-readable storage medium 900 may be an electronic memory such as flash memory, EEPROM (Electrically Erasable Programmable Read-Only Memory), EPROM, hard disk, or ROM. Optionally, the computer-readable storage medium 900 includes a non-transitory computer-readable storage medium. The computer-readable storage medium 900 has storage space for program code 910 that performs any of the method steps described above. This program code can be read from or written to one or more computer program products. The program code 910 may be compressed, for example, in a suitable form.
[0110] This application provides a memory reclamation method, apparatus, electronic device, and storage medium. During memory reclamation, a first memory page is reclaimed via a memory reclamation thread, and a second memory page is reclaimed via a first asynchronous thread. The memory reclamation thread and the first asynchronous thread are different, and the first memory page and the second memory page are different; the second memory page corresponds to a driver program's memory page. Through this method, different memory pages are reclaimed asynchronously via the memory reclamation thread and the first asynchronous thread, allowing for parallel reclamation of different memory pages. This effectively improves the speed of memory reclamation and enhances the user's performance experience.
[0111] The embodiments of the present invention have been described above with reference to the accompanying drawings. However, the present invention is not limited to the specific embodiments described above. The specific embodiments described above are merely illustrative and not restrictive. Those skilled in the art can make many other forms under the guidance of the present invention without departing from the spirit and scope of the claims, and all of these forms are within the protection scope of the present invention.
Claims
1. A memory reclamation method, characterized in that, The method includes: During memory reclamation, the first memory page is reclaimed through a memory reclamation thread; The second memory page is reclaimed through a first asynchronous thread, wherein the memory reclamation thread is different from the first asynchronous thread. The memory reclamation thread is the original thread that performs the memory reclamation operation, while the first asynchronous thread is a newly created thread used to asynchronously reclaim the memory page corresponding to the driver. The first memory page is different from the second memory page. The first memory page includes anonymous pages and file pages, while the second memory page is the memory page corresponding to the driver.
2. The method according to claim 1, characterized in that, The process of memory reclamation, before the memory reclamation thread reclaims the first memory page, also includes: If the current free memory is lower than the preset memory, wake up the first asynchronous thread.
3. The method according to claim 1, characterized in that, The driver includes multiple components, and the step of reclaiming the second memory page via a first asynchronous thread includes: The first asynchronous thread serially reclaims the second memory pages corresponding to each of the multiple drivers.
4. The method according to claim 3, characterized in that, The step of serially reclaiming the second memory pages corresponding to each of the multiple drivers through the first asynchronous thread includes: During the process of serially reclaiming the second memory pages corresponding to each of the multiple drivers through the first asynchronous thread, the memory reclamation time corresponding to the first driver is obtained, wherein the first driver is the driver currently performing second memory page reclamation; If the memory reclamation time corresponding to the first driver exceeds a preset time threshold, and the reclamation of the second memory page corresponding to the first driver has not yet been completed, then a second asynchronous thread is created. The second asynchronous thread reclaims the second memory page corresponding to the second driver, wherein the second driver is the driver that performs the reclamation of the second memory page after the first driver.
5. The method according to claim 4, characterized in that, Before obtaining the memory reclamation time corresponding to the first driver, the following steps are also included: Start the timer; The timer is used to monitor the memory reclamation time corresponding to the first driver.
6. The method according to claim 4, characterized in that, The creation of the second asynchronous thread includes: Get the number of second asynchronous threads that have already been created; If the number of existing second asynchronous threads is less than a preset threshold, create a second asynchronous thread.
7. The method according to claim 1, characterized in that, During the memory reclamation process, the first memory page is reclaimed through a memory reclamation thread, including: During background memory reclamation, the first memory page is reclaimed via a background reclamation thread; or... During direct memory reclamation, the first memory page is reclaimed by a user thread.
8. A memory reclamation device, characterized in that, The device includes: The first reclamation unit is used to reclaim the first memory page through the memory reclamation thread during the memory reclamation process; The second reclamation unit is used to reclaim the second memory page through a first asynchronous thread. The memory reclamation thread is different from the first asynchronous thread. The memory reclamation thread is the original thread that performs the memory reclamation operation, while the first asynchronous thread is a newly created thread used to asynchronously reclaim the memory page corresponding to the driver. The first memory page is different from the second memory page. The first memory page includes anonymous pages and file pages, while the second memory page is the memory page corresponding to the driver.
9. An electronic device, characterized in that, It includes a memory and one or more processors; one or more programs are stored in the memory and configured to be executed by the one or more processors according to any one of claims 1-7.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores program code, wherein the program code, when executed by a processor, performs the method according to any one of claims 1-7.