Memory management method and device
By adding hot file pages to the protection group according to the access frequency of the file page, the problem of low memory management efficiency in the existing technology is solved, the error phenomenon and memory IO oscillation are reduced, and the performance of the device is improved.
Patent Information
- Application Number
- CN202311043225.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-08-16
- Publication Date
- 2025-02-21
AI Technical Summary
The existing technology is difficult to effectively manage memory, resulting in reduced application fluency and affecting user experience.
By obtaining the access frequency of file pages, add hot file pages to protect grouping to avoid misrecycling, and at the same time, allow cold file pages to be recycled in time to alleviate memory pressure.
It reduces the probability of occurrence of the false phenomenon, avoids memory IO oscillation, and improves the performance of the device.
Smart Images

Figure CN119493657A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of terminal technology, and in particular to a memory management method and device. Background Art
[0002] With the increasing popularity of mobile phones and other electronic devices, users are increasingly demanding smooth application performance. Memory is a crucial system resource in electronic devices. Inefficient system memory allocation significantly reduces application performance, causing application lag and negatively impacting the user experience. Therefore, there is an urgent need for a memory management method to improve device performance, such as ensuring smooth application performance. Summary of the Invention
[0003] Provided are a memory management method and device, which can effectively manage memory and improve terminal performance.
[0004] In order to achieve the above purpose, the following technical solutions are adopted:
[0005] In a first aspect, a memory management method is provided. This method can be executed by a terminal or a component capable of implementing terminal functions (such as a terminal chip). Taking a terminal as an example, the method includes: obtaining the access frequency of a first file page; when the first file page is read into memory, adding the first file page to a protection group in the memory based on the access frequency of the first file page, wherein the first file page is a file page in the first file.
[0006] By adding the first file page in the first file to the protection group, on the one hand, the probability of the first file page being mistakenly recycled can be reduced, thereby reducing the probability of the refault phenomenon. On the other hand, by distinguishing the frequency of access to the file pages in the first file, only a part of the file pages (such as the first file page) are added to the protection group, and not adding all the file pages of the first file to the protection group, it is possible to avoid the memory overhead caused by too many file pages in the protection group, and the file pages that are not added to the protection group can be recycled in time, which helps to effectively alleviate memory pressure. It can be seen that in this method, while ensuring the available memory as much as possible, it is possible to reduce the probability of the refault phenomenon, thereby improving the performance of the device.
[0007] In a possible design of the first aspect, obtaining the access frequency of the first file page includes:
[0008] The access frequency of the first file page is determined according to the information that the first file page is read into the memory.
[0009] In a possible design of the first aspect, the information of the first file page read into the memory includes at least one of the following parameters: number of accesses, access interval, and reentry distance value.
[0010] In this method, the terminal can determine the frequency of file page access through multi-dimensional information, thereby improving the accuracy of the judgment.
[0011] In a possible design of the first aspect, adding the first file page to a protection group of the memory according to a frequency of access of the first file page includes:
[0012] If at least one of the following first conditions is met, the first file page is added to the protection group:
[0013] The number of accesses is greater than the 2nd quartile of the number of accesses required to load the file page from disk to memory; the access interval is less than the 2nd quartile of the access interval required to load the file page from disk to memory; the reentry distance value is less than the 2nd quartile of the reentry distance value required to load the file page from disk to memory.
[0014] The binary quartile of the number of file page accesses from disk to memory can be understood as sorting all file page accesses from disk to memory from smallest to largest and taking the binary quartile. Similarly, the binary quartile of the access intervals between file page loads from disk to memory can be understood as sorting all file page access intervals from disk to memory from smallest to largest and taking the binary quartile. The binary quartile of the reentry distance between file pages from disk to memory can be understood as sorting all reentry distances between file pages from disk to memory from smallest to largest and taking the binary quartile.
[0015] Meeting at least one of the above conditions means that the first file page is frequently accessed and is a hot page in the first file. This technical solution can identify hot file pages in a file and add them to the protection group for protection. This can reduce memory IO fluctuations caused by refaults and improve memory usage.
[0016] Alternatively, thresholds may be set separately, and when the relationship between the access interval, the number of accesses, the reentry distance and each threshold satisfies certain conditions, the file page is a frequently used hot page.
[0017] In a possible design of the first aspect, obtaining the access frequency of the first file page includes:
[0018] The access frequency of the first file page is tracked based on the loading path of the first file page from the disk to the memory. In this way, the terminal can accurately determine the access frequency of the first file page based on the loading path of the first file page, so as to add the first file page with high access frequency to the protection group for protection.
[0019] In a possible design of the first aspect, the first file is a file of the first application.
[0020] In this way, the terminal can add the first file page with high usage frequency in the first application to the protection group to avoid the first file page of the first application from being mistakenly recycled, thereby improving the usage performance of the first application.
[0021] In one possible design of the first aspect, the protection group is configured such that when normal memory reclamation is triggered, file pages in the protection group cannot be reclaimed. Normal memory reclamation refers to a scenario where memory reclamation is triggered but memory is not extremely scarce. For example, memory reclamation is triggered manually by a user, or when available memory falls below a watermark low value.
[0022] That is, when memory reclamation is triggered but memory is not extremely tight, the file pages in the protection group cannot be reclaimed. This can avoid memory shock caused by mistakenly reclaiming frequently used file pages.
[0023] In a possible design of the first aspect, the protection group is further configured such that when memory recycling is triggered and memory is extremely tight, the file pages in the protection group can be recycled.
[0024] Extreme memory shortage may include: available memory being lower than a first threshold, and / or the duration of direct memory reclamation exceeding a second threshold.
[0025] That is, when memory recycling is triggered but the memory is not extremely tight, the file pages in the protection group can be recycled, thus alleviating the memory pressure when the memory is extremely tight.
[0026] In a possible design of the first aspect, the method further includes: obtaining a frequency of access to a second file page in the first file; and adding the second file page to an unprotected group based on the frequency of access to the second file page. The protection level of the protected group is higher than the protection level of the unprotected group.
[0027] In this method, the terminal may add the second file page to the non-protected group according to the access frequency of the second file page, so as to reduce the occupation overhead of the protected group.
[0028] In a possible design of the first aspect, the non-protected group is configured such that when normal memory reclamation is triggered, the file pages in the non-protected group can be reclaimed.
[0029] In this way, when normal memory reclamation is triggered, the terminal can reclaim file pages from non-protected groups, thereby ensuring that the reclamation requirements in this reclamation scenario are met as much as possible.
[0030] In a possible design of the first aspect, the second file page satisfies at least one of the following conditions: the number of accesses is less than or equal to a third threshold, the access interval is greater than or equal to a fourth threshold, and the re-entry distance value is greater than or equal to a fifth threshold.
[0031] Optionally, the third threshold value may be the quintile of the number of accesses for loading the above-mentioned file page from disk to memory, the fourth threshold value may be the quintile of the access interval for loading the above-mentioned file page from disk to memory, and the fifth threshold value may be the quintile of the re-entry distance value for loading the above-mentioned file page from disk to memory.
[0032] Meeting at least one of the above conditions means that the second file page is accessed less frequently and is a cold page in the first file. Cold pages are not added to the protection group, so that when memory is reclaimed, cold pages can be reclaimed in time, alleviating memory pressure.
[0033] In a possible design of the first aspect, the method further includes: when memory reclamation is triggered, reclaiming file pages from the non-protected group.
[0034] That is to say, when normal memory recycling is triggered and the memory is not extremely tight, the terminal does not recycle hot file pages from the protection group, but recycles cold file pages from the non-protection group. This can avoid memory shock caused by mistakenly recycling hot pages, while at the same time reclaiming cold pages as much as possible to alleviate memory pressure.
[0035] In a possible design of the first aspect, the method further includes:
[0036] When memory recycling is triggered and the total amount of file pages in the memory is less than N times the total amount of file pages in the protection group, file pages are recycled from the protection group and the non-protection group, where N is an integer less than or equal to 3.
[0037] That is, when memory reclaim is triggered and memory is extremely tight, the terminal can reclaim file pages from protected groups and non-protected groups to alleviate memory pressure to a greater extent.
[0038] N is only an example of a threshold value. In other embodiments, the value of N may be configured in combination with the performance of the terminal without limitation.
[0039] In a possible design of the first aspect, the total amount of file pages recovered from the protection group is the same as the total amount of file pages recovered from the non-protection group.
[0040] Reclaiming file pages from the non-protection group list and the protection group list at the same recycling ratio can ensure that the ratio of the number of file pages protected in the protection group list to the total amount of file page cache in memory is consistent, which helps to efficiently manage file pages in memory.
[0041] In a possible design of the first aspect, adding the first file page to a protection group of the memory according to a frequency of access of the first file page includes:
[0042] According to the frequency of access to the first file page, when the protection group is not full, the first file page is added to the protection group.
[0043] That is, the total amount of file pages in the protection group has an upper limit. When the protection group is not full, the terminal can continue to add hot pages to the protection group. When the protection group is full, the terminal can only add hot pages to the non-protection group.
[0044] In a possible design of the first aspect, the number of the first document pages is one or more;
[0045] Adding the first file page to a protection group of the memory according to a frequency of access of the first file page includes:
[0046] Before starting the first application, the first file page is added to the protection group according to the frequency with which the first file page is accessed.
[0047] In this method, before starting the first application, the hot pages (first file pages) of the first application are added to the protection group in advance to prevent these hot pages from being mistakenly recycled, thereby improving the performance of the first application.
[0048] In a possible design of the first aspect, the method further includes:
[0049] Switching the first application to background operation;
[0050] When memory recycling is triggered and the total amount of file pages in the memory is higher than N times the total amount of file pages in the protection group, file pages are recycled from the non-protection group.
[0051] For example, the first application is a high-frequency application. After the high-frequency application moves to the background, if memory recycling is triggered, the hot pages of the high-frequency application are not released as much as possible, but the file pages are recycled from the non-protected group to ensure the performance of the high-frequency application.
[0052] In a possible design of the first aspect, the method further includes:
[0053] Switching the first application to background operation;
[0054] When memory recycling is triggered and the total amount of file pages in the memory is less than N times the total amount of file pages in the protection group, file pages are recycled from the protection group and the non-protection group.
[0055] After these high-frequency applications are moved to the background, if the terminal triggers memory reclamation and memory is extremely low (for example, if the user has many applications open, resulting in a low total file page cache in memory), the terminal can quickly reclaim a batch of file pages corresponding to the background applications in the protected group, as well as file pages in the unprotected group. This allows for the timely reclamation of a large number of file pages. Furthermore, when reclaiming multiple file pages corresponding to the background application in the protected group, it avoids scanning each file page individually, reducing reclamation overhead.
[0056] In a possible design of the first aspect, the available memory is lower than the first threshold, including: the total amount of file page cache in the memory is lower than the seventh threshold, and / or the total amount of free file pages in the memory is lower than the sixth threshold.
[0057] In a second aspect, a terminal device is provided, comprising: a memory for storing computer program instructions; and a processor for executing the instructions, so that the terminal device implements the method described in any possible implementation of the first aspect.
[0058] According to a third aspect, a computer-readable storage medium is provided, on which computer-readable instructions are stored. When the computer-readable instructions are executed by a processor, the method according to any possible implementation of the first aspect is implemented.
[0059] In a fourth aspect, a chip system is provided, comprising a processor and a memory, wherein the memory stores instructions; when the instructions are executed by the processor, the method according to any possible implementation of the first aspect is implemented. The chip system may be composed of a chip alone, or may include a chip and other discrete components.
[0060] In a fifth aspect, a computer program product is provided, comprising computer-readable instructions, which, when executed on a computer, implement the method in any possible implementation of the first aspect. BRIEF DESCRIPTION OF THE DRAWINGS
[0061] Figure 1A Sample images of the document page and anonymous page provided for this manual;
[0062] Figure 1B An example diagram of the watermark and corresponding memory management mechanism provided for this manual;
[0063] Figure 1C An example diagram of the recycling method of the document pages and anonymous pages provided in this manual;
[0064] Figure 2 An example diagram of the hardware structure of the device provided in this manual;
[0065] Figure 3 An example diagram of the software architecture of the device provided in this manual;
[0066] Figure 4 Example diagrams of hot and cold pages included in the documentation provided for this manual;
[0067] Figure 5 This is a scene example diagram of the memory management method provided in this manual;
[0068] Figure 6 This is an example flow chart of the memory management method provided in this manual;
[0069] Figure 7 This is an example diagram of the file reading and writing method provided in this manual;
[0070] Figure 8 This is an example flow chart of the memory management method provided in this manual;
[0071] Figure 9 This is a statistical example of the number of reentry times provided in this manual;
[0072] Figure 10 This is a scene example diagram of the memory management method provided in this manual;
[0073] Figure 11 This is a scene example diagram of the memory management method provided in this manual;
[0074] Figure 12A An example diagram of the protection grouping provided for this specification;
[0075] Figure 12B An example diagram of the protection grouping provided for this specification;
[0076] Figure 13 This is a scene example diagram of the memory management method provided in this manual;
[0077] Figure 14 This is an example flow chart of the memory management method provided in this manual;
[0078] Figure 15 This is an example flow chart of the memory management method provided in this manual;
[0079] Figure 16 A schematic diagram of the structure of a terminal device provided in this manual;
[0080] Figure 17 This is a schematic diagram of the structure of a chip system provided in this specification. DETAILED DESCRIPTION
[0081] In the description of this specification, unless otherwise specified, “ / ” means or, for example, A / B can mean A or B; “and / or” in this article is only a way to describe the association relationship of associated objects, indicating that there can be three relationships, for example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone.
[0082] Hereinafter, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of the technical features being referred to. Thus, a feature specified as "first" or "second" may explicitly or implicitly include one or more of such features. Throughout this specification, unless otherwise specified, "plurality" means two or more.
[0083] In this specification, words such as "exemplary" or "for example" are used to indicate examples, illustrations, or descriptions. Any embodiment or design described in this specification as "exemplary" or "for example" should not be construed as preferred or advantageous over other embodiments or designs. Rather, the use of words such as "exemplary" or "for example" is intended to present the relevant concepts in a concrete manner.
[0084] First, some terms involved in this manual are introduced:
[0085] 1. Memory paging mechanism
[0086] Currently, memory management can be implemented based on memory units. Memory units include, but are not limited to, memory pages. In some solutions, memory can be divided into memory pages of a certain size (e.g., 4KB). This memory paging mechanism can improve memory access efficiency.
[0087] It should be noted that, as technology evolves, memory units can also be implemented in other ways, not limited to memory pages. It should be understood that this specification uses memory pages as an example of memory units, but this does not constitute a limitation on memory units.
[0088] 2. Anonymous Pages and File Pages
[0089] like Figure 1A ,The whole memory of an electronic device can include kernel memory, file-backed pages, anonymous pages, free memory, and other memory. Different applications can use different memory to support the running of the application. Figure 1A, application 1 uses a file page and an anonymous page, and application 2 uses a file page and an anonymous page. It should be noted that the file page used by application 1 and application 2 can be the same file page, in which case the file page can be called a shared page. Alternatively, the file page used by application 1 and application 2 can be different file pages. Similarly, the anonymous pages used by application 1 and application 2 can be the same or different anonymous pages.
[0090] A file page is a memory page (referred to as a memory page or page) with a source backing page in an external storage space (such as a disk). The file page has a mapping relationship with the source backing page in the external storage space. As a possible implementation, a program can read a file from disk through basic operations such as read / mmap. The system can then request pages to store the content read from the disk. These pages used to store disk file content can be considered file pages. For example, file pages include the code segment of a process.
[0091] Anonymous pages are memory pages that don't have corresponding files in external storage. For example, pages used by a process's heap or stack can be anonymous pages. Another example is pages requested by a process. External storage space can refer to storage space other than memory.
[0092] In some schemes, the kernel can manage file pages and anonymous pages through the least recently used (LRU) linked list. According to the active state of the linked list, the linked list can be divided into two levels: active and inactive linked lists. Figure 1A As shown, the active linked list includes: an active anonymous page linked list for managing anonymous pages, and an active file page linked list for managing file pages, and the inactive linked list includes: an inactive anonymous page linked list for managing anonymous pages, and an inactive file page linked list for managing file pages.
[0093] Multiple memory pages can be stored in a linked list. For example, the active file page linked list can store multiple active file pages, and the inactive file page linked list can store multiple inactive file pages. The active anonymous page linked list can store multiple active anonymous pages, and the inactive anonymous page linked list can store multiple inactive anonymous pages. Active memory pages can be memory pages that a process frequently uses, while inactive memory pages can be memory pages that a process rarely uses.
[0094] 3. Control group (Cgroup) technology
[0095] This technology can group the memory pages used by the process, and perform fine-grained control over memory allocation, sorting, and management, thereby improving memory management efficiency. In this technology, memory recycling management is performed by the recycling thread (kswapd). When memory is insufficient, the kswapd thread traverses all memory page groups to determine the memory pages that need to be recycled. For example, the kswapd thread scans Figure 1A The active linked list and inactive linked list shown are used to determine the memory pages that need to be reclaimed.
[0096] like Figure 1B As shown, watermarks can include a high watermark (i.e., the upper threshold), a low watermark (i.e., the lower threshold), and a minimum watermark (i.e., the min watermark). As applications request memory, the amount of free memory decreases. After kswapd is awakened and determines that the free memory has dropped below the low watermark, it begins memory reclamation and stops when the free memory reaches the high watermark. In addition, if it is determined that the free memory has dropped below the min watermark, the system executes a direct reclamation process to quickly obtain free memory.
[0097] 4. Recycling methods for anonymous pages
[0098] For anonymous pages, during the memory recovery process, for some anonymous pages currently used, such as Figure 1C , electronic devices can compress it into compression space (also called swap space, or swap partition, such as zram). This memory recycling method can be called page swapping.
[0099] For anonymous pages that are currently unused (such as free pages), the electronic device can discard them to the free space of the buddy system. Later, when these free pages are needed again, they can be retrieved from the buddy system. This memory recycling method is called page discarding.
[0100] 5. File page recycling method
[0101] During memory reclamation, the electronic device selects infrequently used file pages. If the content of this file page matches the content of the corresponding file on disk, it is considered a clean file page. Therefore, the electronic device does not need to write the file page back to disk. Instead, it can directly treat the file page as a free page and discard it to the free space of the partner system. This memory reclamation method is called page discarding.
[0102] On the contrary, if the data stored in the file page is inconsistent with the data stored in the corresponding file on the disk, it means that the file page is a dirty page. Figure 1C, the electronic device can write the contents of the file page back to the corresponding file on disk. Afterwards, the file page is released to the partner system as a free page. This memory reclamation method is called page writeback.
[0103] The process of transferring a file page from memory to external storage (disk) is called swapping out. The process of transferring a file page from external storage (disk) to memory is called swapping in.
[0104] As can be seen, when an application references a file page, it reads the page into memory. When memory becomes scarce, the page is cleared from memory and needs to be read again the next time it is used. This can cause memory oscillation or a ping-pong effect, adding additional load to the system. For example, a file page is frequently used and may be used by both foreground and background processes. When memory becomes scarce, the system reclaims the page from the background process. When the background process switches to the foreground, the foreground process must read the page back, resulting in heavy disk read and write activity and potentially causing lag.
[0105] The phenomenon in which the memory page is immediately used again after being recycled, resulting in secondary page loading, is called the refault phenomenon.
[0106] To reduce the probability of refaults, the industry has proposed a memory lock solution. This technology locks frequently used files into memory. For example, during the boot phase, frequently used files are locked into memory by calling mmap(). Subsequently, frequently used files are not allowed to be reclaimed; for example, locked files are marked as non-reclaimable by the kernel. This prevents frequent files from being repeatedly swapped in and out, reducing the likelihood of a ping-pong effect. While this approach can reduce the likelihood of a ping-pong effect to a certain extent, as the number and types of applications increase, the number of frequently used files also increases. Locking a large number of frequently used files into memory can easily waste resources and reduce available memory outside of the locked memory, impacting terminal performance. For example, the total size of frequently used files in a system is approximately 1.2GB. Mobile phones have a total memory size of 1GB to 16GB. Using 1.2GB of memory to lock 1.2GB of frequently used files results in too many files being locked into memory, reducing available memory. Consequently, when memory is limited, memory reclaim may fail, resulting in an inability to request memory. Furthermore, the low memory killer (LMK) detection threshold will be raised, leading to a more severe system attack against background malware. Simply put, under the current solution, locking 100MB of memory will result in a 100MB reduction in system memory. The more files locked, the greater the risk of memory recovery failure, which in turn degrades terminal performance.
[0107] In order to solve the above technical problems, this specification provides a memory management method. Considering that each file has cold pages and hot pages, not all file pages of high-frequency files are hot pages. In most cases, the cold pages of files are more than the hot pages. Otherwise, it would be more reasonable to directly sink the file code to the kernel resident, and there is no need to dynamically load the file. In this specification, by adding the hot pages in the first file to the protection group and not adding the cold pages to the protection group, it is possible to avoid memory shock caused by mistakenly recovering the hot pages in the protection group, while at the same time recovering cold pages as much as possible to relieve memory pressure.
[0108] In this specification, an application using (referencing) a memory page may also be referred to as an application calling a memory page or an application accessing a memory page.
[0109] The memory management method of this specification can be applied to electronic devices. For example, the electronic devices may be mobile phones, tablet computers, personal computers (PCs), netbooks, and other devices requiring memory optimization. This specification does not place any particular restrictions on the specific form of the electronic devices.
[0110] Figure 2 An exemplary structure of an electronic device is shown, and the electronic device includes at least one processor 201 and a memory 202. Optionally, the electronic device may further include at least one transceiver 203.
[0111] The processor 201 may be a general-purpose central processing unit (CPU), a microprocessor, an application-specific integrated circuit (ASIC), or one or more integrated circuits for controlling the execution of the program of the present disclosure.
[0112] The above components may include communication lines for transmitting information between the above components.
[0113] Transceiver 203 is used to communicate with other devices. In this specification, a transceiver may be a module, circuit, bus, interface, or other device capable of implementing communication functions, used to communicate with other devices. Alternatively, the transceiver may be a standalone transmitter that can be used to send information to other devices, or a standalone receiver that can be used to receive information from other devices. Alternatively, the transceiver may be a component that integrates the functions of sending and receiving information. This specification does not limit the specific implementation of the transceiver.
[0114] The memory 202 may be a read-only memory (ROM) or other type of static storage device that can store static information and instructions, a random access memory (RAM) or other type of dynamic storage device that can store information and instructions, or an electrically erasable programmable read-only memory (EEPROM), a compact disc read-only memory (CD-ROM) or other optical disc storage, an optical disc storage (including a compact disc, laser disc, optical disc, digital versatile disc, Blu-ray disc, etc.), a magnetic disk storage medium or other magnetic storage device, or any other medium that can be used to carry or store desired program code in the form of instructions or data structures and can be accessed by a computer, but is not limited thereto. The memory may exist independently and be connected to the processor via a communication line. The memory may also be integrated with the processor.
[0115] The memory 202 is used to store computer-executable instructions for implementing the solutions of this specification, and the execution is controlled by the processor 201. The processor 201 is used to execute the computer-executable instructions stored in the memory 202, thereby implementing the various methods provided in the following embodiments of this specification.
[0116] Optionally, the computer-executable instructions in this specification may also be referred to as application code, instructions, computer program or other names, which are not specifically limited in this specification.
[0117] In a specific implementation, as an embodiment, the processor 201 may include one or more CPUs.
[0118] In a specific implementation, as an embodiment, an electronic device may include multiple processors. Each of these processors may be a single-core (single-CPU) processor or a multi-core (multi-CPU) processor. The processor here may refer to one or more devices, circuits, and / or processing cores for processing data (e.g., computer program instructions).
[0119] The electronic device mentioned above can be any device with data processing function, for example, it can be a computer device with data processing function. Figure 2 It should be understood that the electronic device shown is only an example, and in actual application, the electronic device may have more Figure 2 More or fewer components may be shown, two or more components may be combined, or the illustrations may have different configurations of components.
[0120] Optionally, the software system of the electronic device may adopt a layered architecture, an event-driven architecture, a micro-kernel architecture, a micro-service architecture, or a cloud architecture. The embodiment of the present invention takes a layered architecture system as an example to exemplify the software structure of the electronic device.
[0121] Figure 3 It is a software structure block diagram of the electronic device according to an embodiment of the present invention.
[0122] A layered architecture divides software into several layers, each with distinct roles and responsibilities. Layers communicate with each other via software interfaces. In some embodiments, the system is divided into four layers: application layer, application framework layer, runtime and system libraries, and kernel layer.
[0123] The application layer can include a series of application packages.
[0124] like Figure 3 As shown, the application package may include applications such as camera, gallery, calendar, call, map, navigation, WLAN, Bluetooth, music, video, short message, etc.
[0125] The application framework layer provides an application programming interface (API) and programming framework for the applications in the application layer. The application framework layer includes some predefined functions.
[0126] like Figure 3 As shown, the application framework layer may include a window manager, a content provider, a view system, a phone manager, a resource manager, a notification manager, and the like.
[0127] In this specification, the framework layer includes a configuration module. The configuration module may also be referred to as an enhanced LRU configuration module, or may have other names. In some embodiments, the server may send information about the identified hot files (also referred to as high-frequency files) to the electronic device, and the electronic device may store the information about the hot files in a configuration file. When the electronic device is turned on, the hot file information may be read from the configuration file through the configuration module of the framework layer, and the hot file information may be passed to the kernel layer. The kernel layer may subsequently lock the hot pages in the hot file into memory, thereby protecting the locked hot pages and reducing the probability of the hot pages being mistakenly recycled.
[0128] like Figure 4, a file may include hot pages and cold pages. Among them, hot pages may refer to memory pages with high frequency of use in a file. Cold pages may refer to memory pages with low frequency of use in a file. This specification, by locking the hot pages in the hot file to the memory, on the one hand, can reduce the probability of the hot pages being mistakenly recycled, thereby reducing the probability of the refault phenomenon. On the other hand, by distinguishing between hot and cold memory pages in the hot file, only locking the hot pages and not locking the cold pages, it is possible to avoid the memory overhead caused by locking too many memory pages, and the unlocked cold pages can be recycled in time, which helps to effectively alleviate the memory pressure. It can be seen that in this method, while ensuring the available memory as much as possible, it is possible to reduce the probability of the refault phenomenon, thereby improving the performance of the device.
[0129] Locking a hot page to memory can be achieved by adding the hot page to a protection group. Optionally, the terminal can mark the hot page as a protected page. As a possible implementation method, the terminal sets a protected field for each file page, which can be recorded as Is protect. The field value of this field is used to indicate whether the file page is protected. If a file page is determined to be a hot page, the terminal can set the field value of the protected field corresponding to the hot page to protected. Subsequently, when memory recycling is triggered, if the protected field of a file page indicates that the file page is protected, the terminal skips the file page and does not recycle the file page.
[0130] The runtime consists of a core library and a virtual machine. The runtime is responsible for system scheduling and management. The core library consists of two parts: one for the Java language's callable functions and the other for the core library. The application layer and application framework layer run in the virtual machine. The virtual machine executes the Java files in the application layer and application framework layer as binary files. The virtual machine performs functions such as object lifecycle management, stack management, thread management, security and exception management, and garbage collection.
[0131] The system library can include multiple functional modules, such as surface manager, media library, 3D graphics processing library (such as OpenGL ES), 2D graphics engine (such as SGL), etc.
[0132] The kernel layer is the layer between hardware and software. The kernel layer includes at least display driver, camera driver, audio driver, and sensor driver.
[0133] In this specification, the kernel layer may further include a memory management module. The memory management module may be used to obtain hot file information from the framework layer (such as the configuration module) and lock hot pages in the hot file into memory.
[0134] The memory management module can also be called the enhanced LRU module.
[0135] In some embodiments, the memory management module of the kernel layer can also be used to mark hot pages as protected pages.
[0136] In some embodiments, the kernel-layer memory management module can also be used to clear the protected page flag on a memory page. For example, when the available memory on the entire machine is extremely low, a hot page can be reclaimed. After the hot page is reclaimed, the protected page flag on the memory page is cleared.
[0137] It should be noted that the structure of electronic equipment can also refer to Figure 3 The electronic device may have a structure as shown. Figure 3 The structures shown may have more or fewer components, or some components may be combined or separated, or different component arrangements may be used. The components shown may be implemented in hardware, software, or a combination of software and hardware.
[0138] In the related art, when memory needs to be reclaimed, the terminal can choose to reclaim the file page in the memory page, or it can choose to reclaim the anonymous page. This specification provides a memory management method, and the terminal can manage the file page according to this method. For example, Figure 5 , the device can read the information of the hot file and read the hot file into the memory. Take the hot file including file 1 and file 2 as an example, Figure 5 At time t1, the file pages read into memory from file 1 include file pages H1, H2, H3, and C1 (marked with slashes from upper left to lower right); the pages read into memory from file 2 include file pages H1, C1, H2, and C2. Later, when the device's memory is low, the cold page recycling can be triggered, such as Figure 5 At time t2, file pages H1, H2, and C1 of file 1 are recycled, and file pages C1, H2, and C2 of file 2 are recycled.
[0139] Later, due to the use requirements of the process, some file pages may be loaded into memory again. Figure 5 At time t3, the three file pages H1, H2 and C1 of file 1 are loaded into the memory again (marked with slashes from the lower left to the upper right), and the file page C1 of file 2 is loaded into the memory again.
[0140] As each file page is used by a process, it may be swapped into memory. When the process no longer uses the file page, or the device's memory is low, the device may swap out some of the file pages in memory. In this specification, the device may lock the frequently swapped file pages into memory to reduce the IO overhead caused by the frequent swapping of file pages. Figure 5At time t4, assuming that the swap-in and swap-out frequencies of the three file pages H1, H2, and C1 of file 1 and the file page C1 of file 2 are high, the device can use these four file pages as hot pages and lock these four hot pages (marked with black filling) into the memory.
[0141] In this way, by locking the hot pages in the hot file that are frequently swapped in and out to the memory, on the one hand, the hot pages can no longer be swapped out under normal circumstances, reducing the overhead caused by frequent swapping of hot pages. On the other hand, the cold pages in the hot file that are less frequently swapped in and out are not locked to the memory, which can effectively reduce the memory occupancy. In addition, the cold pages that are not locked to the memory can be recycled in time, which helps to alleviate the memory pressure of the device. In addition, since the frequency of swapping in and out of cold pages is low, the impact of fewer swap-out recycling on the device overhead is relatively small. It can be seen that the solution of this specification can ensure the balance between hot page protection and system performance as much as possible, and comprehensively improve the performance of the device.
[0142] The technical solution provided in this specification is described in detail as follows.
[0143] First, the identification method of hot files is introduced. Figure 6 An example of a method for identifying hot files is shown.
[0144] S101. The server identifies and installs high-frequency applications based on authorized user data.
[0145] As a possible implementation method, high-frequency applications frequently used by different users can be identified based on authorized user big data. High-frequency applications can be applications that are used frequently by users. For example, the server obtains authorized data from users of an authorized beta system, such as information about applications that users frequently use, to obtain application-level data samples. Different applications can use different hot files, and hot files can refer to files that are used frequently. In this specification, hot files can also be referred to as high-frequency files.
[0146] For example, shopping and short video apps frequently access image files, so these files can be considered hot files for these apps. Instant messaging apps access more text processing-related files, so these files can be considered hot files for these apps.
[0147] Examples of high-frequency applications are shown in Table 1 below:
[0148] Table 1
[0149]
[0150]
[0151] For example, assuming that based on the authorization data of 100 users, the 99th percentile can refer to the authorization data of the 99th user. As shown in Table 1, the number of applications used by the 99th user in a single day is 38, and the number of application opens in a single day is 1044. Based on the authorization data of multiple users, the server can identify high-frequency applications frequently used by different users.
[0152] After the server identifies a high-frequency application that is frequently used by the user, it installs the high-frequency application so as to collect statistics on the file usage of the high-frequency application.
[0153] S102: The server constructs a user feature script based on the authorized user data.
[0154] As a possible implementation method, the server clusters the user's authorized data to obtain a user feature script. The server runs the user feature script to simulate different users' operations on high-frequency applications.
[0155] Optionally, the clustering method includes but is not limited to k-means clustering.
[0156] S103: The server runs a user feature script to obtain usage of each file by high-frequency applications.
[0157] The server runs user-specific scripts to simulate different user operations on high-frequency applications, such as launching high-frequency applications and performing swipe and click operations on high-frequency applications. During these operations, the server records file usage by high-frequency applications. This file usage includes, but is not limited to, the frequency and number of times a file is used.
[0158] S104: The server determines hot files from among the files according to usage of the files by high-frequency applications.
[0159] Optionally, hot files are files that are frequently used by different users, or are used frequently. For example, files whose usage frequency exceeds a certain threshold can be designated as hot files. For another example, a preset number of files ranked first in terms of usage frequency can be designated as hot files. This specification does not limit the specific method for determining hot files or the number of hot files.
[0160] Hot files for different users are similar. For example, the most frequently used hot files are shown in Table 2. As shown in Table 2, the most frequently used hot files for different users using the terminal include files such as / system / framework / a.jar and / system / framework / b.jar. For example, the files shown in Table 2 may be Java-based. In other embodiments, files in other languages may also serve as hot files. This specification does not limit the language type of hot files.
[0161] Table 2
[0162] Hot Files / system / framework / a.jar / system / framework / b.jar / system / lib64 / c.so …
[0163] The above is only an example method for obtaining high-frequency applications and hot files. In other embodiments, high-frequency applications and hot files can be obtained through other methods, which are not limited in this specification.
[0164] After the server determines the hot file, it can send the hot file information to the terminal. Subsequently, the server can update the hot file information based on the new authorized user data and push the new hot file information to the terminal.
[0165] In this method, the server can determine the hot files based on the authorized user big data. The terminal does not need to calculate and determine the hot files, but can directly identify the hot pages in the hot files based on the hot files determined by the server, thereby improving the efficiency of the terminal in identifying hot pages.
[0166] After receiving the hot file information from the server, the terminal can lock and protect the hot pages in the hot file according to the hot file information to reduce the probability of hot pages being recycled. Figure 8 , shows an example of a method flow for locking and protecting a hot page, which may include the following steps:
[0167] S201. The terminal reads the file page into memory.
[0168] Exemplarily, the terminal calls the add_to_page_cache() function to read the file page into the memory.
[0169] S202: The terminal determines whether the protection group linked list in the memory is full. If the protection group linked list is full, the terminal executes the following step S206; if the protection group linked list is not full, the terminal executes the following step S203.
[0170] In this specification, building upon the existing linked lists in related art, a new protection group (referred to as prot memcg) is added. A protection group can include one or more linked lists, and the linked lists within a protection group are referred to as protection group linked lists. This allows hot pages to be removed from existing linked lists and added to the protection group. File pages within the protection group will not be reclaimed under normal circumstances (e.g., when memory reclaim is triggered but memory is not extremely low).
[0171] Optionally, the size of the protection group linked list is 20% of the hot file size. For example, if the total size of the hot files in the terminal is 1000MB, the size of the protection group linked list can be 200MB. Alternatively, the size of the protection group linked list can be flexibly configured, such as 10%-40%. This specification does not limit the specific configuration method of the protection group linked list size. In this specification, the protection group linked list size can also be referred to as the protection zone size.
[0172] As a possible implementation method, the memory recycling management policy of the protection group linked list can be configured. The memory recycling management policy of the protection group linked list can be: when the total amount of file pages cached in the memory is greater than or equal to the first lock loss threshold, the kswapd thread does not recycle the file pages in the protection group linked list. Alternatively, the memory recycling management policy can be: when the free file pages other than the protection group linked list in the memory are greater than or equal to the second lock loss threshold, the kswapd thread does not recycle the file pages in the protection group linked list. Alternatively, the memory recycling management policy can be: when the duration of direct memory recycling does not exceed a certain threshold, the kswapd thread does not recycle the file pages in the protection group linked list. Optionally, the threshold can be an application not responding (ANR) threshold, for example, when the duration of direct memory recycling does not exceed 8 seconds, the kswapd thread does not recycle the file pages in the protection group linked list.
[0173] The total amount of file pages cached in memory (the total amount of file page cache) is the sum of the number of protected file pages (referred to as locked files) in the protection group list and the number of free unprotected file pages (referred to as free file pages).
[0174] Optionally, the first lock loss threshold can be set to 1 to 3 times the total amount of file pages in the protection group. For example, it can be set to 2 times. For example, if the total size of the existing file pages in the protection group is 300M, the first lock loss threshold can be set to 600M. Correspondingly, the memory recycling management policy is: if the total amount of file pages cached in the memory is greater than or equal to 600M, the kswapd thread will not recycle the file pages in the protection group linked list. The first lock loss threshold can be flexibly configured according to the scenario and terminal usage requirements, and this manual does not impose any restrictions.
[0175] Optionally, the second lock loss threshold can be set to the size of the protection group linked list. For example, if the size of the protection group linked list is 300M, the second lock loss threshold can be set to 300M. Correspondingly, the memory recycling management strategy is: if the number of free file pages in the memory other than the protection group linked list is greater than or equal to 300M, it means that there is a lot of reclaimable memory, then the kswapd thread will not recycle the file pages in the protection group linked list. The second lock loss threshold can be flexibly configured according to the scenario and terminal usage requirements, and this manual does not impose any restrictions.
[0176] Optionally, the memory recycling management policy can be configured in the above-mentioned configuration file. Alternatively, the memory recycling management policy can be configured in an independent configuration file. This specification does not limit the configuration method of the policy. The memory recycling management policy can also be other policies, such as not recycling file pages in the protection group linked list when the total amount of file pages cached in the memory is greater than or equal to the amount of file pages protected in the protection group.
[0177] Based on the newly added protection group linked list, the terminal can add file pages that need to be locked and protected to the protection group linked list. Subsequently, when the terminal needs to reclaim some file pages, if the total amount of file pages cached in the memory is large (such as greater than the second lock loss threshold), when the kswapd thread scans each group linked list, when the protection group linked list is identified, the kswapd thread can directly skip the protection group linked list according to the memory recovery management policy of the protection group linked list. The kswapd thread no longer scans and no longer reclaims file pages in the protection group linked list, but reclaims file pages from the non-protection group linked list. On the one hand, it can reduce the overhead of scanning resources and avoid the file pages in the protection group linked list from being mistakenly recovered, thereby reducing the IO overhead caused by the refault of the file. On the other hand, when the available memory is low, the terminal can still reclaim file pages from the non-protection group linked list to relieve memory pressure.
[0178] Available memory refers to the sum of free anonymous pages in memory and the total file page cache. The total file page cache is the sum of locked file pages in the protection group list and unlocked file pages in memory.
[0179] In this step, when the file page is read into the memory, the terminal determines whether the protection group linked list is full. It is understood that if the protection group linked list is full, it means that no new file page can be added to the protection group linked list. In this case, the terminal can execute S206 to add the file page to the non-protection group linked list (such as the linked list already known in the related art). Conversely, if the protection group linked list is not full, the terminal executes S203 and other related steps to determine whether the file page needs to be added to the protection group linked list.
[0180] For example, if the size of the protection group linked list is 200M and the number of file pages currently in the protection group linked list is 100M, then the protection group linked list is not full. The terminal can add the hot page of the hot file to the protection group linked list. For another example, if the size of the protection group linked list is 200M and the number of file pages currently in the protection group linked list is 200M, then the protection group linked list is full. In this case, the terminal adds the hot page of the hot file to the non-protection group linked list.
[0181] It should be noted that this specification does not limit the specific method of triggering memory recycling. For example, memory recycling can be triggered when the free memory is low (such as below the watermark low value). For another example, in some cases, when the system memory is high, the user can use some commands to trigger the terminal to reclaim memory.
[0182] S203: The terminal determines whether the file page is a hot file. If it is a hot file, the terminal executes the following step S204; if it is not a hot file, the terminal executes the following step S206.
[0183] When a file page is read into memory, the terminal determines whether the file page belongs to a frequently used hot file. If the file page is not a file page in a hot file, it means that with a high probability, the file page is used less frequently. In this case, the terminal does not need to lock the file page to reduce the memory overhead occupied by the file page. On the contrary, if the file page is a file page in a hot file, it means that with a high probability, the file page is used more frequently. In this case, the terminal needs to execute S204 and other steps to determine whether the file page needs to be added to the protection group linked list, and then lock the file page for protection.
[0184] S204: The terminal determines whether the reentry distance value of the file page is less than the cold / hot threshold.
[0185] As a possible implementation, Figure 7 , considering that the add_to_page_cache() function based on C language is called every time a file page is read into memory (adding a page to the kernel buffer (memory)), and the delete_from_page_cache() function based on C language is called every time a file page is cleared from memory (deleting a page from the kernel buffer), the server can track the number of times the file page is used based on the situation in which the above two functions are called by the file page. For example, each time the add_to_page_cache() function is called, the terminal can increase the number of times the file page is swapped in by one, and each time the delete_from_page_cache() function is called, the terminal can increase the number of times the file page is swapped out by one, thereby counting the number of times the file page is used.
[0186] It should be noted that the add_to_page_cache() and delete_from_page_cache() functions are merely examples of the path for loading file pages from external storage (e.g., disk) into memory. The file page loading path can also be other functions with similar functionality. Functions in other languages that perform similar functions are also included in this solution.
[0187] The terminal can determine the refault distance value of the file page based on the swapping-in and swapping-out status of the file page. The refault distance value of the file page can represent the interval between the file page being swapped out and being swapped in again.
[0188] For example, a smaller refault distance indicates a shorter interval between a file page being swapped out and being swapped in again, and the file page is swapped in again quickly after being swapped out, indicating that the file page is hotter. Conversely, a larger refault distance indicates a longer interval between a file page being swapped out and being swapped in again, and the file page is swapped in again after a long time after being swapped out, indicating that the file page is colder.
[0189] In this specification, after determining the refault distance of a file page, the terminal may store the refault distance. Subsequently, the terminal may determine whether the file page is a hot page or a cold page based on the refault distance of the file page. As one possible implementation, the terminal determines whether the file page is a hot page based on the refault distance of the file page and a hot / cold threshold.
[0190] In some cases, if the refault distance of a file page is less than the hot / cold threshold, it means that the file page is usually swapped in again soon after being swapped out, and the probability of the file page being loaded into the memory again is high. In this case, the terminal will treat the file page as a hot page and execute the following step S205 to add the file page to the protection group linked list for lock protection. In other cases, if the refault distance of a file page is greater than or equal to the hot / cold threshold, it means that the file page is usually swapped in again a long time after being swapped out, and the probability of the file page being loaded into the memory again is low. In this case, the terminal will treat the file page as a cold page and execute the following step S206 to add the file page to the non-protection group linked list (such as the linked list already known in the related art).
[0191] As a possible implementation method, the server can use the above-mentioned user feature script to simulate the user's operation on the application, and count the refaultdistane values of the file pages belonging to hot files used by the application during the operation of the application, determine the hot and cold thresholds based on the refaultdistane values of each file, and send the hot and cold thresholds to the terminal.
[0192] For example, Figure 9 The server counts the refault distance values of each file page swapped in and out, and sorts these values from smallest to largest, generating a distribution graph with the horizontal axis representing the number of swaps in and out, and the vertical axis representing the refault distance value of each swap. Based on simulated user behavior, the service can use the 2-quantile value of these refault distance values as the initial values for the hot and cold thresholds, and then distribute these thresholds to the terminal.
[0193] The server can then update the hot and cold thresholds and send them to the terminal. For example, the server can dynamically adjust the hot and cold thresholds based on authorized user data and user habits, taking the 2nd quantile of the refault distance values collected over a 24-hour period.
[0194] This allows the terminal to separate hot and cold pages at the page level based on the hot and cold thresholds, thereby protecting hot pages and preventing them from being mistakenly reclaimed by the kswapd thread, which could cause memory oscillation. This also improves the accuracy and efficiency of memory reclamation.
[0195] The above only lists one method for determining the hot and cold thresholds. Other methods can also be used to determine the hot and cold thresholds, such as the terminal determining the hot and cold thresholds independently. This specification does not limit this.
[0196] S205: The terminal adds the file page to the protection group linked list.
[0197] For example, Figure 10 (a), when the terminal wants to load file page A into the memory, it obtains the refault distane value of file page A. If the refault distane value is less than the hot and cold thresholds, it means that file page A is frequently used. In this case, the terminal can treat file page A as a hot page and add file page A to the protection group linked list to lock and protect file page A to prevent file page A from being accidentally recycled.
[0198] Subsequently, the terminal can manage the file pages in the protection group linked list. As a possible implementation method, the terminal obtains the usage frequency of the file page at the end of the protection group linked list and determines whether to migrate the file page out of the protection group linked list based on the usage frequency.
[0199] like Figure 10(b), if the file page B at the end of the protection group list is frequently used, such as the refault distance value is less than the hot and cold threshold, it means that the file page B at the end is frequently accessed, then the file page B will be moved to the head of the protection group list to prevent the file page B from being removed in a short time. On the contrary, if Figure 10 (c), if the usage frequency of the tail file page C is low, such as refault distance is greater than the cold and hot threshold, it means that the file page C is not frequently accessed, then the tail file page C is migrated to the non-protection group list to avoid the cold page occupying the overhead of the protection group list.
[0200] It is understood that the migration of other file pages in the protection group linked list can refer to the migration process of the tail file page described above. For example, for other file pages in the protection group linked list that are frequently used, the file page is migrated to the head of the protection group linked list. For other file pages in the protection group linked list that are less frequently used, the file page is migrated to the non-protection group linked list.
[0201] In this way, hot and cold file pages are separated at the page level based on the frequency of file page usage, and hot pages are added or migrated to the protection group list for lock protection. For file pages in the protection group list, only file pages with low usage frequency are migrated to the non-protection group list (such as the inactive list), while file pages with high usage frequency remain in the protection group list. Since file pages with high usage frequency are more likely to be accessed repeatedly, not migrating these file pages to the non-protection group list can avoid or reduce the probability of page refaults.
[0202] S206: The terminal adds the file page to the non-protected group linked list.
[0203] For example, Figure 11 (a), the terminal reads file page E from the external storage space (such as a disk). The file page E has been used less frequently in history, so the terminal treats the file page E as a cold page and adds the file page E to the non-protected group linked list.
[0204] Subsequently, the terminal can manage the file pages in the non-protected group list. For example, taking the management of the file page at the end of the non-protected group list as an example, Figure 11 In (b), the terminal obtains the usage frequency of file page D at the tail of the non-protection group list (such as the active list or the inactive list) during its life cycle. If the usage frequency is high, such as refaultdistane is less than the cold / hot threshold, the file page D is migrated to the head of the protection group list.
[0205] The above solution determines the memory access history by obtaining the usage frequency of a file page. This access history can then be used to predict the importance of that file page. Generally speaking, file pages with fewer historical accesses and larger intervals between accesses are often unimportant. Therefore, consider adding or migrating these unimportant file pages to a non-protected group list. This allows the terminal to promptly reclaim unimportant file pages (also known as cold pages) from the non-protected group list when memory is insufficient, alleviating memory pressure.
[0206] Optionally, there may be one or more non-protected group lists in this specification. There may also be one or more protected group lists. Figure 12A , there are two non-protected group linked lists and two protected group linked lists.
[0207] Optionally, if there are multiple protection group lists, the granularity of different protection group lists can be the same or different. For example, Figure 12B In [1], the protection group linked list includes a file-level linked list and an application-level linked list. Different types of protection group linked lists can have different memory recycling management strategies.
[0208] As a possible implementation, using the method of this specification, the terminal can lock the hot pages in the hot file into the file-granular protection group linked list 1. For example, when memory needs to be reclaimed, if the total amount of file page cache in the memory is lower than the first lock loss threshold, the terminal can reclaim the cold pages at the end of the protection group linked list 1. The reclaimed multiple cold pages can belong to different files.
[0209] Optionally, the file-granularity protection group linked list 1 can be used to lock system file pages, such as framework layer file pages. Of course, the protection group linked list 1 can also be used to lock other file pages.
[0210] As a possible implementation method, through the method of this specification, after multiple iterations, the terminal can separate the hot pages in the high-frequency application hot file and lock the hot pages with high frequency of use of high-frequency applications to such as Figure 12B The protection group linked list of the application granularity is shown. When the high-frequency application is moved to the background, the hot pages of the high-frequency application are not released as much as possible to ensure the performance of the high-frequency application. When memory recycling is triggered and the total amount of file pages in the memory is higher than N times the total amount of file pages in the protection group, the file pages are recycled from the non-protected group.
[0211] However, when the memory is extremely tight, for example, the user opens many applications, making the total amount of file page cache in the memory lower than the first lock loss threshold, the terminal can quickly recycle a batch of file pages corresponding to the above-mentioned background applications in the protection group linked list 2, avoiding scanning the file pages one by one, and reducing the recovery overhead.
[0212] For example, the terminal predicts that the user is likely to use payment applications A and B between 10:00 and 12:00. In this case, before starting payment applications A and B, the mobile phone pre-loads the hot pages of payment applications A and B, and locks the hot pages to the application-granular protection group linked list 2 to avoid the hot pages of payment applications A and B from being mistakenly recycled, thereby improving the performance of payment applications A and B. After that, the probability of users using payment applications A and B becomes smaller, and payment applications A and B are marked as unprotected. The next time memory recycling is triggered, when the total amount of file pages in the memory is less than N times the total amount of file pages in the protection group, the terminal can quickly recycle the pages corresponding to the unprotected payment applications A and B, as well as other cold file pages from the non-protection group, to avoid file pages of non-high-frequency applications occupying the protection group linked list.
[0213] This method allows the terminal to accurately identify the foreground application or thread and lock the hot pages frequently used by the foreground application or thread into a protection group list, thereby improving the performance of the foreground application. Furthermore, the terminal can reduce or prohibit the preloading of file pages by threads unrelated to the foreground (such as background threads) to avoid memory waste.
[0214] The above mainly takes the example of determining the usage frequency of a file page based on the reentry distance value to judge whether the file page is a hot page. In other embodiments, the usage frequency of a file page can also be determined based on other parameters, such as the number of historical accesses to the file page and the access interval to determine the usage frequency of the file page to judge whether the file page is a hot page.
[0215] Exemplarily, the access interval may refer to the number of times between two adjacent accesses to the same page. For example, each time a process is detected accessing a file page, the terminal increases the total number of accesses by one. ① When a process is detected accessing virtual address A', the total number of accesses is n; ② When a process is detected accessing virtual address B', the total number of accesses is increased by one, which is n+1... ③ When a process is detected accessing virtual address N, the total number of accesses is increased by one, which is m-1; ④ When a process is detected accessing virtual address A' again, the total number of accesses is increased by one, which is m... The terminal may use the number of times mn between two adjacent accesses to the same page of virtual address A' as the access interval corresponding to the page.
[0216] Alternatively, the access interval can refer to the time interval between two adjacent accesses to the same page. For example, each time a process is detected accessing a file page, the terminal records the timestamp of this access. Specifically, ① when a process is detected accessing virtual address A', the timestamp t1 of this access is recorded; ② when a process is detected accessing virtual address B', the timestamp t2 of this access is recorded... ③ when a process is detected accessing virtual address N, the timestamp t3 of this access is recorded; ④ when a process is detected accessing virtual address A', the timestamp t4 of this access is recorded... The terminal can use the interval mn between two adjacent accesses to the same page at virtual address A' as the access interval corresponding to the page.
[0217] The smaller the access interval of a file page, the more frequently the file page is used, or the more active it is. A terminal may designate a file page that meets at least one of the following conditions as a hot page. The at least one condition includes: the number of accesses is greater than threshold A, the access interval is less than threshold B, and the reentry distance is less than threshold C. This specification does not limit the parameters used to determine a hot page, nor does it limit the method for determining a hot page.
[0218] In some embodiments, the access interval may also refer to the time interval between two times a file page is read into the memory.
[0219] In some embodiments, the number of accesses may also refer to the number of times a file page is read into the memory, for example, the number of times a file page is read into the memory within a period of time.
[0220] This specification also provides a memory management method, when the terminal's available memory is lower than a certain threshold, the file pages locked in the protection group linked list can be released to relieve the terminal's memory pressure. Figure 13 At time t4, the terminal locks the file pages H1, H2, C1 of file 1 and the file page C1 of file 2 into the protection group linked list.
[0221] Later, when the terminal's available memory is low, for example, when the total amount of file page cache in memory falls below the first lock loss threshold, indicating extreme memory shortage, the terminal can reclaim file pages from the protection group linked list. As a possible implementation, when memory is extremely low, the kswapd thread can quickly scan the protection group linked list to temporarily reclaim protected file pages, alleviating system memory pressure.
[0222] For example, Figure 13At time t5, the total amount of file page cache in the terminal memory is less than twice the total amount of file pages in the protection group, which means that the system memory is very tight. In this case, temporary recovery of protected file pages is allowed. The terminal can recover file pages H2 and C1 of file 1 locked in the protection group linked list, file page H3 of file 1 stored in the non-protection group linked list, and file page H1, H2, and C2 of file 2 stored in the non-protection group linked list to relieve the terminal's memory pressure.
[0223] As a possible implementation method, when the total amount of file page cache in the terminal memory is lower than the first unlock threshold, the terminal will give priority to reclaiming the file pages in the non-protected group list. The terminal can determine the file pages to be reclaimed from the protected group list based on the total file pages to be reclaimed and the file pages to be reclaimed from the non-protected group list. Figure 13 ,Assume that a total of 6 file pages need to be recovered, the terminal can prioritize recovering 1 file page (H3) from the non-protection group linked list of file 1, and recovering 3 file pages (H1, H2, C2) from the non-protection group linked list of file 2. If it is determined that the file pages recovered from the non-protection group linked list are not enough, the terminal can scan the protection group linked list, such as Figure 13 After scanning the protection group linked list, the remaining two file pages that need to be reclaimed out of the six file pages can be reclaimed from the protection group linked list, such as file pages H2 and C1 of file 1. In this method, the scanning of the protection group linked list is initiated only when insufficient file pages are reclaimed from the non-protection group linked list. That is, the protection group linked list is not scanned in all cases, which can reduce the probability of scanning the protection group linked list. Furthermore, since some file pages have already been reclaimed from the non-protection group linked list before scanning the protection group linked list, the number of file pages reclaimed from the protection group linked list can be reduced, thereby minimizing memory oscillation caused by reclaiming high-frequency file pages in the protection group linked list.
[0224] As another possible implementation method, another scenario example of the terminal reclaiming file pages is that when the total amount of file page cache in the terminal memory is lower than the first unlock threshold, the terminal can scan the non-protected group linked list and the protected group linked list, and reclaim the required file pages from the protected group linked list and the non-protected group linked list. Optionally, the ratio of file pages recovered from the non-protected group linked list to file pages recovered from the protected group linked list is 1:1. The file pages protected in the protected group linked list are 150M, and the total amount of file page cache in the memory is 300M. If memory recycling is triggered and 100M of file pages need to be recycled, 50M will be recycled from both the non-protected group linked list and the protected group linked list. Then, there are still 100M file pages in the protected group linked list, and the total amount of file page cache in the memory becomes 200M.
[0225] It can be seen that by recycling file pages from the non-protection group linked list and the protection group linked list according to the same recycling ratio, the ratio of the number of file pages protected in the protection group linked list to the total amount of file page cache in the memory can be kept consistent, which helps to efficiently manage the file pages in the memory.
[0226] Optionally, a protection group may also contain unprotected multiple file pages in its protected fields. For example, if the usage frequency of some file pages decreases with user usage or due to other reasons, as a possible implementation, when reclaiming memory in extremely low memory conditions, these file pages in the protection group are prioritized for reclaiming.
[0227] Compared to related techniques that lock and protect a large number of file pages, resulting in wasted memory, the method in this specification takes into account that as applications increase, terminals may often experience extremely low memory. When the total amount of terminal memory file page cache falls below the first lock loss threshold, the memory in the protection group linked list is treated as reclaimable memory, which can alleviate memory pressure. Therefore, it usually does not lead to excessive killing of background processes in LMK. It can be seen that this method can achieve a balance between file page protection and system performance.
[0228] As a possible implementation, when the total file page cache in the terminal memory is lower than the first lock loss threshold, the terminal only reclaims file pages from the protection group linked list. For example, if 10 file pages need to be reclaimed, the terminal reclaims 10 file pages from the protection group linked list.
[0229] Optionally, after a file page locked to the protection group linked list is released, the terminal clears the protected flag of the file page, which can be understood as deleting the protected field of the file page or setting the protected field to "unprotected".
[0230] During the application's execution, some frequently used file pages are loaded into memory again. Figure 13 , file pages H2 and H3 of file 1, and file pages H1 and H2 of file 2 are reloaded into memory for use by the application. As the application uses each file page, the terminal obtains the usage frequency of each file (such as the reentry distance value) and locks the file pages with high usage frequency into the protection group linked list.
[0231] Table 3 shows the comparison of the performance of the terminal using the technical solution of this specification and the related technology.
[0232] Table 3
[0233]
[0234] As shown in Table 3, compared to related technologies, this specification achieves a 98.46% probability of application startup delays of less than 1 second, significantly reducing startup delays. Furthermore, the number of frame drops is reduced to approximately 44,104. This demonstrates that the technical solutions in this specification significantly improve terminal performance.
[0235] The above mainly takes the example of the server pushing the hot file information to the terminal. In other embodiments, the terminal can also identify the hot files in the terminal by itself, and this specification does not limit this. Figure 14 , shows an example of a method for calculating hot files by a terminal. The method may include:
[0236] S301: The terminal collects statistics on the usage of each file in the terminal.
[0237] It should be understood that different users may have different usage habits, and the terminal can determine the file usage that is consistent with the usage habits of the terminal user. For example, when user A uses the terminal, the file usage determined by the terminal may be different from that determined by user B using the terminal.
[0238] For the specific implementation method of the terminal counting the usage of each file (such as the usage frequency), please refer to the relevant introduction of the server counting the usage of each file, which will not be repeated here.
[0239] S302: The terminal determines hot files from among the files according to usage of the files.
[0240] Hot files are files that are frequently used by users of this terminal.
[0241] For example, user A prefers to use entertainment applications, and user B prefers to use office applications. Then, the hot files determined by the terminal when user A uses the terminal may be different from the hot files determined by the terminal when user B uses the terminal.
[0242] In this method, the terminal determines the hot files in the terminal by itself in combination with the usage habits of the terminal user, so that the determined hot files can better meet the user's usage requirements and be more accurate.
[0243] The aforementioned thresholds, such as the first unlock threshold representing the degree of memory shortage, are merely examples, and one or more of these thresholds may be configured separately based on the terminal's usage requirements. The specific numerical values of the thresholds are not limited herein. When a terminal has different values for corresponding parameters in a corresponding scenario, some of which satisfy the results described in the corresponding scenario, it can be understood that when a threshold set in the terminal's computer instructions satisfies a threshold condition and the corresponding steps are executed, the terminal is deemed to fall within the scope of protection of this technical solution.
[0244] Figure 15Another example of a process of the method of this specification is shown. The method includes:
[0245] S401: The terminal obtains the access frequency of a first file page.
[0246] The frequency with which the first file page is accessed may also be referred to as usage frequency.
[0247] S402: When a first file page is read into the memory, the terminal adds the first file page to a protection group in the memory according to the access frequency of the first file page, wherein the first file page is a file page in the first file.
[0248] As a possible implementation manner, when the usage frequency of the first file page is high, the terminal determines that the first file page is a hot page, and then adds the hot page to a protection group for protection.
[0249] It should be noted that the above-mentioned multiple embodiments can be combined and the combined scheme can be implemented. Optionally, some operations in the process of each method embodiment are optionally combined, and / or the order of some operations is optionally changed. In addition, the execution order between the steps of each process is only exemplary and does not constitute a limitation on the execution order between the steps. There can also be other execution orders between the steps. It is not intended to indicate that the execution order is the only order in which these operations can be performed. Ordinary technicians in this field will think of many ways to reorder the operations in this article. In addition, it should be pointed out that the process details involved in a certain embodiment of this article are also applicable to other embodiments in a similar manner, or different embodiments can be used in combination.
[0250] Furthermore, some steps in the method embodiments may be equivalently replaced with other possible steps. Alternatively, some steps in the method embodiments may be optional and may be deleted in certain usage scenarios. Alternatively, other possible steps may be added to the method embodiments.
[0251] Furthermore, the above method embodiments may be implemented separately or in combination.
[0252] This specification may be based on background technology and related technologies and used in combination with these technologies, but is not limited thereto. This specification may also be based on other background technology and related technologies.
[0253] It is understood that the devices in this specification include hardware structures and / or software modules that perform the corresponding functions in order to implement the above functions. In combination with the units and algorithm steps of each example described in the embodiments disclosed in this application, this specification can be implemented in the form of hardware or a combination of hardware and computer software. Whether a function is executed in the form of hardware or computer software driving hardware depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered to exceed the scope of the technical solution of this specification.
[0254] This specification allows for the division of terminal devices into functional units based on the above-described method examples. For example, individual functional units may be divided according to their respective functions, or two or more functions may be integrated into a single processing unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units. It should be noted that the division of units in this specification is illustrative and represents only a logical functional division. In actual implementation, other division methods may be used.
[0255] This application also provides a device, which can be the above-mentioned terminal or corresponding component. The device may include: a memory and one or more processors. The memory and the processor are coupled. 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 device can perform the various functions or steps performed by the terminal or network device in the above-mentioned method embodiment. The structure of the device can refer to Figure 2 The structure of the device shown.
[0256] The core structure of the device (also called an apparatus) can be expressed as Figure 16 In the structure shown, the device includes: a processing module 1301 and a storage module 1303.
[0257] Processing module 1301 may include at least one of a central processing unit (CPU), an application processor (AP), or a communication processor (CP). Processing module 1301 may perform operations or data processing related to control and / or communication with at least one of the other components of the user terminal device.
[0258] The storage module 1303 may include a volatile memory and / or a non-volatile memory. The storage module is used to store at least one related instruction or data of other modules of the device.
[0259] Optionally, a communication module 1305 is also included to support the device to communicate with other devices (via a communication network). For example, the communication module can be connected to a network via wireless communication or wired communication to communicate with other devices. Wireless communication can adopt at least one of cellular communication protocols, such as Long Term Evolution (LTE), Advanced Long Term Evolution (LTE-A), Code Division Multiple Access (CDMA), Wideband Code Division Multiple Access (WCDMA), Universal Mobile Telecommunications System (UMTS), Wireless Broadband (WiBro) or Global System for Mobile Communications (GSM). Wireless communication may include, for example, short-range communication. Short-range communication may include at least one of Wireless Fidelity (Wi-Fi), Bluetooth, Near Field Communication (NFC), Magnetic Stripe Transmission (MST) or GNSS.
[0260] This specification also provides a chip system, such as Figure 17 As shown, the chip system includes at least one processor 1401 and at least one interface circuit 1402. The processor 1401 and the interface circuit 1402 can be interconnected via lines. For example, the interface circuit 1402 can be used to receive signals from other devices (such as the memory of the terminal device). For another example, the interface circuit 1402 can be used to send signals to other devices (such as the processor 1401). Exemplarily, the interface circuit 1402 can read the instructions stored in the memory and send the instructions to the processor 1401. When the instruction is executed by the processor 1401, the terminal device can execute the various steps in the above embodiments. Of course, the chip system can also include other discrete components, which is not specifically limited in this specification.
[0261] This specification also provides a computer storage medium, which includes computer instructions. When the computer instructions are executed on the above-mentioned terminal device, the terminal device executes each function or step executed by the mobile phone in the above-mentioned method embodiment.
[0262] This specification also provides a computer program product. When the computer program product is run on a computer, it enables the computer to execute each function or step executed by the mobile phone in the above method embodiment.
[0263] Through the description of the above implementation methods, technical personnel in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional modules is used as an example. In actual applications, the above-mentioned functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above.
[0264] In the several embodiments provided in this application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of modules or units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another device, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0265] Units described as separate components may or may not be physically separate, and components shown as units may be one physical unit or multiple physical units, that is, they may be located in one place or distributed in multiple places. Some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0266] In addition, the functional units in the various embodiments of the present application may be integrated into a single processing unit, or each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.
[0267] 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 readable storage medium. Based on this understanding, the technical solution of this specification, 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, which is stored in a storage medium and includes several instructions for enabling a device (which can be a single-chip microcomputer, chip, etc.) or a processor to execute all or part of the steps of the various embodiments of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0268] The above content is only a specific embodiment of this application, but the scope of protection of this application is not limited to this. Any changes or replacements within the technical scope disclosed in this application should be included in the scope of protection of this application. Therefore, the scope of protection of this application should be based on the scope of protection of the claims.
Claims
1. A memory management method, characterized in that: Applied to a terminal, the method comprises: Get the access frequency of the first file page; When the first file page is read into the memory, the first file page is added to the protection group of the memory according to the frequency of access of the first file page, wherein the first file page is a file page in the first file.
2. The method according to claim 1, characterized in that The obtaining of the access frequency of the first file page includes: The access frequency of the first file page is determined according to the information that the first file page is read into the memory.
3. The method according to claim 1 or 2, characterized in that: The obtaining the access frequency of the first file page includes: The access frequency of the first file page is tracked according to a loading path of the first file page from the disk to the memory.
4. The method according to any one of claims 1 to 3, characterized in that The first file is a file of the first application.
5. The method according to claim 2, characterized in that: The information about the first file page being read into the memory includes at least one of the following parameters: access times, access interval, and reentry distance value.
6. The method according to any one of claims 1 to 5, characterized in that The method further comprises: Obtaining a frequency at which a second file page in the first file is accessed; According to the frequency of access to the second file page, the second file page is added to a non-protected group; the protection level of the protected group is higher than the protection level of the non-protected group.
7. The method according to any one of claims 1 to 6, characterized in that The method further comprises: When memory reclamation is triggered, file pages are reclaimed from the non-protection group.
8. The method according to any one of claims 1 to 6, characterized in that The method further comprises: When memory recycling is triggered and the total amount of file pages in the memory is less than N times the total amount of file pages in the protection group, file pages are recycled from the protection group and the non-protection group, where N is an integer less than or equal to 3.
9. The method according to claim 8, characterized in that The total amount of file pages recovered from the protection group is the same as the total amount of file pages recovered from the non-protection group.
10. The method according to any one of claims 1 to 9, characterized in that: According to the frequency of access of the first file page, adding the first file page to the protection group of the memory includes: According to the frequency of access to the first file page, when the protection group is not full, the first file page is added to the protection group.
11. The method according to any one of claims 1 to 10, characterized in that: The first document page is one or more; According to the frequency of access of the first file page, adding the first file page to the protection group of the memory includes: Before starting the first application, the first file page is added to the protection group according to the frequency with which the first file page is accessed.
12. The method according to claim 11, characterized in that The method further comprises: Switching the first application to background operation; When memory recycling is triggered and the total amount of file pages in the memory is higher than N times the total amount of file pages in the protection group, file pages are recycled from the non-protection group.
13. The method according to claim 11, characterized in that The method further comprises: Switching the first application to background operation; When memory recycling is triggered and the total amount of file pages in the memory is less than N times the total amount of file pages in the protection group, file pages are recycled from the protection group and the non-protection group.
14. An electronic device, characterized in that: The electronic device comprises: a memory for storing computer program instructions; A processor is used to execute the instruction so that the electronic device executes the method according to any one of claims 1 to 13.
15. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-readable instructions, and when the computer-readable instructions are executed by a processing circuit, the method according to any one of claims 1 to 13 is implemented.
Citation Information
Cited By
Hot page threshold adjustment method and device, medium, product and memory system
CN120371535A
Storage space scrubbing optimization method, system and device, chip, network interface card, equipment, medium and program product
CN121255114A
Memory optimization method, electronic device and computer program product
CN121301038A