Memory recovery method and electronic equipment

By differently reclaiming system memory according to the application popularity of the application, the problem of low memory recycling efficiency in the prior art is solved, and more efficient memory management is achieved.

CN119441069APending Publication Date: 2025-02-14HONOR DEVICE CO LTD
View PDF 0 Cites 2 Cited by

Patent Information

Application Number
CN202310991388.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-08-07
Publication Date
2025-02-14

AI Technical Summary

Technical Problem

In the memory recycling process of prior art, a large amount of data may be quickly loaded back to system memory after being released, affecting the memory recycling efficiency of electronic devices.

Method used

Depending on the application's application popularity, the system memory occupied by the application is differentially recycled to reduce the amount of data repeatedly released and reloaded. The specific method is to determine the amount of memory corresponding to the application running in the background, reduce the amount of recycling as the application popularity increases, and increase the amount of recycling when the application popularity decreases.

Benefits of technology

It improves memory recycling efficiency, avoids excessive memory recycling when application popularity is high, and excessive memory usage when application popularity is low, and reduces the amount of data repeatedly released and reloaded.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119441069A_ABST
    Figure CN119441069A_ABST
Patent Text Reader

Abstract

The invention provides a memory recovery method and electronic equipment, and relates to the technical field of computers. According to the scheme, the memory recovery efficiency of the electronic equipment starting a memory control subsystem mechanism is improved. According to the specific scheme, first memory capacity corresponding to a first application running in the background is determined, the first memory capacity indicates memory capacity needing to be recycled from a system memory occupied by the first application, and the first memory capacity is reduced along with increase of application heat of the first application; the application popularity indicates the active degree of the application program running in the background; and recovering the system memory occupied by the first application according to the first memory amount.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a memory recovery method and electronic device. Background Art

[0002] The internal memory of an electronic device (such as called RAM) is used to temporarily store the CPU's calculation data and the data exchanged with external storage such as hard disk. The memory is the bridge for communication between the external storage and the CPU. All applications need to run based on memory resources.

[0003] Of course, the memory resources of electronic devices are limited. When the memory is insufficient, the operating system will clean up the data of infrequently used applications in the memory, which is called memory recycling.

[0004] In the related art, in an electronic device with a memory control subsystem mechanism enabled, during the memory recycling process, a large amount of data may be released and then quickly loaded back into the system memory, affecting the memory recycling efficiency of the electronic device. Summary of the invention

[0005] The embodiments of the present application provide a memory recovery method and an electronic device, which differentially recover the system memory occupied by the data of the application according to the application popularity of the application, reduce the amount of data that is repeatedly released and reloaded, and improve the memory recovery efficiency.

[0006] To achieve the above objectives, the embodiments of the present application adopt the following technical solutions:

[0007] In the first aspect, an embodiment of the present application provides a memory recovery method. The method is applied to an electronic device, in which each application corresponds to a memory control subsystem memcg, and the method includes: determining a first memory amount corresponding to a first application running in the background, wherein the first memory amount indicates the amount of memory to be recovered from the system memory occupied by the first application, and the first memory amount decreases as the application heat of the first application increases, and the application heat indicates the activity level of the application running in the background. According to the first memory amount, the system memory occupied by the first application is recovered.

[0008] In the above embodiment, the electronic device that enables the memcg mechanism, in the process of reclaiming system memory, realizes differential recovery of the system memory occupied by the application according to the application heat. For example, as the heat of the application increases, the amount of memory recycled will decrease accordingly, and as the heat of the application decreases, the amount of memory recycled will increase accordingly. On the one hand, it can avoid a large amount of system memory occupied by being recycled when the application heat is high, reduce the released application data (such as anonymous pages), and quickly reload it back into the system memory. On the other hand, it can avoid applications with low application heat from occupying a large amount of system memory for a long time.

[0009] In some embodiments, before determining the first amount of memory corresponding to the first application running in the background, the method further includes: determining a first amount of reclaimable memory corresponding to the first application; the first amount of memory is not greater than the first amount of reclaimable memory.

[0010] In the above embodiment, the size of the determined first memory amount is limited by the amount of reclaimable memory of the application, so as to avoid too much system memory being reclaimed at a single time for a single application, resulting in the system memory occupied by some applications not being reclaimed, that is, the problem of uneven system memory recovery between multiple background applications.

[0011] In some embodiments, determining a first amount of reclaimable memory corresponding to the first application includes: determining an application score of the first application as a first score; the application score of the first application decreases as the application popularity of the first application increases; determining the first amount of reclaimable memory based on the first score, a second amount of memory corresponding to the first application, and a third amount of memory; wherein the second amount of memory indicates the size of all anonymous pages in the first application, and the third amount of memory indicates the size of anonymous pages in the first application that have been swapped out from the system memory.

[0012] In the above embodiment, the amount of reclaimable memory of the application is not only affected by the popularity of the application, but also by the total size of all anonymous pages of the application and the size of the transferred anonymous pages, so as to avoid the problem that the total amount of system memory is too high after multiple memory recycling for applications with high popularity.

[0013] In some embodiments, the first reclaimable memory amount is determined based on the first score, the second memory amount and the third memory amount corresponding to the first application, including: determining the transfer storage ratio corresponding to the first score; the transfer storage ratio is used to determine the ratio of the transferable anonymous page size in a single application, and the higher the first score, the higher the corresponding transfer storage ratio; the product of the transfer storage ratio and the second memory amount is determined as the first anonymous page size; and the third memory amount is removed from the first anonymous page size to determine the first reclaimable memory amount.

[0014] In the above embodiment, the problem of excessive total amount of system memory being reclaimed after multiple memory reclaims for application programs with high application popularity is avoided.

[0015] In some embodiments, when the determined first amount of reclaimable memory is less than zero, its value is revised to zero.

[0016] In the above embodiment, in a scenario where the application score of the application program changes, the above method may calculate a first reclaimable memory amount less than zero, and the fault tolerance is improved by correcting the first reclaimable memory amount less than zero.

[0017] In some embodiments, determining the first amount of memory includes: obtaining a first ratio between a first amount of reclaimable memory and a fourth amount of memory, the fourth amount of memory referring to the sum of the reclaimable memory amounts corresponding to all the applications whose application scores are the first score and are running in the background; and determining the first amount of memory by multiplying the first ratio by a fifth amount of memory, the fifth amount of memory referring to the total amount of memory that needs to be reclaimed from all the applications whose application scores are the first score.

[0018] In the above embodiment, the amount of system memory to be reclaimed from applications with the same application heat is evenly distributed according to the ratio between the amount of reclaimable memory and the total amount of reclaimable memory of applications with the same score. In this way, for applications with the same heat, the amount of system memory to be reclaimed is determined by the size of all anonymous pages of the application and the size of the transferred anonymous pages. This avoids the problem that too much system memory is reclaimed for some applications with the same application heat, while the system memory occupied by other applications is not reclaimed, thereby improving the efficiency of memory recovery.

[0019] In some embodiments, each of the applications corresponds to a memcg data structure, and the memcg data structure includes a field for carrying the amount of reclaimable memory; after determining the first amount of reclaimable memory, the method also includes: writing the determined first amount of reclaimable memory into the memcg data structure of the corresponding first application; the determination of the first amount of memory also includes: obtaining a fourth amount of memory; obtaining the fourth amount of memory includes: after the reclaimable memory has been written into the memcg data structure of the application running in the background, obtaining the reclaimable memory from the memcg data structure of the application running in the background whose application score is the first score; adding all the obtained reclaimable memory amounts to obtain the corresponding fourth amount of memory.

[0020] In some embodiments, after reclaiming the system memory occupied by the first application according to the first memory amount, the method further includes: updating the reclaimable memory amount in the memcg data structure of the first application using the difference between the first reclaimable memory amount of the first application and the first memory amount.

[0021] In the above embodiment, after each recycling, the amount of reclaimable memory in the memcg data structure can be updated. In the next recycling, the process of determining the amount of reclaimable memory again can be omitted, shortening the time required for memory recycling and improving memory recycling efficiency.

[0022] In some embodiments, determining the first memory amount also includes: counting the total amount of reclaimable memory corresponding to all the applications running in the background of the electronic device; obtaining the fifth memory amount; wherein the proportion of the fifth memory amount in the total amount of memory that needs to be reclaimed by the electronic device is equal to the proportion of the fourth memory amount in the total amount of reclaimable memory of the electronic device.

[0023] In some embodiments, the system memory occupied by the first application is reclaimed according to the first memory amount, including: in the anonymous pages of the first application, determining a target memory page whose system memory occupancy is the first memory amount, the activity level of the target memory page being lower than other anonymous pages of the first application; swapping the target memory page out of the system memory.

[0024] In the above example, the amount of system memory reclaimed for the application is affected by the popularity of the application. During the memory reclaim process, the activity of the application data in the application also affects whether the application data is released, reducing the possibility of the application data being loaded back into the system memory quickly after being released.

[0025] In some embodiments, the method also includes: determining a sixth memory amount corresponding to a second application running in the background, wherein the sixth memory amount indicates the amount of memory that needs to be reclaimed from the system memory occupied by the second application, and the anonymous page size stored by the second application in the system memory and the anonymous page size swapped out from the system memory are both the same as those of the first application; when the application heat of the second application is lower than the application heat of the first application, the sixth memory amount is greater than the first memory amount; according to the sixth memory amount, the system memory occupied by the second application is reclaimed, and the sum of the first memory amount and the sixth memory amount is not greater than the total memory amount that needs to be reclaimed by the electronic device.

[0026] In the above embodiment, different applications have different application popularity and different amounts of reclaimed system memory, so as to avoid excessive system memory being reclaimed from active applications while inactive applications occupy system memory for a long time.

[0027] In a second aspect, an embodiment of the present application provides an electronic device, the electronic device comprising one or more processors and a memory; the memory is coupled to the processor, the memory is used to store computer program code, the computer program code comprises computer instructions, when one or more processors execute the computer instructions, the one or more processors are used to: determine a first amount of memory corresponding to a first application running in the background, wherein the first amount of memory indicates the amount of memory to be reclaimed from the system memory occupied by the first application, the first amount of memory decreases as the application heat of the first application increases, the application heat indicates the level of activity of the application running in the background; according to the first amount of memory, reclaim the system memory occupied by the first application.

[0028] In some embodiments, before determining the first amount of memory corresponding to the first application running in the background, the one or more processors are used to: determine a first amount of reclaimable memory corresponding to the first application; the first amount of memory is not greater than the first amount of reclaimable memory.

[0029] In some embodiments, the one or more processors are further used to: determine that an application score of the first application is a first score; the application score of the first application decreases as the application popularity of the first application increases; determine the first reclaimable memory amount based on the first score, the second memory amount and the third memory amount corresponding to the first application; wherein the second memory amount indicates the size of all anonymous pages in the first application, and the third memory amount indicates the size of anonymous pages in the first application that have been swapped out from the system memory.

[0030] In some embodiments, the one or more processors are further used to: determine the transfer storage ratio corresponding to the first score; the transfer storage ratio is used to determine the ratio of the anonymous page size that can be transferred in a single application, and the higher the first score, the higher the corresponding transfer storage ratio; multiply the product of the transfer storage ratio and the second memory amount to determine the first anonymous page size; and remove the third memory amount from the first anonymous page size to determine the first reclaimable memory amount.

[0031] In some embodiments, the one or more processors are further used to: obtain a first ratio between a first reclaimable memory amount and a fourth memory amount, wherein the fourth memory amount refers to the sum of the reclaimable memory amounts corresponding to all the applications whose application scores are the first score and are running in the background; and determine the product of the first ratio and the fifth memory amount as the first memory amount, wherein the fifth memory amount refers to the total amount of memory that needs to be reclaimed from all the applications whose application scores are the first score.

[0032] In some embodiments, each of the applications corresponds to a memcg data structure, and the memcg data structure includes a field for carrying the amount of reclaimable memory; after determining the first amount of reclaimable memory, the one or more processors are also used to: write the determined first amount of reclaimable memory into the memcg data structure of the corresponding first application; the determination of the first amount of memory also includes: obtaining a fourth amount of memory; obtaining the fourth amount of memory includes: after the reclaimable memory has been written into the memcg data structure of the application running in the background, obtaining the reclaimable memory from the memcg data structure of the application running in the background whose application score is the first score; adding all the obtained reclaimable memory amounts to obtain the corresponding fourth amount of memory.

[0033] In some embodiments, the one or more processors are further used to: update the reclaimable memory amount in the memcg data structure of the first application using the difference between the first reclaimable memory amount of the first application and the first memory amount.

[0034] In some embodiments, the one or more processors are further used to: count the total amount of reclaimable memory corresponding to all the applications running in the background of the electronic device; obtain the fifth amount of memory; wherein the proportion of the fifth amount of memory in the total amount of memory that needs to be reclaimed by the electronic device is equal to the proportion of the fourth amount of memory in the total amount of reclaimable memory of the electronic device.

[0035] In some embodiments, the one or more processors are further used to: determine, in the anonymous pages of the first application, a target memory page whose system memory occupancy is the first memory amount, and the activity level of the target memory page is lower than other anonymous pages of the first application; and swap the target memory page out of the system memory.

[0036] In some embodiments, the one or more processors are further used to: determine a sixth memory amount corresponding to a second application running in the background, wherein the sixth memory amount indicates the amount of memory that needs to be reclaimed from the system memory occupied by the second application, and the anonymous page size stored by the second application in the system memory and the anonymous page size swapped out from the system memory are both the same as those of the first application; when the application heat of the second application is lower than the application heat of the first application, the sixth memory amount is greater than the first memory amount; according to the sixth memory amount, the system memory occupied by the second application is reclaimed, and the sum of the first memory amount and the sixth memory amount is not greater than the total memory amount that needs to be reclaimed by the electronic device.

[0037] In a third aspect, an embodiment of the present application provides a computer storage medium, including computer instructions. When the computer instructions are executed on an electronic device, the electronic device executes the method in the above-mentioned first aspect and possible embodiments thereof.

[0038] In a fourth aspect, the present application provides a computer program product. When the computer program product is run on the above-mentioned electronic device, the electronic device executes the method in the above-mentioned first aspect and possible embodiments thereof.

[0039] It can be understood that the electronic devices, computer storage media and computer program products provided in the above-mentioned aspects are all applied to the corresponding methods provided above. Therefore, the beneficial effects that can be achieved can refer to the beneficial effects in the corresponding methods provided above and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS

[0040] Figure 1 This is an example diagram of the corresponding least recently used linked list when the memcg mechanism is not enabled;

[0041] Figure 2 This is an example diagram of the least recently used linked list when the memcg mechanism is enabled;

[0042] Figure 3 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application;

[0043] Figure 4 An example diagram of the software structure of an electronic device provided in an embodiment of the present application;

[0044] Figure 5 A flowchart of a memory recycling method provided in an embodiment of the present application;

[0045] Figure 6 for Figure 5 The sub-step flow chart of S101 in FIG.

[0046] Figure 7 This is an example diagram of the first space size, the second space size, and the third space size corresponding to the application a in the embodiment of the present application;

[0047] Figure 8 An example diagram of a scenario for performing memory recycling on application programs in the same application set provided in an embodiment of the present application;

[0048] Fig. 9 An example diagram of a scenario for performing memory recycling on application programs in different application sets provided in an embodiment of the present application. DETAILED DESCRIPTION

[0049] The technical scheme in the embodiment of the present application will be described below in conjunction with the drawings in the embodiment of the present application. Wherein, in the description of the present application, unless otherwise specified, "at least one" refers to one or more, and "a plurality of" refers to two or more than two. In addition, in order to facilitate the clear description of the technical scheme of the embodiment of the present application, in the embodiment of the present application, the words "first", "second" and the like are used to distinguish the same items or similar items with substantially the same functions and effects. Those skilled in the art will understand that the words "first", "second" and the like do not limit the quantity and execution order, and the words "first", "second" and the like do not limit necessarily different.

[0050] For ease of understanding, the technical terms involved in the embodiments of the present application are first introduced below.

[0051] External memory is the "external memory" relative to the internal memory of electronic devices. Among them, external memory can include read only memory (ROM), hard disk, floppy disk, CD, USB flash drive, embedded multimedia card (embedded multimedia card, emmc), universal flash storage (UFS), multimedia card, etc.

[0052] Internal storage, also known as memory, is used to store page data corresponding to applications. In addition, memory may include system memory and swap partitions. In a system built on the Linux kernel, page data is mainly stored in system memory in the form of file-backed pages and anonymous pages.

[0053] The above anonymous pages and file pages can be collectively referred to as memory pages.

[0054] Among them, anonymous pages are memory pages that are not marked with names by the operating system. The virtual memory address (VMA) cannot distinguish different anonymous pages by name. In this way, memory pages without name tags are called anonymous pages. When anonymous pages are recycled, in order for the kernel to be able to access the corresponding anonymous pages later, the anonymous pages cannot be directly released (i.e. deleted) in the system memory. They need to be swapped out from the system memory to the Swap partition for compressed storage. The data in the Swap partition can be accessed by the CPU. Anonymous pages specifically refer to system memory pages that are dynamically allocated during program execution, including heaps, stacks, data segments, etc.

[0055] File pages, the operating system provides memory pages with name tags, and the corresponding file names are recorded on the VMA. Different file pages can be distinguished according to the file names. The system memory pages with name tags are called file pages. When the file page is recycled, the file page can be directly released (that is, deleted) in the system memory. Afterwards, the kernel can load the file page from the external memory to the system memory according to the file name of the file page, and then read it. File pages specifically refer to memory pages that store file-type data, including code segments, library files, and file data accessed by program interactions.

[0056] Taking the scenario of an electronic device playing music as an example, the system memory occupied by the program data of the electronic device running the player belongs to anonymous pages, and the system memory occupied by the music files loaded from the electronic device from the external storage into the system memory belongs to file pages.

[0057] zram, also known as zRAM, is a function of the CPU core that provides virtual memory compression. For example, the zram function compresses anonymous pages swapped out to the swap partition.

[0058] The swap partition is an area allocated from the memory. If the memory space is insufficient, there is no need to kill the application. Instead, the anonymous pages of the application can be swapped out to the swap partition and compressed. When the application is enabled again, this part of the data can be directly restored to the system memory, saving the time required to restart the application.

[0059] The memory swap mechanism (swap mechanism) is to write the data stored in the memory space that is not frequently accessed to the external memory first, reclaim this part of the memory space, and provide it to other processes. For example, a swap area is divided on the hard disk. Through the swap area, inactive memory pages in the memory are swapped to the hard disk to achieve the effect of increasing the memory.

[0060] The least recently used (LRU) algorithm is a commonly used memory page replacement algorithm. It is understandable that if a piece of data has not been accessed in the recent period of time, then the possibility of being accessed in the future is also very small. When memory recovery is required, the least recently used memory page is selected to be recovered to reclaim the memory space of this part of the memory page. In the LRU linked list, the memory pages are arranged in order from small to large according to the corresponding first time interval. Among them, the first time interval refers to the time interval between the time point when the memory page was last used and the current time point. In this way, in the LRU linked list, the memory page that is closer to the end of the linked list is accessed earlier (that is, the memory page is colder), and the memory page that is closer to the head of the linked list is accessed more recently (that is, the memory page is hotter). When memory recovery is started from the end of the linked list.

[0061] In some embodiments, a memory watermark may be preconfigured in the electronic device, and the memory watermark is used to indicate the conditions for triggering memory space reclamation. When the amount of system memory occupied in the electronic device exceeds the memory watermark, reclamation of the system memory may be triggered.

[0062] In some embodiments, the memory pages of all applications in the electronic device are mounted on the LRU linked list of the same node. There are multiple LRU linked lists corresponding to the same node, such as an inactive anonymous linked list, an active anonymous linked list, an inactive file linked list, an active file linked list, and a non-recycling linked list.

[0063] Exemplarily, anonymous pages that have not been accessed in the recent period are mounted on the inactive anonymous linked list. For example, the inactive anonymous linked list includes page descriptors of anonymous pages that have not been accessed in the recent period. Among them, the end point of the recent period is the current time point, and the length is a preset duration. When memory is recycled, the anonymous page to be released (such as the target memory page) can be determined from the inactive anonymous linked list and swapped out to the swap partition. In addition, the anonymous pages in the inactive anonymous linked list can be arranged according to the interval duration of the corresponding first time interval, so that the anonymous page with a shorter first time interval is arranged near the head of the linked list, and the anonymous page with a longer first time interval is arranged near the tail of the linked list. The electronic device can preferentially determine the anonymous page near the tail of the linked list as the target memory page.

[0064] The active anonymous linked list is used to mount anonymous pages that have been accessed in the recent period. The anonymous pages in the active anonymous linked list cannot be swapped out directly to the swap partition. In some embodiments, the first time interval corresponding to the anonymous page in the active anonymous linked list is greater than a preset length, that is, if the anonymous page has not been accessed in the recent period, it can be placed in the inactive anonymous linked list first.

[0065] The inactive file list is used to mount file pages that have not been accessed in the recent period. When memory is recycled, the system memory occupied by the file pages in the inactive file list can be directly recycled. It is similar to the inactive anonymous list, but the difference is that the type of memory page mounted is different.

[0066] The active file list is used to mount the file pages that have been accessed recently. The system memory occupied by the file pages in the active file list cannot be directly reclaimed. It is similar to the active anonymous list, but the difference is that the type of memory page mounted is different.

[0067] The non-recyclable linked list is used to mount locked memory pages that cannot be recycled. They may be anonymous pages or file pages.

[0068] like Figure 1As shown, the electronic device includes running APP1, APP2, APP3 and APP4. Among them, APP1, APP2, APP3 and APP4 can be running in the foreground or in the background, which is not specifically limited in this example. Figure 1 As shown, in the memory pages corresponding to APP1, APP2, APP3 and APP4, anonymous pages (inactive anonymous) that have not been accessed in the recent period are mounted on the inactive anonymous linked list corresponding to the node. Exemplarily, in the memory pages corresponding to APP1, APP2, APP3 and APP4, anonymous pages (active anonymous) that have been accessed in the recent period are mounted on the active anonymous linked list of the node. Exemplarily, in the memory pages corresponding to APP1, APP2, APP3 and APP4, file pages (inactive file) that have not been accessed in the recent period are mounted on the inactive file linked list of the node. Exemplarily, in the memory pages corresponding to APP1, APP2, APP3 and APP4, file pages (active file) that have been accessed in the recent period are mounted on the active file linked list of the node. Exemplarily, in the memory pages corresponding to APP1, APP2, APP3 and APP4, locked memory pages (unevictable) are mounted on the non-recyclable linked list of the node.

[0069] In the scenario where the amount of system memory in the electronic device exceeds the corresponding memory waterline, the electronic device can traverse the inactive anonymous linked list and inactive file linked list corresponding to the node, and select the target memory page that can be released (e.g., deleted or swapped out to the swap partition and compressed). For example, the memory page at the end of the inactive anonymous linked list or at the end of the inactive file linked list is used as the target memory page.

[0070] If the target memory page is a file page, the file page can be deleted to reclaim the system memory occupied by the file page. If the target memory page is an anonymous page, the anonymous page is swapped out to the swap partition and compressed. In this way, the system memory occupied by the target memory page can be reclaimed, which can also be called reclaiming system memory.

[0071] In the subsequent embodiments, description is made by taking the target memory page to be released as an anonymous page as an example. Determining the target memory page from the LRU linked list of the application mentioned in the subsequent embodiments may refer to determining the target memory page from an inactive anonymous linked list.

[0072] In other embodiments, the electronic device can manage the system memory occupied by different applications through a memory control subsystem (memory cgroup, memcg) mechanism. In this way, the electronic device can create a memcg for each application. Each application corresponds to a data node, and the data nodes of different applications are different. In addition, each data node corresponds to multiple LRU linked lists, and the memory pages of the same application are mounted on the LRU linked list of the same data node.

[0073] like Figure 2 As shown, the electronic device includes running APP1 and APP2. APP1 and APP2 can be running in the foreground or in the background, which is not specifically limited in this example.

[0074] For example, Figure 2 As shown, memcg1 is mounted after the node in the electronic device, and the memcg1 is used to manage the memory page corresponding to APP1. The anonymous page that has not been accessed in the recent period is mounted on the inactive anonymous linked list corresponding to memcg1.

[0075] like Figure 2 As shown, in the memory page corresponding to APP1, the anonymous pages that have been accessed in the recent period are mounted on the active anonymous linked list of memcg1. In the memory page corresponding to APP1, the file pages that have not been accessed in the recent period are mounted on the inactive file linked list of memcg1. In the memory page corresponding to APP1, the file pages that have been accessed in the recent period are mounted on the active file linked list of memcg1. In the memory page corresponding to APP1, the locked memory pages are mounted on the non-recycling linked list of memcg1.

[0076] like Figure 2 As shown, memcg2 is mounted after the node in the electronic device, and in the memory page corresponding to APP2, the anonymous page that has been accessed in the recent period is mounted on the active anonymous linked list of memcg2. In the memory page corresponding to APP2, the file page that has not been accessed in the recent period is mounted on the inactive file linked list of memcg2. In the memory page corresponding to APP2, the file page that has been accessed in the recent period is mounted on the active file linked list of memcg2. In the memory page corresponding to APP2, the locked memory page is mounted on the non-recycling linked list of memcg2. Of course, in the case where there are more applications running in the electronic device, the electronic device can also include more data nodes, and the LRU linked list of each data node is mounted with the memory page of the corresponding application.

[0077] In the scenario where the amount of system memory occupied in an electronic device exceeds the corresponding memory waterline, the electronic device can traverse the LRU linked list corresponding to each data node in turn, select the target memory page, and reclaim the system memory occupied by the target memory page. When the system memory meets the preset conditions, stop traversing the LRU linked list corresponding to the remaining data nodes. Among them, the preset conditions can be: the amount of system memory occupied after recovery is not greater than the memory waterline, or the difference between the memory waterline and the amount of system memory occupied after recovery is not less than the preset threshold.

[0078] It is understandable that after the electronic device adopts the memcg mechanism to manage the system memory, the management granularity of the electronic device for the system memory can be refined to the application level. At present, the memcg mechanism has been widely used in most electronic devices.

[0079] However, for an electronic device with the memcg mechanism enabled, if the electronic device contains multiple applications running in the background, during the memory recycling process, the system memory occupied by applications with a higher probability of use may be recycled, while the system memory occupied by applications with a lower probability of use may not be recycled.

[0080] It is understandable that in the process of memory recycling by the electronic device, the LRU linked list of each application is traversed indiscriminately to determine the target memory page that can be released. In this way, there may be a scenario where the LRU linked list of an application with a higher probability of use is traversed before the LRU linked list of an application with a lower probability of use. In addition, after the system memory occupied by the application with a higher probability is recycled, if the system memory meets the preset conditions, the system memory occupied by other applications will also stop being recycled. In this way, in the scenario where the LRU linked list of an application with a higher probability of use is traversed before the LRU linked list of an application with a lower probability of use, there will also be a situation where the system memory occupied by the application with a higher probability of use is recycled, while the system memory occupied by the application with a lower probability of use is not recycled.

[0081] In addition, the target memory page determined from the LRU linked list of the application with a higher probability of use has a lower access probability than other memory pages of the application, but has a higher access probability than the memory pages of the application with a lower probability of use. After the system memory occupied by the target memory page of the application with a higher probability of use is reclaimed, it may appear that after a short time interval, the target memory page is loaded back into the system memory again, and even triggers memory recycling again. Obviously, not only does it fail to alleviate memory pressure, but it also affects the service life of the storage device.

[0082] Taking the scenario where the system memory occupied by anonymous page 1 of APP1 is recycled while APP1 and APP2 are kept alive in the background as an example, the usage probability of APP1 is higher than that of APP2. When recycling memory, first traverse the LRU linked list corresponding to APP1, and determine anonymous page 1 as the target memory page. After anonymous page 1 is swapped out to the swap partition (or external memory), it is determined that the recycled system memory meets the preset conditions, and the electronic device does not continue to recycle the system memory occupied by APP2.

[0083] Afterwards, the electronic device detects the user's instruction to run APP1 in the foreground, and needs to reload anonymous page 1 from the swap partition (or external memory) back to the system memory. In this process, the time interval from swapping out to the swap partition or external memory to reloading back to the system memory can be called the reload interval.

[0084] It is understandable that the probability of using APP1 is higher than that of using APP2, and the probability of anonymous page 1 of APP1 being accessed is also higher than that of anonymous page 2 of APP2, wherein anonymous page 2 is a memory page mounted on the inactive anonymous linked list corresponding to APP2. After anonymous page 1 is recycled, since the system memory has met the preset conditions, the electronic device will not recycle the system memory occupied by anonymous page 2. However, the probability of anonymous page 2 being accessed is lower than that of anonymous page 1. In comparison, the reload interval corresponding to anonymous page 1 is more likely to be shorter than the reload interval corresponding to anonymous page 2. However, the electronic device only recycles anonymous page 1 and does not recycle anonymous page 2, causing anonymous page 2 to occupy the system memory all the time. After anonymous page 1 is released, it will be reloaded into the system memory. In this way, repeated release and reloading not only fails to relieve memory pressure, but also reduces the life of the storage device.

[0085] In addition, even for applications with the same usage probability, due to the different order in which they are traversed, it is possible that the system memory occupied by some applications is reclaimed while the system memory occupied by other applications is not reclaimed. In the unequal recycling scenario, if the recycled application needs to run in the foreground, the memory page size required to be loaded back into the system memory is larger and takes longer.

[0086] In order to improve the above problems, an embodiment of the present application provides a memory recovery method that can be applied to an electronic device with the memcg mechanism enabled. In a scenario where memory recovery is required, the electronic device can predetermine the size of the memory to be recovered for each application. Among them, the higher the probability of use of the application, the smaller the corresponding memory to be recovered. The lower the probability of use of the application, the larger the corresponding memory to be recovered. Between applications with the same probability of use, the size of the memory to be recovered corresponding to each application is determined according to the size of the recyclable memory.

[0087] Afterwards, when traversing the LRU linked list of each application, the system memory occupied by the application is reclaimed according to the size of the memory to be reclaimed corresponding to the application.

[0088] In this way, electronic devices that enable the memcg mechanism can also recycle system memory evenly, avoiding the situation where memory pages with a higher access probability are recycled while memory pages with a lower access probability are not recycled. It also reduces the memory recycling size of a single application and improves the efficiency of reloading the recycled memory pages of the application.

[0089] Exemplarily, the electronic device may be a mobile phone, a tablet computer, a laptop, a handheld computer, a notebook computer, an ultra-mobile personal computer (UMPC), a netbook, a personal digital assistant (PDA), an augmented reality (AR) or a virtual reality (VR) device, etc. The embodiment of the present application does not impose any special restrictions on the specific form of the electronic device. In the subsequent embodiments, a mobile phone configured with a Linux kernel is mainly used as an example for explanation.

[0090] Please refer to Figure 3 , is a structural diagram of a mobile phone provided in an embodiment of the present application.

[0091] like Figure 3 As shown, the mobile phone may include: a processor 110, an external memory interface 120, an internal memory 121, a universal serial bus (USB) interface 130, a charging management module 140, a power management module 141, a battery 142, an antenna 1, an antenna 2, a mobile communication module 150, a wireless communication module 160, an audio module 170, a speaker 170A, a receiver 170B, a microphone 170C, an earphone interface 170D, a sensor module 180, a button 190, a motor 191, an indicator 192, a camera 193, a display screen 194, and a subscriber identification module (SIM) card interface 195, etc.

[0092] Among them, the above-mentioned sensor module 180 may include sensors such as pressure sensor, gyroscope sensor, air pressure sensor, magnetic sensor, acceleration sensor, distance sensor, proximity light sensor, fingerprint sensor, temperature sensor, touch sensor, ambient light sensor and bone conduction sensor.

[0093] It is to be understood that the structure illustrated in this embodiment does not constitute a specific limitation on the mobile phone. In other embodiments, the mobile phone may include more or fewer components than shown in the figure, or combine some components, or split some components, or arrange the components differently. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.

[0094] The processor 110 may include one or more processing units, for example, the processor 110 may include an application processor (AP), a modem processor, a graphics processor (GPU), an image signal processor (ISP), a controller, a memory, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural-network processing unit (NPU), etc. Different processing units may be independent devices or integrated into one or more processors.

[0095] The controller can be the nerve center and command center of the mobile phone. The controller can generate operation control signals according to the instruction operation code and timing signal to complete the control of fetching and executing instructions.

[0096] The processor 110 may also be provided with a memory for storing instructions and data. In some embodiments, the memory in the processor 110 is a cache memory. The memory may store instructions or data that the processor 110 has just used or cyclically used. If the processor 110 needs to use the instruction or data again, it may be directly called from the memory. This avoids repeated access, reduces the waiting time of the processor 110, and thus improves the efficiency of the system.

[0097] In some embodiments, the processor 110 may include one or more interfaces. The interface may include an inter-integrated circuit (I2C) interface, an inter-integrated circuit sound (I2S) interface, a pulse code modulation (PCM) interface, a universal asynchronous receiver / transmitter (UART) interface, a mobile industry processor interface (MIPI), a general-purpose input / output (GPIO) interface, a subscriber identity module (SIM) interface, and / or a universal serial bus (USB) interface, etc.

[0098] It is understandable that the interface connection relationship between the modules illustrated in this embodiment is only a schematic illustration and does not constitute a structural limitation on the mobile phone. In other embodiments, the mobile phone may also adopt different interface connection methods in the above embodiments, or a combination of multiple interface connection methods.

[0099] The mobile phone implements display functions through a GPU, a display screen 194, and an application processor. The GPU is a microprocessor for image processing, which connects the display screen 194 and the application processor. The GPU is used to perform mathematical and geometric calculations for graphics rendering. The processor 110 may include one or more GPUs that execute program instructions to generate or change display information.

[0100] The external memory interface 120 can be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the mobile phone. The external memory card communicates with the processor 110 through the external memory interface 120 to implement a data storage function. For example, files such as music and videos can be stored in the external memory card.

[0101] The internal memory 121 can be used to store computer executable program codes, and the executable program codes include instructions. The internal memory 121 may include a program storage area and a data storage area. In addition, the internal memory 121 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, a universal flash storage (UFS), etc. The processor 110 executes various functional applications and data processing of the mobile phone by running instructions stored in the internal memory 121 and / or instructions stored in a memory provided in the processor.

[0102] The display screen 194 is used to display images, videos, etc. The display screen 194 includes a display panel. The display panel can be a liquid crystal display (LCD), an organic light-emitting diode (OLED), an active-matrix organic light-emitting diode or an active-matrix organic light-emitting diode (AMOLED), a flexible light-emitting diode (FLED), Miniled, MicroLed, Micro-oLed, a quantum dot light-emitting diode (QLED), etc.

[0103] The mobile phone can realize the shooting function through ISP, camera 193, video codec, GPU, display 194 and application processor.

[0104] ISP is used to process the data fed back by camera 193. For example, when taking a photo, the shutter is opened, and the light is transmitted to the camera photosensitive element through the lens. The light signal is converted into an electrical signal, and the camera photosensitive element transmits the electrical signal to ISP for processing and converts it into an image visible to the naked eye. ISP can also perform algorithm optimization on the noise, brightness, and skin color of the image. ISP can also optimize the exposure, color temperature and other parameters of the shooting scene. In some embodiments, ISP can be set in camera 193.

[0105] The camera 193 is used to capture still images or videos. The object generates an optical image through the lens and projects it onto the photosensitive element. The photosensitive element can be a charge coupled device (CCD) or a complementary metal-oxide-semiconductor (CMOS) phototransistor. The photosensitive element converts the optical signal into an electrical signal, and then passes the electrical signal to the ISP to be converted into a digital image signal. The ISP outputs the digital image signal to the DSP for processing. The DSP converts the digital image signal into an image signal in a standard RGB, YUV or other format. In some embodiments, the mobile phone may include N cameras 193, where N is a positive integer greater than 1.

[0106] Digital signal processors are used to process digital signals. In addition to processing digital image signals, they can also process other digital signals. For example, when a mobile phone selects a frequency point, the digital signal processor is used to perform Fourier transform on the frequency point energy.

[0107] Video codecs are used to compress or decompress digital videos. A mobile phone can support one or more video codecs. In this way, the mobile phone can play or record videos in multiple coding formats, such as: Moving Picture Experts Group (MPEG) 1, MPEG2, MPEG3, MPEG4, etc.

[0108] NPU is a neural network (NN) computing processor. It can quickly process input information and continuously self-learn by drawing on the structure of biological neural networks, such as the transmission mode between neurons in the human brain. NPU can realize applications such as intelligent cognition of mobile phones, such as image recognition, face recognition, voice recognition, text understanding, etc.

[0109] On top of the above components, a software system developed based on the Linux kernel is run. Among them, the above software system can adopt a layered architecture, an event-driven architecture, a micro-core architecture, a micro-service architecture, or a cloud architecture, etc. In the embodiment of the present application, the Android system with a layered architecture is taken as an example to illustrate the software system architecture of the mobile phone.

[0110] like Figure 4 As shown, the above software system architecture includes an application layer (Application, APP), an application framework layer (also called application framework layer or Framework) and a kernel layer (Kernel).

[0111] Exemplarily, the application layer may include a series of application packages. Figure 4As shown, the application package may include applications such as camera, gallery, calendar, call, map, etc.

[0112] Exemplarily, the application framework layer provides an application programming interface (API) and a programming framework for the application programs of the application layer. The application framework layer includes some predefined functions.

[0113] In addition, the application framework layer can also manage applications and record the running status of each application (for example, foreground, background or stopped running status), as well as the time point of state switching.

[0114] In some embodiments, Figure 4 As shown, the application framework layer also includes an application evaluation module, which can obtain the running state of the application and the time point of the state switching from the application framework layer. In addition, the application evaluation module can also calculate the application score corresponding to each application according to the running state and the time point of the state switching according to the preconfigured rules. Among them, when the application scores are different, the corresponding usage probabilities are also different. When the application scores are the same, the corresponding usage probabilities are the same. In addition, the change trend of the application score can indicate the change trend of the usage probability. Exemplarily, the higher the application score, the lower the usage probability of the application, and the lower the application score, the higher the usage probability of the application. Of course, in other embodiments, it can also be that the higher the application score, the higher the usage probability of the application, and the lower the application score, the lower the usage probability of the application, which depends on the rules used to calculate the application score. The embodiment of the present application does not specifically limit this. In the subsequent embodiments, the higher the application score, the lower the usage probability, and the lower the application score, the higher the usage probability.

[0115] Exemplarily, the kernel layer is a layer between hardware and software, providing interaction with hardware. The kernel layer may include various functional modules and drivers.

[0116] like Figure 4 As shown, the kernel layer includes a memory recovery module. After the memory recovery module obtains the application score from the application evaluation module, it can determine the size of the memory to be recovered in the system memory occupied by each application. The process of determining the size of the memory to be recovered can refer to the subsequent embodiments and will not be described here. Afterwards, in the process of traversing the LRU linked list of each application, the system memory occupied by each application is recovered according to the size of the memory to be recovered of each application.

[0117] In addition, the memory recycling module can also monitor the memory status of the electronic device, and trigger a memory recycling process when it is detected that the memory recycling condition is met. The memory recycling condition may include that the system memory usage exceeds the memory waterline.

[0118] It can be understood that the above software system architecture may also include Figure 4 Layers not shown in the figure, such as the HAL layer, Android runtime, system library, etc., are not described here.

[0119] The following describes, in conjunction with the accompanying drawings, a process of memory recycling in an electronic device with the memcg mechanism enabled in an embodiment of the present application.

[0120] In an embodiment of the present application, the electronic device may evaluate the application score of an application program running in the background.

[0121] In some embodiments, the electronic device may periodically evaluate application scores corresponding to application programs running in the background.

[0122] As an implementation method, the method of evaluating the application score corresponding to the application running in the background is as follows:

[0123] S1, the application evaluation module can obtain the running status and state switching time point of each application from the application framework layer at preset intervals.

[0124] Among them, the application evaluation module can determine the application currently running in the background according to the running status of each application. The application evaluation module can also determine the time point of the most recent switch to the background operation according to the state switching time point of the application running in the background. Then, the application evaluation module can also determine the current corresponding background operation time of the application running in the background according to the current system time and the time point of the most recent switch to the background operation. In addition, the application evaluation module can also determine the usage frequency corresponding to each application running in the background according to the state switching time point. For example, the above-mentioned usage frequency can be the number of times each application is switched to the foreground operation in a specified period, and the specified period can be the period between the first time point and the current time point. The above-mentioned first time point can be the early morning of the same day, or it can be a time point with a specified time interval from the current time point. In addition, the application evaluation module can also predict the user's usage habits for each application according to the state switching time point, such as the period of time when the application is often used, and multiple groups of applications that are often jumped and used.

[0125] S2, after obtaining the running status and state switching time point of each application, the application evaluation module can also evaluate the corresponding score of the application from multiple dimensions such as application type, background running time, usage frequency, and usage habits.

[0126] In some embodiments, the application evaluation module can evaluate the scores corresponding to the application programs based on the operation record data of the application programs from multiple dimensions such as application type, background operation time, usage frequency, and usage habits. The operation record data may include the time point of the foreground operation of the application program, the time point of the background operation, and the application service provision status information during the background operation.

[0127] Taking the score of the application type dimension as an example, the score of the first type of application in the application type dimension is 1, and the score of non-first type of application in the application type dimension is 2, and the score of 1 is less than the score of 2.

[0128] The first type may indicate an application in a specific state. For example, the specific state may be an application running in the background and providing corresponding application services normally. Thus, in an exemplary scenario, the music application plays music normally while running in the background, and its score in the application type dimension is determined to be a value of 1.

[0129] In other embodiments, the first type may also indicate an application with a specific identifier. For example, the specific identifier may be an identifier assigned to the application in response to a user's pre-selection of the application, and the specific identifier may be used to distinguish applications that are not pre-selected by the user.

[0130] Taking the evaluation of the score of the background running time dimension as an example, the longer the background running time of the application, the higher the score of the background running time dimension, and the shorter the background running time of the application, the lower the application score corresponding to the score of the background running time dimension. Different background running time intervals and corresponding different scores can be pre-configured in the electronic device, so that the score of the application in the running time dimension can be matched according to the background running time of the application.

[0131] Taking the frequency of use dimension as an example, the lower the frequency of use of an application, the higher the frequency of use score. The higher the frequency of use of an application, the lower the score of the application corresponding to the frequency of use dimension.

[0132] Taking the score of the usage habit dimension as an example, the usage habit corresponding to the application indicates that the application will be used in the current scenario, and the score in the application type dimension is 3. The usage habit corresponding to the application indicates that the application will not be used in the current scenario, and the score in the application type dimension is 4, which is greater than 3.

[0133] S3, after obtaining the scores of the application in each dimension, the application evaluation module can fit the application score corresponding to the application according to the scores corresponding to each dimension.

[0134] In some embodiments, a preconfigured algorithm may be used to fit the scores corresponding to each dimension to obtain the application score corresponding to the application. For example, the scores of each dimension may be processed in combination with the weights of different dimensions, and then the processed scores may be superimposed to obtain the application score.

[0135] In other embodiments, when the running state of any application changes, the electronic device may be triggered to evaluate the application score corresponding to the application running in the background. For example, when an application running in the foreground switches to running in the background, the electronic device may be triggered to evaluate the application score corresponding to the application currently running in the background.

[0136] In other embodiments, the electronic device may not only periodically evaluate the application scores corresponding to the background running applications, but may also trigger the evaluation of the application scores corresponding to any application when the application switches from the foreground running state to the background running state.

[0137] In the embodiment of the present application, after determining the application scores of the application programs running in the background, the electronic device may also determine the size of the to-be-reclaimed memory corresponding to each application program running in the background.

[0138] Exemplarily, each time the application evaluation module obtains an application score of an application program, the application score may be sent to the memory recovery module.

[0139] As another example, when the memory recovery module identifies a scenario where memory recovery is required, the application score of each application program running in the background is obtained from the application evaluation module.

[0140] As an implementation mode, the memory recovery module in the electronic device can calculate the size of the to-be-reclaimed memory corresponding to each application program running in the background according to the application score from the application evaluation module.

[0141] In some embodiments, Figure 5 As shown, the process of calculating the size of the memory to be reclaimed corresponding to each background application is as follows:

[0142] S101, determining the reclaimable memory size of each application running in the background, wherein the value of the reclaimable memory size is affected by the popularity of the application.

[0143] The heat of an application may indicate the activity of the application during background operation. For example, an application with a higher probability of being used (or a higher probability of being switched to the foreground) has a higher heat, while an application with a lower probability of being used (or a higher probability of being switched to the foreground) has a lower heat.

[0144] Exemplarily, the application popularity of the above-mentioned application can be indicated by the application score of the application. In the aforementioned embodiment, the process of evaluating the application score can also be an evaluation of the application popularity. Of course, the higher the application score, the lower the application popularity corresponding to the application, and the lower the application score, the higher the application popularity corresponding to the application. In the actual operation process, the application score of the application can decrease as the application popularity increases, and increase as the application popularity increases.

[0145] In some embodiments, the higher the application score, the larger the value of the determined reclaimable memory size is, and the lower the application score, the smaller the value of the determined reclaimable memory size is.

[0146] In some other embodiments, in addition to being affected by the application heat, the reclaimable memory size of the application may also be affected by the size of anonymous pages that have been dumped by the application.

[0147] like Figure 6 As shown, the above S101 may also include the following sub-steps:

[0148] S101-1, traverse the memcg corresponding to each application running in the background.

[0149] In some embodiments, the memory recycling module may traverse the memcg data structure corresponding to the application running in the background. Exemplarily, the memcg data structure may include a data node for associating an LRU linked list, a field for recording an application identifier, etc. The application identifier is used to uniquely indicate an application.

[0150] As another example, the above memcg data structure may also include a field for recording the application score. That is, after the application evaluation module determines the application score of the application, the application score may be passed to the kernel layer and written into the field for recording the application score in the memcg data structure of the application.

[0151] As another example, the memcg data structure may also include other types of data, such as anonymous pages of the application in the system memory and the size of the storage space occupied, such as the first space size.

[0152] The anonymous pages of the application that have been swapped out to the swap partition (such as compressed pages) and the storage space occupied by the compressed pages in the swap partition are called the second space size. For another example, the anonymous pages of the application that have been swapped out to the external memory (such as dump pages) and the storage space occupied by the dump pages in the external memory are called the third space size.

[0153] for example, Figure 7As shown, the storage area a1 in the system memory is the memory space for storing anonymous pages of application a, the storage area a2 in the swap partition is the memory space for storing compressed pages of application a, and the storage area a3 in the external memory is the external memory space for storing the dumped pages of application a. Among them, the space size of storage area a1 is 40MB, the space size of storage area a2 is 5MB, and the space size of storage area a3 is 15MB. In this way, the corresponding first space size in the memcg data structure of application a is 40MB, the second space size is 5MB, and the corresponding third space size is 15MB.

[0154] As an implementation method, the memory recovery module can match the memcg data structure corresponding to the background running application according to the application identifier of the background running application, and then traverse the memcg data structure of each background running application in turn.

[0155] S101 - 2 , when traversing to the memcg of application a, obtaining the application score a, the first space size, the second space size, and the third space size of application a.

[0156] In some embodiments, the above-mentioned application a is an example of an application running in the background. It can be understood that when traversing the memcg of other applications running in the background, the application score, first space size, second space size and third space size of the application can also be obtained.

[0157] In some embodiments, the sum of the first space size, the second space size, and the third space size of the first application may be referred to as a second memory amount. The second memory amount indicates the size of all anonymous pages of the application. The sum of the second space size and the third space size of the first application may be referred to as a third memory amount. The third memory amount indicates the size of anonymous pages of the application that have been transferred to storage.

[0158] In addition, the first space size, the second space size, the third space size and the application score a corresponding to the application a can all be obtained from the memcg data structure corresponding to the application a. The second space size and the third space size can indicate the storage space occupied by the anonymous pages that have been swapped out of the system memory in the application a.

[0159] S101 - 3 , querying a corresponding transfer-storage ratio a according to the application score a and the pre-configured first list.

[0160] In some embodiments, the electronic device may preconfigure a first list. The first list may indicate the transfer storage ratios corresponding to different application scores. For example, the first list includes application score a and corresponding transfer storage ratio a.

[0161] When the application a is the first application, the first score of the first application is application score a, and the transfer storage ratio corresponding to the first score can be determined through the first list as transfer storage ratio a. In addition, the higher the first score, the higher the corresponding transfer storage ratio.

[0162] Among them, the transfer storage ratio can indicate the ratio between the anonymous page size of the application that can be transferred and stored and the size of all anonymous pages (anonymous pages included in the system memory, compressed pages in the swap partition, and transfer pages in the external memory). The size of the anonymous page that can be transferred and stored is not less than the size of the anonymous pages that have been swapped out to the swap partition and the external memory. In addition, in the first list, the higher the application score, the larger the corresponding transfer storage ratio, and the lower the application score, the smaller the corresponding transfer storage ratio. In this way, after determining the application score a of application a, the memory recovery module can use the application score a to match the transfer storage ratio a corresponding to the application score a from the first list.

[0163] S101 - 4 , calculating the reclaimable memory size a corresponding to the application a according to the transfer storage ratio a, the first space size, the second space size, and the third space size corresponding to the application a.

[0164] In some embodiments, the first space size, the second space size, and the third space size corresponding to the application a may be superimposed to obtain the size corresponding to all anonymous pages of the application a. Then, the anonymous page size that can be transferred is determined using the transfer ratio a and the size corresponding to all anonymous pages. Then, the transferred anonymous page size is removed from the anonymous page size that can be transferred to obtain the reclaimable memory size a. The transferred anonymous page size is equal to the sum of the second space size and the third space size.

[0165] S101 - 5 , when the reclaimable memory size a is not less than 0, the reclaimable memory size a is associated with the memcg of the application a.

[0166] In some embodiments, the above-mentioned reclaimable memory size a (or reclaimable memory amount) can be written into the field for storing the reclaimable memory amount in the memcg data structure of application a, so that the reclaimable memory size a is associated with the memcg of application a.

[0167] It can be understood that, in theory, the above reclaimable memory size a is not less than 0. Of course, in the actual operation process, based on the transfer storage ratio a, the first space size, the second space size and the third space size corresponding to the application a, the calculated reclaimable memory size a may be less than 0. For example, in the scenario where the application score of the application changes from a larger value to a smaller value. When the application score is a larger value, some anonymous pages have been swapped out to the swap partition or external memory. Before the application score becomes a smaller value, if the anonymous pages that have been swapped out to the swap partition or external memory are not loaded back into the system memory, after the application score has become a smaller value, based on the transfer storage ratio corresponding to the smaller application score, the calculated "anonymous page size that can be transferred" may be smaller than the actual transferred anonymous page size. According to the above S101-4, the value of the determined reclaimable memory size a is less than 0. In this scenario, the value of the calculated reclaimable memory size a can be revised to 0. After the value of the reclaimable memory size a is revised to 0, the reclaimable memory size a can also be associated with the memcg of the application a.

[0168] In the above embodiment, the higher the corresponding application score, the larger the reclaimable memory size determined by the above method, and the lower the corresponding application score, the smaller the reclaimable memory size determined by the above method. In this way, the reclaimable memory size (maximum reclaimable amount) corresponding to the application with a higher probability of use is smaller, avoiding excessive reclaim of system memory occupied by applications with a higher probability of use, and providing conditions for reclaiming the system memory occupied by various applications in a balanced manner within a time limit.

[0169] As an implementation method, the algorithm for determining the reclaimable memory size of each application can be implemented as follows:

[0170] do{

[0171] score = memcg_score; / / indicates to obtain the application score of the traversed application;

[0172] ub_mem2zram=memcg_ub_mem2zram / / indicates to obtain the corresponding transfer storage ratio according to the application score;

[0173] anon = memcg_inactive_anon + memcg_active_anon; / / Indicates to obtain the total amount of all anonymous pages of the application in the system memory, for example, to obtain the total size of all anonymous pages of the application mounted on the active anonymous list and the inactive anonymous list;

[0174] zram=memcg_zram / / Indicates to obtain the size of the anonymous page of the application in the swap partition;

[0175] eswap = memcg_eswap / / indicates to obtain the size of the anonymous page that the application has stored in the external memory;

[0176] ub_mem2zram=memcg_ub_mem2zram;

[0177] can_reclaim_memcg = (anon + zram + eswap) * ub_mem2zram - (zram + eswap); / / indicates to calculate the anonymous page size that the application can dump, that is, the reclaimable memory size of the application mentioned in the above embodiment;

[0178] can_reclaim_memcg = can_reclaim_memcg>0? can_reclaim_memcg: 0 / / indicates that when the reclaimable memory size of the application is less than 0, its value is set to 0;

[0179] level_can_reclaim[score]+=can_reclaim_memcg / / indicates the total reclaimable memory size corresponding to the application programs with the same application score, for example, the total reclaimable memory size corresponding to the application set mentioned in the above embodiment;

[0180] total_can_reclaim += can_reclaim_memcg; / / indicates the size of memory that can be reclaimed by all applications;

[0181] }while(memcg=memcg_next,memcg!=NULL); / / End after all applications have been traversed.

[0182] It is understandable that the above reclaimable memory size refers to the maximum amount of memory that can be reclaimed from the system memory occupied by the application program determined according to the application score. After obtaining the reclaimable memory sizes corresponding to all the application programs, the process may enter S102.

[0183] S102, traverse each application running in the background, and when traversing to application a, determine the memory size to be reclaimed corresponding to application a, and reclaim the system memory occupied by application a according to the memory size to be reclaimed, and the memory size to be reclaimed is not larger than the reclaimable memory size.

[0184] The size of memory to be reclaimed refers to the amount of memory to be reclaimed from the system memory occupied by the application when memory reclaim is actually performed. It can also be referred to as the amount of memory to be reclaimed from the application. The value of the size of memory to be reclaimed is limited by the above-mentioned reclaimable memory size. At the same time, the above-mentioned reclaimable memory size is affected by the popularity of the application. In this way, the amount of memory to be reclaimed corresponding to each application will be affected by the popularity of the application, avoiding too much system memory being reclaimed for applications with high application popularity.

[0185] In addition, the size of the memory to be reclaimed corresponding to the first application can be called the first memory amount, and the size of the memory to be reclaimed corresponding to the second application can be called the sixth memory amount. The first memory amount is affected by the application popularity of the first application. The higher the application popularity of the first application, the lower the corresponding first memory amount, and the lower the popularity of the first application, the higher the corresponding first memory amount. In short, the first memory amount decreases as the application popularity of the first application increases. Of course, the first memory amount is also limited by the reclaimable memory size of the first application (that is, the first reclaimable memory amount). When the application popularity of the first application is high, it is avoided that the total amount of system memory reclaimed by the first application is too much after multiple memory recycling.

[0186] Similarly, the second memory amount is affected by the application popularity of the second application. The higher the application popularity of the second application, the lower the corresponding second memory amount, and the lower the popularity of the second application, the higher the corresponding second memory amount. In short, the second memory amount decreases as the application popularity of the second application increases. Of course, the second memory amount is also limited by the reclaimable memory size of the second application (that is, the second reclaimable memory amount).

[0187] In addition, in a scenario where the first space size, the second space size, and the third space size corresponding to the first application and the second application are the same, if the application popularity of the second application is lower than the application popularity of the first application, the second memory amount is greater than the first memory amount.

[0188] In some embodiments, the process of determining the size of the to-be-reclaimed memory corresponding to each application program may be as follows:

[0189] A1, dividing the application programs running in the background into a plurality of application sets according to the application scores, wherein the divided application set a includes the application program a.

[0190] In some embodiments, application programs with the same application score may be grouped into the same application set, and application programs with different application scores may belong to different application sets.

[0191] In other embodiments, multiple continuous score intervals may be pre-divided, and then applications with scores in the same score interval are considered as applications in the same application set. In the following embodiments, the description is mainly based on the method of grouping applications with the same scores into one group.

[0192] A2, determine the total memory space that needs to be reclaimed this time.

[0193] In some embodiments, the memory recovery module may obtain the currently occupied system memory size and the memory watermark, and obtain the total memory space size to be recovered according to the currently occupied system memory size and the memory size indicated by the memory watermark.

[0194] In other embodiments, each time the electronic device executes memory recycling, the size of the recycled memory space is a fixed size. In this scenario, the memory recycling module can obtain a preconfigured memory size value as the total memory space size that needs to be recycled.

[0195] In another embodiment, the memory recovery module can obtain the currently occupied system memory size and the target memory size, and determine the total memory space size that needs to be recovered based on the difference between the currently occupied system memory size and the target memory size. Among them, the above-mentioned target memory size can be smaller than the memory size indicated by the memory waterline. The above-mentioned target memory size refers to the size of the occupied system memory retained after each memory recovery, and the target space size can be a fixed value or a variable value. For example, the target memory size can be determined based on the actual available system memory size and a set ratio. In the scenario where the actual available system memory becomes less, the above-mentioned target memory size can also change.

[0196] A3, according to the total memory space size that needs to be reclaimed, determine the memory size that needs to be reclaimed from the system memory occupied by each application set, such as the fourth space size.

[0197] In some embodiments, the fourth space size corresponding to each application set may also be allocated according to the application score corresponding to each application set. The higher the application score corresponding to the application set, the larger the corresponding fourth space size. The lower the application score corresponding to the application set, the lower the corresponding fourth space size.

[0198] In this implementation, differentiated system memory recycling can be implemented for application sets with different application scores, reducing the problem of uneven system memory recycling, that is, more system memory is recycled for application sets with high usage frequency, and less system memory is recycled for application sets with low usage frequency.

[0199] As an implementation manner, the above-mentioned manner of allocating the corresponding fourth space size to each group of application sets may include:

[0200] Get a matching allocation ratio table. Among them, multiple allocation ratio tables can be pre-configured in the electronic device, and the allocation ratio table includes multiple application scores and corresponding allocation ratio values. There are differences in the application scores corresponding to different allocation ratio tables. For example, the number of application scores included in different allocation ratio tables is different, or the value of at least one application score in different allocation ratio tables is different. The sum of multiple allocation ratio values ​​in the same allocation ratio table is 100%. At the same time, in the allocation ratio table, the larger the application score, the higher the corresponding allocation ratio value. The smaller the application score, the lower the corresponding allocation ratio value.

[0201] For example, allocation ratio table 1 includes application score 1, application score 2 and application score 3. In the scenario where application score 1 is greater than application score 2 and application score 2 is greater than application score 3, the allocation ratio value corresponding to application score 1 in allocation ratio table 1 can be 70%, the allocation ratio value corresponding to application score 2 can be 20%, and the allocation ratio value corresponding to application score 3 can be 10%.

[0202] For another example, allocation ratio table 2 includes application score 1 and application score 4. In a scenario where application score 1 is greater than application score 4, the allocation ratio value corresponding to application score 1 in allocation ratio table 2 may be 60%, and the allocation ratio value corresponding to application score 4 may be 40%.

[0203] It can be understood that the pre-configured multiple different allocation ratio tables can cope with the scenario of dividing different application sets. The allocation ratio value in each allocation ratio table can be an empirical value.

[0204] In the multiple allocation ratio tables, the application scores contained in the matching allocation ratio table are the same as the application scores of the actually divided application sets. Thus, in different application set division scenarios, the matching allocation ratio tables determined are different.

[0205] For example, the application sets actually divided include application set 1, application set 2, and application set 3, and the application sets correspond to application score 1. Application set 2 corresponds to application score 2. Application set 3 corresponds to application score 3. In this scenario, the matching allocation ratio table may be allocation list 1, and the application scores (application score 1, application score 2, and application score 3) in allocation list 1 are exactly the same as the application scores of the application sets actually divided (application set 1, application set 2, and application set 3). The application sets actually divided include application set 1 and application set 4, and the application sets correspond to application score 1. Application set 4 corresponds to application score 4. In this scenario, the matching allocation ratio table may be allocation list 2, and the application scores (application score 1, application score 4) in allocation list 2 are exactly the same as the application scores of the application sets actually divided (application set 1, application set 4).

[0206] After determining the matching allocation ratio table, the corresponding allocation ratio value is queried according to the application scores of each divided application set, and then the fourth space size for comparison of the application set is determined according to the product between the allocation ratio value and the total memory space size to be reclaimed.

[0207] In this way, the memory size (e.g., the size of the fourth space) required to be reclaimed for different application sets is affected by the application score. For application sets with a higher probability of use (corresponding to a lower application score), the memory size required to be reclaimed is smaller, and for application sets with a lower probability of use (corresponding to a lower application score), the memory size required to be reclaimed is larger. This avoids the situation where an application with a higher probability of use has too much memory space reclaimed, and an application with a lower probability of use has too little memory space reclaimed.

[0208] As another implementation manner, the above-mentioned method of allocating a corresponding fourth space size to each group of application sets may also include:

[0209] (1) According to the reclaimable memory size of the application programs in each application set, the total reclaimable memory size corresponding to each application set is calculated, such as the fifth space size or the fourth memory amount. For example, the reclaimable memory sizes of all the application programs in the same application set are added together to obtain the fifth space size corresponding to the application set.

[0210] (2) According to the fifth space size corresponding to each application set, the total reclaimable memory size corresponding to the electronic device is calculated, which is called the sixth space size. For example, the fifth space sizes corresponding to all application sets are added together to obtain the sixth space size corresponding to the electronic device.

[0211] (3) Determine the fourth space size corresponding to the application set according to the sixth space size corresponding to the electronic device, the fifth space size of the application set, and the total memory space size that needs to be reclaimed. For example, determine the recycling ratio corresponding to the application set according to the ratio between the fifth space size of the application set and the sixth space size of the electronic device, and then determine the fourth space size corresponding to the application set according to the product of the recycling ratio of the application set and the total memory space size that needs to be reclaimed.

[0212] In this way, for different application sets, the memory size required to be reclaimed (such as the size of the fourth space) is not only affected by the application score, but also by the reclaimable memory size of the application set. Application sets with a higher probability of use (corresponding to a lower application score) require less memory to be reclaimed, and application sets with a lower probability of use (corresponding to a lower application score) require more memory to be reclaimed. It can also avoid the situation where applications with a higher probability of use have too much memory space reclaimed, and applications with a lower probability of use have too little memory space reclaimed.

[0213] A4, determining the to-be-reclaimed memory size corresponding to the application according to the fourth space size corresponding to the application set, the total reclaimable memory size corresponding to the application set, and the reclaimable memory size of the application.

[0214] Among them, the total reclaimable memory size corresponding to the above application set can also be called the fifth space size.

[0215] For example, the ratio 1 (such as the first ratio) between the reclaimable memory size a of application a and the fifth space size corresponding to application set a can be obtained, and then the product between the ratio 1 and the fourth space size (such as the fifth memory amount) corresponding to application set a can be obtained as the to-be-reclaimed memory size corresponding to application a. Of course, the to-be-reclaimed memory sizes corresponding to other applications in application set a can also be determined in the same way.

[0216] It can be understood that the fourth space size of each application set obtained by the above A1 to A3 can be unchanged during the process of traversing the application. After executing A1 to A3, the fourth space size of each application set can be stored. After that, each time any application is traversed, the fourth space size of the corresponding application set can be queried, and then, based on A4, the size of the memory to be reclaimed of the application is calculated.

[0217] In other embodiments, the electronic device may not be divided into application sets. Thus, as another implementation, the process of determining the size of the memory to be reclaimed corresponding to each application program may also be as follows:

[0218] B1. After traversing each application running in the background, obtain the application score of the traversed application. For example, when traversing to application a, obtain application score a.

[0219] B2. Then, the sum of the reclaimable memory sizes of all application programs corresponding to the application score a is counted to obtain the fifth space size (eg, called the first ratio) corresponding to the application score a.

[0220] B3. Then, the sum of the reclaimable memory sizes of all applications in the electronic device is counted to obtain a sixth space size, which is called the total reclaimable memory.

[0221] B4. Determine the total memory size that needs to be reclaimed this time. The method for obtaining the total memory size that needs to be reclaimed this time can refer to the above A2, which will not be repeated here.

[0222] B5. Afterwards, based on the sixth space size, the total memory size that needs to be reclaimed this time and the fifth space size corresponding to the application score a, determine the memory size that needs to be reclaimed from all application programs corresponding to the application score a, that is, it can be called the fourth space size corresponding to the application score a.

[0223] B6. After obtaining the fourth space size corresponding to the application score a, the memory size to be reclaimed of application a is determined according to the fourth space size and the fifth space size of the application score a and the reclaimable memory size a corresponding to application a. Then, the next application is traversed and the above steps are repeated.

[0224] It can be understood that the above-mentioned memory size to be reclaimed refers to the actual size of space that needs to be reclaimed from the application. After calculating the memory size to be reclaimed in the above manner, memory can be reclaimed evenly for the applications in the same application set. Of course, in the same application set, the more reclaimable memory size an application has, the more corresponding memory size to be reclaimed, and the less reclaimable memory size an application has, the less corresponding memory size to be reclaimed.

[0225] In an embodiment of the present application, after determining the size of the memory to be reclaimed corresponding to the application, the electronic device may also reclaim the system memory occupied by the application according to the size of the memory to be reclaimed.

[0226] For example, the recycling process includes: when traversing to the memcg corresponding to application a, determining the size of the memory to be recycled corresponding to application a. Then, selecting n target memory pages from the LRU linked list corresponding to application a, the activity of the selected target memory pages is lower than other anonymous pages of application a.

[0227] In addition, the system memory usage of the n target memory pages is the same as or similar to the memory size to be reclaimed. When the system memory usage of the n target memory pages is similar to the memory size to be reclaimed, the system memory usage of the n target memory pages is not less than the memory size to be reclaimed. It can also be said that the memory size occupied by the n target memory pages matches the memory size to be reclaimed corresponding to application a.

[0228] Exemplarily, n can be a positive integer greater than 0. When n=1, the system memory size occupied by the corresponding target memory page is not less than the to-be-reclaimed memory size corresponding to application a. When n is greater than 1, the total system memory size occupied by the corresponding n target memory pages is not less than the to-be-reclaimed memory size corresponding to application a, and the total system memory size occupied by the corresponding n-1 target memory pages is less than the to-be-reclaimed memory size corresponding to application a. Then, the system memory occupied by the target memory page of application a is reclaimed.

[0229] In some embodiments, during the process of traversing the memcg of the application, if the size of the memory actually reclaimed is equal to or larger than the size of the memory required to be reclaimed, the traversal is stopped.

[0230] In some embodiments, each time a memcg is traversed and the corresponding target memory page is determined and reclaimed, the memory size occupied by the target memory page (such as the seventh space size) can be obtained. Then, the difference between the reclaimable memory size of the application and the seventh space size is obtained, such as the eighth space size. Then, the eighth space size is written into the field for storing the reclaimable size in the memcg data structure.

[0231] In this way, when the application score of the application program does not change, there is no need to repeatedly execute the step of calculating the reclaimable memory size of the application program, thereby shortening the processing time of memory reclaiming.

[0232] Exemplary scenarios such as Figure 8 As shown, APP1, APP2 and APP3 have the same application scores, and in the first list, the transfer storage ratio corresponding to the application scores is 60%. Figure 8In the scenario shown, the anonymous page size of APP1 in the system memory is 40MB, and the anonymous page size that has been transferred is 20MB (that is, the size of the space occupied in the swap partition and the external memory). The anonymous page size of APP2 in the system memory is 60MB, and the anonymous page size that has been transferred is 20MB. The anonymous page size of APP3 in the system memory is 30MB, and the anonymous page size that has been transferred is 20MB. According to the method provided in the above embodiment, it can be determined that the recyclable memory size corresponding to APP1 is 16MB, the recyclable memory size corresponding to APP2 is 28MB, and the recyclable memory size corresponding to APP3 is 10MB. The total recyclable memory size corresponding to the application set to which APP1, APP2 and APP3 belong is 54MB. Of course, the recyclable memory size is determined based on the transfer storage ratio corresponding to the application score, and the maximum space size that can be reclaimed in the system memory occupied by the application is determined. The actual size of the space to be reclaimed of the application is not greater than the recyclable memory size. When determining that 27MB of system memory needs to be reclaimed from the application set consisting of APP1, APP2 and APP3, according to the above embodiment, it can be determined that the size of the space to be reclaimed corresponding to APP1 is (27 / 54)*16=8MB, the size of the space to be reclaimed corresponding to APP2 is (27 / 54)*28=14MB, and the size of the space to be reclaimed corresponding to APP3 is (27 / 54)*10=5MB.

[0233] In this way, after memory recycling, the anonymous page size of APP1 in the system memory is 32MB, and the transferred anonymous page size is 28MB. The anonymous page size of APP2 in the system memory is 46MB, and the transferred anonymous page size is 34MB. The anonymous page size of APP3 in the system memory is 25MB, and the transferred anonymous page size is 25MB.

[0234] In short, among applications with the same application score (or the same application popularity), under different system memory usage (first space size) and different transfer storage sizes (the sum of the second space size and the third space size), the corresponding reclaimable memory amount (or, reclaimable memory size) is different, and the amount of memory to be reclaimed (or, memory size to be reclaimed) is also different.

[0235] Exemplary scenarios such as Fig. 9 As shown, the application scores corresponding to APP1 and APP4 are different, the application score of APP1 is greater than that of APP4, and in the first list, the transfer storage ratio corresponding to the application score of APP1 is 60%, and the transfer storage ratio corresponding to the application score of APP4 is 50%. Fig. 9In the scenario shown, the anonymous page size of APP1 in the system memory is 40MB, and the anonymous page size that has been transferred is 20MB. The anonymous page size of APP4 in the system memory is 40MB, and the anonymous page size that has been transferred is 20MB. According to the method provided in the aforementioned embodiment, it can be determined that the reclaimable memory size corresponding to APP1 is 16MB, and the reclaimable memory size corresponding to APP4 is 10MB. When the occupied system memory and the transferred anonymous page size are the same, due to the difference in application scores, the reclaimable memory sizes corresponding to APP1 and APP4 are different.

[0236] In addition, due to the difference in application scores, there are also differences in the size of the fourth space corresponding to the application sets to which APP1 and APP4 belong. For example, 27MB needs to be recovered from the application set to which APP1 belongs, and the total recyclable memory size corresponding to the application set is 54MB. 20MB needs to be recovered from the application set to which APP4 belongs, and the total recyclable memory size corresponding to the application set is 30MB. In this way, according to the aforementioned embodiment, it can be determined that the size of the space to be recovered corresponding to APP1 is (27 / 54)*16=8MB, and the size of the space to be recovered corresponding to APP4 is (20 / 30)*10=6.6MB. In this way, after memory recovery, the anonymous page size of APP1 in the system memory is 32MB, and the size of the anonymous page that has been transferred to storage is 28MB. The anonymous page size of APP4 in the system memory is 33.4MB, and the size of the anonymous page that has been transferred to storage is 26.6MB.

[0237] Obviously, applications with different application scores can achieve differentiated memory recycling through the method provided in the above embodiment, avoiding the situation where too much system memory is recycled for applications with high application popularity and less system memory is recycled for applications with low application popularity, reducing the number of anonymous pages that are repeatedly released and reloaded, and improving the recycling efficiency of system memory.

[0238] As an implementation method, the algorithm for traversing the application and reclaiming memory can be as follows:

[0239] do{

[0240] score = memcg_score / / Get the application score of the traversed application;

[0241] reclaim_size = level_reclaim_size [score] / / Get the memory size to be reclaimed from the application set corresponding to each application score;

[0242] reclaim_memcg_sz = can_reclaim_memcg / level_can_reclaim[score]*reclaim_size / / indicates to obtain the size of the memory to be reclaimed corresponding to the application;

[0243] nr_reclaimed_memcg = try_to_free_mem_cgroup_pages(memcg, reclaim_memcg_sz) / / indicates to reclaim the system memory occupied by the application according to the size of the memory to be reclaimed corresponding to the application;

[0244] can_reclaim_memcg -= nr_reclaimed_memcg / / Change the reclaimable memory size corresponding to the memcg data structure of the application;

[0245] nr_reclaimed+=nr_reclaimed_memcg / / Count the size of memory that has been reclaimed;

[0246] level_can_reclaim[score]-=nr_reclaimed_memcg / / Determine the remaining reclaimable memory size in the application set;

[0247] if(nr_reclaimed>=nr_to_reclaim) / / where nr_to_reclaim refers to the total system memory size to be reclaimed this time, and nr_reclaimed indicates the current system memory size that has been reclaimed. If nr_reclaimed is not less than nr_to_reclaim, memory reclaim ends. If nr_reclaimed is less than nr_to_reclaim, traverse the next application to reclaim memory;

[0248] break;

[0249] }while(memcg=memcg_next,memcg!=NULL);

[0250] The embodiment of the present application also provides an electronic device, which may include: a memory and one or more processors. The memory is coupled to the processor. The memory is used to store computer program code, and the computer program code includes computer instructions. When the processor executes the computer instructions, the electronic device can execute the various steps executed by the mobile phone in the above embodiment. Of course, the electronic device includes but is not limited to the above memory and one or more processors.

[0251] The embodiment of the present application also provides a chip system, which can be applied to the terminal device in the aforementioned embodiment. The chip system includes at least one processor and at least one interface circuit. The processor can be the processor in the above-mentioned electronic device. The processor and the interface circuit can be interconnected through a line. The processor can receive and execute computer instructions from the memory of the above-mentioned electronic device through the interface circuit. When the computer instructions are executed by the processor, the electronic device can execute the various steps executed by the mobile phone in the above-mentioned embodiment. Of course, the chip system can also include other discrete devices, which are not specifically limited in the embodiment of the present application.

[0252] In some embodiments, through the description of the above implementation methods, technicians in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above functional modules is used as an example. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above. The specific working process of the system, device and unit described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.

[0253] Each functional unit in each embodiment of the present application can be integrated into a processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The above integrated unit can be implemented in the form of hardware or in the form of software functional units.

[0254] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the embodiment of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as flash memory, mobile hard disk, read-only memory, random access memory, disk or optical disk.

[0255] The above is only a specific implementation of the embodiment of the present application, but the protection scope of the embodiment of the present application is not limited thereto, and any changes or replacements within the technical scope disclosed in the embodiment of the present application should be included in the protection scope of the embodiment of the present application. Therefore, the protection scope of the embodiment of the present application should be based on the protection scope of the claims.

Claims

1. A memory recovery method, characterized in that: Applied to an electronic device, each application in the electronic device corresponds to a memory control subsystem memcg, and the method includes: Determine a first memory amount corresponding to a first application running in the background, wherein the first memory amount indicates an amount of memory that needs to be reclaimed from the system memory occupied by the first application, and the first memory amount decreases as the application heat of the first application increases, and the application heat indicates the activity level of the application running in the background; The system memory occupied by the first application is reclaimed according to the first memory amount.

2. The method according to claim 1, characterized in that Before determining the first memory amount corresponding to the first application running in the background, the method further includes: Determine a first reclaimable memory amount corresponding to the first application; the first memory amount is not greater than the first reclaimable memory amount.

3. The method according to claim 2, characterized in that The determining a first reclaimable memory amount corresponding to the first application includes: Determining that an application score of the first application is a first score; the application score of the first application decreases as the application popularity of the first application increases; The first reclaimable memory amount is determined based on the first score, the second memory amount and the third memory amount corresponding to the first application; wherein the second memory amount indicates the size of all anonymous pages in the first application, and the third memory amount indicates the size of anonymous pages that have been swapped out from the system memory in the first application.

4. The method according to claim 3, characterized in that Determining the first reclaimable memory amount according to the first score, the second memory amount corresponding to the first application, and the third memory amount includes: Determine a transfer storage ratio corresponding to the first score; the transfer storage ratio is used to determine a ratio of a size of anonymous pages that can be transferred in a single application program, and the higher the first score, the higher the corresponding transfer storage ratio; Determine the product of the transfer storage ratio and the second memory amount as a first anonymous page size; The first reclaimable memory amount is determined by subtracting the third memory amount from the first anonymous page size.

5. The method according to claim 3, characterized in that: The determining the first memory amount includes: Obtaining a first ratio between a first reclaimable memory amount and a fourth memory amount, wherein the fourth memory amount refers to the sum of reclaimable memory amounts corresponding to all the application programs whose application scores are the first scores and running in the background; The product of the first ratio and a fifth memory amount is determined as the first memory amount, and the fifth memory amount refers to the total amount of memory that needs to be recovered from all application programs whose application scores are the first scores.

6. The method according to claim 4, characterized in that Each of the applications corresponds to a memcg data structure, and the memcg data structure includes a field for carrying the amount of reclaimable memory; after determining the first amount of reclaimable memory, the method further includes: Writing the determined first reclaimable memory amount into the memcg data structure of the corresponding first application; The determining the first memory amount further includes: obtaining a fourth memory amount; The obtaining of the fourth memory amount includes: after the reclaimable memory amount has been written into the memcg data structure of the application running in the background, obtaining the reclaimable memory amount from the memcg data structure of the application running in the background whose application score is the first score; adding all the obtained reclaimable memory amounts to obtain the corresponding fourth memory amount.

7. The method according to claim 6, characterized in that After reclaiming the system memory occupied by the first application according to the first memory amount, the method further includes: The reclaimable memory amount in the memcg data structure of the first application is updated by using a difference between the first reclaimable memory amount of the first application and the first memory amount.

8. The method according to claim 5, characterized in that The determining the first memory amount further comprises: Counting the total amount of reclaimable memory corresponding to all the applications running in the background of the electronic device; Obtain the fifth amount of memory; wherein the proportion of the fifth amount of memory in the total amount of memory that needs to be recycled by the electronic device is equal to the proportion of the fourth amount of memory in the total amount of recyclable memory of the electronic device.

9. The method according to claim 1, characterized in that: The reclaiming the system memory occupied by the first application according to the first memory amount includes: Determine, among anonymous pages of the first application, a target memory page whose system memory occupancy is the first memory amount, wherein the activity level of the target memory page is lower than that of other anonymous pages of the first application; The target memory page is swapped out from the system memory.

10. The method according to claim 1, characterized in that The method further comprises: Determine a sixth memory amount corresponding to a second application running in the background, wherein the sixth memory amount indicates an amount of memory to be reclaimed from the system memory occupied by the second application, and an anonymous page size stored in the system memory and an anonymous page size swapped out from the system memory by the second application are both the same as those of the first application; and when an application heat of the second application is lower than an application heat of the first application, the sixth memory amount is greater than the first memory amount; According to the sixth memory amount, the system memory occupied by the second application is reclaimed, and the sum of the first memory amount and the sixth memory amount is not greater than the total memory amount that needs to be reclaimed by the electronic device.

11. An electronic device, characterized in that: Each application in the electronic device corresponds to a memory control subsystem memcg, and the electronic device includes: a processor and a memory, and the memory is used to store computer instructions. When the processor executes the computer instructions, the electronic device executes the method as described in any one of claims 1-10.

12. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes a computer program or instructions. When the computer program or instructions are executed on a computer, the computer is caused to perform the method according to any one of claims 1 to 10.

Citation Information

Cited By

  • Memory reclaim method and electronic device

    EP4708057A1

  • Memory reclaim method and electronic device

    WO2025030858A1