Memory recycling method and device
By monitoring and managing memory thresholds in the Android system, memory recycling methods are optimized to avoid manslaughtering the foreground process, the problem of manslaughtering the Android system when memory is insufficient and the user experience is improved.
Patent Information
- Application Number
- CN202111395891.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-11-23
- Publication Date
- 2024-11-19
- Estimated Expiration
- 2041-11-23
AI Technical Summary
Android systems are prone to kill the foreground process by mistake when there is insufficient memory, which will affect the user experience.
By monitoring whether the electronic device needs to kill a process with an oom_adj value of 0, if the free memory and idle cache are greater than or equal to the first threshold and are less than the second threshold, only the earliest started target process will be killed; if both are less than the first threshold, Kill all target processes.
Prevent errors from killing the startup foreground process, preventing the foreground process from crashing, and fully freeing memory when there is insufficient memory, avoiding system restarts, and improving user experience.
Smart Images

Figure CN114090465B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of computer technology, and in particular to a memory recovery method and device. Background Art
[0002] When the free memory of the system is insufficient, an electronic device based on the Android system will kill some running processes according to the Low Memory Killer (LMK) mechanism to recycle memory, thereby ensuring the normal operation of the electronic device.
[0003] The LMK mechanism will classify all processes, and each category of processes will have its corresponding oom_adj value. The larger the oom_adj value, the less important the process. When the system executes the process killing operation, it will start from the process with the highest oom_adj value. Currently, the Android system uses Pressure Stall Information (PSI) to report memory pressure events, and the Low Memory Killer Daemon (LMKD) process implements the above LMK mechanism.
[0004] After PSI triggers the LMKD process, if the LMKD process needs to kill a process with an oom_adj value of 0, since the oom_adj value of the foreground process is 0, the oom_adj value of the application (APP) is also 0 during the restart process after being killed. Therefore, when the memory pressure is high, all foreground processes will still be killed even if there are a lot of free caches, which can easily cause accidental killing and cause the foreground process to crash, thus affecting the user experience. Summary of the invention
[0005] The embodiments of the present application provide a memory recovery method and device, which can solve the technical problem of easily killing the foreground process by mistake in the prior art.
[0006] In a first aspect, an embodiment of the present application provides a memory recovery method, the method comprising:
[0007] Monitoring whether the electronic device needs to kill a target process, where the target process is a process in the electronic device whose oom_adj is 0;
[0008] When it is detected that the electronic device needs to kill at least two target processes, determining the current free memory and free cache of the electronic device;
[0009] If any one of the free memory and the free cache is greater than or equal to a first preset threshold, and both the free memory and the free cache are less than a second preset threshold, then killing the target process started earliest among the at least two target processes; wherein the first preset threshold is less than the second preset threshold, and the second preset threshold is the minimum free memory corresponding to when the electronic device needs to kill the target process;
[0010] If both the free memory and the free cache are smaller than the first preset threshold, the at least two target processes are killed.
[0011] In a feasible implementation manner, after determining the current free memory and free cache of the electronic device, the method further includes:
[0012] If any one of the free memory and the free cache is greater than or equal to the second preset threshold, and both the free memory and the free cache are less than a third preset threshold, determining whether the electronic device is currently running a first process whose oom_adj value is greater than a first preset value; wherein the first preset value is not less than 0, and the third preset threshold is the minimum free memory corresponding to when the electronic device needs to kill the first process;
[0013] When the electronic device currently runs the first process, killing the first process;
[0014] When the electronic device is not currently running the first process, it is determined whether to kill the target process started earliest among the at least two target processes according to a preset judgment condition.
[0015] In a feasible implementation manner, determining whether to kill the target process started earliest among the at least two target processes according to a preset judgment condition includes:
[0016] Determining whether there is a critical event in the PSI generated by the electronic device and the usage of the SWAP partition in the electronic device;
[0017] When a critical event exists in the PSI generated by the electronic device and the usage of the SWAP partition is greater than a preset usage threshold, the earliest started target process among the at least two target processes is killed.
[0018] In a feasible implementation manner, after determining the current free memory and free cache of the electronic device, the method further includes:
[0019] If any one of the free memory and the free cache is greater than or equal to the third preset threshold, the second process currently running on the electronic device and having an oom_adj value greater than a second preset value is killed; wherein the second preset value is greater than the first preset value.
[0020] In a feasible implementation manner, the monitoring whether the electronic device needs to kill the target process includes:
[0021] When at least one of the following detection items is detected to be met, it is determined that the electronic device needs to kill the target process:
[0022] The usage of the SWAP partition in the electronic device is greater than a preset usage threshold;
[0023] The current page jitter degree of the electronic device is greater than a preset page jitter degree threshold;
[0024] There are critical events in the PSI generated by the electronic device;
[0025] The current free memory in the electronic device is less than a preset memory waterline value.
[0026] In a second aspect, an embodiment of the present application provides a memory recovery device, the device comprising:
[0027] A monitoring module, used for monitoring whether the electronic device needs to kill a target process, wherein the target process is a process in the electronic device whose oom_adj is 0;
[0028] A processing module, configured to determine current free memory and free cache of the electronic device when it is detected that the electronic device needs to kill at least two target processes;
[0029] The processing module is further configured to kill the target process started earliest among the at least two target processes if any one of the free memory and the free cache is greater than or equal to a first preset threshold, and both the free memory and the free cache are less than a second preset threshold; wherein the first preset threshold is less than the second preset threshold, and the second preset threshold is the minimum free memory corresponding to when the electronic device needs to kill the target process;
[0030] The processing module is further configured to kill the at least two target processes if both the free memory and the free cache are smaller than the first preset threshold.
[0031] In a feasible implementation manner, the processing module is further used for:
[0032] If any one of the free memory and the free cache is greater than or equal to the second preset threshold, and both the free memory and the free cache are less than a third preset threshold, determining whether the electronic device is currently running a first process whose oom_adj value is greater than a first preset value; wherein the first preset value is not less than 0, and the third preset threshold is the minimum free memory corresponding to when the electronic device needs to kill the first process;
[0033] When the electronic device currently runs the first process, killing the first process;
[0034] When the electronic device is not currently running the first process, it is determined whether to kill the target process started earliest among the at least two target processes according to a preset judgment condition.
[0035] In a feasible implementation manner, the processing module is specifically used for:
[0036] Determining whether there is a critical event in the PSI generated by the electronic device and the usage of the SWAP partition in the electronic device;
[0037] When a critical event exists in the PSI generated by the electronic device and the usage of the SWAP partition is greater than a preset usage threshold, the earliest started target process among the at least two target processes is killed.
[0038] In a feasible implementation manner, the processing module is further used for:
[0039] If any one of the free memory and the free cache is greater than or equal to the third preset threshold, the second process currently running on the electronic device and having an oom_adj value greater than a second preset value is killed; wherein the second preset value is greater than the first preset value.
[0040] In a feasible implementation manner, the monitoring module is used to:
[0041] When at least one of the following detection items is detected to be met, it is determined that the electronic device needs to kill the target process:
[0042] The usage of the SWAP partition in the electronic device is greater than a preset usage threshold;
[0043] The current page jitter degree of the electronic device is greater than a preset page jitter degree threshold;
[0044] There are critical events in the PSI generated by the electronic device;
[0045] The current free memory in the electronic device is less than a preset memory waterline value.
[0046] In a third aspect, an embodiment of the present application provides an electronic device, including: at least one processor and a memory;
[0047] The memory stores computer-executable instructions;
[0048] The at least one processor executes the computer-executable instructions stored in the memory, so that the at least one processor performs the memory recycling method provided in the first aspect.
[0049] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, in which computer-executable instructions are stored. When a processor executes the computer-executable instructions, a memory recovery method as provided in the first aspect is implemented.
[0050] In a fifth aspect, an embodiment of the present application provides a computer program product, including a computer program, which, when executed by a processor, implements the memory recovery method provided in the first aspect.
[0051] The memory recovery method and device provided in the embodiment of the present application, when it is monitored that the electronic device needs to kill at least two target processes with an oom_adj value of 0, if any one of the current free memory and free cache of the electronic device is greater than or equal to the above-mentioned first preset threshold, and the free memory and free cache are both less than the above-mentioned second preset threshold, then only the earliest started target process is killed, thereby preventing the foreground process that is being started from being mistakenly killed and avoiding the foreground process from crashing; when the free memory and free cache are both less than the first preset threshold, all target processes are killed, thereby fully releasing memory, avoiding system restart, and thus improving user experience. BRIEF DESCRIPTION OF THE DRAWINGS
[0052] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the following is a brief introduction to the drawings required for use in the embodiments of the present application or the description of the prior art. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0053] Figure 1 A schematic diagram of a memory recovery method provided in an embodiment of the present application Figure 1 ;
[0054] Figure 2 A schematic diagram of a memory recovery method provided in an embodiment of the present application Figure 2 ;
[0055] Figure 3 A schematic diagram of a memory recovery method provided in an embodiment of the present application Figure 3 ;
[0056] Figure 4 A schematic diagram of a program module of a memory recovery device provided in an embodiment of the present application;
[0057] Figure 5 A schematic diagram of the hardware structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0058] In order to make the purpose, technical scheme and advantages of the embodiments of the present application clearer, the technical scheme 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. Obviously, the described embodiments are part of the embodiments of the present application, rather than all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application. In addition, although the disclosure in the present application is introduced according to one or more exemplary examples, it should be understood that each aspect of these disclosures can also constitute a complete implementation method separately.
[0059] It should be noted that the brief description of terms in this application is only for the convenience of understanding the embodiments described below, and is not intended to limit the embodiments of this application. Unless otherwise specified, these terms should be understood according to their common and usual meanings.
[0060] The terms "first", "second", etc. in the specification and claims of this application and the above drawings are used to distinguish similar or similar objects or entities, and do not necessarily mean to limit a specific order or sequence, unless otherwise noted. It should be understood that the terms used in this way can be interchangeable under appropriate circumstances, for example, they can be implemented in an order other than those given in the diagrams or descriptions of the embodiments of this application.
[0061] In addition, the terms "include" and "have" and any variations thereof are intended to cover but not exclude inclusion, for example, a product or device comprising a list of components is not necessarily limited to those components expressly listed but may include other components not expressly listed or inherent to such products or devices.
[0062] The term "module" as used in this application refers to any known or later developed hardware, software, firmware, artificial intelligence, fuzzy logic, or combination of hardware and / or software code that is capable of performing the functions associated with that element.
[0063] The embodiments of the present application can be applied to various types of electronic devices, such as smart phones, tablet computers (pads), smart watches, smart TVs, set-top boxes, etc., and the embodiments of the present application are not limited to this.
[0064] At present, when the system free memory of an electronic device based on the Android system is insufficient, the electronic device will kill some running processes according to the LMK mechanism to recycle the memory, thereby ensuring the normal operation of the electronic device.
[0065] In the LMK mechanism, all processes are classified, and each category of processes has a corresponding oom_adj value. The larger the oom_adj value, the less important the process.
[0066] When the system executes the process killing operation, it will start from the process with the highest oom_adj value. Currently, the Android system uses PSI to report memory pressure events, and the LMKD process implements the above LMK mechanism.
[0067] In order to better understand the embodiments of the present application, refer to Figure 1 , Figure 1 A schematic diagram of a memory recovery method provided in an embodiment of the present application Figure 1 ; In some embodiments, when a PSI event triggers the LMKD process, the LMKD process collects the current operating data of the electronic device and determines whether memory recovery is required based on the collected operating data; if so, the selected process is killed according to the LMK mechanism; if not, the current operating data of the electronic device continues to be collected.
[0068] In some embodiments, if the LMKD process needs to kill a process with an oom_adj value of 0, since the oom_adj value of the foreground process is 0, the oom_adj value of the application (APP) is also 0 during the restart process after being killed. Therefore, when the memory pressure is high, even if there is a lot of free cache, all foreground processes will still be killed, which can easily cause accidental killing and cause the foreground process to crash, thereby affecting the user experience.
[0069] In the face of the above technical problems, a memory recovery method is provided in an embodiment of the present application, by setting a first threshold and a second threshold, the second threshold is the minimum free memory corresponding to the process with an oom_adj value of 0 when the electronic device needs to kill, and the first preset threshold is less than the second preset threshold; when it is detected that the electronic device needs to kill at least two target processes with an oom_adj value of 0, if any one of the current free memory and free cache of the electronic device is greater than or equal to the above first threshold, and the free memory and free cache are both less than the above second threshold, then only the earliest started target process is selected to be killed, thereby preventing the foreground process being started from being killed by mistake and avoiding the foreground process from flashing back; when the free memory and free cache are both less than the first threshold, all target processes are killed, thereby fully releasing the memory, avoiding system restart, and thus improving the user experience. The following detailed embodiments are used for detailed description.
[0070] Reference Figure 2 , Figure 2 A schematic diagram of a memory recovery method provided in an embodiment of the present application Figure 2 In a feasible implementation manner, the above memory recovery method includes:
[0071] S201, monitoring whether the electronic device needs to kill the target process with oom_adj being 0.
[0072] In a feasible implementation, at least one of the following detection items in the electronic device can be monitored to determine whether the electronic device needs to kill the target process: the usage of the SWAP partition in the electronic device, the current page thrashing level of the electronic device, the PSI generated by the electronic device, and the current free memory in the electronic device.
[0073] Among them, when it is monitored that at least one or a combination of two of the above detection items meets the following conditions, it can be determined that the electronic device needs to kill the process with an oom_adj value of 0: the usage of the SWAP partition in the electronic device is greater than the preset usage threshold, the current page thrashing degree of the electronic device is greater than the preset page thrashing degree threshold, there is a critical event in the PSI generated by the electronic device, and the current free memory in the electronic device is less than the preset memory waterline value.
[0074] S202: When it is detected that the electronic device needs to kill at least two target processes, determine the current free memory and free cache of the electronic device.
[0075] In a feasible implementation, when it is determined that the electronic device needs to kill a process with an oom_adj value of 0, the number of processes with an oom_adj value of 0 in the electronic device is determined. When the electronic device has only one process with an oom_adj value of 0, the process is killed directly; when the electronic device has two or more processes with an oom_adj value of 0, it is necessary to determine the current free memory and free cache of the electronic device.
[0076] S203: Determine whether both the free memory and the free cache are less than a first preset threshold value. If yes, execute S204; if no, execute S205.
[0077] In an embodiment of the present application, the minimum free memory corresponding to when the electronic device needs to kill a process with an oom_adj value of 0 can be set in advance to the second preset threshold minfree[2]; in addition, the first preset threshold minfree[1] is set, minfree[1]<minfree[2].
[0078] In one possible implementation, minfree[1]=minfree[2]*80%.
[0079] S204, kill all target processes and return to S201.
[0080] S205, determining whether both the free memory and the free cache are less than a second preset threshold, if so, executing S206, if not, returning to executing S201.
[0081] S206, kill the target process started earliest and return to S201.
[0082] In the embodiment of the present application, when the above-mentioned free memory and free cache are both lower than minfree[1], the earliest started target process is killed, so as to avoid the foreground process from crashing.
[0083] It is understandable that when the system free memory and free cache are both lower than minfree[1], but all the detection items do not meet the conditions, the system will restart due to lack of memory. To avoid this problem, when the system free memory and free cache are both lower than minfree[1], all target processes are killed to fully release memory, thereby avoiding system restart.
[0084] The memory recovery method provided in the embodiment of the present application, when it is monitored that the electronic device needs to kill at least two target processes with an oom_adj value of 0, if any one of the current free memory and free cache of the electronic device is greater than or equal to the above-mentioned first preset threshold, and the free memory and free cache are both less than the above-mentioned second preset threshold, then only the earliest started target process is killed, thereby preventing the foreground process that is being started from being killed by mistake and avoiding the foreground process from crashing; when the free memory and free cache are both less than the first preset threshold, all target processes are killed, thereby fully releasing memory, avoiding system restart, and thus improving user experience.
[0085] Based on the contents described in the above embodiments, Figure 3 , Figure 3 A schematic diagram of a memory recovery method provided in an embodiment of the present application Figure 3 In a feasible implementation manner, the above memory recovery method includes:
[0086] S301, monitoring whether the electronic device needs to kill the target process with oom_adj being 0.
[0087] S302: When it is detected that the electronic device needs to kill at least two target processes, determine the current free memory and free cache of the electronic device.
[0088] In a feasible implementation, when it is determined that the electronic device needs to kill a process with an oom_adj value of 0, the number of processes with an oom_adj value of 0 in the electronic device is determined. When the electronic device has only one process with an oom_adj value of 0, the process is killed directly; when the electronic device has two or more processes with an oom_adj value of 0, it is necessary to determine the current free memory and free cache of the electronic device.
[0089] S303: Determine whether both the free memory and the free cache are less than a first preset threshold value. If yes, execute S304; if no, execute S305.
[0090] In an embodiment of the present application, the minimum free memory corresponding to when the electronic device needs to kill a process with an oom_adj value of 0 can be set in advance to the second preset threshold minfree[2]; in addition, the first preset threshold minfree[1] is set, minfree[1]<minfree[2].
[0091] In one possible implementation, minfree[1]=minfree[2]*80%.
[0092] S304, kill all target processes and return to S301.
[0093] S305, determining whether both the free memory and the free cache are less than a second preset threshold, if so, executing S306, if not, executing S307.
[0094] S306, kill the target process started earliest and return to S301.
[0095] S307, determine whether both the free memory and the free cache are less than a third preset threshold. If yes, execute S308, if no, execute S3012.
[0096] S308, determining whether the electronic device is currently running a first process whose oom_adj value is greater than a first preset value. If yes, executing S309, if no, executing S3010.
[0097] In the embodiment of the present application, a third preset threshold minfree[3] may be pre-set.
[0098] The first preset value is not less than 0, and the third preset threshold minfree[3] is the minimum free memory corresponding to when the electronic device needs to kill the first process.
[0099] Exemplarily, when the first preset value is 100, the third preset threshold minfree[3] is the minimum free memory corresponding to when the electronic device needs to kill a process with an oom_adj value of 100.
[0100] S309, killing the first process.
[0101] S3010, determining whether the electronic device meets a preset condition. If yes, executing S3011, if no, returning to executing S301.
[0102] S3011, kill the target process started earliest and return to S301.
[0103] In the embodiment of the present application, it can be determined whether to kill the target process that is started earliest among at least two target processes based on a preset judgment condition.
[0104] Optionally, determine whether there is a critical event in the PSI generated by the electronic device and the usage of the SWAP partition in the electronic device; when there is a critical event in the PSI generated by the electronic device and the usage of the SWAP partition is greater than a preset usage threshold, kill the earliest started target process among the target processes with an oom_adj value of 0.
[0105] S3012, kill the second process currently running on the electronic device and whose oom_adj value is greater than the second preset value.
[0106] Among them, the second preset value is greater than the first preset value.
[0107] Exemplarily, when the first preset value is 100, the second preset value may be 200.
[0108] The memory recovery method and device provided in the embodiment of the present application, when the system free memory and free cache are both lower than minfree[3], kill the first process whose oom_adj value is greater than a first preset value, so as to prevent the foreground process from being killed when there is a large amount of free memory and free cache; if the electronic device is not currently running the first process, when the swap usage reaches a threshold and the PSI reports a critical event, the earliest started process with an oom_adj of 0 is selected again to be killed, thereby releasing memory and avoiding system restart.
[0109] Based on the contents described in the above embodiments, the present application also provides a memory recovery device, which is applied to electronic devices. Figure 4 , Figure 4 This is a program module diagram of a memory recovery device provided in an embodiment of the present application, wherein the memory recovery device comprises:
[0110] The monitoring module 401 is used to monitor whether the electronic device needs to kill a target process, where the target process is a process whose oom_adj is 0 in the electronic device.
[0111] The processing module 402 is used to determine the current free memory and free cache of the electronic device when it is detected that the electronic device needs to kill at least two target processes.
[0112] The processing module 402 is also used to kill the target process that is started earliest among at least two target processes if any one of the free memory and the free cache is greater than or equal to a first preset threshold, and both the free memory and the free cache are less than a second preset threshold; wherein the first preset threshold is less than the second preset threshold, and the second preset threshold is the minimum free memory corresponding to when the electronic device needs to kill the target process.
[0113] The processing module 402 is further configured to kill the at least two target processes if both the free memory and the free cache are less than a first preset threshold.
[0114] The memory recovery device provided in the embodiment of the present application, when it is monitored that the electronic device needs to kill at least two target processes with an oom_adj value of 0, if any one of the current free memory and free cache of the electronic device is greater than or equal to the above-mentioned first preset threshold, and the free memory and free cache are both less than the above-mentioned second preset threshold, then only the earliest started target process is killed, thereby preventing the foreground process that is being started from being killed by mistake and avoiding the foreground process from crashing; when the free memory and free cache are both less than the first preset threshold, all target processes are killed, thereby fully releasing memory, avoiding system restart, and thus improving user experience.
[0115] In a feasible implementation manner, the processing module 402 is further configured to:
[0116] If any one of the free memory and the free cache is greater than or equal to the second preset threshold, and both the free memory and the free cache are less than the third preset threshold, it is determined whether the electronic device is currently running a first process whose oom_adj value is greater than the first preset value; wherein the first preset value is not less than 0, and the third preset threshold is the minimum free memory corresponding to when the electronic device needs to kill the first process.
[0117] When the electronic device currently runs the first process, the first process is killed; when the electronic device currently does not run the first process, it is determined whether to kill the target process started earliest among at least two target processes according to a preset judgment condition.
[0118] In a feasible implementation manner, the processing module 402 is specifically configured to:
[0119] Determine whether there are critical events in the PSI generated by the electronic device and the usage of the SWAP partition in the electronic device.
[0120] When a critical event exists in the PSI generated by the electronic device and the usage of the SWAP partition is greater than a preset usage threshold, the earliest started target process among at least two target processes is killed.
[0121] In a feasible implementation manner, the processing module 402 is further configured to:
[0122] If any one of the free memory and the free cache is greater than or equal to the third preset threshold, the second process currently running on the electronic device and having an oom_adj value greater than a second preset value is killed; wherein the second preset value is greater than the first preset value.
[0123] In a feasible implementation, the monitoring module 401 is used to:
[0124] When at least one of the following detection items is detected, it is determined that the electronic device needs to kill the target process:
[0125] The usage of the SWAP partition in the electronic device is greater than a preset usage threshold;
[0126] The current page jitter degree of the electronic device is greater than a preset page jitter degree threshold;
[0127] There are critical events in the PSI generated by electronic devices;
[0128] The current free memory in the electronic device is less than the preset memory waterline value.
[0129] It should be noted that the specific contents of the monitoring module 401 and the processing module 402 in the embodiment of the present application can be referred to in Figures 1 to 3 The various steps of the memory recycling method described in the illustrated embodiment are not described in detail here.
[0130] Furthermore, based on the contents described in the above embodiments, an electronic device is also provided in an embodiment of the present application, which includes at least one processor and a memory; wherein the memory stores computer-executable instructions; the at least one processor executes the computer-executable instructions stored in the memory to implement the various steps of the memory recovery method described in the above embodiments, which will not be repeated in this embodiment.
[0131] In order to better understand the embodiments of the present application, refer to Figure 5 , Figure 5 A schematic diagram of the hardware structure of an electronic device provided in an embodiment of the present application.
[0132] like Figure 5 As shown, the electronic device 50 of this embodiment includes: a processor 501 and a memory 502; wherein:
[0133] Memory 502, used to store computer-executable instructions;
[0134] The processor 501 is used to execute the computer-executable instructions stored in the memory to implement the various steps of the memory recycling method described in the above embodiment. For details, please refer to the relevant description in the above method embodiment.
[0135] Optionally, the memory 502 may be independent or integrated with the processor 501 .
[0136] When the memory 502 is independently provided, the device further includes a bus 503 for connecting the memory 502 and the processor 501 .
[0137] Furthermore, based on the contents described in the above embodiments, a computer-readable storage medium is also provided in the embodiments of the present application, in which computer execution instructions are stored. When the processor executes the computer execution instructions, the various steps of the memory recovery method described in the above embodiments can be implemented. For details, please refer to the relevant description in the above method embodiments.
[0138] Furthermore, based on the contents described in the above embodiments, a computer program product is also provided in the embodiments of the present application, including a computer program. When the computer program is executed by a processor, the various steps of the memory recovery method described in the above embodiments can be implemented. For details, please refer to the relevant description in the above method embodiments.
[0139] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the modules is only a logical function division. There may be other division methods in actual implementation, such as multiple modules can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or modules, which can be electrical, mechanical or other forms.
[0140] The modules described as separate components may or may not be physically separated, and the components shown as modules may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0141] In addition, each functional module in each embodiment of the present application can be integrated into a processing unit, each module can exist physically separately, or two or more modules can be integrated into one unit. The above module integration unit can be implemented in the form of hardware or in the form of hardware plus software functional units.
[0142] The above-mentioned integrated module implemented in the form of a software function module can be stored in a computer-readable storage medium. The above-mentioned software function module is stored in a storage medium, including a number of instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor (English: processor) to perform some steps of the method described in each embodiment of the present application.
[0143] It should be understood that the processor may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), etc. A general-purpose processor may be a microprocessor or any conventional processor. The steps of the method disclosed in the application may be directly implemented as being executed by a hardware processor, or may be implemented by a combination of hardware and software modules in the processor.
[0144] The memory may include a high-speed RAM memory, and may also include a non-volatile storage NVM, such as at least one disk memory, and may also be a USB flash drive, a mobile hard disk, a read-only memory, a magnetic disk or an optical disk, etc.
[0145] The bus may be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus, etc. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, the bus in the drawings of the present application is not limited to only one bus or one type of bus.
[0146] The above storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk. The storage medium can be any available medium that can be accessed by a general or special purpose computer.
[0147] An exemplary storage medium is coupled to a processor so that the processor can read information from the storage medium and write information to the storage medium. Of course, the storage medium can also be a component of the processor. The processor and the storage medium can be located in an application specific integrated circuit (ASIC). Of course, the processor and the storage medium can also exist as discrete components in an electronic device or a main control device.
[0148] Those skilled in the art can understand that all or part of the steps of implementing the above-mentioned method embodiments can be completed by hardware related to program instructions. The aforementioned program can be stored in a computer-readable storage medium. When the program is executed, the steps of the above-mentioned method embodiments are executed; and the aforementioned storage medium includes: ROM, RAM, disk or optical disk and other media that can store program codes.
[0149] 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 it. 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 replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present application.
Claims
1. A memory recovery method, characterized in that: The method comprises: Monitoring whether the electronic device needs to kill a target process, where the target process is a process in the electronic device whose insufficient memory adjustment parameter oom_adj is 0; When it is detected that the electronic device needs to kill at least two target processes, determining the current free memory and free cache of the electronic device; If any one of the free memory and the free cache is greater than or equal to a first preset threshold, and both the free memory and the free cache are less than a second preset threshold, then killing the target process started earliest among the at least two target processes; wherein the first preset threshold is less than the second preset threshold, and the second preset threshold is the minimum free memory corresponding to when the electronic device needs to kill the target process; If both the free memory and the free cache are smaller than the first preset threshold, the at least two target processes are killed.
2. The method according to claim 1, characterized in that After determining the current free memory and free cache of the electronic device, the method further includes: If any one of the free memory and the free cache is greater than or equal to the second preset threshold, and both the free memory and the free cache are less than a third preset threshold, determining whether the electronic device is currently running a first process whose oom_adj value is greater than a first preset value; wherein the first preset value is not less than 0, and the third preset threshold is the minimum free memory corresponding to when the electronic device needs to kill the first process; When the electronic device currently runs the first process, killing the first process; When the electronic device is not currently running the first process, it is determined whether to kill the target process started earliest among the at least two target processes according to a preset judgment condition.
3. The method according to claim 2, characterized in that The step of determining whether to kill the target process started earliest among the at least two target processes according to a preset judgment condition includes: Determine whether there is a critical event in the pressure blocking information PSI generated by the electronic device, and the usage of the swap partition in the electronic device; When a critical event exists in the PSI generated by the electronic device and the usage of the SWAP partition is greater than a preset usage threshold, the earliest started target process among the at least two target processes is killed.
4. The method according to claim 2, characterized in that: After determining the current free memory and free cache of the electronic device, the method further includes: If any one of the free memory and the free cache is greater than or equal to the third preset threshold, the second process currently running on the electronic device and having an oom_adj value greater than a second preset value is killed; wherein the second preset value is greater than the first preset value.
5. The method according to any one of claims 1 to 4, characterized in that: The monitoring of whether the electronic device needs to kill the target process includes: When at least one of the following detection items is detected to be met, it is determined that the electronic device needs to kill the target process: The usage of the SWAP partition in the electronic device is greater than a preset usage threshold; The current page jitter degree of the electronic device is greater than a preset page jitter degree threshold; There are critical events in the PSI generated by the electronic device; The current free memory in the electronic device is less than a preset memory waterline value.
6. A memory recovery device, characterized in that: The device comprises: A monitoring module, used to monitor whether the electronic device needs to kill a target process, wherein the target process is a process in the electronic device whose insufficient memory adjustment parameter oom_adj is 0; A processing module, configured to determine current free memory and free cache of the electronic device when it is detected that the electronic device needs to kill at least two target processes; The processing module is further configured to kill the target process started earliest among the at least two target processes if any one of the free memory and the free cache is greater than or equal to a first preset threshold, and both the free memory and the free cache are less than a second preset threshold; wherein the first preset threshold is less than the second preset threshold, and the second preset threshold is the minimum free memory corresponding to when the electronic device needs to kill the target process; The processing module is further configured to kill the at least two target processes if both the free memory and the free cache are smaller than the first preset threshold.
7. The device according to claim 6, characterized in that The processing module is also used for: If any one of the free memory and the free cache is greater than or equal to the second preset threshold, and both the free memory and the free cache are less than a third preset threshold, determining whether the electronic device is currently running a first process whose oom_adj value is greater than a first preset value; wherein the first preset value is not less than 0, and the third preset threshold is the minimum free memory corresponding to when the electronic device needs to kill the first process; When the electronic device currently runs the first process, killing the first process; When the electronic device is not currently running the first process, it is determined whether to kill the target process started earliest among the at least two target processes according to a preset judgment condition.
8. The device according to claim 7, characterized in that The processing module is specifically used for: Determine whether there is a critical event in the pressure blocking information PSI generated by the electronic device, and the usage of the swap partition in the electronic device; When a critical event exists in the PSI generated by the electronic device and the usage of the SWAP partition is greater than a preset usage threshold, the earliest started target process among the at least two target processes is killed.
9. The device according to claim 7, characterized in that The processing module is also used for: If any one of the free memory and the free cache is greater than or equal to the third preset threshold, the second process currently running on the electronic device and having an oom_adj value greater than a second preset value is killed; wherein the second preset value is greater than the first preset value.
10. The device according to any one of claims 6 to 9, characterized in that The monitoring module is used for: When at least one of the following detection items is detected to be met, it is determined that the electronic device needs to kill the target process: The usage of the SWAP partition in the electronic device is greater than a preset usage threshold; The current page jitter degree of the electronic device is greater than a preset page jitter degree threshold; There are critical events in the PSI generated by the electronic device; The current free memory in the electronic device is less than a preset memory waterline value.
11. An electronic device, characterized in that: include: at least one processor and memory; The memory stores computer-executable instructions; The at least one processor executes the computer-executable instructions stored in the memory, so that the at least one processor executes the memory recycling method as described in any one of claims 1 to 5.
12. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, and when the processor executes the computer-executable instructions, the memory recycling method according to any one of claims 1 to 5 is implemented.
13. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the memory recovery method described in any one of claims 1 to 5 is implemented.
