Memory management method and device, electronic equipment and storage medium
By grouping and managing memory pages, the lowest priority memory pages are swapped out to the target storage area according to page priority, which solves the problem of inaccurate memory management in the existing technology and improves the accuracy and flexibility of memory management.
Patent Information
- Application Number
- CN202410303287.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-03-15
- Publication Date
- 2025-09-16
AI Technical Summary
The memory management in the prior art has poor flexibility and cannot accurately manage the swapping of memory pages, resulting in unnecessary replacement of frequently accessed or important memory pages.
By grouping memory pages, memory pages in the memory group corresponding to the lowest page priority are swapped out to the target storage area according to page priority, preventing memory pages with higher page priorities from being mistakenly swapped out.
Improves the accuracy and flexibility of memory management, avoids the accidental swapping out of important memory pages, and improves system stability and performance.
Smart Images

Figure CN120653415A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of electronic devices, and more specifically, to a memory management method, device, electronic device, and storage medium. Background Art
[0002] With the rapid advancement of science and technology and living standards, electronic devices (such as smartphones and tablets) have become one of the most commonly used electronic products in people's lives. In electronic devices, memory is one of the important components, and it serves as a bridge for communication with the central processing unit (CPU). Since the capacity of memory is limited, it is usually necessary to manage the memory. In related technologies, memory swapping mechanisms are usually used to manage memory to increase available memory. However, the flexibility of memory swapping management in related technologies is poor, and it is impossible to accurately manage memory swapping of memory pages. Summary of the Invention
[0003] This application proposes a memory management method, device, electronic device and storage medium, which can improve the accuracy of memory management.
[0004] In a first aspect, an embodiment of the present application provides a memory management method, the method comprising: grouping memory pages in the memory according to the page priority corresponding to each memory page in the memory, obtaining memory groups corresponding to different page priorities, and the page priorities corresponding to memory pages in the same memory group are matched; if it is currently necessary to swap out memory pages in the memory, at least part of the memory pages in the memory group corresponding to the lowest page priority are swapped out to the target storage area.
[0005] In the second aspect, an embodiment of the present application provides a memory management device, which includes: a memory grouping module and a memory swapping module, wherein the memory grouping module is used to group memory pages in the memory according to the page priority corresponding to each memory page in the memory, and obtain memory groups corresponding to different page priorities, and the page priorities corresponding to memory pages in the same memory group are matched; the memory swapping module is used to swap out at least part of the memory pages in the memory group corresponding to the lowest page priority to the target storage area if the memory pages in the memory currently need to be swapped out.
[0006] In a third aspect, an embodiment of the present application provides an electronic device comprising: one or more processors; a memory; and one or more applications, wherein the one or more applications are stored in the memory and configured to be executed by the one or more processors, and the one or more applications are configured to execute the memory management method provided in the first aspect above.
[0007] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, in which program code is stored. The program code can be called by a processor to execute the memory management method provided in the first aspect above.
[0008] The solution provided by the present application is to group the memory pages in the memory according to the page priority corresponding to each memory page in the memory, thereby obtaining memory groups corresponding to different page priorities, and the page priorities corresponding to the memory pages in the same memory group are matched; if the memory pages in the memory currently need to be swapped out, at least some of the memory pages in the memory group corresponding to the lowest page priority are swapped out to the target storage area. Thus, by grouping the memory pages, when performing memory swapping, memory pages are selected from the memory group corresponding to the lowest page priority for swapping out, thereby preventing memory pages with higher page priorities from being swapped out by mistake, thereby improving the accuracy of memory management. BRIEF DESCRIPTION OF THE DRAWINGS
[0009] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present application. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.
[0010] Figure 1 A flow chart of a memory management method according to an embodiment of the present application is shown.
[0011] Figure 2 A flow chart of a memory management method according to another embodiment of the present application is shown.
[0012] Figure 3 A flowchart of a memory management method according to another embodiment of the present application is shown.
[0013] Figure 4 A flowchart of a memory management method according to another embodiment of the present application is shown.
[0014] Figure 5 A block diagram of a memory management device according to an embodiment of the present application is shown.
[0015] Figure 6 It is a block diagram of an electronic device for executing the memory management method according to an embodiment of the present application.
[0016] Figure 7 It is a storage unit of an embodiment of the present application for storing or carrying program codes for implementing the memory management method according to an embodiment of the present application. DETAILED DESCRIPTION
[0017] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application.
[0018] RAM (also known as random access memory) refers to the memory used when running programs (i.e., running memory). It can only temporarily store data and is used to exchange cache data with the processor, but it cannot be used for long-term data storage. When an electronic device is running, the CPU will transfer the data to the memory for calculation. After the calculation is complete, the CPU will transmit the result. The operation of the memory also determines the stable operation of the electronic device. Because the physical memory of an electronic device is limited and the memory usage of a process is uncertain, there is always the possibility that the physical memory will run out. To deal with the shortage of memory resources, electronic devices often manage memory through memory swapping.
[0019] Among them, in Android or other Linux-based systems, memory swap is a traditional memory management strategy that allows the operating system to "swap out" (Swap out) data in memory to the hard disk, compress it to other memory areas, flash memory and other storage media to free up memory space for other processes. When the "swapped out" data is needed again, the operating system will "swap it back" (Swap in) from the above storage media to the memory. However, if the above storage medium is a physical disk, due to the access rate limit of the physical disk (current mainstream disk read and write rates are usually at the level of hundreds of megabytes / second), the entire process is very time-consuming. During this process, users may feel that the system is abnormally slow.
[0020] The following is an introduction to the technical terms involved in memory swapping in related technologies.
[0021] Swap Space: A special hard disk partition used by the system as swap storage space. The size of the swap space is generally set by the system administrator during system installation or configuration, and is usually determined based on the system's RAM size and application requirements.
[0022] Swapiness: This is a system parameter that controls the system's reliance on swap space. Swapiness values range from 0 to 100. A higher value indicates a greater tendency for the system to swap data out to swap space, while a lower value indicates a greater tendency for the system to retain data in memory as much as possible.
[0023] Swap In and Swap Out: When the system memory is insufficient, the operating system triggers a Swap Out, swapping selected memory pages to the Swap Space. When a swapped-out page is needed again, the system triggers a Swap In, swapping the page from the Swap Space back into the main memory.
[0024] Usually, when performing Swap Out, that is, memory page interaction, electronic devices can use different memory replacement strategies. In related technologies, memory page management can usually be implemented based on the Second Chance Algorithm, which is also called the clock page replacement algorithm. It is an improved FIFO (first-in-first-out) page replacement algorithm. The page replacement algorithm determines whether a page needs to be replaced each time a page replacement is required. It considers the access bit of the page. If the bit is set (that is, the access bit is 1), then the page will get a "second chance", that is, it will not be replaced temporarily, and the access bit will be cleared. In the next round of inspection, if the access bit of the page is still 0, then it will be replaced. In such a replacement strategy, the access bit of a memory page may be set to 0 in the previous round of inspection, and the memory page has not been accessed before the next round of inspection. Then, since its access bit is 0, the memory page may be replaced. However, the memory page may be a memory page with a high access frequency, or a more important memory page, which just happened not to be accessed within a round of inspection. In this way, the frequently accessed pages or the more important memory pages will be replaced unnecessarily, so there will be a problem of inaccurate replaced memory pages.
[0025] To address the above issues, the inventors have proposed the memory management method, device, electronic device, and storage medium provided in the embodiments of this application. By grouping memory pages, when swapping out memory, memory pages are selected from the memory group corresponding to the lowest page priority. This can prevent memory pages with higher page priorities from being mistakenly swapped out, thereby improving the accuracy of memory management. The specific memory management method is described in detail in the subsequent embodiments.
[0026] See also Figure 1 , Figure 1 FIG. 1 is a flow chart showing a memory management method provided by an embodiment of the present application. In a specific embodiment, the memory management method is applied to Figure 5 The memory management device 400 and the electronic device 100 ( Figure 6). The following will take electronic devices as an example to illustrate the specific process of this embodiment. Of course, it can be understood that the electronic devices used in this embodiment can be smart phones, tablet computers, smart watches, e-books, etc., which are not limited here. Figure 1 The process shown in FIG. 1 is described in detail. Specifically, the memory management method may include the following steps:
[0027] Step S110: grouping memory pages in the memory according to the page priority corresponding to each memory page in the memory to obtain memory groups corresponding to different page priorities. The page priorities corresponding to memory pages in the same memory group are matched.
[0028] A memory page is a management unit of memory management. In memory paging storage management, a process's virtual memory space is divided into several equal-sized segments, called pages. Page sizes vary, such as 4KB pages, 16KB pages, 64KB pages, 2MB pages, and 1GB pages. Memory pages can include anonymous pages and file pages, which are not limited here. Anonymous pages refer to pages without file context, such as the heap, stack, and data segments. File pages refer to pages with file context. They are cached in memory after a program reads a file and are also called file caches. The virtual address space of a process in the kernel is managed as a virtual memory area, including the code segment virtual memory area and the data segment virtual memory area.
[0029] In an embodiment of the present application, the electronic device can group memory pages in the memory according to the page priority corresponding to each memory page, and obtain memory groups corresponding to different page priorities. In the obtained memory groups, for the same memory group, the page priorities corresponding to the memory pages therein are matched, that is, the page priorities corresponding to any two memory pages in any memory group are matched. Optionally, the memcg grouping mechanism in the Linux system can be used as an implementation carrier of the memory group.
[0030] In some embodiments, the page priority of a memory page may be determined based on at least one of the access frequency, importance, and modification frequency of the memory page. The importance of a memory page may be determined based on at least one of the importance of the operating entity to which it belongs and the memory usage requirement. The operating entity may be an application, a system service to which it belongs, or the importance of the process to which it belongs. For different page priorities, a higher page priority corresponds to a higher access frequency, importance, and modification frequency of the corresponding memory page.
[0031] In some embodiments, considering that when an electronic device performs memory swap management on a memory, it mainly selects the memory pages that need to be swapped out and swaps them out to other storage areas when the memory is insufficient (for example, the remaining memory is less than the first memory threshold, the memory requirements of the current process cannot be met, memory space cannot be allocated for a new process, etc.), therefore, the electronic device can, when it is determined that the remaining memory is less than the second memory threshold, execute the grouping of the memory pages in the memory according to the page priority corresponding to each memory page in the memory, and obtain memory groups corresponding to different page priorities. Among them, the second memory threshold can be greater than the first memory threshold, that is, when the remaining memory of the electronic device is small but the conditions for memory page replacement are not met, the memory pages are grouped, so that when the memory page replacement is performed later, the memory page replacement can be performed according to the grouped memory groups.
[0032] In some embodiments, after the electronic device has been powered on for a first period of time, the electronic device may group the memory pages in the memory according to the page priority corresponding to each memory page in the memory, thereby obtaining memory groups corresponding to different page priorities. It is understood that when an electronic device is first powered on, the memory generally has fewer memory pages. Therefore, the memory pages in the memory may be grouped after the electronic device has been powered on and running for a period of time.
[0033] In some embodiments, considering that when an electronic device performs memory swap management on its memory, it mainly selects memory pages that need to be swapped out and swaps them out to other storage areas when the memory is insufficient (for example, the remaining memory is less than a set memory threshold, the memory requirements of the current process cannot be met, memory space cannot be allocated for a new process, etc.), therefore, it can be predicted whether the electronic device will perform memory swap management after a second time period. According to the prediction result, if the prediction result indicates that memory swap management will be performed after the second time period, the memory pages in the memory can be grouped according to the page priority corresponding to each memory page in the memory at the current moment to obtain memory groups corresponding to different page priorities. The specific value of the second time period is not limited, for example, it can be 3 minutes, 5 minutes, 10 minutes, etc. In this way, it can be achieved that before the predicted electronic device performs memory swap management, the memory pages corresponding to each running system service are grouped, so that before performing memory swap management, the memory pages in the memory can be grouped according to the page priority corresponding to each memory page in the memory to obtain memory groups corresponding to different page priorities, so that when memory swap management is required, the memory pages can be replaced according to the grouped memory groups. The electronic device may predict whether the electronic device will perform memory swap management after a second time period at every first time period. The first time period may be 30 seconds, 1 minute, 3 minutes, or the like.
[0034] In one possible embodiment, a pre-trained memory swap prediction model is stored in the electronic device. The memory swap prediction model may be pre-trained based on historical device information collected at different times. The historical device information is device information collected by the electronic device during historical operation, and the historical device information is marked with a corresponding memory swap tag. The historical device information may include: historical moment, total memory size, remaining memory size at the historical moment, running applications at the historical moment, and other information related to the current state of the electronic device at the historical moment. The memory swap tag is used to characterize whether the electronic device performs memory swap management after the second duration of the historical moment. Among them, the memory swap prediction model may be a support vector machine (SVM), a neural network, a naive Bayes classifier, etc., and the specific model type of the memory swap prediction model may not be limited.
[0035] In the above embodiment, when the electronic device needs to predict whether memory exchange management will be performed after the second period of time, it can obtain the current device information, namely the current time, the total memory size, the remaining memory size of the electronic device at the current time, the application running at the current time and other information, and then input the current device information into the above memory exchange prediction model to obtain a prediction result. According to the prediction result, it can be determined whether the electronic device will perform memory exchange management after the second period of time.
[0036] Optionally, the prediction result can be a classification result of whether the electronic device will perform memory exchange management after the second time period, and the classification result includes two categories: performing memory exchange management and not performing memory exchange management. Therefore, the classification result can directly indicate whether the electronic device will perform memory exchange management after the second time period; the prediction result can also be a probability value characterizing whether the electronic device will perform memory exchange management after the second time period, and the probability value is positively correlated with the possibility that the electronic device will perform memory exchange management after the second time period, that is, the larger the probability value, the greater the possibility that the electronic device will perform memory exchange management after the second time period, and the smaller the collision probability value, the smaller the possibility that the electronic device will perform memory exchange management after the second time period. Therefore, based on the prediction result, it can also be determined whether the electronic device will perform memory exchange management after the second time period.
[0037] Optionally, the above memory exchange prediction model can be trained in the following manner: obtain the historical device information of the electronic device at different historical moments in the historical operation process, and obtain the record of whether memory exchange management is performed after the second time period of each historical moment. According to the obtained record, the historical device information of each historical moment can be marked with the above memory exchange label, thereby obtaining a sample data set; when the memory exchange prediction model is trained based on the sample data set, the historical device information can be input into the initial prediction model to obtain the prediction result output by the initial prediction model for the input historical device information. According to the prediction result and the memory exchange label marked by the historical device information, the target loss value of the initial prediction model can be determined; after determining the target loss value corresponding to the initial detection model, the initial prediction model can be iteratively trained according to the target loss value to obtain the final memory exchange prediction model. Among them, the initial prediction model can be a neural network, a Softmax logistic regression model, a support vector machine, etc., and the specific initial prediction model can be not limited; when determining the target loss value, the loss value can be determined based on the difference between the prediction result and the memory exchange label marked by the historical device information.
[0038] When iteratively training the initial prediction model based on the target loss value, the model parameters of the initial prediction model can be adjusted based on the calculated target loss value; the historical device information is repeatedly input into the initial prediction model to obtain the prediction results output by the memory swap prediction model for the input historical device information. Based on the prediction results and the memory swap labels marked with the historical device information, the target loss value corresponding to the initial prediction model can be determined, and the model parameters of the initial prediction model can be adjusted based on the target loss value until the training end conditions are met, thereby obtaining the trained memory swap prediction model. The training end conditions for iterative training may include: the number of iterative training reaches the target number; or the total loss value of the initial prediction model meets the set conditions.
[0039] In the above embodiment, if it is determined that the electronic device does not perform memory swap management after the second time period at the current moment, the memory pages of the running applications may not be grouped, that is, the memory pages of each application are not divided into corresponding memory groups, thereby reducing the consumption of processing resources of the electronic device.
[0040] In some implementations, when a new memory page is added to the memory, the new memory page may also be divided into a corresponding memory group among the memory groups obtained above according to the page priority corresponding to the new memory page.
[0041] Step S120: If it is currently necessary to swap out memory pages in the memory, at least part of the memory pages in the memory group corresponding to the lowest page priority are swapped out to the target storage area.
[0042] In an embodiment of the present application, after grouping the memory pages in the memory, the electronic device can determine whether the memory pages in the memory currently need to be swapped out, that is, whether memory swap management is required; if it is determined that the memory pages in the memory currently need to be swapped out, the electronic device can swap out at least some of the memory pages therein from the memory group corresponding to the lowest page priority to the target storage area based on the memory group obtained by the above grouping. The target memory area can be a compressed memory area, a flash memory, an area in a disk, etc. Thus, it can be achieved that when the memory pages in the memory currently need to be swapped out, at least some of the memory pages in the memory group corresponding to the lowest page priority are swapped out, thereby avoiding memory pages with higher page priorities from being mistakenly swapped out, thereby improving the accuracy of memory management.
[0043] In some embodiments, the electronic device may determine that a memory page needs to be swapped out from the memory if it is determined that the current remaining memory meets a memory swap condition. The memory swap condition may include: the remaining memory is less than a first memory threshold, the memory requirement of the current process cannot be met, or memory space cannot be allocated for a new process.
[0044] In some embodiments, the electronic device may swap out a portion of the memory pages in the memory group corresponding to the lowest page priority to the target storage area at one time, for example, swap out half of the memory pages in the memory group corresponding to the lowest page priority to the target storage area. The electronic device may also swap out a portion of the memory pages in the memory group corresponding to the lowest page priority to the target storage area multiple times. Of course, the specific manner in which the electronic device swaps out the memory pages in the memory group corresponding to the lowest page priority to the target storage area is not limited.
[0045] The memory management method provided by the embodiment of the present application is to group the memory pages in the memory according to the page priority corresponding to each memory page in the memory, thereby obtaining memory groups corresponding to different page priorities, and the page priorities corresponding to the memory pages in the same memory group are matched; if the memory pages in the memory currently need to be swapped out, at least part of the memory pages in the memory group corresponding to the lowest page priority are swapped out to the target storage area. Thus, by grouping the memory pages, when performing memory swapping, memory pages are selected from the memory group corresponding to the lowest page priority for swapping out, thereby avoiding memory pages with higher page priorities from being swapped out by mistake, thereby improving the accuracy of memory management.
[0046] See also Figure 2 , Figure 2A flow chart of a memory management method provided by another embodiment of the present application is shown. The memory management method is applied to the above electronic device. Figure 2 The process shown in FIG. 1 is described in detail. Specifically, the memory management method may include the following steps:
[0047] Step S210: Determine the page priority corresponding to each memory page in the memory according to the access frequency corresponding to each memory page, wherein the access frequencies corresponding to memory pages of the same page priority are matched, and the page priority is positively correlated with the access frequency.
[0048] In an embodiment of the present application, the page priority of the above memory pages can be determined based on the access frequency corresponding to the memory page. When grouping the memory pages in the memory, the access frequency corresponding to each memory page can be obtained first, and the page priority corresponding to each memory page in the memory can be determined, and the access frequencies corresponding to memory pages with the same page priority are matched. Among them, the access frequency corresponding to the memory page can refer to the number of accesses per unit time. In addition, the page priority is positively correlated with the access frequency of the memory page, that is, the greater the page priority, the higher the access frequency of the corresponding memory page.
[0049] In some implementations, the electronic device may store access records of each memory page; based on the access records, the number of accesses per unit time may be determined, thereby obtaining the access frequency corresponding to the memory page.
[0050] Step S220: grouping the memory pages in the memory according to the page priority corresponding to each memory page in the memory to obtain memory groups corresponding to different page priorities. The page priorities corresponding to the memory pages in the same memory group are matched.
[0051] In the embodiment of the present application, step S220 can refer to the content of the aforementioned embodiment and will not be repeated here.
[0052] Step S230: If the change amplitude of the access frequency corresponding to the first target memory page is greater than the target amplitude threshold, the page priority of the first target memory page is updated to the first target priority based on the current access frequency of the first target memory page.
[0053] In an embodiment of the present application, taking into account that the access frequency of some memory pages may change during the operation of the electronic device, the electronic device can also monitor the changes in the access frequency of each memory page in the memory, and when it is detected that the access frequency of any memory page has changed, determine the change amplitude of the access frequency of the memory page; compare the change amplitude of the access frequency corresponding to the memory page whose access frequency has changed with the target amplitude threshold; according to the comparison result, if the change amplitude of the access frequency corresponding to the first target memory page is greater than the target amplitude threshold, then based on the current access frequency of the first target memory page, the page priority of the first target memory page is updated to the first target priority, and the access frequency of the memory page corresponding to the updated first target priority matches the current access frequency of the first target memory page; if the change amplitude of the access frequency corresponding to the first target memory page is less than or equal to the target amplitude threshold, the page priority of the first target memory page is not updated.
[0054] Among them, the above-mentioned first target memory page can be any memory page whose access frequency changes. It can be understood that when the access frequency of the memory page changes and the change amplitude of the access frequency is greater than the target amplitude threshold, it means that the access frequency of the memory page has changed significantly, and the page priority of the memory page is determined according to the access frequency of the memory page. Therefore, the page priority of the memory page can be updated based on the current access frequency of the memory page, so that the memory page can be re-divided into the corresponding memory group later to ensure the accuracy of memory page replacement.
[0055] Step S240: Divide the first target memory page into a memory group corresponding to the first target priority.
[0056] In an embodiment of the present application, after the page priority of the first target memory page is updated to the first target priority based on the current access frequency of the first target memory page, the first target memory page can be divided into a memory group corresponding to the first target priority, so that when the access frequency of the memory page changes greatly, the memory group to which the memory page belongs can be updated, thereby ensuring the accuracy of the memory grouping, and further ensuring the accuracy of the memory page replacement.
[0057] Step S250: If it is currently necessary to swap out memory pages in the memory, at least part of the memory pages in the memory group corresponding to the lowest page priority are swapped out to the target storage area.
[0058] In the embodiment of the present application, step S250 can refer to the content of the aforementioned embodiment and will not be repeated here.
[0059] The memory management method provided by the embodiment of the present application is to group the memory pages in the memory according to the page priority corresponding to each memory page in the memory, thereby obtaining memory groups corresponding to different page priorities, and the page priorities corresponding to the memory pages in the same memory group are matched; if the memory pages in the memory currently need to be swapped out, at least part of the memory pages in the memory group corresponding to the lowest page priority are swapped out to the target storage area. Thus, by grouping the memory pages, when performing memory swapping, memory pages are selected from the memory group corresponding to the lowest page priority for swapping out, thereby avoiding memory pages with higher page priorities from being swapped out by mistake, thereby improving the accuracy of memory management.
[0060] See also Figure 3 , Figure 3 A flow chart of a memory management method according to another embodiment of the present application is shown. The memory management method is applied to the above electronic device. Figure 3 The process shown in FIG. 1 is described in detail. Specifically, the memory management method may include the following steps:
[0061] Step S310: grouping memory pages in the memory according to the page priority corresponding to each memory page in the memory to obtain memory groups corresponding to different page priorities. The page priorities corresponding to memory pages in the same memory group are matched.
[0062] In the embodiment of the present application, step S310 can refer to the content of the aforementioned embodiment and will not be repeated here.
[0063] Step S320: if the first residence time of the second target memory page in the first memory group reaches the target time threshold, the page priority of the second target memory page is reduced to a second target priority.
[0064] In an embodiment of the present application, taking into account that memory pages whose page priority is not the lowest page priority will not be in the memory group corresponding to the lowest page priority, in this way, when memory pages are swapped out, these memory pages will not be swapped out to other storage areas. However, in memory pages whose page priority is not the lowest page priority, there may also be memory pages that have not been accessed for a long time. Therefore, for each memory page whose page priority is not the lowest page priority, the residence time in the memory group where it is located can be recorded. If the residence data (as the first residence time) of any memory page (as the second target memory page) in the memory group (as the first memory group) where it is located reaches the target time threshold, the page priority of the second target memory page can be reduced to the second target priority. In other words, for a memory page that is not the lowest page priority, as long as its residence time in the memory group where it is located reaches the target time threshold, its page priority will be reduced so that it can be assigned to the memory group corresponding to the reduced page priority.
[0065] In some embodiments, when the first residence time of the second target memory page in the first memory group reaches the target time threshold, the page priority of the second target memory page can be reduced by one page priority level, that is, based on the current page priority of the second target memory page, the page priority of the second target memory page is reduced to the next page priority level of the current page priority. In this way, memory pages that are not of the lowest page priority can also be allocated to different memory groups, thereby improving the flexibility of memory grouping and further improving the accuracy of memory page replacement.
[0066] In some embodiments, the above target time threshold may correspond to the first memory group where the second target memory page is located, and the target time threshold corresponding to the first memory group may be determined based on at least one of the access frequency and modification frequency corresponding to the memory page in the first memory group where the second target memory page is located. That is, if the first residence time of the second target memory page in the first memory group where it is located reaches the target time threshold corresponding to the first memory group, the page priority of the second target memory page is reduced to the second target priority. Among them, the target time threshold may be positively correlated with the access frequency and modification frequency corresponding to the memory page in the first memory group. For example, if the target time threshold is determined based on the access frequency corresponding to the memory page in the first memory group, the greater the access frequency corresponding to the memory page in the first memory group, the greater the target time threshold corresponding to the first memory group. In this way, for memory pages with greater access frequency and modification frequency, the longer they stay in the memory group to which they belong, which can ensure that memory pages with greater access frequency and modification frequency will not be swapped out as much as possible.
[0067] Step S330: Divide the second target memory page into a memory group corresponding to the second target priority.
[0068] Step S340: If it is currently necessary to swap out memory pages in the memory, at least part of the memory pages in the memory group corresponding to the lowest page priority are swapped out to the target storage area.
[0069] In the embodiment of the present application, step S340 can refer to the content of the aforementioned embodiment and will not be repeated here.
[0070] The memory management method provided in the embodiment of the present application groups memory pages in the memory according to the page priority corresponding to each memory page in the memory to obtain memory groups corresponding to different page priorities. The page priorities corresponding to memory pages in the same memory group are matched. When performing memory swapping, memory pages are selected from the memory group corresponding to the lowest page priority for swapping out, thereby avoiding memory pages with higher page priorities from being mistakenly swapped out, thereby improving the accuracy of memory management; in addition, for memory pages that are not of the lowest page priority, when their residence time in the memory group reaches the target time threshold, by lowering their page priority, memory pages that are not of the lowest page priority can also be allocated to different memory groups, thereby improving the flexibility of memory grouping and further improving the accuracy of memory page replacement.
[0071] See also Figure 4 , Figure 4 A flow chart of a memory management method provided by another embodiment of the present application is shown. The memory management method is applied to the above electronic device. Figure 4 The process shown in FIG. 1 is described in detail. Specifically, the memory management method may include the following steps:
[0072] Step S410: grouping memory pages in the memory according to the page priority corresponding to each memory page in the memory to obtain memory groups corresponding to different page priorities. The page priorities corresponding to memory pages in the same memory group are matched.
[0073] In the embodiment of the present application, step S410 can refer to the content of the aforementioned embodiment and will not be repeated here.
[0074] Step S420: If a memory page in the memory currently needs to be swapped out, a third target memory page to be swapped out is selected from the memory group corresponding to the lowest page priority.
[0075] In an embodiment of the present application, after the electronic device groups the memory pages in the memory, when it determines that the memory page in the memory currently needs to be swapped out, it can select the third target memory page to be swapped out from the memory group corresponding to the lowest page priority, so that the third target memory page can be subsequently swapped out to the target storage area to increase the available memory.
[0076] In some embodiments, when it is determined that a memory page in the memory currently needs to be swapped out, a third target memory page to be swapped out can be selected from the memory group corresponding to the lowest page priority based on the target memory swap strategy. The target memory swap strategy can be a swap strategy corresponding to a First Input First Output (FIFO) algorithm, a swap strategy for Least Recently Used (LRU), a swap strategy corresponding to a clock page replacement algorithm, etc. In this way, when a memory page needs to be swapped out, not only is the memory page swapped out from the memory group corresponding to the lowest page priority, but the corresponding memory swap strategy is also combined to select a memory page from the memory group corresponding to the lowest page priority for swapping out, thereby further ensuring the accuracy of the memory page swapping out.
[0077] Step S430: swapping out the third target memory page to the target storage area corresponding to the third target memory page.
[0078] In an embodiment of the present application, after selecting the third target memory page to be swapped out from the memory group corresponding to the lowest page priority, the third target memory page can be swapped out to the target storage area corresponding to the third target memory page. It can be understood that for different memory pages, corresponding swap areas can be set to achieve swapping out different memory pages in the memory group corresponding to the lowest page priority to the corresponding target storage area. Among them, the target storage area can be a compressed memory area, a flash memory, an area in a disk, etc. In this way, it can be avoided that all memory pages are swapped out to ZRAM first, thereby avoiding the high occupancy rate of ZRAM and causing some memory pages that need to be swapped back (Swap In) at a faster speed to be unable to be swapped out to ZRAM, and different storage resources can be effectively utilized.
[0079] In some embodiments, when the third target memory page is swapped out to the target storage area corresponding to the third target memory page, the swap area corresponding to the maximum tolerance time can be determined based on the maximum tolerance time when the third target memory page is accessed, as the target storage area corresponding to the third target memory page, and then the third target memory page is swapped out to the target storage area. Wherein, the maximum tolerance time when the third target memory page is accessed refers to the maximum waiting time (or maximum delay time) that can be tolerated when the third target memory page is accessed. Wherein, the maximum tolerance time can be positively correlated with the data read and write speed of the swap area, that is, the greater the data read and write speed of the swap area, the greater the corresponding maximum tolerance time is, and the smaller the data read and write speed of the swap area, the smaller the corresponding maximum tolerance time is. Thus, it is possible to determine the destination to which the memory page is swapped out based on the maximum tolerance time of the memory page, so that the memory page with a fast access speed is swapped out to the swap area with a faster data read and write speed, thereby improving the flexibility of memory replacement and effectively utilizing different storage resources.
[0080] In some embodiments, the electronic device can record the residence time of each memory page in its memory group. When swapping out a third target memory page to the target storage area corresponding to the third target memory page, the swap area corresponding to the second residence time can be determined based on the second residence time of the third target memory page in the second memory group. The residence time of a memory page can be negatively correlated with the data read / write speed of the swap area. That is, the faster the data read / write speed of the swap area, the shorter the corresponding residence time. The slower the data read / write speed of the swap area, the longer the corresponding maximum tolerance time. It is understandable that when the residence time of a memory page is relatively short, the memory page is more likely to be accessed again, so it can be swapped out to a swap area with a relatively fast data read / write speed. Memory pages with relatively long residence times are less likely to be accessed again, so they can be swapped out to a swap area with a relatively slow data read / write speed. Thus, the destination to which a memory page is swapped out can be determined based on the residence time of the memory page, so that memory pages with short residence times are swapped out to swap areas with faster data read / write speeds. This improves the flexibility of memory swapping and enables the effective utilization of different storage resources.
[0081] The memory management method provided in the embodiment of the present application groups memory pages in the memory according to the page priority corresponding to each memory page in the memory to obtain memory groups corresponding to different page priorities. The page priorities corresponding to memory pages in the same memory group are matched. When performing memory swapping, memory pages are selected from the memory group corresponding to the lowest page priority for swapping out, thereby avoiding memory pages with higher page priorities from being mistakenly swapped out, thereby improving the accuracy of memory management. In addition, when swapping out memory pages in the memory group corresponding to the lowest page priority, the selected memory pages are swapped out to the target storage area corresponding to the memory page, thereby improving the flexibility of memory swapping and effectively utilizing the storage resources of the electronic device.
[0082] See also Figure 5 , which shows a structural block diagram of a memory management device 500 provided in an embodiment of the present application. The memory management device 500 applies the above-mentioned electronic device, and the memory management device 500 includes: a memory grouping module 510 and a memory swapping module 520. The memory grouping module 510 is used to group the memory pages in the memory according to the page priority corresponding to each memory page in the memory, and obtain memory groups corresponding to different page priorities, and the page priorities corresponding to the memory pages in the same memory group are matched; the memory swapping module 520 is used to swap out at least part of the memory pages in the memory group corresponding to the lowest page priority to the target storage area if the memory pages in the memory currently need to be swapped out.
[0083] In some embodiments, the memory grouping module 510 can also be used to determine the page priority corresponding to each memory page in the memory according to the access frequency corresponding to each memory page before grouping the memory pages in the memory according to the page priority corresponding to each memory page in the memory to obtain memory groups corresponding to different page priorities, wherein the access frequencies corresponding to memory pages of the same page priority are matched, and the page priority is positively correlated with the access frequency.
[0084] In a possible embodiment, the memory grouping module 510 can also be used to group the memory pages in the memory according to the page priority corresponding to each memory page in the memory to obtain memory groups corresponding to different page priorities. If the change amplitude of the access frequency corresponding to the first target memory page is greater than the target amplitude threshold, then based on the current access frequency of the first target memory page, the page priority of the first target memory page is updated to the first target priority; and the first target memory page is divided into the memory group corresponding to the first target priority.
[0085] In some embodiments, the memory grouping module 510 can also be used to group the memory pages in the memory according to the access priority corresponding to each memory page in the memory. After obtaining multiple memory groups, if the first residence time of the second target memory page in the first memory group reaches the target time threshold, the page priority of the second target memory page is reduced to the second target priority; and the second target memory page is divided into the memory group corresponding to the second target priority.
[0086] In a possible embodiment, the memory grouping module 510 can also be used to reduce the page priority of the second target memory page to a second target priority when the first residence time of the second target memory page in the first memory group reaches the target time threshold corresponding to the first memory group. The target time threshold corresponding to the first memory group is determined based on at least one of the access frequency and modification frequency corresponding to the memory page in the first memory group.
[0087] In some embodiments, the memory swap module 520 can be specifically used to: if a memory page in the memory currently needs to be swapped out, select a third target memory page to be swapped out from the memory group corresponding to the lowest page priority; swap out the third target memory page to the target storage area corresponding to the third target memory page.
[0088] In a possible embodiment, the memory swap module 520 can also be used to determine the swap area corresponding to the maximum tolerance time when the third target memory page is accessed, as the target storage area corresponding to the third target memory page; and swap out the third target memory page to the target storage area.
[0089] In a possible embodiment, the memory swap module 520 can also be used to determine the swap area corresponding to the second residence time of the third target memory page in the second memory group, as the target storage area corresponding to the third target memory page; and swap out the third target memory page to the target storage area.
[0090] In a possible implementation, the memory swap module 520 can also be used to select a third target memory page to be swapped out from the memory group corresponding to the lowest page priority based on the target memory swap policy if a memory page in the memory currently needs to be swapped out.
[0091] Those skilled in the art will clearly understand that, for the convenience and brevity of description, the specific working processes of the above-described devices and modules can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0092] In several embodiments provided in this application, the coupling between modules may be electrical, mechanical or other forms of coupling.
[0093] In addition, the functional modules in the various embodiments of the present application may be integrated into a processing module, or each module may exist physically separately, or two or more modules may be integrated into a single module. The above-mentioned integrated modules may be implemented in the form of hardware or software functional modules.
[0094] In summary, the solution provided by the present application is to group the memory pages in the memory according to the page priority corresponding to each memory page in the memory, thereby obtaining memory groups corresponding to different page priorities, and the page priorities corresponding to the memory pages in the same memory group are matched; if the memory pages in the memory currently need to be swapped out, at least part of the memory pages in the memory group corresponding to the lowest page priority are swapped out to the target storage area. Thus, by grouping the memory pages, when performing memory swapping, memory pages are selected from the memory group corresponding to the lowest page priority for swapping out, thereby avoiding memory pages with higher page priorities from being swapped out by mistake, thereby improving the accuracy of memory management.
[0095] Please refer to Figure 6 , which shows a structural block diagram of an electronic device provided in an embodiment of the present application. The electronic device 100 can be an electronic device capable of running applications, such as a smartphone, a tablet computer, a smart watch, an e-book, etc. The electronic device 100 in the present application may include one or more of the following components: a processor 110, a memory 120, and one or more applications, wherein the one or more applications may be stored in the memory 120 and configured to be executed by one or more processors 110, and the one or more applications are configured to execute the method described in the aforementioned method embodiment.
[0096] The processor 110 may include one or more processing cores. The processor 110 utilizes various interfaces and circuits to connect various components within the electronic device 100. It executes instructions, programs, code sets, or instruction sets stored in the memory 120, and accesses data stored in the memory 120 to perform various functions and process data within the electronic device 100. Optionally, the processor 110 may be implemented using at least one of the following hardware forms: a digital signal processing (DSP), a field-programmable gate array (FPGA), or a programmable logic array (PLA). The processor 110 may integrate one or a combination of a central processing unit (CPU), a graphics processing unit (GPU), and a modem. The CPU primarily processes the operating system, user interface, and application programs; the GPU is responsible for rendering and drawing display content; and the modem handles wireless communications. It is understood that the modem may not be integrated into the processor 110 and may be implemented separately via a communications chip.
[0097] The memory 120 may include a random access memory (RAM) or a read-only memory (ROM). The memory 120 may be used to store instructions, programs, codes, code sets, or instruction sets. The memory 120 may include a program storage area and a data storage area, wherein the program storage area may store instructions for implementing an operating system, instructions for implementing at least one function (such as a touch function, a sound playback function, an image playback function, etc.), instructions for implementing the following various method embodiments, etc. The data storage area may also store data created by the electronic device 100 during use (such as a phone book, audio and video data, chat history data), etc.
[0098] Please refer to Figure 7 , which shows a block diagram of a computer-readable storage medium provided in an embodiment of the present application. The computer-readable medium 800 stores program code, which can be called by a processor to execute the method described in the above method embodiment.
[0099] The computer-readable storage medium 800 can be an electronic memory such as a flash memory, an EEPROM (Electrically Erasable Programmable Read-Only Memory), an EPROM, a hard disk, or a ROM. Alternatively, the computer-readable storage medium 800 includes a non-transitory computer-readable storage medium. The computer-readable storage medium 800 has storage space for program code 810 for executing any of the method steps described above. These program codes can be read from or written to one or more computer program products. The program code 810 can be compressed, for example, in a suitable form.
[0100] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A memory management method, characterized in that: The method comprises: Grouping memory pages in the memory according to a page priority corresponding to each memory page in the memory to obtain memory groups corresponding to different page priorities, wherein the page priorities corresponding to memory pages in the same memory group are matched; If it is currently necessary to swap out memory pages in the memory, at least part of the memory pages in the memory group corresponding to the lowest page priority are swapped out to the target storage area.
2. The method according to claim 1, characterized in that Before grouping the memory pages in the memory according to the page priority corresponding to each memory page in the memory to obtain memory groups corresponding to different page priorities, the method further includes: According to the access frequency corresponding to each memory page, the page priority corresponding to each memory page in the memory is determined, wherein the access frequencies corresponding to memory pages with the same page priority are matched, and the page priority is positively correlated with the access frequency.
3. The method according to claim 2, characterized in that After grouping the memory pages in the memory according to the page priority corresponding to each memory page in the memory to obtain memory groups corresponding to different page priorities, the method further includes: If the change amplitude of the access frequency corresponding to the first target memory page is greater than the target amplitude threshold, updating the page priority of the first target memory page to a first target priority based on the current access frequency of the first target memory page; The first target memory page is divided into a memory group corresponding to the first target priority.
4. The method according to claim 1, wherein After grouping the memory pages in the memory according to the access priority corresponding to each memory page in the memory to obtain a plurality of memory groups, the method further includes: If the first residence time of the second target memory page in the first memory group reaches the target time threshold, lowering the page priority of the second target memory page to a second target priority; The second target memory page is divided into a memory group corresponding to the second target priority.
5. The method according to claim 4, characterized in that If the first residence time of the second target memory page in the first memory group reaches the target time threshold, lowering the page priority of the second target memory page to a second target priority includes: If the first residence time of the second target memory page in the first memory group reaches the target time threshold corresponding to the first memory group, the page priority of the second target memory page is reduced to the second target priority. The target time threshold corresponding to the first memory group is determined based on at least one of the access frequency and modification frequency corresponding to the memory page in the first memory group.
6. The method according to any one of claims 1 to 5, characterized in that If the memory page in the memory currently needs to be swapped out, swapping out at least part of the memory pages in the memory group corresponding to the lowest page priority to the target storage area includes: If a memory page in the memory currently needs to be swapped out, selecting a third target memory page to be swapped out from the memory group corresponding to the lowest page priority; The third target memory page is swapped out to a target storage area corresponding to the third target memory page.
7. The method according to claim 6, characterized in that The step of swapping out the third target memory page to a target storage area corresponding to the third target memory page includes: Based on a maximum tolerance time when the third target memory page is accessed, determining a swap area corresponding to the maximum tolerance time as a target storage area corresponding to the third target memory page; The third target memory page is swapped out to the target storage area.
8. The method according to claim 6, characterized in that The step of swapping out the third target memory page to a target storage area corresponding to the third target memory page includes: Based on a second residence time of the third target memory page in the second memory group, determining a swap area corresponding to the second residence time as a target storage area corresponding to the third target memory page; The third target memory page is swapped out to the target storage area.
9. The method according to claim 6, characterized in that If a memory page in the memory currently needs to be swapped out, selecting a third target memory page to be swapped out from the memory group corresponding to the lowest page priority includes: If a memory page in the memory currently needs to be swapped out, a third target memory page to be swapped out is selected from the memory group corresponding to the lowest page priority based on the target memory swap policy.
10. A memory management device, characterized in that: The device includes: a memory grouping module and a memory swapping module, wherein: The memory grouping module is used to group memory pages in the memory according to the page priority corresponding to each memory page in the memory, to obtain memory groups corresponding to different page priorities, and the page priorities corresponding to memory pages in the same memory group are matched; The memory swap-out module is configured to swap out at least some of the memory pages in the memory group corresponding to the lowest page priority to the target storage area if memory pages in the memory currently need to be swapped out.
11. An electronic device, characterized in that: include: one or more processors; Memory; One or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, and the one or more programs are configured to perform the method according to any one of claims 1 to 9.
12. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores program code, which can be called by a processor to execute the method according to any one of claims 1 to 9.