Memory allocation method and device and storage medium
By reserving part of the memory in the system memory, the problem of memory applications being blocked in low-memory scenarios is solved, and rapid memory applications are achieved, avoiding lag in electronic devices.
Patent Information
- Application Number
- CN202310985768.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-08-07
- Publication Date
- 2025-02-14
AI Technical Summary
In low memory scenarios, the kernel cannot obtain sufficient memory through memory recovery, resulting in memory application blocking and causing electronic devices to stutter.
By reserving part of the system memory as reserved memory, when the system memory is insufficient, directly apply for memory resources from the reserved memory without waiting for the task process to recover memory.
This greatly accelerates the memory application process in low-memory scenarios, avoids electronic device lag caused by memory application, and ensures user experience.
Smart Images

Figure CN119440794A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a memory allocation method, device and storage medium. Background Art
[0002] When a process (application) is started, it will apply for memory. However, memory application is closely related to the memory watermark. Specifically, when triggering memory application, the memory watermark needs to be checked first. If the memory watermark does not meet the conditions, the memory recycling process will be triggered to obtain memory that can be applied.
[0003] However, in actual usage scenarios, especially in low-memory scenarios, the kernel may not be able to obtain sufficient memory through memory recycling, and the only way to reclaim memory is to kill the application, that is, to kill the process. However, application killing will block memory requests, and since the time of application killing is uncontrollable, it will cause electronic devices to freeze. Summary of the invention
[0004] The present application provides a memory allocation method, device and storage medium, aiming to solve the problem of electronic device freezing caused by memory application.
[0005] In the first aspect, the present application provides a memory allocation method. The method is applied to an electronic device, including: receiving a first operation of a user; in response to the first operation, starting a first application; creating a first task process of the first application during the operation of the first application; wherein, in the process of creating the first task process, the first application applies for memory resources from the allocatable memory resources of the system memory of the electronic device; in the case of failure to apply for memory resources from the system memory, applying for memory resources from the reserved memory resources of the system memory, and reclaiming the memory resources occupied by the second task process, the second task process is the task process of other applications running in the background of the electronic device, and the second task process is the task process with the largest memory resources currently occupied, and the reserved memory resources are the memory resources pre-applied from the system memory; adding the memory resources recovered from the second task process to the reserved memory and / or the system memory.
[0006] For example, in a possible implementation, the first operation is, for example, a user clicking an application icon corresponding to the first application on the desktop of the electronic device. Figure 3 The icon of the camera application is displayed in the user interface 10c shown in (2).
[0007] For example, in another possible implementation, the first operation is, for example, an operation in which the user switches the first application from the background to the foreground. Figure 4 The interface of the camera application displayed in the user interface 10d shown in (4) is shown.
[0008] It should be understood that the above description is merely an example listed for a better understanding of the technical solution of the present application and is not intended to be the sole limitation to the present application.
[0009] Exemplarily, in a possible implementation, the allocatable memory resources of the system memory, such as in the following embodiment Figure 6B The system memory is shown as filled with shaded areas. Memory resources are reserved, for example, Figure 6B The reserved memory is shown filled with shaded space.
[0010] Exemplarily, in another possible implementation, memory resources are reserved, for example, in the following embodiment Figure 6C The memory resources between the low_new watermark and the low (low_old) watermark in the system memory are shown. The allocatable memory resources of the system memory are Figure 6C The portion of system memory shown excluding the memory resources between the low_new watermark and the low (low_old) watermark.
[0011] Exemplarily, in another possible implementation, memory resources are reserved, for example, in the following embodiment Fig.6D The memory resources between the low watermark and pre_low watermark in the system memory are shown. The allocatable memory resources of the system memory are Figure 6C Shows the portion of system memory excluding memory resources between low watermark and pre_low.
[0012] In the case where the memory resource application from the system memory fails, the memory resource is applied from the reserved memory, and the logic of reclaiming the memory resource occupied by the second task process can be seen in the following embodiment. Fig. 7A and Figure 7B , I will not go into details here.
[0013] Exemplarily, in some possible implementations, allocatable memory resources of the system memory may be regarded as system memory, and reserved memory resources may be regarded as reserved memory.
[0014] Therefore, by pre-applying for a portion of memory resources from the system memory and managing it separately as reserved memory, when the allocatable memory resources currently managed by the system resources are insufficient, the required memory resources can be directly applied for from the reserved memory resources without waiting for the process of reclaiming memory resources by the killing task process to be completed before the memory application process can proceed. Since the memory application is no longer blocked in the killing process, it greatly speeds up the memory application in specific scenarios, especially in low-memory scenarios, thereby solving the problem of electronic device freezes caused by memory application.
[0015] According to the first aspect, the first application applies for memory resources from the allocatable memory resources of the system memory of the electronic device, including: when the allocatable memory resources of the system memory are greater than or equal to the memory resources corresponding to the first memory waterline, the application for memory resources from the system memory is successful; when the allocatable memory resources of the system memory are less than the memory resources corresponding to the first memory waterline, the application for memory resources from the system memory fails.
[0016] Among them, the first internal waterline, for example, the min waterline mentioned in the following embodiment.
[0017] Understandably, for the management of system memory resources, three memory watermarks are currently set, namely high watermark, low watermark and min watermark. For the size of memory resources for these three memory watermarks, please refer to FIG. 6A to FIG. 6D , which will not be elaborated here. Among them, when the allocatable memory resources of the system memory management fall below the low waterline, the system will wake up the kswapd thread for asynchronous memory recovery until the allocatable memory resources of the system memory are recovered to the high waterline position. When the allocatable memory resources of the system memory management fall below the min waterline, it is necessary to block the memory allocation, check and kill the task processes corresponding to the background applications, and then reclaim the memory resources occupied by the task processes corresponding to the background applications until the operation of killing the task processes is completed. After the memory resources are recovered, the memory resources can be allocated from the system memory to the task processes that currently apply for memory resources.
[0018] Based on this, when the allocatable memory resources of the system memory have not dropped below the min waterline, the first application can directly apply for memory resources from the system memory. When the allocatable memory resources of the system memory drop below the min waterline, triggering the operation of killing the second task process, the memory allocation operation of allocating memory resources to the first task process from the reserved memory is performed simultaneously, so that the memory allocation operation will not be blocked in the operation of killing the task process, and the memory allocation of the task process corresponding to the foreground application can be completed in time, thereby ensuring the user experience of using the foreground application.
[0019] According to the first aspect, or any implementation method of the first aspect above, the situation where the allocatable memory resources of the system memory are greater than or equal to the memory resources corresponding to the first memory waterline includes: the allocatable memory resources of the system memory are greater than or equal to the memory resources corresponding to the second memory waterline; or, the allocatable memory resources of the system memory are less than the memory resources corresponding to the second memory waterline, and greater than or equal to the memory resources corresponding to the first memory waterline; wherein the memory resources corresponding to the second memory waterline are greater than the memory resources corresponding to the first memory waterline.
[0020] The second internal waterline is, for example, the low waterline mentioned in the following embodiments.
[0021] Since the memory resources corresponding to the second memory waterline are greater than the memory resources corresponding to the first memory waterline, and it can be seen from the above description of the low waterline that when the allocatable memory resources of the system memory drop below the low waterline, asynchronous memory recycling will be performed. Therefore, by further determining the relationship between the allocatable memory resources of the system memory and the memory resources corresponding to the second memory waterline, asynchronous memory recycling can be performed in a timely manner during the memory allocation process, thereby minimizing the operation of killing task processes.
[0022] According to the first aspect, or any implementation of the first aspect above, when the allocatable memory resources of the system memory are less than the memory resources corresponding to the second memory waterline, but greater than or equal to the memory resources corresponding to the first memory waterline, the method also includes: in the process of applying for memory resources from the allocatable memory resources of the system memory, reclaiming the memory resources occupied by the task processes of other applications running in the background of the electronic device, until the allocatable memory resources of the system memory are greater than or equal to the memory resources corresponding to the third memory waterline; wherein the memory resources corresponding to the third memory waterline are greater than the memory resources corresponding to the second memory waterline.
[0023] Among them, the third memory waterline, such as the high waterline mentioned in the following embodiment. When the allocatable memory resources of the system memory are not less than the memory resources corresponding to the high waterline, it means that the allocatable memory resources of the system memory are sufficient, and when the memory allocation operation occurs, the memory resources can be directly allocated without asynchronous memory recycling, and the operation of killing the task process will not be triggered.
[0024] According to the first aspect, or any implementation of the first aspect above, the method also includes: in the event of a failure in applying for memory resources from the reserved memory resources, reclaiming the memory resources occupied by the second task process; after adding the memory resources recovered from the second task process to the reserved memory and / or system memory, the first application applies for memory resources from the allocatable memory resources of the system memory; in the event of a failure in applying for memory resources from the system memory, applying for memory resources from the reserved memory resources, and reclaiming the third memory resources occupied by the third task process, the third task process being the task process of other applications running in the background of the electronic device, and the third task process is the task process that currently occupies the largest memory resources.
[0025] Among them, if the application of memory resources from the reserved memory fails, that is, the reserved memory resources are also insufficient. For this scenario, it is necessary to wait for the operation of killing the task process to be completed, reclaim the memory resources occupied by the task process, and then execute the operation of allocating memory resources to the first task process, so as to ensure that memory resources can always be allocated to the first task process.
[0026] According to the first aspect, or any implementation method of the first aspect above, the memory resources recovered from the second task process include a first memory block and a second memory block; the memory resources recovered from the second task process are added to the reserved memory and / or the system memory, including: detecting whether the reserved memory resources are less than the memory resources pre-applied from the system memory; when the reserved memory resources are less than the memory resources pre-applied from the system memory, adding the first memory block and the second memory block to the reserved memory.
[0027] Understandably, since the memory resources corresponding to the reserved memory are smaller than the memory resources corresponding to the system memory, the memory resources that need to be filled are smaller than the memory resources required by the system memory. Therefore, after the memory resources are recovered through the task detection and killing process, the reserved memory is filled first to ensure that the allocatable memory resources managed by the reserved memory can always be no less than the size of the memory resources pre-applied from the system memory. In this way, when the allocatable memory resources of the system memory are insufficient, such as being smaller than the memory resources corresponding to the first memory waterline, it can also be ensured that memory resources can be applied for from the fully filled reserved memory, thereby avoiding memory allocation being blocked in the task detection and killing process.
[0028] According to the first aspect, or any implementation method of the first aspect above, adding a first memory block and a second memory block to the reserved memory includes: adding the first memory block to the reserved memory; detecting whether the reserved memory resources are less than the memory resources pre-applied from the system memory; when the reserved memory resources are less than the memory resources pre-applied from the system memory, adding the second memory block to the reserved memory.
[0029] In this way, in the process of adding the memory resources recovered by killing the task process to the reserved memory, by detecting the allocatable memory resources of the reserved memory, it is avoided that when the reserved memory is sufficient (or reaches a preset threshold), memory resources are added to the reserved memory, causing a waste of resources.
[0030] According to the first aspect, or any implementation of the first aspect above, the method further includes: when the reserved memory resources are greater than or equal to the memory resources pre-applied from the system memory, adding the second memory block to the system memory.
[0031] Therefore, when a large amount of memory resources are recovered by killing the task process, after the reserved memory is filled, the remaining memory resources continue to be filled back into the system memory, so that the system memory can be restored to the min waterline, or even the low waterline, or above the high waterline in time.
[0032] According to the first aspect, or any implementation of the first aspect above, the method further includes: when the reserved memory resources are greater than or equal to the memory resources pre-applied from the system memory, adding the first memory block and the second memory block to the system memory.
[0033] Therefore, when there is no need to add memory resources to the reserved memory, all memory resources recovered by killing the task process are directly added to the system memory, so that the system memory can be restored to the min waterline, or even the low waterline, or above the high waterline in time.
[0034] According to the first aspect, or any implementation of the first aspect above, the first application is a photo-taking application.
[0035] Exemplarily, in some possible implementations, the first application may also be a browser type, social media type, game type, multimedia type, virtualization type, or other application that has a large load and requires a large amount of memory resources.
[0036] In this way, by applying for a portion of memory resources from the system memory in advance as reserved memory for separate management, when these applications with heavy loads that require a large amount of memory resources are running, in the process of allocating memory resources for the task processes of these applications, even if the allocatable memory resources of the system memory are insufficient and the background process check operation is triggered, these applications can also apply for memory resources from the allocatable memory resources managed by the reserved memory, effectively avoiding the memory application process of the task processes of these applications being blocked in the task process checking process, thereby ensuring the user experience.
[0037] In a second aspect, the present application provides an electronic device. The electronic device includes: a memory and a processor, the memory and the processor are coupled; the memory stores program instructions, and when the program instructions are executed by the processor, the electronic device executes instructions of the method in the first aspect or any possible implementation of the first aspect.
[0038] The second aspect and any implementation of the second aspect correspond to the first aspect and any implementation of the first aspect respectively. The technical effects corresponding to the second aspect and any implementation of the second aspect can refer to the technical effects corresponding to the first aspect and any implementation of the first aspect, which will not be repeated here.
[0039] In a third aspect, the present application provides a computer-readable medium for storing a computer program, wherein the computer program includes instructions for executing the method in the first aspect or any possible implementation of the first aspect.
[0040] The third aspect and any implementation of the third aspect correspond to the first aspect and any implementation of the first aspect, respectively. The technical effects corresponding to the third aspect and any implementation of the third aspect can refer to the technical effects corresponding to the first aspect and any implementation of the first aspect, which will not be repeated here.
[0041] In a fourth aspect, the present application provides a computer program, comprising instructions for executing the method in the first aspect or any possible implementation of the first aspect.
[0042] The fourth aspect and any implementation of the fourth aspect correspond to the first aspect and any implementation of the first aspect, respectively. The technical effects corresponding to the fourth aspect and any implementation of the fourth aspect can refer to the technical effects corresponding to the above-mentioned first aspect and any implementation of the first aspect, which will not be repeated here.
[0043] In a fifth aspect, the present application provides a chip comprising a processor, wherein the processor is used to call a computer program in a memory to execute the method in the first aspect or any possible implementation manner of the first aspect.
[0044] The fifth aspect and any implementation of the fifth aspect correspond to the first aspect and any implementation of the first aspect, respectively. The technical effects corresponding to the fifth aspect and any implementation of the fifth aspect can refer to the technical effects corresponding to the first aspect and any implementation of the first aspect, which will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] Figure 1 A memory application logic diagram is shown as an example;
[0046] Figure 2A Based on Figure 1 The memory application logic shown is a user interface diagram that causes the interface to freeze during the memory application process;
[0047] Figure 2B for Figure 2A Schematic diagram of the corresponding time trend in different scenarios;
[0048] Figure 3 Based on Figure 1 The memory application logic shown is a user interface diagram of the application crashing during the memory application process;
[0049] Figure 4 Based on Figure 1 The memory application logic shown is a user interface diagram of the background application being cleared during the memory application process;
[0050] Figure 5 A schematic diagram of the timing between various execution entities in the memory allocation method provided in an embodiment of the present application is exemplarily shown;
[0051] Fig. 6A and Figure 6BA schematic diagram showing an exemplary method of setting the reserved memory using the first setting method;
[0052] Figure 6C A schematic diagram showing an exemplary method of setting the reserved memory using the second setting method;
[0053] Fig.6D A schematic diagram showing an exemplary method of setting the reserved memory using the third setting method;
[0054] Fig. 7A A flowchart of a memory allocation method provided by an embodiment of the present application is exemplified;
[0055] Figure 7B A flowchart of a memory allocation method provided by an embodiment of the present application is exemplified;
[0056] Figure 8 A flowchart of a memory allocation method provided by another embodiment of the present application is exemplified;
[0057] Fig. 9 is a schematic diagram of the hardware structure of an electronic device shown as an example;
[0058] Fig.10 The figure is a schematic diagram of the software structure of an electronic device for example. DETAILED DESCRIPTION
[0059] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0060] The term "and / or" in this article is merely a description of the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone.
[0061] The terms "first" and "second" in the description and claims of the embodiments of the present application are used to distinguish different objects rather than to describe a specific order of objects. For example, a first target object and a second target object are used to distinguish different target objects rather than to describe a specific order of target objects.
[0062] In the embodiments of the present application, words such as "exemplary" or "for example" are used to indicate examples, illustrations or descriptions. Any embodiment or design described as "exemplary" or "for example" in the embodiments of the present application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of words such as "exemplary" or "for example" is intended to present related concepts in a specific way.
[0063] In the description of the embodiments of the present application, unless otherwise specified, the meaning of "multiple" refers to two or more than two. For example, multiple processing units refer to two or more processing units; multiple systems refer to two or more systems.
[0064] Memory, also known as main memory, is the storage space that can be directly addressed by the central processing unit (CPU) (hereinafter referred to as system memory). In order to speed up reading and writing, processes (applications) in electronic devices usually need to use memory to complete data processing during the startup process.
[0065] For example, during the startup process, a process (application) will obtain data required by the task process, such as obtaining the data required by the task process from a physical memory.
[0066] For example, in this process, the task process needs to apply for memory first, for example, through the malloc function. Understandably, what is applied for at this time is a continuous 4G virtual memory address, and the corresponding page table entry will be set. However, physical memory will not be allocated, that is, the corresponding physical memory address will not be mapped.
[0067] Then, after completing the above operations, the task process will read the page table entries set above. During this process, the CPU will access the virtual memory. Since the corresponding virtual memory is not mapped to the physical memory, it cannot obtain the required data, and a page fault will occur. At this time, the CPU will call the page fault interrupt function. If there is enough memory when the page fault interrupt function is called, memory will be allocated for it, and the code and data of the task process in the disk will be copied to the physical memory, and a mapping from the virtual memory address to the physical memory address will be established.
[0068] It should be noted that memory application is closely related to memory watermark. Specifically, when triggering memory application, the memory watermark needs to be checked first. If the memory watermark does not meet the conditions, the memory recycling process will be triggered to obtain memory that can be applied.
[0069] However, in actual usage scenarios, especially in low-memory scenarios, such as memory overload scenarios, gaming scenarios, and photo-taking scenarios, the kernel may be unable to obtain sufficient memory space (memory size / memory amount) through memory recycling. In this case, it can only trigger OOM (out of memory), that is, to reclaim memory by killing the application (memory) (killing the process), so as to obtain enough memory to complete the memory application. Figure 1 As shown, the two are in a linear relationship. That is, when the available memory space of the system memory (hereinafter referred to as: the remaining free memory amount) is insufficient (for example, less than the memory application amount of this memory application), the memory application processing flow needs to wait for the process killing process to be completed before it can be executed. However, during the application killing process, the kernel needs to make various judgments and calculate the complexity of the scene, and perform different levels of killing in turn.
[0070] For example, in the current chip platform, if the application killing is triggered by the user layer, in some possible implementations, the user layer can kill the task process through the "kill-9pid" command to reclaim the memory. If the application killing is triggered by the kernel layer, in some possible implementations, the kernel can directly kill the task process through OOM.
[0071] For example, in the process of killing the task process, the kernel will apply for (reclaim) 100MB of memory space every 200ms. Therefore, if more memory space needs to be reclaimed, the killing time will be longer. This makes the killing time uncontrollable, and the memory application is blocked in the process of killing the application to release the memory. Therefore, it will cause the started process to fail to apply for the appropriate memory space in time, and the memory pages that need to be obtained cannot be read from the disk to the memory space in time, causing the electronic device to freeze, the application to crash, and other applications in the background to be cleaned up.
[0072] In order to better understand the above abnormal phenomena caused by memory application in electronic devices, the following Figure 2A , Figure 2B ,as well as Figure 3 and Figure 4 Provide specific instructions.
[0073] The following is the relevant content related to the phenomenon of electronic devices freezing due to memory application:
[0074] For example, in a possible implementation, when an electronic device, such as a mobile phone, is in a photo-taking scene, its corresponding user interface may be as follows: Figure 2A The user interface 10a shown in (1) .
[0075] See also Figure 2AIn (1), the user interface 10a may include one or more functional controls / options, which are not limited in this embodiment. For example, when the mobile phone is in the user interface 10a, if the user wants to view the taken photos, videos, etc., a feasible way may be, for example, by clicking the control 10a-1 in the user interface 10a.
[0076] For example, after the user clicks on the control 10a-1, the mobile phone responds to the operation behavior and can Figure 2A The user interface 10a shown in (1) jumps to the user interface corresponding to the gallery application, such as Figure 2A The user interface 10b is shown in (2).
[0077] against Figure 2A In the scenario shown in FIG. 1 , when the system memory is sufficient, that is, no processing flow such as killing is required to reclaim memory, when the user clicks the control 10a-1 displayed in the user interface 10a, for example Figure 2B As shown in FIG. 1 , after the control 10 a - 1 is clicked at time T0 , the mobile phone will respond to the operation immediately, such as displaying the user interface 10 b at time T1 .
[0078] In addition, it should be noted that due to Figure 2B In the case of (1), the memory application process will not be blocked in the killing process, so the speed of adjusting from the user interface 10a to the user interface 10b is fast, that is, the time difference between T0 and T1 is small, and the user does not perceive it.
[0079] Continue to see Figure 2B , for example, in the case of insufficient system memory, through the above Figure 1 As can be seen from the description, the memory application processing flow will be blocked in the process of checking and killing, that is, the memory application processing flow will not be executed until the checking and killing is completed and the remaining free memory of the system memory is sufficient for the memory application required for this memory application. Therefore, in this case, when the user clicks the control 10a-1 at time T0, the mobile phone will respond to the operation behavior and will first execute the process of checking and killing. If the process of checking and killing has not been completed at time T1, the mobile phone will still display the user interface 10a at time T1.
[0080] Continue to see Figure 2B For example, if the mobile phone completes the killing after time T1 and before time T2, and the remaining free memory of the system memory is sufficient for the memory application required for this memory application, the memory application processing flow will be executed at this time, and then after obtaining the corresponding memory space and reading the memory pages in the disk into the memory space, for example, the above operations are completed at time T2, and the mobile phone can jump from user interface 10a to user interface 10b.
[0081] It should be noted that when an electronic device loses 2 frames (or more), the user will feel the interface freeze. Figure 2B In the case of (1), the time difference between T0 and T1 is usually less than the memory supply time corresponding to 2 frames. Figure 2B In case (2), the time difference between T0 and T2 is usually not less than the memory supply time corresponding to 2 frames.
[0082] The memory supply time mentioned above is usually related to the refresh rate of the electronic device. Therefore, for electronic devices with different refresh rates, the corresponding memory supply time is different.
[0083] For example, for an electronic device with a refresh rate of 120 Hz, the memory supply delay is about 8 ms. Therefore, for this type of electronic device, the time difference between T0 and T1 is less than 16 ms, and the time difference between T0 and T2 is not less than 16 ms (minimum 16 ms).
[0084] For example, for an electronic device with a refresh rate of 90 Hz, the memory supply delay is about 11 ms. Therefore, for this type of electronic device, the time difference between T0 and T1 is less than 22 ms, and the time difference between T0 and T2 is not less than 22 ms (minimum 22 ms).
[0085] For example, for an electronic device with a refresh rate of 60 Hz, the memory supply delay is about 16 ms. Therefore, for this type of electronic device, the time difference between T0 and T1 is less than 32 ms, and the time difference between T0 and T2 is not less than 32 ms (minimum 32 ms).
[0086] It should be understood that the above description is merely an example listed for a better understanding of the technical solution of this embodiment, and is not intended to be the sole limitation to this embodiment.
[0087] The following is related content related to the phenomenon of application crashes due to memory requests on electronic devices:
[0088] For example, in another possible implementation, when an electronic device, such as a mobile phone, is in a photo-taking scene, its corresponding user interface may be as follows: Figure 3 The user interface 10a shown in (1) is shown in FIG. Figure 3 The user interface 10a shown in (1) is the same as Figure 2A The user interface 10a shown in (1) of FIG. 1 , the functional controls / options included in the user interface, and the method of opening the gallery application in the user interface 10a are similar to those described above for Figure 2A The description of (1) is the same as that of (1) and will not be repeated here.
[0089] For example, after the user clicks on the control 10a-1, the mobile phone responds to the operation. If the system memory is insufficient at this time, in addition to the above-mentioned Figure 2B In the case of (2), there may also be Figure 3 That is, after the user clicks on the control 10a-1, the mobile phone will flash back in response to the operation, such as directly exiting the camera application (the camera application may be killed) and returning to the main interface of the mobile phone, such as Figure 3 The user interface 10c shown in (2) in the figure has a serious impact on the user experience.
[0090] It should be understood that the above description is merely an example listed for a better understanding of the technical solution of this embodiment, and is not intended to be the sole limitation to this embodiment.
[0091] The following is the relevant content related to the cleaning of other applications in the background due to memory application on electronic devices:
[0092] Understandably, for the sake of user experience and performance, the operating system currently used by electronic devices does not actually kill the task process corresponding to the application that has retreated to the background, but caches it. In this way, when the user uses the application again, it can be directly awakened from the background by hot start, without having to restart the application by cold start, so that the application in the background can be quickly started and accessed. However, as more applications are opened, more task processes are cached in the background, and more and more system memory is occupied. Therefore, when the system memory is insufficient, the kernel will kill some or all of the cached task processes according to the corresponding recycling mechanism. That is, the application in the background is cleaned up.
[0093] For example, in another possible implementation, the task process that is cached can be returned to the background as follows: Figure 4 The user interface 10d (multi-task management interface) shown in (1) includes a task process corresponding to the "software update" interface / application, a task process corresponding to the "information" application, and the like.
[0094] In the above scenario, if the user clicks on the main interface, Figure 3 The icon of the camera application in the user interface 10c is displayed. The mobile phone responds to the operation. If the system memory is insufficient at this time, in one possible case, a freeze may occur, that is, after a certain delay (the length of time perceivable by the user), the memory is requested, and then the camera application is started and switched to the user interface 10a; in another possible case, while the freeze occurs, the task process in the background will also be killed.
[0095] For the sake of convenience, this embodiment takes the killing of a background task process as an example. Figure 4 In (2), for example, after starting the camera application and displaying the user interface 10a, if the user slides upward from the bottom of the mobile phone screen in the direction of the arrow, the mobile phone will respond to the operation and return to the main interface, such as the user interface 10c, and at the same time retreat the camera application to the background cache.
[0096] See also Figure 4 In (3), for example, after returning to the user interface 10c, if the user slides upward from the bottom of the mobile phone screen in the direction of the arrow, the mobile phone will switch to the multitasking management interface in response to the operation behavior, such as Figure 4 The user interface 10d shown in (4) .
[0097] See also Figure 4 In (4), for example, when the camera application is started, the system memory is insufficient, and the task process cached in the background is killed (taking killing all the cached task processes as an example). Figure 4 In the method shown in (2), after the camera application is put into the background, the cached task process is only that of the camera application, and the task process corresponding to the "Software Update" interface / application and the task process corresponding to the "Message" application have been cleared. In this case, if the user wants to open the message application or access the "Software Update" interface again, the phone must be cold-started, which requires more time to wait.
[0098] For cold start and hot start, please refer to existing literature, which will not be explained in this embodiment.
[0099] It should be understood that the above description is only an example of a scenario of abnormal phenomena caused by memory application for a better understanding of the technical solution of this embodiment, and is not intended to be the only limitation to this embodiment.
[0100] In view of this, in order to solve the above-mentioned problems of electronic device freezes, application crashes, and other applications in the background being cleared due to memory applications, the present application provides a memory allocation method. The memory allocation method reserves part of the memory space, so that when there is currently no free memory of sufficient size available for application, the required memory can be directly applied from the reserved part of the memory space without waiting for the killing process to be completed and the memory application process can only be performed when the available memory is released. Since the memory application is no longer blocked in the killing process, the memory application in specific scenarios, especially low-memory scenarios, is greatly accelerated, thereby solving the phenomenon of electronic device freezes, application crashes, and other applications in the background being cleared due to memory applications, thereby ensuring the user experience.
[0101] In order to facilitate the description of the memory allocation method provided by this application, first combine Figure 5 , explained from the perspective of user mode and kernel mode. It should be understood that Figure 5 This is only an exemplary description. In actual implementation, other functional modules may be involved, and the operations implemented by the kernel may also be implemented by different functional modules. For example, Fig.10 The memory management module, memory recovery module, etc. shown in the figure can be specifically determined according to actual conditions, and this embodiment does not impose any limitation on this.
[0102] See also Figure 5 , exemplarily, the memory allocation method provided in this embodiment may include steps 201 to 207.
[0103] 201, APP1 has a memory application requirement.
[0104] The memory application requirement indicates that APP1 needs to obtain memory space, and APP1 may be any application installed in the electronic device.
[0105] From the above description of memory, it can be seen that in order to speed up the reading and writing speed, during the startup of APP1 (the corresponding task process), it is usually necessary to use the memory to complete data processing. That is, the memory page required by APP1 is obtained from the linked list corresponding to the memory. However, when the memory page required by APP1 does not exist in the memory, a page fault interrupt will occur. In this case, APP1 has a memory application requirement. That is, the premise for triggering the memory application is that a page fault interrupt has occurred.
[0106] 202, memory application information of APP1.
[0107] For example, when APP1 has a memory application requirement, the task process corresponding to APP1 may obtain the memory space required by APP1 by sending memory application information to the kernel (eg, the memory management module integrated in the kernel as described below).
[0108] 203. Determine the memory that APP1 needs to apply for according to the memory application information.
[0109] The memory application information sent by the task process to the kernel may include order information, wherein the order information may indicate the size of the memory space to be applied for this memory application, that is, the memory that APP1 in this embodiment needs to apply for.
[0110] In addition, in some possible implementations, the memory application information may further include flag information, wherein the flag information may be used to determine how the memory space to be applied for by the task process that sends the memory application information needs to be applied for.
[0111] 204 , allocating the required memory for the task process of APP1 from the current system memory.
[0112] Specifically, after receiving the memory application request from the task process of APP1, the kernel will allocate the required memory to the task process of APP1 from the current system memory.
[0113] Exemplarily, if the kernel allocates the required memory to the task process of APP1 through the operation of step 204, it indicates that the current system memory is sufficient, and the memory application process triggered by the task process of APP1 is also executed.
[0114] For example, if the kernel fails to allocate the required memory to the task process of APP1 through the operation of step 204, it indicates that the current system memory is insufficient. In some possible implementations, the kernel first reclaims memory through a memory reclaim operation to obtain sufficient system memory, and then allocates the required memory to APP1. In other possible implementations, if the memory application requirements of APP1 cannot be met through reclaiming, a killing operation can be performed through OOM.
[0115] Specifically, in the technical solution provided in this embodiment, in the face of the above-mentioned scenario of insufficient system memory, that is, when the kernel cannot obtain the memory required by APP1 from the system memory, the reserved memory will be enabled. Based on the reserved memory, the memory application requirements of APP1 can be met first. Then, after the reserved memory is used to complete the memory allocation for APP1, or in the process of using the reserved memory to allocate memory for APP1, the killing operation is performed by means of OOM. In this way, the memory application of APP1 will not be blocked in the killing process.
[0116] 205 , allocating memory from the reserved memory to the task process of APP1.
[0117] The reserved memory is a portion of the memory space that the kernel pre-applies from the system memory. That is, the reserved size in the system memory is used as the reserved memory. Since the reserved memory is pre-applied, the system memory considers the reserved memory as the allocated memory space. That is, when determining the current available memory space of the system memory, the reserved memory is not included, but the memory space of the reserved memory can be allocated.
[0118] From the above description of reserved memory, it can be seen that when the kernel cannot obtain the memory required by APP1's task process from the system memory, and the kernel cannot obtain enough memory by performing memory recovery operations, the kernel will obtain the memory required by APP1's task process from the reserved memory according to the memory allocation method provided by this application, and allocate the memory to APP1's task process. Due to the preset reserved memory, even if sufficient memory cannot be obtained through memory recovery, and the killing operation is triggered, the reserved memory can be used to complete this memory allocation, thereby avoiding the memory application processing flow being blocked in the killing operation processing flow.
[0119] It should be noted that, in order to ensure that the reserved memory of the reserved size mentioned above can satisfy the current memory application, in some possible implementations, the reserved memory needs to satisfy at least one memory application.
[0120] In addition, in order to avoid the reserved memory occupying too much memory space and affecting the use of system memory, in some possible implementations, the reserved memory can be set to be no larger than the memory application requirement twice.
[0121] It should be noted that in actual operations, the memory required for each memory application is not fixed. However, the memory required for a memory application is usually within 10MB. Therefore, in some possible implementations, the range corresponding to the reserved memory may be [10MB, 20MB].
[0122] In addition, in some other possible implementations, the preset memory can also be set by counting the amount of memory recovered per unit time during the killing process. For example, 10% of the amount of memory recovered per unit time (in actual operation, it can be set as needed, and can be larger or smaller than 10%) is set as the preset memory.
[0123] Regarding the above-mentioned unit time, for example, 200 ms. From the above description, it can be known that when the kernel kills the task process through the "kill-9pid" command, 100 MB of memory space can be applied for (reclaimed) every 200 ms. For this scenario, the second memory amount is, for example, 10 MB.
[0124] It should be understood that the above-mentioned descriptions of various ways of setting the reserved memory are merely examples listed for a better understanding of the technical solution of this embodiment, and are not intended to be the sole limitation to this embodiment.
[0125] Regarding the setting method of the first reserved memory, the following three setting methods are described in detail below.
[0126] The first setting method:
[0127] Understandably, memory (system memory) is one of the most important core resources in a computer system. The Buddy system is the lowest-level memory management mechanism of Linux, which uses Page granularity to manage memory. Usually, the size of a Page is 4K, and the smallest unit of allocation, release, and recycling in the Buddy system is Page. That is, all the content and system memory in the system are in Buddy.
[0128] For the management of system memory in Buddy, three memory watermarks are currently set. In order of high, low, and min, Fig. 6A shown.
[0129] It should be noted that, ideally, Buddy's system memory is above high. If it is lower than high, or low, or min, different operations will be triggered, such as process recycling (release of file pages) and transfer of anonymous pages (cannot be released directly, but need to be transferred from memory to swap partition).
[0130] Because under high load conditions, the system memory is already very tight. Therefore, frequent memory applications have a fierce competition for the Buddy lock (only after seizing the memory pool lock can memory space be obtained from the memory pool). Therefore, in order to alleviate the competition for the Buddy corresponding lock, in a possible implementation, the memory allocation can be implemented based on the PCP mechanism. That is, in some possible situations, an asynchronous filling memory pool can be created directly and independently of the Buddy as reserved memory.
[0131] It should be noted that the PCP mentioned above is per_cpu_pageset. For its specific characteristics and uses, please refer to existing documents, and this application will not explain it again.
[0132] In addition, it should be noted that when the memory pool is filled asynchronously as reserved memory, the memory space to be applied for needs to be applied for from the system memory in Buddy. Fig. 6A As shown, a memory pool / process pool can be created in advance as reserved memory, and then a preset size of memory can be applied to the system memory as reserved memory. The pre-approved size of memory applied for can be, for example, a value between 10M and 20M. Another example is 10% of the memory recovered by a check and kill during the historical use of the device. This application does not impose any restrictions on this.
[0133] Exemplarily, the kernel applies for corresponding memory space from the system memory according to the above preset size, and allocates the applied memory of the preset size to the reserved memory, such as Figure 6B As shown in the figure, the pre-set reserved memory is realized.
[0134] For the first setting method, it is sufficient to ensure that the preset size of memory can be applied from the system memory as reserved memory. As for whether the system memory meets the setting of reserved memory, it can be dynamically checked and killed based on the three memory waterlines mentioned above to reclaim memory and ensure that the system memory can use the preset size of memory as reserved memory.
[0135] Regarding the operations triggered based on the three memory watermarks, for example, when the system memory drops below the low watermark, the kswapd thread is awakened to perform asynchronous memory recycling until the system memory is recycled to the high watermark. The memory recycling performed in this process is executed in parallel with the memory application, so the memory recycling process will not block the ongoing memory application process. However, if the system memory drops below the min watermark, the memory application process needs to be blocked and the killing operation process needs to be executed first.
[0136] That is to say, when using the first setting method to set the reserved memory, the kernel will trigger the check and kill at the appropriate time according to the three memory waterlines of high, low, and min, and then recycle the memory to ensure that the preset size mentioned above, such as 20M, is used as reserved memory.
[0137] In addition, it should be noted that based on the reserved memory set in the first setting method, when the system memory is insufficient and the application is triggered to kill, after obtaining the recycled memory, it will first determine whether the memory in the asynchronous filling memory pool serving as the reserved memory is at the reserved memory waterline. If not, the recycled memory will be filled into the asynchronous filling memory pool first, otherwise it will be directly returned to Buddy, that is, the system memory.
[0138] When refreshing from the asynchronous filling memory pool to Buddy, the system will make relevant hooks and then formally fill the reclaimed memory into the corresponding memory pool, such as the asynchronous filling memory pool or Buddy. Since the asynchronous filling memory pool created based on the PCP mechanism is lock-free and the refresh is a one-time refresh, not a single-page refresh, you only need to hold the lock once to fill the reclaimed memory from Buddy to the asynchronous filling memory pool, thereby reducing the competition for the Buddy lock and greatly reducing the kernel pressure.
[0139] The second setting method:
[0140] From the above description of the functions of the three memory watermarks, we can know that when the system memory drops below the low watermark, the kswapd thread is awakened for asynchronous memory recovery; when the system memory drops below the min watermark, the memory application process needs to be blocked and the killing process is executed first. Therefore, in the second setting method, in the process of setting the reserved memory, the original low watermark can be dynamically raised, and then the system memory between the raised low watermark and the original low watermark is used as the reserved memory.
[0141] For example Figure 6C As shown in the figure, the original low watermark is regarded as low_old. According to business needs, such as the size of reserved memory that needs to be set, low_old is raised to the position of low_new watermark. In this way, when the system memory drops below the low_new watermark, the kswapd thread will be awakened for asynchronous memory recycling.
[0142] Therefore, through this setting method, the kernel can wake up the kswapd thread in advance to perform asynchronous memory recovery, thereby ensuring that there is always allocable memory in the system memory and avoiding the memory application process being blocked in the killing process.
[0143] The third setting method:
[0144] Set a waterline between the original low waterline and the min waterline, such as Fig.6D The pre_low shown in the figure, and then the memory space between low and pre_low is used as reserved memory.
[0145] Since the kernel will wake up the kswapd thread to perform asynchronous memory recovery when the system memory drops to low, the memory space between low and pre_low can be allocated and used normally, thus avoiding the memory application processing flow being blocked in the application killing processing flow.
[0146] It should be understood that the description of the above three configuration modes is merely an example listed for a better understanding of the technical solution of this embodiment, and is not intended to be the sole limitation to this embodiment.
[0147] In addition, it should be noted that the specific locations of the three memory waterlines mentioned above can be set and adjusted by the kernel itself. As for the adjustment of the waterlines by the kernel, manufacturers of different electronic devices can customize them according to their needs.
[0148] In addition, it should be noted that the setting of the watermark and the operation of setting the reserved memory using the above three implementation methods can be, for example, each time the electronic device is turned on, when the program for setting the watermark and any of the above programs for setting the reserved memory are run, the setting of the watermark and the setting of the reserved memory are implemented by executing the corresponding program and then following the corresponding principles.
[0149] It should be understood that the description of the above three configuration modes is merely an example listed for a better understanding of the technical solution of this embodiment, and is not intended to be the sole limitation to this embodiment.
[0150] 206, memory allocated for the task process of APP1.
[0151] Exemplarily, after the memory management module processes the memory application processing flow triggered by the task process of APP1, it will inform the task process of the memory (memory space) obtained from the reserved memory and the address information of the specific memory space that can be allocated.
[0152] 207, perform memory recovery or kill operation.
[0153] It should be noted that, since the reserved memory is pre-set in the memory allocation method provided in this embodiment, when the system memory is insufficient, the memory management module can apply for memory from the reserved memory, that is, execute the memory application processing flow. At the same time, the memory recovery module in the kernel can also trigger the application killing processing flow according to the current memory pressure. That is, the memory application processing flow and the killing processing flow can be performed in parallel.
[0154] Therefore, by parallelizing the memory application and the killing process, the memory application process will not be blocked in the killing process.
[0155] In addition, it should also be understood that, since part of the memory space is reserved in advance (reserved memory), when the current system memory cannot meet the current memory application, the required memory can be directly applied from the reserved part of the memory space. That is, from the overall point of view, even if the killing is not currently performed, the smooth completion of the memory application can be guaranteed. Therefore, the memory application and the killing can also be processed linearly, that is, the memory application is executed first, and after the processing flow of the memory application is completed, the processing flow of the killing is executed. Since the processing flow of the memory application is executed before the processing flow of the killing, the processing flow of the memory application will not be blocked in the processing flow of the killing.
[0156] That is to say, based on the technical solution provided by this embodiment, whether the memory application and the killing are processed in parallel or processed linearly in the above order, the memory application process can be prevented from being blocked in the killing process. Therefore, based on the technical solution provided by this embodiment, the memory application in specific scenarios, especially in low-memory scenarios, can be accelerated, thereby solving the problems of electronic device freezes, application crashes, and other background applications being cleared due to memory applications, thereby ensuring user experience.
[0157] The following combination Fig. 7A , Figure 7B , Figure 8 and Fig. 9 , from the perspective of kernel state, the detailed processing logic of the memory allocation method provided by this application is explained.
[0158] Exemplarily, a functional module in the kernel, such as a memory management module, receives a memory request (such as Fig. 7A Step 301 / Figure 7B In step 401), the memory management module will directly obtain memory from the system memory, and allocate memory to the task process that triggered the memory request. It can be seen from the description of the above embodiment that when the system memory is sufficient, the operation will be completed directly. If the system memory is insufficient, memory recycling will be performed. If the system memory still cannot meet the current memory request after memory recycling, a killing operation will be triggered. In the memory allocation method provided in the present application, in the scenario of insufficient system memory, memory will be obtained from the reserved memory, and memory will be allocated to the task process that triggered the memory request. Therefore, in order to better understand these two scenarios, this embodiment shows the process of determining whether the system memory is sufficient in the corresponding drawings, such as Fig. 7A Step 302 / Figure 7B Step 402 in .
[0159] Continue to see Fig. 7A and Figure 7B , exemplarily, when the system memory is sufficient, the memory management module can directly handle the relevant processes of memory application, such as allocating the required memory to the task process that triggers the memory application, that is, executing step 303 / step 403.
[0160] Continue to see Fig. 7A and Figure 7B For example, when the system memory is insufficient, the memory allocation processing flow provided in this embodiment may be entered. Fig. 7A / Figure 7B In step 304 / step 404, it is first determined whether the reserved memory satisfies the memory application. Regarding the setting of the reserved memory, the three setting methods given in the above embodiment can be referred to, which will not be repeated here.
[0161] For example, if the amount of memory that can be currently allocated from the reserved memory satisfies the amount of memory required for this memory application, in a possible implementation, Fig. 7A In the manner shown, step 305 and step 306 can be executed in parallel. That is, the processing flow of allocating memory (memory application) and the processing flow of checking and killing are executed in parallel.
[0162] For example, if the amount of memory that can be currently allocated from the reserved memory satisfies the amount of memory required for this memory application, in another possible implementation, such as Figure 7B In the manner shown, step 405 may be executed first, and step 406 may be executed after step 405 is executed. That is, the memory allocation (memory application) process is executed first, and after the memory management module allocates the corresponding memory space for the task thread that initiates the memory application, the killing process is executed.
[0163] Regarding the scenario where the reserved memory meets the memory application, let's take an example. Assuming that the current reserved memory is 12M, if the memory size required for this memory application is 10M, it can be determined that the reserved memory meets the memory application. If the reserved memory is 12M and the memory size required for this memory application is 15M, it can be determined that the reserved memory does not meet the memory application.
[0164] It should be understood that the above description is merely an example listed for a better understanding of the technical solution of this embodiment, and is not intended to be the sole limitation to this embodiment.
[0165] Continue to see Fig. 7A and Figure 7B For example, if the amount of memory that can be currently allocated from the reserved memory does not meet the amount of memory required for this memory application, the memory management module will notify the memory recovery module to perform a check and kill based on the current memory pressure, that is, execute step 307 / step 407.
[0166] Continue to see Fig. 7A and Figure 7B For example, whether it is step 306 / step 406, reclaiming the memory obtained, or step 307 / step 407 reclaiming the memory obtained, before filling the reclaimed memory back into the system memory, it is necessary to first determine whether the reserved memory needs to be filled, that is, execute step 308 / step 408.
[0167] Continue to see Fig. 7A and Figure 7B For example, when the reserved memory needs to be filled, the reclaimed memory is first filled into the reserved memory, that is, a portion of the memory space is always guaranteed to be available for memory application, and memory application and killing can be processed in parallel as much as possible.
[0168] Taking the preset memory size of 12M as an example, after this memory application obtains 10M of memory from the 12M reserved memory, when the memory is reclaimed, the reclaimed memory can be filled into the currently reserved memory of only 2M, so that the memory space of the reserved memory returns to 12M.
[0169] In addition, it should be noted that if the reclaimed memory is much larger than the amount of memory that needs to be filled in the reserved memory, in one implementation, a portion of the reclaimed memory can be directly filled into the reserved memory, and the remaining portion can be filled back into the system memory. In other possible implementations, the remaining memory can be filled back into the system memory after the reserved memory is filled.
[0170] Still taking the preset memory size of 12M as an example, if the memory recovered this time is 15M, 10M can be filled into the reserved memory so that the allocatable memory of the reserved memory is restored from 2M to 12M, and the excess 5M can be filled back into the system memory.
[0171] It should be understood that the above description is merely an example listed for a better understanding of the technical solution of this embodiment, and is not intended to be the sole limitation to this embodiment.
[0172] Continue to see Fig. 7A and Figure 7B ,For example, if the amount of memory that can be allocated by the reserved memory is full, the reclaimed memory can be directly filled into the system memory.
[0173] In addition, it should be noted that, when the memory recovery module is triggered to kill because the reserved memory does not meet the memory application, that is, step 307 / step 407 is executed, after completing one or a set number of kills, or after the set time is recovered according to the corresponding command, the memory management module can re-execute step 302 / step 402. Then, according to the judgment result, Fig. 7A and the processing logic shown in FIG. B is executed.
[0174] In addition, it should be noted that in some possible implementations, when the system memory is insufficient, that is, when the judgment result of step 302 / step 402 is "no", while executing step 304 / step 404, an asynchronous line process can also be started to execute step 306 / step 406 or step 307 / step 407 in parallel. That is, when judging whether the reserved memory meets the memory application amount required for this memory application, the killing process is executed in parallel.
[0175] It should be understood that the above description is merely an example listed for a better understanding of the technical solution of this embodiment, and is not intended to be the sole limitation to this embodiment.
[0176] Therefore, the memory allocation method provided in this embodiment reserves part of the memory space (reserved memory), so that when the current system memory does not have enough memory to allocate, the required memory can be directly applied from the reserved part of the memory space, without waiting for the killing process to be completed and the memory application process to be performed when the available memory is released. Since the memory application is no longer blocked in the killing process, the memory application in specific scenarios, especially low memory scenarios, is greatly accelerated, thereby solving the problem of electronic device freezes caused by memory application.
[0177] See also Figure 8 , which exemplifies another memory allocation method of the present application. Specifically, in actual application, when the electronic device is in a specific scenario, such as a low memory scenario (such as a memory overload scenario, a game scenario, a camera photo scenario), memory requests will be frequently triggered, while other scenarios (hereinafter referred to as normal memory scenarios) will not. That is, in a normal memory scenario, using Figure 1 In the memory application method shown, even if the memory application process is blocked in the process of checking and killing, the time is short and the user will not be aware of it. That is, there will be no interface freeze, application crash, or background application cleanup. Therefore, in order to reasonably use the kernel and reduce unnecessary judgment and processing, in some possible implementations, it can be set that when the electronic device is currently in a low memory scenario, Fig. 7A or Figure 7B The memory allocation method shown is used in normal memory scenarios. Figure 1 The memory allocation method shown.
[0178] Continue to see Figure 8 This embodiment includes steps 501 to 509, wherein steps 503 to 509 are Figure 7B Steps 402 to 408 in the illustrated embodiment are the same and will not be described in detail herein.
[0179] Continue to see Figure 8 ,exist Figure 8 In the illustrated embodiment, upon receiving a memory request (step 501), a functional module in the kernel, such as a memory management module, will first determine the current usage scenario of the electronic device (step 502). The current usage scenario of the electronic device can be determined by the currently running task process, CPU resources, memory pressure, etc. The specific determination method is not limited in this embodiment.
[0180] Continue to see Figure 8 , for example, when it is determined that the current scenario is low memory, Figure 8 The memory allocation method shown is as follows Figure 8The processing logic of step 503 to step 509 shown in the figure performs memory allocation. Otherwise, when it is determined that the current memory scene is normal, the memory allocation is performed. Figure 1 The memory allocation method shown is as follows Figure 1 The processing logic from step 102 to step 105 shown is for performing memory allocation.
[0181] It should be understood that Figure 8 is Figure 7B The implementation in the illustrated scenario is only an example for better understanding the technical solution of this embodiment and is not the only limitation to this embodiment. Figure 8 The solution shown in the figure introduces scenario judgment, and then adopts different memory application processes according to different scenarios. Fig. 7A Implemented in the scenario shown.
[0182] In addition, it should be noted that in the memory allocation method provided in this application, in addition to executing application killing according to the "kill-9pid" command, the kernel can also call killing-related interface functions such as add_to_oom_reaper to implement application killing.
[0183] In addition, it should be noted that in the memory allocation method provided in the present application, the kworker can implement the memory application processing flow and the killing processing flow in parallel.
[0184] Understandably, kworker is a placeholder process for the kernel worker thread, which performs most of the actual processing of the kernel, especially in the case of interrupts, timers, I / O, etc. Regarding the working mechanism of kworker to implement the above two processing flows in parallel, please refer to the existing literature, and this application will not explain it again.
[0185] In order to better understand the technical solution provided by the embodiments of the present application, before describing the technical solution of the embodiments of the present application, the hardware structure and software architecture of the electronic device to which the embodiments of the present application are applicable are first described in conjunction with the accompanying drawings.
[0186] The electronic devices to which the embodiments of the present application are applicable may be, for example, mobile phones, tablet computers, smart watches, PC devices, etc., which are not listed one by one here, and the present embodiment does not limit this. For the sake of convenience, the present embodiment is described by taking a mobile phone as an example.
[0187] See also Fig. 9The mobile phone 100 may include: a processor 110, an external memory interface 120, an internal memory 121, a universal serial bus (USB) interface 130, a charging management module 140, a power management module 141, a battery 142, an antenna 1, an antenna 2, a mobile communication module 150, a wireless communication module 160, an audio module 170, a sensor module 180, a button 190, a motor 191, an indicator 192, a camera 193, a display screen 194, and a subscriber identification module (SIM) card interface 195, etc.
[0188] Among them, the processor 110 may include one or more processing units, for example: the processor 110 may include an application processor (application processor, AP), a modem processor (Modem), a graphics processor (graphics processing unit, GPU), an image signal processor (image signal processor, ISP), a controller, a video codec, a digital signal processor (digital signal processor, DSP), a baseband processor, and / or a neural-network processing unit (neural-network processing unit, nPU), etc., which are not listed one by one here and the present application does not limit this.
[0189] In a possible implementation, the memory allocation method provided in each embodiment of the present application can be implemented by a controller, a core, etc. included in a processor. Exemplarily, in this implementation, various memory processing involved in the memory allocation process, such as memory application, memory recycling, and killing operations, are all implemented by the core.
[0190] In another possible implementation, the memory allocation method provided by each embodiment of the present application can be implemented by the controller and kernel included in the processor, as well as the memory management module and memory recovery module located in the application framework layer. Exemplarily, in this implementation, the memory management module can interact with the kernel, hardware, etc. through the interface provided by the kernel layer, and then determine the current remaining free memory amount of the system memory (such as the first remaining free memory amount mentioned above), and then determine whether to allocate memory from the system memory to the task process or from the reserved memory space (such as the reserved memory mentioned above) according to the memory amount to be applied this time and the current remaining free memory amount of the system memory; the memory recovery module can perform memory recovery or killing in the process of the memory management module allocating memory for the task process. That is, these two functional modules can be integrated in the kernel layer.
[0191] In addition, it should be noted that no matter which of the above implementation methods is used, the controller can be regarded as the nerve center and command center of the mobile phone, which can generate operation control signals according to the instruction operation code and timing signal to complete the control of fetching and executing instructions. That is, triggering the kernel, memory management module, memory recovery module, etc. to perform corresponding operations.
[0192] In addition, it should be noted that, regarding the processor 110 including the above-mentioned processing units, in some implementations, different processing units may be independent devices. That is, each processing unit may be regarded as a processor. In other implementations, different processing units may also be integrated into one or more processors.
[0193] It should be understood that the above description is merely an example listed for a better understanding of the technical solution of this embodiment, and is not intended to be the sole limitation to this embodiment.
[0194] In addition, a memory may be provided in the processor 110 for storing instructions and data. In some implementations, the memory in the processor 110 is a cache memory (which may be represented as a memory in practical applications, i.e., the system memory mentioned above). The memory may store instructions or data that the processor 110 has just used or circulated. If the processor 110 needs to use the instruction or data again, it may be directly called from the memory. Repeated access is avoided, and the waiting time of the processor 110 is reduced, thereby improving the efficiency of the system. The memory allocation method provided in the embodiment of the present application is for the memory in the processor, i.e., the system memory.
[0195] Continue to see Fig. 9 The external memory interface 120 can be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the mobile phone 100. The external memory card communicates with the processor 110 through the external memory interface 120 to implement a data storage function. For example, files such as music and videos can be stored in the external memory card.
[0196] Continue to see Fig. 9, the internal memory 121 can be used to store computer executable program codes, which include instructions. The processor 110 executes various functional applications and data processing of the mobile phone 100 by running the instructions stored in the internal memory 121. The internal memory 121 may include a program storage area and a data storage area. Among them, the program storage area may store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, and the stereo recording function mentioned in the embodiment of the present application, etc.). The data storage area may store data created during the use of the mobile phone 100 (such as stereo audio data recorded based on the technical solution provided in the embodiment of the present application), etc. In addition, the internal memory 121 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, a universal flash storage (UFS), etc.
[0197] It is understandable that in the technical solution provided in the present application, the memory space obtained through memory application can be used to store data memory read from the internal memory, or it can be used to store data memory read from the external memory, and the present application does not impose any restrictions on this.
[0198] Continue to see Fig. 9 , the charging management module 140 is used to receive charging input from the charger, and the power management module 141 is used to connect the battery 142, the charging management module 140 and the processor 110. The power management module 141 receives input from the battery 142 and / or the charging management module 140, and provides power to the processor 110, the internal memory 121, the external memory, the display screen 194, the camera 193, and the wireless communication module 160. The power management module 141 can also be used to monitor parameters such as battery capacity, battery cycle number, battery health status (leakage, impedance), etc. In some other implementations, the power management module 141 can also be set in the processor 110. In other implementations, the power management module 141 and the charging management module 140 can also be set in the same device.
[0199] Continue to see Fig. 9 The wireless communication function of the mobile phone 100 can be realized through the antenna 1, the antenna 2, the mobile communication module 150, the wireless communication module 160, the modem processor and the baseband processor.
[0200] Continue to see Fig. 9The mobile communication module 150 can provide solutions for wireless communications including 2G / 3G / 4G / 5G applied on the mobile phone 100. The wireless communication module 160 can provide solutions for wireless communications including wireless local area networks (WLAn) (such as wireless fidelity (Wi-Fi) networks), bluetooth (BT), global navigation satellite system (GnSS), frequency modulation (FM), near field communication technology (nFC), infrared technology (IR), etc. applied on the mobile phone 100.
[0201] Continue to see Fig. 9 The audio module 170 may include a speaker 170A, a receiver 170B, a microphone 170C, an earphone interface 170D, etc. For example, the mobile phone 100 may implement audio functions through the application processor and the speaker 170A, the receiver 170B, the microphone 170C, the earphone interface 170D, etc. in the audio module 170, such as recording and video recording functions.
[0202] It should be understood that the above description is merely an example listed for a better understanding of the technical solution of this embodiment, and is not intended to be the sole limitation to this embodiment.
[0203] In addition, in some implementations, the sensor module 180 may include a pressure sensor, a gyroscope sensor, an air pressure sensor, a magnetic sensor, an acceleration sensor, a distance sensor, a proximity light sensor, a fingerprint sensor, a temperature sensor, a touch sensor, an ambient light sensor, a bone conduction sensor, etc., which are not listed here one by one and the present application does not limit this.
[0204] The hardware structure of the mobile phone 100 is introduced here. It should be understood that: Fig. 9 The illustrated mobile phone 100 is merely an example, and in a specific implementation, the mobile phone 100 may have more or fewer components than those shown in the figure, may combine two or more components, or may have a different component configuration. Fig. 9 The various components shown in the EMBODIMENTS 2000 may be implemented in hardware, software, or a combination of hardware and software, including one or more signal processing and / or application specific integrated circuits.
[0205] To better understand Fig. 9The software structure of the mobile phone 100 is shown, and the software structure of the mobile phone 100 is described below. Before describing the software structure of the mobile phone 100, the architecture that can be adopted by the software system of the mobile phone 100 is first described.
[0206] Specifically, in actual applications, the software system of the mobile phone 100 may adopt a layered architecture, an event-driven architecture, a microkernel architecture, a microservice architecture, or a cloud architecture.
[0207] In addition, it is understandable that the software systems used by the current mainstream electronic devices include but are not limited to Windows systems, Android systems and iOS systems. For ease of explanation, the present application embodiment takes the layered Android system as an example to exemplify the software structure of the mobile phone 100.
[0208] In addition, the memory allocation method provided in the subsequent embodiments of the present application is also applicable to other systems in specific implementations.
[0209] See also Fig.10 , which is a software structure block diagram of the mobile phone 100 according to an embodiment of the present application.
[0210] like Fig.10 As shown, the layered architecture of the mobile phone 100 divides the software into several layers, each with clear roles and division of labor. The layers communicate with each other through software interfaces. In some implementations, the Android system is divided into five layers, from top to bottom, the application layer / application layer (Applications) belonging to the application part, the framework layer / application framework layer (Application Framework, FWK) belonging to the core part, the runtime (Runtime) and the system library belonging to the bottom layer, the hardware abstract layer (HAL) and the Linux kernel (Linux Kernel) layer.
[0211] The application layer can include a series of application packages. Fig.10 As shown, the application package may include camera, game, video, live broadcast and other applications, which are not listed here one by one and are not limited in this application.
[0212] The framework layer can provide application programming interfaces (APIs) and programming frameworks for applications in the application layer. In some implementations, these programming interfaces and programming frameworks can be described as functions. Fig.10As shown, the framework layer may include functions such as a content provider and a window manager. As can be seen from the above description, in some possible implementations, the framework layer may also include functions such as a memory management module and a memory recycling module. These functions are not listed here one by one, and this application does not limit them.
[0213] For specific details about the memory management module and the memory recovery module implementing the memory allocation method of the present application, please refer to the description of the above embodiment and will not be repeated here.
[0214] In addition, it should be noted that in some possible cases, the functions implemented by the memory management module and the memory recycling module can also be integrated into the kernel layer and implemented by the kernel. For ease of description, this application takes integration in the kernel as an example.
[0215] In addition, in practical applications, the above-mentioned functional modules may also be represented as services or frameworks, such as a memory recycling control service or a memory recycling control framework, etc., which is not limited in this embodiment.
[0216] In addition, it should be noted that the window manager located in the framework layer is used to manage window programs. The window manager can obtain the size of the display screen, determine whether there is a status bar, lock the screen, capture the screen, etc.
[0217] In addition, it should be noted that the content provider located in the framework layer is used to store and obtain data and make the data accessible to the application. The data may include video, images, audio, dialed and received calls, browsing history and bookmarks, phone book, etc., which are not listed here one by one and are not limited by this application.
[0218] Continue to see Fig.10 , exemplarily, the runtime, specifically the Android runtime (Android Runtime) may include a core library and a virtual machine, which is mainly responsible for the scheduling and management of the Android system.
[0219] The core library consists of two parts: one is the function that the Java language needs to call, and the other is the Android core library. The application layer and the framework layer run in the virtual machine. The virtual machine executes the Java files of the application layer and the framework layer as binary files. The virtual machine is used to perform object life cycle management, stack management, thread management, security and exception management, and garbage collection.
[0220] Continue to see Fig.10Exemplarily, the system library may include multiple functional modules, such as a surface manager, a media library, a three-dimensional (3D) graphics processing library (such as OpenGL ES), a two-dimensional (2D) graphics engine (such as SGL), etc.
[0221] The surface manager is used to manage the display subsystem and provide fusion of 2D and 3D layers for multiple applications. The media library supports multiple commonly used audio and video format playback and recording, as well as static image files. The media library can support multiple audio and video encoding formats, such as: MPEG4, H.264, MP3, AAC, AMR, JPG, PnG, etc. The 3D graphics processing library is used to implement 3D graphics drawing, image rendering, synthesis, and layer processing.
[0222] In addition, it can be understood that the 2D graphics engine mentioned above is a drawing engine for 2D drawing.
[0223] Continue to see Fig.10 For example, the HAL layer is an interface layer between the operating system kernel (kernel layer) and the hardware circuit. Its purpose is to isolate the FWK from the kernel so that Android does not rely too much on the kernel, so that the development of FWK can be carried out without considering the driver.
[0224] Continue to see Fig.10 For example, the HAL layer may include various interfaces, such as audio and video interface, GPS interface, call interface, WiFi interface, etc., which are not listed here one by one and are not limited in this application.
[0225] Continue to see Fig.10 , exemplarily, the kernel layer in the Android system is the layer between hardware and software. The kernel layer may include various processes / threads, such as the memory management module and memory recycling module used to implement the memory allocation method provided in each embodiment of the present application. Exemplarily, when an application is started and a page fault interrupt occurs, thereby triggering a memory request, the memory management module and memory recycling module in the kernel will follow Figures 5 to 10 The implementation method in the embodiment performs memory allocation. Since the memory application is no longer blocked in the killing process, the memory application in specific scenarios, especially low memory scenarios, is greatly accelerated, thereby solving the problem of electronic device freeze caused by memory application.
[0226] This concludes the introduction to the software structure of the mobile phone 100. It can be understood that: Fig. 9The layers in the software structure shown and the components included in each layer do not constitute a specific limitation on the mobile phone 100. In other embodiments of the present application, the mobile phone 100 may include more or fewer layers than shown in the figure, and each layer may include more or fewer components, which is not limited in the present application.
[0227] In addition, it should be noted that the memory allocation method provided in the above embodiments implemented by the electronic device in the actual application scenario can also be executed by a chip system included in the electronic device, wherein the chip system may include a processor. The chip system can be coupled to the memory so that when the chip system is running, the computer program stored in the memory is called to implement the steps executed by the above electronic device. The processor in the chip system can be an application processor or a processor other than an application processor.
[0228] In addition, an embodiment of the present application also provides a computer-readable storage medium, which stores computer instructions. When the computer instructions are executed on an electronic device, the electronic device executes the above-mentioned related method steps to implement the memory allocation method in the above-mentioned embodiment.
[0229] In addition, an embodiment of the present application further provides a computer program product. When the computer program product is run on an electronic device, the electronic device executes the above-mentioned related steps to implement the memory allocation method in the above-mentioned embodiment.
[0230] In addition, an embodiment of the present application also provides a chip (which may also be a component or module), which may include one or more processing circuits and one or more transceiver pins; wherein the transceiver pins and the processing circuit communicate with each other through an internal connection path, and the processing circuit executes the above-mentioned related method steps to implement the memory allocation method in the above-mentioned embodiment to control the receiving pin to receive the signal, so as to control the sending pin to send the signal.
[0231] In addition, it can be seen from the above description that the electronic device, computer-readable storage medium, computer program product or chip provided in the embodiments of the present application are all used to execute the corresponding methods provided above. Therefore, the beneficial effects that can be achieved can refer to the beneficial effects in the corresponding methods provided above, and will not be repeated here.
[0232] As described above, the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present application.
Claims
1. A memory allocation method, characterized in that: Applied to electronic equipment, the method comprises: Receiving a first operation from a user; In response to the first operation, starting a first application; During the running of the first application, creating a first task process of the first application; Wherein, in the process of creating the first task process, the first application applies for memory resources from the allocatable memory resources of the system memory of the electronic device; In the case where the memory resource application from the system memory fails, the memory resource is applied from the reserved memory resources of the system memory, and the memory resources occupied by the second task process are recovered, the second task process is the task process of other applications running in the background of the electronic device, and the second task process is the task process with the largest memory resources currently occupied, and the reserved memory resources are the memory resources applied in advance from the system memory; The memory resources obtained by recycling the second task process are added to the reserved memory and / or the system memory.
2. The method according to claim 1, characterized in that: The first application applies for memory resources from the allocatable memory resources of the system memory of the electronic device, including: In the case where the allocatable memory resources of the system memory are greater than or equal to the memory resources corresponding to the first memory waterline, applying for the memory resources from the system memory is successful; When the allocatable memory resources of the system memory are less than the memory resources corresponding to the first memory waterline, applying for the memory resources from the system memory fails.
3. The method according to claim 2, characterized in that The situation where the allocatable memory resources of the system memory are greater than or equal to the memory resources corresponding to the first memory waterline includes: The allocatable memory resources of the system memory are greater than or equal to the memory resources corresponding to the second memory waterline; or, The allocatable memory resources of the system memory are less than the memory resources corresponding to the second memory watermark, and greater than or equal to the memory resources corresponding to the first memory watermark; Among them, the memory resources corresponding to the second memory waterline are greater than the memory resources corresponding to the first memory waterline.
4. The method according to claim 3, characterized in that: In a case where the allocatable memory resources of the system memory are less than the memory resources corresponding to the second memory watermark, and are greater than or equal to the memory resources corresponding to the first memory watermark, the method further includes: In the process of applying for the memory resources from the allocatable memory resources of the system memory, reclaiming the memory resources occupied by the task processes of other applications running in the background of the electronic device until the allocatable memory resources of the system memory are greater than or equal to the memory resources corresponding to the third memory waterline; Among them, the memory resources corresponding to the third memory waterline are greater than the memory resources corresponding to the second memory waterline.
5. The method according to claim 1, characterized in that The method further comprises: In case of failure in applying for the memory resource from the reserved memory resource, reclaiming the memory resource occupied by the second task process; After adding the memory resources obtained by recycling the second task process to the reserved memory and / or the system memory, the first application applies for the memory resources from the allocatable memory resources of the system memory; In the event that the memory resources requested from the system memory fail, the memory resources are requested from the reserved memory resources, and the third memory resources occupied by a third task process are reclaimed. The third task process is a task process of other applications running in the background of the electronic device, and the third task process is the task process that currently occupies the largest memory resources.
6. The method according to any one of claims 1 to 5, characterized in that: Reclaiming the memory resources obtained by the second task process includes a first memory block and a second memory block; The step of adding the memory resources recovered from the second task process to the reserved memory and / or the system memory includes: Detecting whether the reserved memory resources are less than the memory resources pre-applied from the system memory; When the reserved memory resources are less than the memory resources pre-applied from the system memory, the first memory block and the second memory block are added to the reserved memory.
7. The method according to claim 6, characterized in that The adding the first memory block and the second memory block to the reserved memory comprises: Adding the first memory block to the reserved memory; Detecting whether the reserved memory resources are less than the memory resources pre-applied from the system memory; When the reserved memory resources are less than the memory resources pre-applied from the system memory, the second memory block is added to the reserved memory.
8. The method according to claim 7, characterized in that The method further comprises: When the reserved memory resources are greater than or equal to the memory resources pre-applied from the system memory, the second memory block is added to the system memory.
9. The method according to claim 6, characterized in that The method further comprises: When the reserved memory resources are greater than or equal to the memory resources pre-applied from the system memory, the first memory block and the second memory block are added to the system memory.
10. The method according to any one of claims 1 to 9, characterized in that: The first application is a photo taking application.
11. An electronic device, characterized in that: The electronic device comprises: a memory and a processor, wherein the memory and the processor are coupled; the memory stores program instructions, and when the program instructions are executed by the processor, the electronic device executes the memory allocation method as described in any one of claims 1 to 10.
12. A computer-readable storage medium, characterized in that: It comprises a computer program, which, when executed on an electronic device, enables the electronic device to execute the memory allocation method as claimed in any one of claims 1 to 10.
13. A chip system, characterized in that: The chip system includes a processor, which is used to call a computer program in a memory to execute the memory allocation method as described in any one of claims 1 to 10.
Citation Information
Cited By
Memory management method for dynamic allocation of operating system and storage medium
CN120653439A
A memory management method for operating system dynamic allocation and storage medium
CN120653439B