WO2021057619A1 - 内存回收方法、装置、电子设备及存储介质 - Google Patents

内存回收方法、装置、电子设备及存储介质 Download PDF

Info

Publication number
WO2021057619A1
WO2021057619A1 PCT/CN2020/116212 CN2020116212W WO2021057619A1 WO 2021057619 A1 WO2021057619 A1 WO 2021057619A1 CN 2020116212 W CN2020116212 W CN 2020116212W WO 2021057619 A1 WO2021057619 A1 WO 2021057619A1
Authority
WO
WIPO (PCT)
Prior art keywords
memory
recovery
ratio
pressure
memory recovery
Prior art date
Application number
PCT/CN2020/116212
Other languages
English (en)
French (fr)
Inventor
周华材
Original Assignee
Oppo广东移动通信有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Oppo广东移动通信有限公司 filed Critical Oppo广东移动通信有限公司
Priority to EP20868443.1A priority Critical patent/EP3979082A4/en
Publication of WO2021057619A1 publication Critical patent/WO2021057619A1/zh
Priority to US17/564,794 priority patent/US20220121495A1/en

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5011Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
    • G06F9/5022Mechanisms to release resources
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F12/00Accessing, addressing or allocating within memory systems or architectures
    • G06F12/02Addressing or allocation; Relocation
    • G06F12/0223User address space allocation, e.g. contiguous or non contiguous base addressing
    • G06F12/023Free address space management
    • G06F12/0253Garbage collection, i.e. reclamation of unreferenced memory
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5011Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
    • G06F9/5016Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals the resource being the memory
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5027Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals
    • G06F9/5038Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals considering the execution order of a plurality of tasks, e.g. taking priority or time dependency constraints into consideration
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5027Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals
    • G06F9/5044Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals considering hardware capabilities
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F2212/00Indexing scheme relating to accessing, addressing or allocation within memory systems or architectures
    • G06F2212/10Providing a specific technical effect
    • G06F2212/1041Resource optimization
    • G06F2212/1044Space efficiency improvement

Definitions

  • This application relates to the field of computer technology, and more specifically, to a memory recovery method, device, electronic equipment, and storage medium.
  • Memory also known as main memory, is a storage space that can be directly addressed by the CPU and is made of semiconductor devices.
  • the space of the memory in the electronic device is limited.
  • the system of the electronic device usually recycles the memory under certain circumstances.
  • this application proposes a memory recovery method, device, electronic equipment, and storage medium to improve the above-mentioned problems.
  • the present application provides a memory recycling method, the method includes: when in a memory recycling scene, acquiring a memory pressure, the memory pressure representing the proportional relationship between unreclaimed memory and scanned memory in a historical time window Obtain the memory recycling ratio corresponding to the memory pressure, the memory recycling ratio characterizing the ratio between a variety of memory recycling objects, where the higher the memory pressure corresponds to the memory recycling ratio, the CPU resources are consumed when recycling The lower the proportion of the memory recycling objects in the, the memory recycling is performed based on the memory recycling ratio.
  • the present application provides a memory recovery device, the device includes: a memory pressure acquisition unit, configured to acquire memory pressure when in a memory recovery scene, and the memory pressure represents the unrecovered memory and the memory in the historical time window.
  • the ratio relationship of the scanned memory the recovery parameter acquisition unit is used to acquire the memory recovery ratio corresponding to the memory pressure, and the memory recovery ratio represents the ratio between a variety of memory recovery objects, where the greater the memory pressure The higher the memory recovery ratio corresponds to the lower the proportion of memory recovery objects that need to consume CPU resources during recovery; the memory recovery unit is configured to perform memory recovery based on the memory recovery ratio.
  • the present application provides an electronic device including one or more processors and a memory; one or more programs are stored in the memory and configured to be executed by the one or more processors To achieve the above method.
  • the present application provides a computer-readable storage medium in which program code is stored, wherein the above-mentioned method is executed when the program code is run by a processor.
  • Fig. 1 shows a flow chart of a method for reclaiming memory provided by an embodiment of the present application.
  • Fig. 2 shows a flow chart of a method for reclaiming memory provided by another embodiment of the present application.
  • FIG. 3 shows a schematic diagram of multiple periodic time periods in a memory recycling method proposed by an embodiment of the present application.
  • FIG. 4 shows a schematic diagram of an application program running in multiple periodic time periods in a memory recycling method proposed in an embodiment of the present application.
  • Fig. 5 shows a flow chart of a method for reclaiming memory proposed in another embodiment of the present application.
  • Fig. 6 shows a flow chart of a method for reclaiming memory provided by another embodiment of the present application.
  • FIG. 7 shows a structural block diagram of a memory recovery device proposed by an embodiment of the present application.
  • FIG. 8 shows a structural block diagram of a memory recovery device proposed by still another embodiment of the present application.
  • FIG. 9 shows a structural block diagram of an electronic device for executing the memory recycling method according to an embodiment of the present application in real time of the present application.
  • FIG. 10 shows a storage unit used to store or carry the program code for implementing the memory recovery method according to the embodiment of the present application in real time of the present application.
  • memory reclamation is a relatively common mechanism.
  • the system needs to provide enough memory for memory applications that come in bursts at any time, so that the use of cache and other related memory will not keep the remaining memory of the system in a very long time. Less state. Also, when an application that is larger than the free memory arrives, it will also trigger memory reclamation.
  • take the Android operating system as an example.
  • the Android operating system there are mainly two memory recovery mechanisms.
  • One is a recycling mechanism based on the kswapd process.
  • the kswapd process runs in the background periodically.
  • the other is a recycling mechanism based on the direct reclaim process.
  • the direct reclaim process starts memory recycling when it receives a memory allocation request. Therefore, it can be found that the memory recovery parameters of the related memory mode are relatively fixed.
  • the inventor studied the memory pressure that characterizes the proportional relationship between the unreclaimed memory and the scanned memory in the historical time window.
  • the inventor proposes that in the case of different memory recovery ratios corresponding to different memory pressures in this application, the current memory pressure can be obtained at the beginning of the memory recovery, and then the current memory pressure can be used to determine which memory recovery objects are suitable for. How much is recycled separately, thereby enhancing the flexibility of the memory recycling process, memory recycling methods, devices, electronic equipment, and storage media.
  • FIG. 1 shows a memory recovery method provided by an embodiment of the present application, and the method includes:
  • Step S110 When in a memory recycling scene, acquire a memory pressure, where the memory pressure represents the proportional relationship between the unreclaimed memory and the scanned memory in the historical time window.
  • the current memory recovery scenario is entered.
  • the running environment of this embodiment is the Android operating system
  • the memory pressure in this embodiment represents the proportional relationship between the unreclaimed memory and the scanned memory in the historical time window.
  • the electronic device can perform the calculation of the memory pressure periodically, and one of the cycles can be understood as the aforementioned time window.
  • the content stored in different memory areas in the memory space may be different, and different memory areas may not be directly reclaimed when scanned, and that is not as long as The scanned memory can be recycled.
  • the memory pressure can be calculated by (1-reclaimed/scanned)*100, where reclaimed represents unreclaimed memory, and scanned represents scanned memory.
  • the length of the time window can be adjusted according to actual conditions.
  • the electronic device can adjust the length of the time window according to the number of applications in a running state. It should be noted that the electronic device will temporarily reapply for memory allocation due to the need to process data during operation. Then when there are more applications running, there will be relatively more possibilities to apply for memory allocation, and it will also More memory is allocated relative to the application. In this case, optionally, the electronic device can periodically obtain the number of currently running applications, match the number of applications with multiple intervals, and then match the length of time corresponding to the matched interval. As the length of the current time window. For example, the sections configured by the electronic device for the number of running applications include section A, section B, section C, and section D.
  • the interval A corresponds to the time window A
  • the interval B corresponds to the time window B
  • the interval C corresponds to the time window C
  • the interval D corresponds to the time window D.
  • Step S120 Obtain a memory recovery ratio corresponding to the memory pressure, where the memory recovery ratio represents the ratio between a variety of memory recovery objects, where the higher the memory pressure corresponds to the memory recovery ratio that needs to be consumed during recovery The lower the proportion of the memory recycling object of the CPU resource.
  • the electronic device acquires the memory pressure, it can match the acquired memory pressure with the aforementioned multiple preset memory pressures, and then match the multiple preset memory pressures with the acquired memory pressure.
  • the corresponding memory recycling ratio is used as the memory recycling ratio used in subsequent memory recycling.
  • the multiple memory pressures stored in the electronic device include memory pressure A, memory pressure B, memory pressure C, and memory pressure D.
  • memory pressure A corresponds to a memory recycling ratio of 0:200
  • memory pressure B corresponds to a memory recycling ratio of 40:160
  • memory pressure C corresponds to a memory recycling ratio of 80:120
  • memory pressure D corresponds to a memory recycling ratio of 200:0. Then after the memory pressure X is obtained, the memory pressure X is matched with the aforementioned memory pressure A, memory pressure B, memory pressure C, and memory pressure D. If the memory pressure X matches the memory pressure C, the corresponding memory The recovery ratio is 80:120.
  • a certain matching error is allowed. For example, if there is a certain matching error between the aforementioned memory pressure X and the memory pressure C, the electronic device may also determine that the memory pressure X matches the memory pressure C therein.
  • Step S130 Perform memory recovery based on the memory recovery ratio.
  • the memory recovery method provided by this application can obtain the memory pressure that characterizes the proportional relationship between the unreclaimed memory and the scanned memory in the historical time window when in a memory recovery scene, and then obtain the memory recovery corresponding to the memory pressure Ratio, memory recycling is performed based on the memory recycling ratio. Therefore, when the memory recovery ratio corresponding to different memory pressures is different, the current memory pressure can be obtained at the beginning of the memory recovery, and then the current memory pressure can be used to determine how much to recover each of the various memory recovery objects, thereby improving The flexibility of the memory recovery process is improved.
  • FIG. 2 shows a memory recovery method provided by an embodiment of the present application, and the method includes:
  • Step S210 When in the memory recovery scenario, the complete historical time period of the one closest to the current time in the periodic historical time period is taken as the designated time period.
  • Step S220 Obtain the stored memory pressure corresponding to the specified time period, where the memory pressure represents the proportional relationship between the unreclaimed memory and the scanned memory in the historical time window.
  • the periodic time period has a start time and an end time for each time period.
  • the start time is 0 and the end time is t1.
  • the start time is t2
  • the end time is t3.
  • the memory pressure corresponding to each time period reflects the memory pressure situation in each time period, and in some cases, the memory pressure changes more frequently. If the memory pressure in the previous time period is used to calculate the memory allocation ratio in this time period, it can reflect the actual memory usage to a certain extent, but it cannot reflect the current real-time situation well.
  • the memory pressure is very similar in some time periods.
  • the similar memory pressure may be because the running applications are similar or the same.
  • there are application A, application B, and application C running and correspondingly, there are also application A, application B, and application A during the time period t4 to t5.
  • Application C is running, then it can be understood that the time period from t4 to t5 is the same or similar to the time period from t1 to t2.
  • the step of obtaining the memory pressure corresponding to the storage in the specified time period includes: obtaining the current application program characteristic parameter, the application program characteristic parameter characterizing the degree of CPU processing resource occupancy of the application program; based on the application program The program feature parameter is calculated to obtain the target historical time period corresponding to the time period at the current moment as the designated time period; and the stored memory pressure in the designated time period is obtained.
  • the electronic device may calculate the application feature parameters corresponding to the time period at the start time and the end time of each time period.
  • the application detected at the beginning of the time period can be an estimate of which applications will run in this time period, so that the same target time period can be calculated more quickly, and when The applications detected at the end of the time period can clearly confirm which applications are actually running in this time period. Therefore, the application feature parameters corresponding to this time period calculated in this time period can be used to compare the same target time period in subsequent time periods, and the current application feature parameters can be calculated at the beginning of each time period. It is possible to estimate the same target time period as the current time period before the current time period ends, so that the corresponding memory recovery ratio can be obtained more quickly and timely.
  • the current application program characteristic parameters it may be the ratio of the number of threads currently started by the application program to the total number of threads that can be processed in parallel by the CPU, where the higher the application program characteristic parameters indicate the degree of CPU occupancy Higher.
  • application A, application B, application C, and application D are currently running, and application A corresponds to a thread running, application B corresponds to b threads running, and application C corresponds to There are c threads running, and application D corresponds to d threads running.
  • the corresponding current application characteristic parameter is (a+b+c+d)/m.
  • the calculation of the application program characteristic parameters at the end of each time period is the ratio of the average number of threads of all applications in the current time period to the total number of threads that can be processed in parallel by the CPU.
  • application B, application C, and application D are running, and application B has run t threads, application C has run k threads, and application D has run l Thread.
  • the characteristic parameter of the application program in this time period is (t+k+l)/3/m.
  • the step of calculating the target historical time period corresponding to the time period at the current moment based on the application feature parameter as the designated time period includes: combining the current application feature parameter with multiple histories The application feature parameters of the time period are compared; the historical time period in which the corresponding application feature parameter is the same as the current application feature parameter is taken as the corresponding target historical time period; the target historical time period is taken as the designated time segment.
  • the method further includes: obtaining the number and priority of applications running in each time period, wherein the higher the priority, the higher the memory occupied during the historical operation; A first score corresponding to the number; obtaining a second score corresponding to the priority; calculated based on the first score and the corresponding first preset weight, and the second score and the corresponding second preset weight The characteristic parameters of the application corresponding to each time period.
  • Step S230 Obtain a memory recovery ratio corresponding to the memory pressure, where the memory recovery ratio represents the ratio between a variety of memory recovery objects, where the higher the memory pressure corresponds to the memory recovery ratio, the memory recovery ratio needs to be consumed during recovery. The lower the proportion of the memory recycling object of the CPU resource.
  • the memory that needs to be recovered may be different.
  • the main focus is to recover anonymous pages.
  • the main focus is on reclaiming file pages.
  • the current memory reclamation scenario can be confirmed based on multiple methods.
  • the identifier of the process currently performing memory recycling can be identified; and the current memory recycling scene can be determined according to the identifier. It is understandable that memory recycling in an electronic device is performed by a specific process. For example, the aforementioned kswapd process or direct reclaim process. Then in this way, it is possible to periodically detect which processes are currently running. When it is detected that the identifier of a specific process for memory recovery is the running process identifier, it is determined to be in the memory recovery corresponding to the identifier. Scenes. For example, if the identification of the process currently performing memory recovery is "kswapd", then the corresponding identification is currently the kswapd memory recovery scenario. If the identification of the process that is currently performing memory reclamation is "direct reclaim", then the corresponding identification is currently a direct reclaim memory reclamation scenario.
  • the current memory recovery scenario can also be determined according to the currently running application.
  • the memory recycling objects mentioned in the embodiment may include anonymous pages (Anonymous pages) and file pages (File pages).
  • anonymous pages can be understood as pages without a file background, such as heaps, stacks, data segments, etc., which do not exist in the form of files.
  • a file page can be understood as a page with a file background. For example, for the data stored in the file page, the corresponding data can be found in the disk.
  • this embodiment mainly focuses on detecting applications running in the foreground.
  • the name of the application currently running in the foreground can be obtained by executing the getRunningTasks method of the ActivityManager.
  • the electronic device can also obtain a list of programs used by the user through the UsageStatsManager, and identify the most recently used application recorded in the list as the current foreground application.
  • the ratio in this embodiment can be understood as the ratio between anonymous pages and document pages.
  • the ratio corresponding to the label "direct reclaim” is "0:200", which means that only document pages are reclaimed, but anonymous pages are not reclaimed.
  • the ratio corresponding to the mark “kswapd” is "200:0”, which means that only anonymous pages are reclaimed, not document pages.
  • the ratio can also be changed as needed.
  • the ratio corresponding to the process marked as "direct reclaim” can also be 50:150 to indicate that file pages are recycled as much as possible, and anonymous pages are recycled in a small amount.
  • the corresponding relationship between the application program and the memory recovery ratio can be established in advance. Furthermore, after detecting that the memory recovery process starts running, the currently running application is obtained, and then the ratio corresponding to the running application is queried as the memory recovery ratio corresponding to the memory pressure of the current memory recovery scenario.
  • Application ID proportion Application A 10 190 Application B 20: 180 Application C 30: 170
  • each application identifier has a corresponding memory recovery ratio. Among them, the higher the proportion of file pages, the more quickly the performance of memory recovery is required. Then after detecting that the process of memory recovery starts running, if it is detected that application A is currently running, then correspondingly, the ratio "10:190" corresponding to application A is used as the memory recovery ratio. If it is detected that application B is currently running, then correspondingly, the ratio "20:180" corresponding to application B is used as the memory recovery ratio.
  • the proportion corresponding to the current scene can be determined by combining the proportion corresponding to the process itself and the proportion of the currently running application program.
  • the ratio corresponding to the process identifier "direct reclaim” is "0:200”
  • the ratio corresponding to application A is "10:190”
  • the proportion corresponding to the running application can be compared with the proportion corresponding to the identification of the process of load memory reclaiming currently in the running state
  • the average value is taken as the memory recycling ratio of the current memory recycling scenario, so that the memory recycling ratio corresponding to the process can be met to a certain extent, and the application's demand for the memory recycling ratio can also be met to a certain extent.
  • the ratio corresponding to the process identifier “direct reclaim” is “0:200”
  • the ratio corresponding to application A is "10:190”
  • the proportion corresponding to the running application can be compared with the proportion corresponding to the identification of the process of load memory reclaiming currently in the running state
  • the average value is taken as the memory recycling ratio
  • a specific application can be marked by creating a list. Then, in this way, in order to quickly determine whether the current application is the aforementioned specific application (application configured with a memory recovery ratio), optionally, when a specific application is started, generate An accompanying file in which the identification of the specific application currently running is written.
  • the memory recovery ratio corresponding to the application recorded in the accompanying file is compared with the currently started responsible memory
  • the memory recycling ratios corresponding to the recycled processes are combined (for example, the aforementioned averaging) to obtain the memory recycling ratio corresponding to the current memory recycling scenario. If it is detected that no accompanying file exists, the memory recovery ratio corresponding to the process responsible for memory recovery is directly used as the memory recovery ratio corresponding to the current memory recovery scenario.
  • the current memory recovery scenario by detecting the application program. It is also possible to determine the current memory reclamation scenario by predicting which applications will be launched soon. For example, in one manner, some application programs of the electronic device are started regularly, for example, an alarm clock program. Or some applications will be routinely launched during a certain time period of the day due to the user's usage habits. For example, take-out applications may be routinely launched every day when they are close to the meal, or video applications may be routinely launched by users when they take the subway every day.
  • the step of obtaining the memory recovery ratio corresponding to the memory pressure includes:
  • mapping relationship between the multiple preset memory pressures and the respective corresponding memory recovery ratios match the memory pressure with the multiple preset memory pressures in a specified order; match the memory pressure with the memory pressure
  • the memory recovery ratio corresponding to the preset memory pressure is used as the memory recovery ratio corresponding to the memory pressure.
  • Step S240 Perform memory recovery based on the memory recovery ratio.
  • the memory recovery method provided by this application can obtain the memory pressure representing the proportional relationship between the unreclaimed memory and the scanned memory in a historical time window when in a memory recovery scenario, where the memory pressure is a periodic historical time period Middle the memory pressure in the complete historical time period of the one closest to the current moment, then obtain the memory recovery ratio corresponding to the memory pressure, and perform memory recovery based on the memory recovery ratio. Therefore, when the memory recovery ratio corresponding to different memory pressures is different, the current memory pressure can be obtained at the beginning of the memory recovery, and then the current memory pressure can be used to determine how much to recover each of the various memory recovery objects, thereby improving The flexibility of the memory recovery process is improved.
  • a memory recovery method provided by an embodiment of the present application, the method includes:
  • Step S310 When in a memory recycling scene, acquire a memory pressure, where the memory pressure represents a proportional relationship between the unreclaimed memory and the scanned memory in the historical time window.
  • Step S320 Obtain a pre-configured memory recovery ratio corresponding to the memory pressure.
  • Step S330 Obtain a pre-calculated proportional correction parameter, the proportional correction parameter characterizing the recovery difficulty of the unreclaimed memory.
  • Step S340 Update the pre-configured memory recycling ratio based on the ratio correction parameter to obtain the memory recycling ratio corresponding to the memory pressure, and the memory recycling ratio represents the ratio between various memory recycling objects, wherein, The higher the memory pressure, the lower the proportion of memory recovery objects that need to consume CPU resources in the memory recovery ratio corresponding to the recovery.
  • the step of updating the pre-configured memory recycling ratio based on the ratio correction parameter to obtain the memory recycling ratio corresponding to the memory pressure includes: if the ratio correction parameter characterizes the use of unreclaimed memory For carrying the file system (it is understandable that because the file system needs to read files, the memory used to carry the file system during the memory recovery process cannot be recovered), reducing the need in the pre-configured memory recovery ratio The proportion of memory recycling objects that consume CPU resources to obtain the memory recycling ratio corresponding to the memory pressure; if the ratio correction parameter indicates that the unreclaimed memory is used for data storage, the pre-configured memory recycling ratio is used as the memory The memory recovery ratio corresponding to the pressure.
  • Step S350 Perform memory recovery based on the memory recovery ratio.
  • the memory recovery method provided by this application can obtain the memory pressure that characterizes the proportional relationship between the unreclaimed memory and the scanned memory in the historical time window when in a memory recovery scene, and then obtain the memory recovery corresponding to the memory pressure Ratio, memory recycling is performed based on the memory recycling ratio. Therefore, when the memory recovery ratio corresponding to different memory pressures is different, the current memory pressure can be obtained at the beginning of the memory recovery, and then the current memory pressure can be used to determine how much to recover each of the various memory recovery objects, thereby improving The flexibility of the memory recovery process is improved.
  • the memory recovery ratio obtained according to the memory pressure is the pre-configured memory recovery ratio. After the pre-configured memory recovery ratio is obtained, it will be further based on the current characterization of the difficulty of recovering the unreclaimed memory.
  • the ratio correction parameter updates the pre-configured memory recycling ratio to obtain the memory recycling ratio actually corresponding to the memory pressure.
  • a memory recovery method provided by an embodiment of the present application, the method includes:
  • Step S410 when in a memory recycling scene, acquire a memory pressure, where the memory pressure represents a proportional relationship between the unreclaimed memory and the scanned memory in the historical time window;
  • Step S420 Obtain a memory recovery ratio corresponding to the memory pressure, where the memory recovery ratio represents the ratio between a variety of memory recovery objects, where the higher the memory pressure corresponds to the memory recovery ratio that needs to be consumed during recovery The lower the proportion of the memory recycling objects of CPU resources;
  • Step S430 Perform memory recovery based on the memory recovery ratio.
  • Step S440 Obtain the memory recovery target corresponding to the current memory recovery scenario
  • Step S450 Determine the respective memory recovery targets corresponding to the various memory recovery objects based on the memory recovery ratio
  • Step S460 If there is a memory recovery target corresponding to a single memory recovery object, which is greater than the current reclaimable memory amount of the single memory recovery object, obtain a memory difference value, where the memory difference value is the memory recovery corresponding to the single memory recovery object The difference between the target and the current amount of reclaimable memory.
  • the memory recovery target corresponding to the current memory recovery scenario is obtained, and the object that has not completed the memory recovery target is the pending memory recovery Object. Then calculate the difference between the memory recovery target corresponding to the to-be-processed memory recovery object and the current reclaimable memory amount as the aforementioned memory difference, and then further recover the memory corresponding to the memory difference from other memory recovery objects. In order to not only perform memory recovery according to the established memory recovery ratio, but also complete the established memory recovery goal.
  • Step S470 Add a memory recovery target corresponding to a memory recovery object other than the single memory recovery object to meet the memory recovery target corresponding to the current memory recovery scenario.
  • the configuration of multiple memory recycling methods is mainly to improve the efficiency of memory recycling.
  • the recovery costs of different memory recovery objects are different.
  • the increase is the amount of recycling of memory recycling objects with lower memory recycling efficiency, it may not be conducive to improving the overall recycling efficiency.
  • the multiple memory recovery objects include anonymous pages and file pages
  • the method further includes: if a memory recovery target is added The memory reclamation object of is the file page; the memory reclamation target corresponding to the memory reclamation object other than the single memory reclamation object is added; if the memory reclamation object of the memory reclamation target is increased, the anonymous page; the single memory reclamation object is not added The memory recovery target corresponding to the memory recovery object other than that.
  • the overall memory recovery efficiency can be improved.
  • a memory recovery device 500 provided by an embodiment of the present application, the device 500 includes:
  • the memory pressure acquiring unit 510 is configured to acquire the memory pressure when in a memory recovery scenario, where the memory pressure represents the proportional relationship between the unreclaimed memory and the scanned memory in the historical time window.
  • the memory pressure acquiring unit 510 is specifically configured to use the complete historical time period of the one closest to the current moment in the periodic historical time period as the designated time period; and obtain the stored memory pressure corresponding to the designated time period. .
  • the memory pressure acquiring unit 510 is specifically configured to acquire the mapping relationship between a plurality of preset memory pressures and their respective corresponding memory recovery ratios; and the memory pressure and the plurality of The preset memory pressure is matched; the memory recovery ratio corresponding to the preset memory pressure matching the memory pressure is used as the memory recovery ratio corresponding to the memory pressure.
  • the recovery parameter acquisition unit 520 is configured to acquire a memory recovery ratio corresponding to the memory pressure, where the memory recovery ratio represents the ratio between a variety of memory recovery objects, where the higher the memory pressure corresponds to the memory recovery ratio The lower the proportion of memory recovery objects that need to consume CPU resources during recovery.
  • the recovery parameter acquisition unit 520 is specifically configured to acquire a pre-configured memory recovery ratio corresponding to the memory pressure; acquire a pre-calculated ratio correction parameter, the ratio correction parameter characterizing the recovery of the unreclaimed memory Difficulty; updating the pre-configured memory recycling ratio based on the ratio correction parameter to obtain the memory recycling ratio corresponding to the memory pressure.
  • the recovery parameter acquisition unit 520 is specifically configured to reduce the memory recovery object that needs to consume CPU resources in the pre-configured memory recovery ratio if the ratio correction parameter indicates that the unreclaimed memory is used to carry the file system.
  • the proportion, the memory recovery ratio corresponding to the memory pressure is obtained; if the ratio correction parameter indicates that the unreclaimed memory is used for data storage, the pre-configured memory recovery ratio is used as the memory recovery ratio corresponding to the memory pressure.
  • the memory recycling unit 530 is configured to perform memory recycling based on the memory recycling ratio.
  • the device 500 further includes:
  • the memory incremental recovery unit 540 is configured to obtain the memory recovery target corresponding to the current memory recovery scenario; determine the memory recovery target corresponding to each of the various memory recovery objects based on the memory recovery ratio; if there is a memory corresponding to a single memory recovery object The recovery target is greater than the current reclaimable memory amount of the single memory reclaimed object, and the memory difference is obtained, where the memory difference is the difference between the memory reclaim target corresponding to the single memory reclaimed object and the current reclaimable memory amount Value; increase the memory recovery target corresponding to the memory recovery object other than the single memory recovery object to meet the memory recovery target corresponding to the current memory recovery scenario.
  • the multiple types of memory recycling objects include anonymous pages and file pages.
  • the memory incremental recovery unit 540 is also used to increase the memory recovery target of the memory recovery target as a file page; execute the increase of the memory recovery target corresponding to the memory recovery object other than the single memory recovery object; if the memory recovery target is increased
  • the memory recovery object is an anonymous page; the memory recovery target corresponding to the memory recovery object other than the single memory recovery object is not increased.
  • an embodiment of the present application also provides another electronic device 100 that can execute the foregoing memory recovery method.
  • the electronic device 100 includes one or more (only one is shown in the figure) a processor 102, a memory 104, and a network module 106 coupled to each other.
  • the memory 104 stores a program that can execute the content in the foregoing embodiment, and the processor 102 can execute the program stored in the memory 104.
  • the processor 102 may include one or more processing cores.
  • the processor 102 uses various interfaces and lines to connect various parts of the entire electronic device 100, and executes by running or executing instructions, programs, code sets, or instruction sets stored in the memory 104, and calling data stored in the memory 104.
  • the processor 102 may use at least one of digital signal processing (Digital Signal Processing, DSP), Field-Programmable Gate Array (Field-Programmable Gate Array, FPGA), and Programmable Logic Array (Programmable Logic Array, PLA).
  • DSP Digital Signal Processing
  • FPGA Field-Programmable Gate Array
  • PLA Programmable Logic Array
  • the processor 102 may be integrated with one or a combination of a central processing unit (CPU), a graphics processing unit (GPU), a modem, and the like.
  • the CPU mainly processes the operating system, user interface, and application programs;
  • the GPU is used for rendering and drawing of display content;
  • the modem is used for processing wireless communication. It can be understood that the above-mentioned modem may not be integrated into the processor 102, but may be implemented by a communication chip alone.
  • the memory 104 may include random access memory (RAM) or read-only memory (Read-Only Memory).
  • the memory 104 may be used to store instructions, programs, codes, code sets or instruction sets.
  • the memory 104 may include a program storage area and a data storage area.
  • a memory recovery device may be stored in the memory 104.
  • the memory recovery device may be the aforementioned device 500.
  • the storage program area can store instructions used to implement the operating system, instructions used to implement at least one function (such as touch function, sound playback function, image playback function, etc.), and instructions used to implement the following various method embodiments Wait.
  • the data storage area can also store data (such as phone book, audio and video data, chat record data) created by the terminal 100 during use.
  • the network module 106 is used to receive and send electromagnetic waves, and realize the mutual conversion between electromagnetic waves and electrical signals, so as to communicate with a communication network or other equipment, such as communicating with an audio playback device.
  • the network module 106 may include various existing circuit elements for performing these functions, for example, an antenna, a radio frequency transceiver, a digital signal processor, an encryption/decryption chip, a subscriber identity module (SIM) card, a memory, etc. .
  • SIM subscriber identity module
  • the network module 106 can communicate with various networks, such as the Internet, an intranet, and a wireless network, or communicate with other devices through a wireless network.
  • the aforementioned wireless network may include a cellular telephone network, a wireless local area network, or a metropolitan area network.
  • the network module 106 can exchange information with the base station.
  • FIG. 10 shows a structural block diagram of a computer-readable storage medium provided by an embodiment of the present application.
  • the computer-readable medium 800 stores program code, and the program code can be invoked by a processor to execute the method described in the foregoing method embodiment.
  • the computer-readable storage medium 800 may be an electronic memory such as flash memory, EEPROM (Electrically Erasable Programmable Read Only Memory), EPROM, hard disk, or ROM.
  • the computer-readable storage medium 800 includes a non-transitory computer-readable storage medium.
  • the computer-readable storage medium 800 has storage space for the program code 810 for executing any method steps in the above-mentioned methods. These program codes can be read from or written into one or more computer program products.
  • the program code 810 may be compressed in a suitable form, for example.
  • the memory recovery method, device, electronic device, and storage medium provided by this application can obtain the memory pressure that characterizes the ratio of unreclaimed memory to the scanned memory in the historical time window when in a memory recovery scenario, and then obtain the The memory recovery ratio corresponding to the memory pressure is used for memory recovery based on the memory recovery ratio. Therefore, when the memory recovery ratio corresponding to different memory pressures is different, the current memory pressure can be obtained at the beginning of the memory recovery, and then the current memory pressure can be used to determine how much to recover each of the various memory recovery objects, thereby improving The flexibility of the memory recovery process is improved.

Abstract

本申请实施例公开了一种内存回收方法、装置、电子设备及存储介质。所述方法包括:当处于内存回收场景时,获取内存压力,所述内存压力表征历史时间窗口内未回收内存与扫描到的内存的比例关系;获取与所述内存压力对应的内存回收比例,所述内存回收比例表征多种内存回收对象之间的比例,其中,所述内存压力越高所对应的内存回收比例中回收时需要消耗CPU资源的内存回收对象的比重越低;基于所述内存回收比例进行内存回收。从而使得在不同内存压力对应的内存回收比例不同的情况下,可以在内存回收开始时先获取当前的内存压力,然后根据当前的内存压力来确定对于多种内存回收对象该各自回收多少,进而提升了内存回收过程的灵活性。

Description

内存回收方法、装置、电子设备及存储介质
相关申请的交叉引用
本申请要求于2019年9月27日提交的申请号为201910927294.5的中国申请的优先权,其在此出于所有目的通过引用将其全部内容并入本文。
技术领域
本申请涉及计算机技术领域,更具体地,涉及一种内存回收方法、装置、电子设备及存储介质。
背景技术
内存又称主存,是CPU能直接寻址的存储空间,由半导体器件制成。电子设备中的内存的空间是有限的,为了保证系统有足够的内存可以使用,电子设备的系统通常会在一定的情况下进行内存回收。但是,在相关的内存回收方式中,还缺乏一定的灵活性。
发明内容
鉴于上述问题,本申请提出了一种内存回收方法、装置、电子设备及存储介质,以改善上述问题。
第一方面,本申请提供了一种内存回收方法,所述方法包括:当处于内存回收场景时,获取内存压力,所述内存压力表征历史时间窗口内未回收内存与扫描到的内存的比例关系;获取与所述内存压力对应的内存回收比例,所述内存回收比例表征多种内存回收对象之间的比例,其中,所述内存压力越高所对应的内存回收比例中回收时需要消耗CPU资源的内存回收对象的比重越低;基于所述内存回收比例进行内存回收。
第二方面,本申请提供了一种内存回收装置,所述装置包括:内存压力获取单元,用于当处于内存回收场景时,获取内存压力,所述内存压力表征历史时间窗口内未回收内存与扫描到的内存的比例关系;回收参数获取单元,用于获取与所述内存压力对应的内存回收比例,所述内存回收比例表征多种内存回收对象之间的比例,其中,所述内存压力越高所对应的内存回收比例中回收时需要消耗CPU资源的内存回收对象的比重越低;内存回收单元,用于基于所述内存回收比例进行内存回收。
第三方面,本申请提供了一种电子设备,包括一个或多个处理器以及存储器;一个或多个程序被存储在所述存储器中并被配置为由所述一个或多个处理器执行以实现上述的方法。
第四方面,本申请提供了一种计算机可读存储介质,所述计算机可读存储介质中存储有程序代码,其中,在所述程序代码被处理器运行时执行上述的方法。
附图说明
为了更清楚地说明本申请实施例中的技术方案,下面将对实施例描述中所需要使用的附图作简单地介绍,显而易见地,下面描述中的附图仅仅是本申请的一些实施例,对于本领域技术人员来讲,在不付出创造性劳动的前提下,还可以根据这些附图获得其他的附图。
图1示出了本申请一实施例提出的一种内存回收方法的流程图。
图2示出了本申请另一实施例提出的一种内存回收方法的流程图。
图3示出了本申请一实施例提出的一种内存回收方法中周期性的多个时间段的示意图。
图4示出了本申请一实施例提出的一种内存回收方法中周期性的多个时间段中应用程序运行的示意图。
图5示出了本申请再一实施例提出的一种内存回收方法的流程图。
图6示出了本申请又一实施例提出的一种内存回收方法的流程图。
图7示出了本申请一实施例提出的一种内存回收装置的结构框图。
图8示出了本申请再一实施例提出的一种内存回收装置的结构框图。
图9示出了本申请实时中的用于执行根据本申请实施例的内存回收方法的电子设备的结构框图。
图10示出了本申请实时中的用于保存或者携带实现根据本申请实施例的内存回收方法的程序代码的存储单元。
具体实施方式
下面将结合本申请实施例中的附图,对本申请实施例中的技术方案进行清楚、完整地描述,显然,所描述的实施例仅仅是本申请一部分实施例,而不是全部的实施例。基于本申请中的实施例,本领域普通技术人员在没有作出创造性劳动前提下所获得的所有其他实施例,都属于本申请保护的范围。
对于操作系统而言,因为内存本身的大小是有限的,所以内存回收是一个较为普遍的机制。而之所以要进行内存回收,主要原因有两个:系统需要为任何时刻突发到来的内存申请提供足够的内存,以便cache的使用和其他相关内存的使用不至于让系统的剩余内存长期处于很少的状态。还有,当有大于空闲内存的申请到来的时候,也会触发内存回收。
而发明人在对相关的内存回收过程进行研究后发现,相关的内存回收方式中,还缺乏一定的灵活性。例如,以Android操作系统为例。在Android操作系统中,主要存在有两种内存回收机制。一种是基于kswapd进程的回收机制,在这种回收机制中,kswapd进程定期在后台运行。另一种是基于direct reclaim进程的回收机制,在这种回收机制中,direct reclaim进程在接收到内存分配需求时,开始进行内存回收。因此,可以发现相关的内存方式的内存回收参数较为固定。
因此,发明人在研究内存回收的过程中,对表征历史时间窗口内未回收内存与扫描到的内存的比例关系的内存压力进行了研究。发明人提出了本申请中的在不同内存压力对应的内存回收比例不同的情况下,可以在内存回收开始时先获取当前的内存压力,然后根据当前的内存压力来确定对于多种内 存回收对象该各自回收多少,进而提升了内存回收过程的灵活性的内存回收方法、装置、电子设备及存储介质。
下面将结合附图具体描述本申请的各实施例。
请参阅图1,示出了本申请实施例提供的一种内存回收方法,所述方法包括:
步骤S110:当处于内存回收场景时,获取内存压力,所述内存压力表征历史时间窗口内未回收内存与扫描到的内存的比例关系。
在本申请实施例中,可以有多种方式来检测当前是否处于内存回收场景。
其中,作为一种方式,可以在检测到有负责进行内存回收的进程开始运行时,判定当前进入内存回收场景。例如,若本实施例所运行的环境为Android操作系统中,那么可以在检测到有kswapd进程或者direct reclaim进程运行时,判定当前进入内存回收场景。
可以理解的是,本实施例中的内存压力为表征历史时间窗口内未回收内存与扫描到的内存的比例关系。其中,电子设备在进行内存压力的计算时可以周期性的进行,而其中的一个周期可以理解为前述的一个时间窗口。需要说明的是,对于内存空间中不同的内存区域所存储的内容有可能是不同的,而其中不同的内存区域并不一定是在扫描到的时候就可以直接被回收,还有就是并不是只要扫描到的内存都能进行回收。可选的,内存压力可以通过(1-reclaimed/scanned)*100计算得到,其中reclaimed表征未回收内存,scanned表征扫描到的内存。
其中,作为一种方式,时间窗口的长度可以根据实际情况进行调整。
可选的,电子设备可以根据处于运行状态的应用程序的数量来调整时间窗口的长度。需要说明的是,电子设备在运行的过程中会因为处理数据的需要临时再申请分配内存,那么当有较多的应用程序在运行时,相对会有更多的可能申请内存分配,并且也会相对申请分配更多的内存。那么在这种情况下,可选的,电子设备可以定期获取当前处于运行状态的应用程序的数量,将应用程序的数量与多个区间段进行匹配,进而将匹配的区间段所对应的时间长度作为当前的时间窗口的长度。例如,电子设备对于处于运行状态的应用程序的数量配置的区间段包括区间段A、区间段B、区间段C以及区间段D。其中,区间段A对应时间窗口A,区间段B对应时间窗口B,区间段C对应时间窗口C,区间段D对应时间窗口D。那么若识别当前运行的应用程序的数量刚好与区间段B匹配,那么电子设备可以将时间窗口B作为当前的时间窗口。
步骤S120:获取与所述内存压力对应的内存回收比例,所述内存回收比例表征多种内存回收对象之间的比例,其中,所述内存压力越高所对应的内存回收比例中回收时需要消耗CPU资源的内存回收对象的比重越低。
可以理解的是,对于不同的内存回收对象因为其本身特性的不同,在被进行回收时所需要消耗的CPU资源是不同的。例如,对于匿名页这种内存回收对象,其在被回收时是需要进行压缩然后存放在RAM中的,所以是需要消耗一定的CPU资源的。所以为了整体提升电子设备的流畅性,在内存压力较大的状态下,就尽量降低再去对CPU资源的占用,进而就降低需要消耗CPU资源的内存回收对象的回收量,从而实现降低内存回收比例中回收时需要消耗CPU资源的 内存回收对象的比重。
作为一种方式,在电子设备中预先存储有多个内存压力与对应的内存回收比例之间的映射关系。那么在电子设备获取到内存压力后,可以将获取到的内存压力与前述的多个预设内存压力进行匹配,进而将多个预设内存压力中与所述获取到的内存压力匹配的内存压力所对应的内存回收比例作为后续进行内存回收时所采用的内存回收比例。例如,在电子设备中存储的多个内存压力包括有内存压力A、内存压力B、内存压力C、内存压力D。其中,内存压力A对应有内存回收比例0:200,内存压力B对应有内存回收比例40:160,内存压力C对应有内存回收比例80:120,内存压力D对应有内存回收比例200:0。那么在获取到内存压力X后,将内存压力X与前述的内存压力A、内存压力B、内存压力C以及内存压力D进行匹配,若内存压力X与其中的内存压力C匹配,那么对应的内存回收比例为80:120。
需要说明的是,在进行内存压力匹配的时候,为了提升匹配的成功率,允许有一定的匹配误差。例如,若前述的内存压力X与内存压力C之间存在一定的匹配误差,电子设备也可以判定内存压力X与其中的内存压力C匹配。
步骤S130:基于所述内存回收比例进行内存回收。
本申请提供的一种内存回收方法,可以在处于内存回收场景时,获取表征历史时间窗口内未回收内存与扫描到的内存的比例关系的内存压力,然后获取与所述内存压力对应的内存回收比例,基于所述内存回收比例进行内存回收。从而使得在不同内存压力对应的内存回收比例不同的情况下,可以在内存回收开始时先获取当前的内存压力,然后根据当前的内存压力来确定对于多种内存回收对象该各自回收多少,进而提升了内存回收过程的灵活性。
请参阅图2,示出了本申请实施例提供的一种内存回收方法,所述方法包括:
步骤S210:当处于内存回收场景时,将周期性的历史时间段中距离当前时刻最近的一个的完整的历史时间段作为指定时间段。
步骤S220:获取所述指定时间段对应存储的内存压力,所述内存压力表征历史时间窗口内未回收内存与扫描到的内存的比例关系。
可选的,周期性的时间段中对于每个时间段都会有开始时间以及结束时间。例如,前述的图3所示的多个时间段中,在0到t1时间段,开始时间为0而结束时间为t1,再者,在t2到t3这个时间段中,开始时间为t2,而结束时间为t3。并且,对于每个时间段对应的内存压力所反应的都是各个时间段内的内存压力情况,而在一些情况下,内存压力变化是比较频繁的。如果用前一个时间段的内存压力来计算得到本时间段中的内存分配比例,可以在一定程度上反应实际的内存的使用情况,但是还不能很好的反应当前实时的情况。
针对上述内容,需要先说明的是,在一些时间段中内存压力是很相似的。而相似的内存压力,可能是因为所运行的应用程序比较相似或者相同。例如,请参阅图4,在t1到t2这个时间段中有应用程序A、应用程序B以及应用程序C在运行,而对应的在t4到t5这个时间段中也有应用程序A、应用程序B以及应用程序C在运行,那么可以理解的是t4到t5这个时间段为与t1到t2这个时间段相同或者相似的时间段。
那么作为一种方式,所述获取指定时间段对应存储的内存压力的步骤包括:获取当前的应用程序特征参数,所述应用程序特征参数表征应用程序对CPU处理资源的占用程度;基于所述应用程序特征参数计算得到与当前时刻所处的时间段对应的目标历史时间段作为指定时间段;获取存储的所述指定时间段内的内存压力。
在这种方式下,可选的,电子设备可以在每个时间段开始时刻以及结束时刻分别计算一次本时间段对应的应用程序特征参数。其中,可以理解的是,在时间段的开始时刻检测到的应用程序可以是对本时间段会有哪些应用程序进行运行的一个预估,以便可以更加快速的计算得到相同的目标时间段,而当在时间段的结束时刻检测到的应用程序是可以明确的确认本时间段实际有哪些应用程序在运行。所以在本时间段计算得到本时间段对应的应用程序特征参数可以用于后续的时间段比对相同的目标时间段,而在每个时间段的开始时刻计算当前的应用程序特征参数,可以用于在当前时间段还未结束之前就可以预估与当前时间段相同的目标时间段,以便可以更加快速及时的获取对应的内存回收比例。
其中,可选的,在计算当前的应用程序特征参数时,可以为应用程序当前启动的线程数与CPU可以并行处理的线程的总数的比值,其中应用程序特征参数越高表征对CPU的占用程度越高。例如,当前有应用程序A、应用程序B、应用程序C以及应用程序D在运行,而其中应用程序A对应有a个线程在运行,应用程序B对应有b个线程在运行,应用程序C对应有c个线程在运行,应用程序D对应有d个线程在运行。在CPU可以并行处理的线程的总数为m的情况下,对应的当前的应用程序特征参数为(a+b+c+d)/m。
可选的,在每个时间段结束时刻计算应用程序特征参数时是将当前时间段所有应用程序的平均线程数与CPU可以并行处理的线程的总数的比值。例如,在某个时间段内有应用程序B、应用程序C以及应用程序D在运行,而其中应用程序B运行过t个线程,应用程序C运行过k个线程,应用程序D运行过l个线程。那么该时间段的应用程序特征参数为(t+k+l)/3/m。
可选的,所述基于所述应用程序特征参数计算得到与当前时刻所处的时间段对应的目标历史时间段作为指定时间段的步骤包括:将所述当前的应用程序特征参数与多个历史时间段的应用程序特征参数进行比对;将对应的应用程序特征参数与所述当前的应用程序特征参数相同的历史时间段作为对应的目标历史时间段;将所述目标历史时间段作为指定时间段。
可选的,所述方法还包括:获取每个时间段内运行的应用程序的数量以及优先级,其中,优先级越高所对应的历史运行过程中所占用的内存越高;获取与所述数量对应的第一评分;获取与所述优先级对应的第二评分;基于所述第一评分以及对应的第一预设权重,以及所述第二评分以及对应的第二预设权重计算得到每个时间段对应的应用程序特征参数。
步骤S230:获取与所述内存压力对应的内存回收比例,所述内存回收比例表征多种内存回收对象之间的比例,其中,所述内存压力越高所对应的内存回收比例中回收时需要消耗CPU资源的内存回收对象的比重越低。
再者,需要说明的是,电子设备中有可能会有多种内存回收场景,而对于不同的内存回收场景,所需要回收的内存可能是不同的。例如,对于kswapd进程运行时的内存回收场景中,因为kswapd进程是定期在后台运行,那么主要以回收匿名页为主。而对于direct reclaim进程运行时的内存回收场景中,因为direct reclaim进程可以在内存分配时运行,那么主要以回收文件页为主。
在本申请实施例中,可以基于多种方式来确认当前的内存回收场景。
作为一种方式,可以识别当前进行内存回收的进程的标识;根据所述标识确定当前的内存回收场景。可以理解的是,在电子设备中进行内存回收都是由特定的进程执行的。例如,前述的有kswapd进程或者direct reclaim进程。那么在这种方式下,可以周期性的检测当前有哪些进程处于运行状态,当检测到有特定的进行内存回收的进程的标识为正在运行的进程标识时,判定处于该标识所对应的内存回收场景。例如,若识别当前进行内存回收的进程的标识为“kswapd”,那么对应的识别当前为kswapd内存回收场景。若识别当前进行内存回收的进程的标识为“direct reclaim”,那么对应的识别当前为direct reclaim内存回收场景。
作为另外一种方式,也可以根据当前所运行的应用程序来确定当前的内存回收场景。
需要说明的是,对于一些应用程序在运行过程中,对于内存消耗是有一定的需求的,那么针对不同的应用程序来确定不同的内存回收场景,可以更好的对应用程序流畅运行带来有利的条件。例如,对于一些应用程序在运行的过程中可能频繁的需要申请内存。例如,对于短视频类的应用程序,可能会增量性的申请内存分配来缓存所请求的短视频。例如,对于即时通信类的应用程序,可能会在运行中检测到进行视频通话时申请分配内存来缓存视频图像数据。再例如,对于一些应用程序本身在启动的时候,就会申请较多的内存。
那么在这种方式下,可选的,当检测到有进行内存回收的进程启动后,会进一步的检测当前有哪些应用程序处于运行状态,然后根据当前处于运行状态的应用程序来确定当前的内存回收场景,以便进而确定内存回收比例。
此外,作为另外一种方式,还可以结合当前处于运行状态的进程标识以及当前处于运行状态的应用程序共同来确定当前的内存回收场景。
如前述内容所示,在本申请实施例中可以有多种方式来确定当前的内存回收场景,而确定的不同的内存回收场景各自对应的内存回收比例是会有所不同的,以便于可以适应不同的场景各自的内存回收需求。示例性的,在实施例中所提及的内存回收对象可以包括匿名页(Anonymous page)以及文件页(File page)。其中,匿名页可以理解为没有文件背景的页,如堆,栈,数据段等,不是以文件形式存在的。文件页可以理解为有文件背景的页,例如,对于文件页所存储的数据可以在磁盘中找到对应的数据。
而其中,对于回收匿名页以及回收文件页的代价是不同的。在回收匿名页的过程中,是将匿名页压缩后存放在RAM中,由于该过程需要使用CPU将匿名页压缩,使用时再解压缩,所以匿名页的回收过程需要消耗一定的CPU时间,相对较慢。对于文件页的回收分两种情况,如果文件页对应的文件内容没有被 修改过,则直接将文件页内容丢弃;而如果文件页对应的文件内容被修改了,则将文件内容写回磁盘再将文件页丢弃。对于被回收的文件页的文件内容再次使用时,需要时重新从磁盘读取。一般而言,很多文件页是没有被修改的,所以回收过程相对较快。
其中,本实施例中主要以检测在前台运行的应用程序为主。以电子设备为Android操作系统为例,可以通过执行ActivityManager的getRunningTasks方法来获取到当前在前台运行的应用程序的名称。此外,电子设备还可以通过UsageStatsManager来获取用户使用的程序的列表,将该列表中记录的最近使用的应用程序识别为当前的前台应用。再者,还可以通过Android自带无障碍功能,监控窗口焦点的变化,拿到焦点窗口对应包名作为当前在前台运行的应用程序。
下面就主要以匿名页以及文件页作为内存回收对象对本实施例所提供的方法进行介绍。其中,本实施例中的比例可以理解为匿名页与文件页之间的比例。
对于识别当前进行内存回收的进程的标识,然后根据所述标识确定当前的内存回收场景的这种方式中,可以预先配置每种内存回收进程进行内存回收时,对匿名页以及文件页的回收比例,以作为在每种内存回收场景下内存压力所对应的内存回收比例。进而,当检测到有内存回收进程开始运行时,对应的获取该内存回收进程的标识,然后按照该内存回收进程的标识查询对应的匿名页以及文件页的回收比例,以作为当前内存回收场景的内存压力所对应的内存回收比例。
标识 比例
direct reclaim 0:200
kswapd 200:0
如上表所示,标识“direct reclaim”对应的比例为“0:200”,表征仅回收文件页,而不回收匿名页。标识“kswapd”对应的比例为“200:0”,表征仅回收匿名页,而不回收文件页。当然,其中的比例还可以根据需要进行改变,例如,对于标识为“direct reclaim”的进程对应的比例还可以为50:150,以表征尽量回收文件页,且少量回收匿名页。
再者,在根据当前所运行的应用程序来确定当前的内存回收场景的这种方式中,可以预先建立应用程序与内存回收比例的对应关系。进而在检测到有内存回收进程开始运行后,获取当前正在运行的应用程序,然后查询该正在运行的应用程序对应的比例作为当前内存回收场景的内存压力所对应的内存回收比例。
应用程序标识 比例
应用程序A 10:190
应用程序B 20:180
应用程序C 30:170
如上表所示,每个应用程序标识都各自对应有一个内存回收比例。其中文件页比重越高,表征越需要更快的内存回收效率。那么在检测到有进行内存回 收的进程开始运行后,如果检测到当前有应用程序A在运行,那么就对应的,将应用程序A对应的比例“10:190”作为内存回收比例。如果检测到当前有应用程序B在运行,那么就对应的,将应用程序B对应的比例“20:180”作为内存回收比例。
需要说明的是,在一种方式中,可以结合进程本身对应的比例以及当前处于运行状态的应用程序的比例共同确定当前场景对应的比例。例如,在前述表中进程标识“direct reclaim”对应的比例为“0:200”,而若检测到应用程序A当前处于运行状态时,该应用程序A对应的比例为“10:190”,那么当同时检测到进程“direct reclaim”处于运行状态,且检测到应用程序A在运行时,可以将处于运行状态的应用程序对应的比例与当前处于运行状态的负载内存回收的进程的标识对应的比例取均值作为当前内存回收场景的内存回收比例,从而使得既可以在一定程度上满足进程对应的内存回收比例,也能够在一定程度上满足应用程序对于内存回收比例的需求。例如,在前述表中进程标识“direct reclaim”对应的比例为“0:200”,而该应用程序A对应的比例为“10:190”,那么取均值后得到的比例为5:195。
可以理解的是,并不一定是任一一个应用程序都会对应一个比例。而可以只是被特定标记的应用程序会对应有一个比例。可选的,在电子设备中,可以通过建立名单的方式来对特定的应用程序进行标记。那么在这种方式下,为了便于可以快速对当前的应用程序是否为前述特定的应用程序(配置有内存回收比例的应用程序)进行判断,可选的,当有特定的应用程序启动以后,生成一个伴随文件,在该伴随文件中写入当前正在运行的特定的应用程序的标识。那么在这种方式下,当检测到有负责内存回收的进程启动以后,可以查询是否有伴随文件生成,若有,则将伴随文件中记录的应用程序对应的内存回收比例与当前启动的负责内存回收的进程对应的内存回收比例进行结合(例如,前述的取平均)来得到当前内存回收场景对应的内存回收比例。而若检测到没有伴随文件存在,则直接将负责内存回收的进程对应的内存回收比例作为当前内存回收场景对应的内存回收比例。
需要说明的是,对于通过检测应用程序来判定当前内存回收场景的方式中。还可以通过预判即将有哪些应用程序即将启动来确定当前的内存回收场景。例如,在一种方式中,电子设备的某些应用程序是定时启动的,例如,闹钟程序。或者有的应用程序会因为用户的使用习惯而在每天的某个时间段内惯例性的启动。例如,外卖类的应用程序,可能会在每天接近饭点的时候惯例性启动,再或者视频类的应用程序可能会在用户每天坐地铁的时候惯例性的被用户启动。
作为一种方式,所述获取与所述内存压力对应的内存回收比例的步骤包括:
获取表征多个预设内存压力与各自对应的内存回收比例之间的映射关系;按照指定的顺序将所述内存压力与所述多个预设内存压力进行匹配;将与所述内存压力匹配的预设内存压力所对应的内存回收比例,作为与所述内存压力对应的内存回收比例。
步骤S240:基于所述内存回收比例进行内存回收。
本申请提供的一种内存回收方法,可以在处于内存回收场景时,获取表征 历史时间窗口内未回收内存与扫描到的内存的比例关系的内存压力,其中该内存压力为周期性的历史时间段中距离当前时刻最近的一个的完整的历史时间段内的内存压力,然后获取与所述内存压力对应的内存回收比例,基于所述内存回收比例进行内存回收。从而使得在不同内存压力对应的内存回收比例不同的情况下,可以在内存回收开始时先获取当前的内存压力,然后根据当前的内存压力来确定对于多种内存回收对象该各自回收多少,进而提升了内存回收过程的灵活性。
请参阅图5,本申请实施例提供的一种内存回收方法,所述方法包括:
步骤S310:当处于内存回收场景时,获取内存压力,所述内存压力表征历史时间窗口内未回收内存与扫描到的内存的比例关系。
步骤S320:获取与所述内存压力对应的预配置的内存回收比例。
步骤S330:获取预先计算的比例修正参数,所述比例修正参数表征所述未回收内存的回收难度。
步骤S340:基于所述比例修正参数对所述预配置的内存回收比例进行更新,得到所述内存压力对应的内存回收比例,所述内存回收比例表征多种内存回收对象之间的比例,其中,所述内存压力越高所对应的内存回收比例中回收时需要消耗CPU资源的内存回收对象的比重越低。
作为一种方式,所述基于所述比例修正参数对所述预配置的内存回收比例进行更新,得到所述内存压力对应的内存回收比例的步骤包括:若所述比例修正参数表征未回收内存用于承载文件系统(可以理解的是,因为文件系统需要进行文件的读取,所以在内存回收过程中用于承载文件系统内存是无法进行回收的),降低所述预配置的内存回收比例中需要消耗CPU资源的内存回收对象的比重,得到所述内存压力对应的内存回收比例;若所述比例修正参数表征所述未回收内存用于数据存储,将所述预配置的内存回收比例作为述内存压力对应的内存回收比例。
步骤S350:基于所述内存回收比例进行内存回收。
本申请提供的一种内存回收方法,可以在处于内存回收场景时,获取表征历史时间窗口内未回收内存与扫描到的内存的比例关系的内存压力,然后获取与所述内存压力对应的内存回收比例,基于所述内存回收比例进行内存回收。从而使得在不同内存压力对应的内存回收比例不同的情况下,可以在内存回收开始时先获取当前的内存压力,然后根据当前的内存压力来确定对于多种内存回收对象该各自回收多少,进而提升了内存回收过程的灵活性。并且,在本实施例中,根据内存压力获取的内存回收比例为预配置的内存回收比例,在获取到预配置的内存回收比例后,会进一步的根据当前表征所述未回收内存的回收难度的比例修正参数对该预配置的内存回收比例进行更新得到与该内存压力实际对应的内存回收比例。
请参阅图6,本申请实施例提供的一种内存回收方法,所述方法包括:
步骤S410:当处于内存回收场景时,获取内存压力,所述内存压力表征历史时间窗口内未回收内存与扫描到的内存的比例关系;
步骤S420:获取与所述内存压力对应的内存回收比例,所述内存回收比例 表征多种内存回收对象之间的比例,其中,所述内存压力越高所对应的内存回收比例中回收时需要消耗CPU资源的内存回收对象的比重越低;
步骤S430:基于所述内存回收比例进行内存回收。
步骤S440:获取当前的内存回收场景所对应的内存回收目标;
步骤S450:基于所述内存回收比例确定多种内存回收对象各自对应的内存回收目标;
步骤S460:若存在单个内存回收对象对应的内存回收目标,大于所述单个内存回收对象当前可回收的内存量,获取内存差值,所述内存差值为所述单个内存回收对象对应的内存回收目标与所述当前可回收的内存量的差值。
可以理解的是,对于多种内存回收对象可以回收到的内存量是有一定限制的。例如,若当前内存中匿名页一共有200MB,那么最多回收到的匿名页的数据量也只有200MB。那么在这种情况下,若既配置有内存回收目标,且需要按照当前的内存回收场景进行内存回收,那么就可能会造成无法回收到预期的内存量。
那么在这种方式下,在以当前的内存回收场景对应的内存回收比例完成内存回收后,获取按照当前的内存回收场景所对应的内存回收目标,未完成内存回收目标的对象为待处理内存回收对象。进而计算该待处理内存回收对象对应的内存回收目标与所述当前可回收的内存量的差值,作为前述的内存差值,然后从其他内存回收对象中再进一步回收该内存差值对应的内存量,以便既可以按照既定的内存回收比例来执行内存回收,也可以完成既定的内存回收目标。
步骤S470:增加所述单个内存回收对象以外的内存回收对象对应的内存回收目标,以满足所述当前的内存回收场景所对应的内存回收目标。
示例性的,以匿名页和文件页为例,内存回收比例为50:150,且内存回收目标为100MB。那么可以理解的是,匿名页需要回收25MB,而文件页需要回收75MB。但是,匿名页当前可以回收的量只有15MB。因此,在按照比例进行回收之后实际只回收了75MB+15MB=90MB,那么可以理解的是,按照前述的内存差值计算规则,得到内存差值为25-15=10MB,那么对应的,会再回收10MB的文件页作为补充。
需要说明的是,根据前述的内存回收原则,配置多种内存回收方式主要是为了提升内存回收效率。而对于多个内存回收对象而言,不同的内存回收对象的回收代价是不同的。那么如果增加的反而是内存回收效率更低的内存回收对象的回收量,可能不利于整体提升回收效率。那么作为一种方式,所述多种内存回收对象包括匿名页以及文件页,所述增加所述单个内存回收对象以外的内存回收对象对应的内存回收目标的步骤之前还包括:若增加内存回收目标的内存回收对象为文件页;执行所述增加所述单个内存回收对象以外的内存回收对象对应的内存回收目标;若增加内存回收目标的内存回收对象为匿名页;不增加所述单个内存回收对象以外的内存回收对象对应的内存回收目标。从而通过这种方式,可以在整体上提升内存回收效率。
请参阅图7,本申请实施例提供的一种内存回收装置500,所述装置500包括:
内存压力获取单元510,用于当处于内存回收场景时,获取内存压力,所述内存压力表征历史时间窗口内未回收内存与扫描到的内存的比例关系。
作为一种方式,内存压力获取单元510,具体用于将周期性的历史时间段中距离当前时刻最近的一个的完整的历史时间段作为指定时间段;获取所述指定时间段对应存储的内存压力。
其中,可选的,内存压力获取单元510,具体用于获取表征多个预设内存压力与各自对应的内存回收比例之间的映射关系;按照指定的顺序将所述内存压力与所述多个预设内存压力进行匹配;将与所述内存压力匹配的预设内存压力所对应的内存回收比例,作为与所述内存压力对应的内存回收比例。
回收参数获取单元520,用于获取与所述内存压力对应的内存回收比例,所述内存回收比例表征多种内存回收对象之间的比例,其中,所述内存压力越高所对应的内存回收比例中回收时需要消耗CPU资源的内存回收对象的比重越低。
作为一种方式,回收参数获取单元520,具体用于获取与所述内存压力对应的预配置的内存回收比例;获取预先计算的比例修正参数,所述比例修正参数表征所述未回收内存的回收难度;基于所述比例修正参数对所述预配置的内存回收比例进行更新,得到所述内存压力对应的内存回收比例。
其中,可选的,回收参数获取单元520,具体用于若所述比例修正参数表征未回收内存用于承载文件系统,降低所述预配置的内存回收比例中需要消耗CPU资源的内存回收对象的比重,得到所述内存压力对应的内存回收比例;若所述比例修正参数表征所述未回收内存用于数据存储,将所述预配置的内存回收比例作为述内存压力对应的内存回收比例。
内存回收单元530,用于基于所述内存回收比例进行内存回收。
请参阅图8所示,所述装置500,还包括:
内存增量回收单元540,用于获取当前的内存回收场景所对应的内存回收目标;基于所述内存回收比例确定多种内存回收对象各自对应的内存回收目标;若存在单个内存回收对象对应的内存回收目标,大于所述单个内存回收对象当前可回收的内存量,获取内存差值,所述内存差值为所述单个内存回收对象对应的内存回收目标与所述当前可回收的内存量的差值;增加所述单个内存回收对象以外的内存回收对象对应的内存回收目标,以满足所述当前的内存回收场景所对应的内存回收目标。
可选的,所述多种内存回收对象包括匿名页以及文件页。内存增量回收单元540,还用于若增加内存回收目标的内存回收对象为文件页;执行所述增加所述单个内存回收对象以外的内存回收对象对应的内存回收目标;若增加内存回收目标的内存回收对象为匿名页;不增加所述单个内存回收对象以外的内存回收对象对应的内存回收目标。
需要说明的是,本申请中装置实施例与前述方法实施例是相互对应的,装置实施例中具体的原理可以参见前述方法实施例中的内容,此处不再赘述。
下面将结合图9对本申请提供的一种电子设备进行说明。
请参阅图9,基于上述的内存回收方法、装置,本申请实施例还提供的另一 种可以执行前述内存回收方法的电子设备100。电子设备100包括相互耦合的一个或多个(图中仅示出一个)处理器102、存储器104以及网络模块106。其中,该存储器104中存储有可以执行前述实施例中内容的程序,而处理器102可以执行该存储器104中存储的程序。
其中,处理器102可以包括一个或者多个处理核。处理器102利用各种接口和线路连接整个电子设备100内的各个部分,通过运行或执行存储在存储器104内的指令、程序、代码集或指令集,以及调用存储在存储器104内的数据,执行电子设备100的各种功能和处理数据。可选地,处理器102可以采用数字信号处理(Digital Signal Processing,DSP)、现场可编程门阵列(Field-Programmable Gate Array,FPGA)、可编程逻辑阵列(Programmable Logic Array,PLA)中的至少一种硬件形式来实现。处理器102可集成中央处理器(Central Processing Unit,CPU)、图像处理器(Graphics Processing Unit,GPU)和调制解调器等中的一种或几种的组合。其中,CPU主要处理操作系统、用户界面和应用程序等;GPU用于负责显示内容的渲染和绘制;调制解调器用于处理无线通信。可以理解的是,上述调制解调器也可以不集成到处理器102中,单独通过一块通信芯片进行实现。
存储器104可以包括随机存储器(Random Access Memory,RAM),也可以包括只读存储器(Read-Only Memory)。存储器104可用于存储指令、程序、代码、代码集或指令集。存储器104可包括存储程序区和存储数据区,例如,存储器104中可以存储有内存回收装置。该内存回收装置可以为前述的装置500。其中,存储程序区可存储用于实现操作系统的指令、用于实现至少一个功能的指令(比如触控功能、声音播放功能、图像播放功能等)、用于实现下述各个方法实施例的指令等。存储数据区还可以存储终端100在使用中所创建的数据(比如电话本、音视频数据、聊天记录数据)等。
所述网络模块106用于接收以及发送电磁波,实现电磁波与电信号的相互转换,从而与通讯网络或者其他设备进行通讯,例如和音频播放设备进行通讯。所述网络模块106可包括各种现有的用于执行这些功能的电路元件,例如,天线、射频收发器、数字信号处理器、加密/解密芯片、用户身份模块(SIM)卡、存储器等等。所述网络模块106可与各种网络如互联网、企业内部网、无线网络进行通讯或者通过无线网络与其他设备进行通讯。上述的无线网络可包括蜂窝式电话网、无线局域网或者城域网。例如,网络模块106可以与基站进行信息交互。
请参考图10,其示出了本申请实施例提供的一种计算机可读存储介质的结构框图。该计算机可读介质800中存储有程序代码,所述程序代码可被处理器调用执行上述方法实施例中所描述的方法。
计算机可读存储介质800可以是诸如闪存、EEPROM(电可擦除可编程只读存储器)、EPROM、硬盘或者ROM之类的电子存储器。可选地,计算机可读存储介质800包括非易失性计算机可读介质(non-transitory computer-readable storage medium)。计算机可读存储介质800具有执行上述方法中的任何方法步骤的程序代码810的存储空间。这些程序代码可以从一个或者多个计算机程序 产品中读出或者写入到这一个或者多个计算机程序产品中。程序代码810可以例如以适当形式进行压缩。
本申请提供的一种内存回收方法、装置、电子设备及存储介质,可以在处于内存回收场景时,获取表征历史时间窗口内未回收内存与扫描到的内存的比例关系的内存压力,然后获取与所述内存压力对应的内存回收比例,基于所述内存回收比例进行内存回收。从而使得在不同内存压力对应的内存回收比例不同的情况下,可以在内存回收开始时先获取当前的内存压力,然后根据当前的内存压力来确定对于多种内存回收对象该各自回收多少,进而提升了内存回收过程的灵活性。
最后应说明的是:以上实施例仅用以说明本申请的技术方案,而非对其限制;尽管参照前述实施例对本申请进行了详细的说明,本领域的普通技术人员当理解:其依然可以对前述各实施例所记载的技术方案进行修改,或者对其中部分技术特征进行等同替换;而这些修改或者替换,并不驱使相应技术方案的本质脱离本申请各实施例技术方案的精神和范围。

Claims (20)

  1. 一种内存回收方法,其特征在于,所述方法包括:
    当处于内存回收场景时,获取内存压力,所述内存压力表征历史时间窗口内未回收内存与扫描到的内存的比例关系;
    获取与所述内存压力对应的内存回收比例,所述内存回收比例表征多种内存回收对象之间的比例,其中,所述内存压力越高所对应的内存回收比例中回收时需要消耗CPU资源的内存回收对象的比重越低;
    基于所述内存回收比例进行内存回收。
  2. 根据权利要求1所述的方法,其特征在于,所述获取内存压力,包括:
    将周期性的历史时间段中距离当前时刻最近的一个的完整的历史时间段作为指定时间段;
    获取所述指定时间段对应存储的内存压力。
  3. 根据权利要求2所述的方法,其特征在于,所述获取所述指定时间段对应存储的内存压力,包括:
    获取当前的应用程序特征参数,所述应用程序特征参数表征应用程序对CPU处理资源的占用程度;
    基于所述应用程序特征参数计算得到与当前时刻所处的时间段对应的目标历史时间段作为指定时间段;
    获取存储的所述指定时间段内的内存压力。
  4. 根据权利要求3所述的方法,其特征在于,所述基于所述应用程序特征参数计算得到与当前时刻所处的时间段对应的目标历史时间段作为指定时间段,包括:
    将所述当前的应用程序特征参数与多个历史时间段的应用程序特征参数进行比对;
    将对应的应用程序特征参数与所述当前的应用程序特征参数相同的历史时间段作为对应的目标历史时间段;
    将所述目标历史时间段作为指定时间段。
  5. 根据权利要求3所述的方法,其特征在于,所述获取当前的应用程序特征参数,包括:
    获取应用程序当前启动的线程数与CPU并行处理的线程的总数的比值;
    将所述比值作为当前的应用程序特征参数,其中,应用程序特征参数越高表征对CPU的占用程度越高。
  6. 根据权利要求3所述的方法,其特征在于,所述获取当前的应用程序特征参数,包括:
    在每个时间段结束时刻获取当前时间段所有应用程序的平均线程数与CPU并行处理的线程的总数的比值;
    将所述比值作为当前的应用程序特征参数。
  7. 根据权利要求2-6任一项所述的方法,其特征在于,所述方法还包括:
    获取每个时间段内运行的应用程序的数量以及优先级,其中,优先级越高所对应的历史运行过程中所占用的内存越高;
    获取与所述数量对应的第一评分;
    获取与所述优先级对应的第二评分;
    基于所述第一评分以及对应的第一预设权重,以及所述第二评分以及对应的第二预设权重计算得到每个时间段对应的应用程序特征参数。
  8. 根据权利要求2所述的方法,其特征在于,所述获取与所述内存压力对应的内存回收比例,包括:
    获取表征多个预设内存压力与各自对应的内存回收比例之间的映射关系;
    按照指定的顺序将所述内存压力与所述多个预设内存压力进行匹配;
    将与所述内存压力匹配的预设内存压力所对应的内存回收比例,作为与所述内存压力对应的内存回收比例。
  9. 根据权利要求1所述的方法,其特征在于,所述获取与所述内存压力对应的内存回收比例之前,所述方法还包括:
    识别当前进行内存回收的进程的标识;
    根据所述标识确定当前的内存回收场景,不同的内存回收场景所需要回收的内存不同。
  10. 根据权利要求9所述的方法,其特征在于,所述获取与所述内存压力对应的内存回收比例,包括:
    获取预先配置的每种内存回收进程进行内存回收时,对匿名页以及文件页的回收比例;
    当检测到有内存回收进程开始运行时,获取所述内存回收进程的标识;
    按照所述标识查询对应的匿名页以及文件页的回收比例,作为当前内存回收场景的内存压力所对应的内存回收比例。
  11. 根据权利要求1所述的方法,其特征在于,所述获取与所述内存压力对应的内存回收比例之前,所述方法还包括:
    根据当前所运行的应用程序来确定当前的内存回收场景,不同的内存回收场景所需要回收的内存不同。
  12. 根据权利要求11所述的方法,其特征在于,所述获取与所述内存压力对应的内存回收比例,包括:
    获取预先建立的应用程序与内存回收比例的对应关系;
    在检测到有内存回收进程开始运行后,获取当前正在运行的应用程序;
    查询所述正在运行的应用程序对应的比例,作为当前内存回收场景的内存压力所对应的内存回收比例。
  13. 根据权利要求1所述的方法,其特征在于,所述获取与所述内存压力对应的内存回收比例,包括:
    获取与所述内存压力对应的预配置的内存回收比例;
    获取预先计算的比例修正参数,所述比例修正参数表征所述未回收内存的回收难度;
    基于所述比例修正参数对所述预配置的内存回收比例进行更新,得到所述内存压力对应的内存回收比例。
  14. 根据权利要求13所述的方法,其特征在于,所述基于所述比例修正参数对所述预配置的内存回收比例进行更新,得到所述内存压力对应的内存回收比例,包括:
    若所述比例修正参数表征所述未回收内存用于承载文件系统,降低所述预配置的内存回收比例中需要消耗CPU资源的内存回收对象的比重,得到所述内存压力对应的内存回收比例;
    若所述比例修正参数表征所述未回收内存用于数据存储,将所述预配置的内存回收比例作为述内存压力对应的内存回收比例。
  15. 根据权利要求1所述的方法,其特征在于,所述基于所述内存回收比例进行内存回收之后,还包括:
    获取当前的内存回收场景所对应的内存回收目标;
    基于所述内存回收比例确定多种内存回收对象各自对应的内存回收目标;
    若存在单个内存回收对象对应的内存回收目标,大于所述单个内存回收对象当前可回收的内存量,获取内存差值,所述内存差值为所述单个内存回收对象对应的内存回收目标与所述当前可回收的内存量的差值;
    增加所述单个内存回收对象以外的内存回收对象对应的内存回收目标,以满足所述当前的内存回收场景所对应的内存回收目标。
  16. 根据权利要求15所述的方法,其特征在于,所述多种内存回收对象包括匿名页以及文件页,所述增加所述单个内存回收对象以外的内存回收对象对应的内存回收目标之前,还包括:
    若增加内存回收目标的内存回收对象为文件页;
    执行所述增加所述单个内存回收对象以外的内存回收对象对应的内存回收目标;
    若增加内存回收目标的内存回收对象为匿名页;
    不增加所述单个内存回收对象以外的内存回收对象对应的内存回收目标。
  17. 根据权利要求1所述的方法,其特征在于,所述当处于内存回收场景时,获取内存压力之前,所述方法还包括:
    在检测到有负责进行内存回收的进程开始运行时,判定进入内存回收场景。
  18. 一种内存回收装置,其特征在于,所述装置包括:
    内存压力获取单元,用于当处于内存回收场景时,获取内存压力,所述内存压力表征历史时间窗口内未回收内存与扫描到的内存的比例关系;
    回收参数获取单元,用于获取与所述内存压力对应的内存回收比例,所述内存回收比例表征多种内存回收对象之间的比例,其中,所述内存压力越高所对应的内存回收比例中回收时需要消耗CPU资源的内存回收对象的比重越低;
    内存回收单元,用于基于所述内存回收比例进行内存回收。
  19. 一种电子设备,其特征在于,包括一个或多个处理器以及存储器;一个或多个程序被存储在所述存储器中并被配置为由所述一个或多个处理器执行权利要求1-17任一所述的方法。
  20. 一种计算机可读存储介质,其特征在于,所述计算机可读存储介质中存储有程序代码,其中,在所述程序代码被处理器运行时执行权利要求1-17任一所述的方法。
PCT/CN2020/116212 2019-09-27 2020-09-18 内存回收方法、装置、电子设备及存储介质 WO2021057619A1 (zh)

Priority Applications (2)

Application Number Priority Date Filing Date Title
EP20868443.1A EP3979082A4 (en) 2019-09-27 2020-09-18 METHOD AND APPARATUS FOR RECOVERING MEMORY, ELECTRONIC DEVICE AND STORAGE MEDIA
US17/564,794 US20220121495A1 (en) 2019-09-27 2021-12-29 Memory reclamation method, electronic device and storage medium

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN201910927294.5 2019-09-27
CN201910927294.5A CN110727606A (zh) 2019-09-27 2019-09-27 内存回收方法、装置以及电子设备

Related Child Applications (1)

Application Number Title Priority Date Filing Date
US17/564,794 Continuation US20220121495A1 (en) 2019-09-27 2021-12-29 Memory reclamation method, electronic device and storage medium

Publications (1)

Publication Number Publication Date
WO2021057619A1 true WO2021057619A1 (zh) 2021-04-01

Family

ID=69218568

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2020/116212 WO2021057619A1 (zh) 2019-09-27 2020-09-18 内存回收方法、装置、电子设备及存储介质

Country Status (4)

Country Link
US (1) US20220121495A1 (zh)
EP (1) EP3979082A4 (zh)
CN (1) CN110727606A (zh)
WO (1) WO2021057619A1 (zh)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114253872A (zh) * 2022-02-28 2022-03-29 荣耀终端有限公司 电子设备及其内存回收方法、介质

Families Citing this family (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110704189A (zh) * 2019-09-27 2020-01-17 Oppo(重庆)智能科技有限公司 内存回收方法、装置以及电子设备
CN110727606A (zh) * 2019-09-27 2020-01-24 Oppo(重庆)智能科技有限公司 内存回收方法、装置以及电子设备
CN111930513B (zh) * 2020-08-31 2023-02-28 Oppo(重庆)智能科技有限公司 文件预读的调整方法、装置、电子设备及存储介质
CN114168065A (zh) * 2020-09-10 2022-03-11 荣耀终端有限公司 调整内存配置参数的方法和装置
CN112199618A (zh) * 2020-10-22 2021-01-08 Oppo广东移动通信有限公司 文件页面预读方法、装置、终端及存储介质
CN116126744B (zh) * 2023-04-04 2023-08-22 荣耀终端有限公司 一种内存回收方法、装置及终端设备
CN116185890B (zh) * 2023-04-23 2023-09-19 荣耀终端有限公司 内存回收方法及电子设备

Citations (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN108563506A (zh) * 2018-04-11 2018-09-21 青岛海信移动通信技术股份有限公司 移动终端的内存回收方法及装置
EP3388946A1 (en) * 2015-12-31 2018-10-17 Huawei Technologies Co., Ltd. Memory collection method and device
CN109857556A (zh) * 2019-01-15 2019-06-07 Oppo广东移动通信有限公司 内存回收方法及装置、存储介质和电子设备
CN109992523A (zh) * 2017-12-29 2019-07-09 广东欧珀移动通信有限公司 内存处理方法和装置、电子设备、计算机可读存储介质
CN110018901A (zh) * 2018-01-10 2019-07-16 广东欧珀移动通信有限公司 内存回收方法、装置、计算机设备和计算机可读存储介质
CN110704189A (zh) * 2019-09-27 2020-01-17 Oppo(重庆)智能科技有限公司 内存回收方法、装置以及电子设备
CN110727606A (zh) * 2019-09-27 2020-01-24 Oppo(重庆)智能科技有限公司 内存回收方法、装置以及电子设备
CN110727605A (zh) * 2019-09-27 2020-01-24 Oppo(重庆)智能科技有限公司 内存回收方法、装置以及电子设备

Family Cites Families (11)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN100590609C (zh) * 2008-02-22 2010-02-17 浙江大学 一种基于非连续页的动态内存管理方法
US20150067238A1 (en) * 2013-08-29 2015-03-05 Sandisk Technologies Inc. Computing Device and Method for Predicting Low Memory Conditions
KR102225525B1 (ko) * 2014-04-08 2021-03-09 삼성전자 주식회사 하드웨어 기반 메모리 관리 장치 및 메모리 관리 방법
CN105511963B (zh) * 2015-11-30 2019-12-10 Tcl集团股份有限公司 一种基于Android系统的内存优化方法及系统
WO2018000128A1 (en) * 2016-06-27 2018-01-04 Intel Corporation Dynamic configuration of compressed virtual memory
CN108228342B (zh) * 2017-08-10 2021-02-09 珠海市魅族科技有限公司 终端设备控制方法及装置、终端设备及计算机可读存储介质
CN109697119A (zh) * 2017-10-20 2019-04-30 中兴通讯股份有限公司 终端内存回收处理方法及装置
CN109992393A (zh) * 2017-12-29 2019-07-09 广东欧珀移动通信有限公司 应用处理方法和装置、电子设备、计算机可读存储介质
CN110018902B (zh) * 2018-01-10 2023-01-31 Oppo广东移动通信有限公司 内存处理方法和装置、电子设备、计算机可读存储介质
CN109857555B (zh) * 2019-01-15 2023-02-28 Oppo广东移动通信有限公司 内存回收方法及装置、存储介质和电子设备
CN110083450A (zh) * 2019-04-09 2019-08-02 Oppo广东移动通信有限公司 内存回收方法、装置、电子设备及存储介质

Patent Citations (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
EP3388946A1 (en) * 2015-12-31 2018-10-17 Huawei Technologies Co., Ltd. Memory collection method and device
CN109992523A (zh) * 2017-12-29 2019-07-09 广东欧珀移动通信有限公司 内存处理方法和装置、电子设备、计算机可读存储介质
CN110018901A (zh) * 2018-01-10 2019-07-16 广东欧珀移动通信有限公司 内存回收方法、装置、计算机设备和计算机可读存储介质
CN108563506A (zh) * 2018-04-11 2018-09-21 青岛海信移动通信技术股份有限公司 移动终端的内存回收方法及装置
CN109857556A (zh) * 2019-01-15 2019-06-07 Oppo广东移动通信有限公司 内存回收方法及装置、存储介质和电子设备
CN110704189A (zh) * 2019-09-27 2020-01-17 Oppo(重庆)智能科技有限公司 内存回收方法、装置以及电子设备
CN110727606A (zh) * 2019-09-27 2020-01-24 Oppo(重庆)智能科技有限公司 内存回收方法、装置以及电子设备
CN110727605A (zh) * 2019-09-27 2020-01-24 Oppo(重庆)智能科技有限公司 内存回收方法、装置以及电子设备

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
See also references of EP3979082A4 *

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114253872A (zh) * 2022-02-28 2022-03-29 荣耀终端有限公司 电子设备及其内存回收方法、介质

Also Published As

Publication number Publication date
EP3979082A4 (en) 2022-08-17
EP3979082A1 (en) 2022-04-06
US20220121495A1 (en) 2022-04-21
CN110727606A (zh) 2020-01-24

Similar Documents

Publication Publication Date Title
WO2021057619A1 (zh) 内存回收方法、装置、电子设备及存储介质
WO2021057622A1 (zh) 内存回收方法、装置、电子设备及存储介质
US10895987B2 (en) Memory compression method of electronic device and apparatus thereof
WO2021057623A1 (zh) 内存回收方法、装置、电子设备及存储介质
CN110764906B (zh) 内存回收处理方法、装置、电子设备以及存储介质
CN111158910B (zh) 内存管理方法、装置、存储介质及电子设备
CN110446062B (zh) 大数据文件传输的接收处理方法、电子装置及存储介质
CN110765031B (zh) 数据存储方法、装置、移动终端及存储介质
CN110888746A (zh) 内存管理方法、装置、存储介质及电子设备
CN106453572A (zh) 基于云服务器同步图像的方法及系统
CN104408064A (zh) 一种数据存储方法及装置
CN113656176A (zh) 云设备的分配方法、装置、系统、电子设备、介质及产品
CN110737606B (zh) 内存回收处理方法、装置、电子设备以及存储介质
CN111984413A (zh) 内存回收方法、装置、电子设备及存储介质
CN103425539B (zh) 信息处理方法及装置
CN114175748A (zh) 通信频率调节方法、装置、电子设备及存储介质
CN115587049A (zh) 内存回收方法、装置、电子设备及存储介质
CN114281724B (zh) 数据传输方法、装置及电子设备
CN110727605B (zh) 内存回收方法、装置以及电子设备
CN108228472B (zh) 终端设备控制方法及装置、终端设备及计算机可读存储介质
WO2021129489A1 (zh) 应用程序预编译的方法、装置、电子设备及存储介质
CN115421907A (zh) 内存回收方法、装置、电子设备及存储介质
CN116303110B (zh) 一种内存垃圾的回收方法及电子设备
CN112840663B (zh) 视频编码方法、装置、电子设备及存储介质
WO2021088624A1 (zh) 应用程序预编译的方法、装置、电子设备以及服务器

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 20868443

Country of ref document: EP

Kind code of ref document: A1

ENP Entry into the national phase

Ref document number: 2020868443

Country of ref document: EP

Effective date: 20211228

NENP Non-entry into the national phase

Ref country code: DE