Memory management method and device and electronic equipment
By splitting the global linked list into sub-link lists and traversing according to priority, the problem of low memory recycling in the Linux kernel is solved, and the user experience is improved.
Patent Information
- Application Number
- CN202311330108.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-10-13
- Publication Date
- 2025-04-15
AI Technical Summary
In the Linux kernel, when the system recycles memory, the shrinkers of each module are registered in a global linked list, which affects the efficiency of memory recycling and affects the user's user experience.
By splitting the global linked list into at least two sub-link lists, registering it in the corresponding sub-link list according to the information of the module's recycler, and traversing the recycled memory according to the priority of the sub-link list.
It improves the efficiency of the system to recycle memory, prioritizes traversing modules with fast recycling speed, reduces the end time of the system in a low memory state, and improves the user experience.
Smart Images

Figure CN119829472A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of electronic devices, and more specifically, to a method, device and electronic device for managing memory. Background Art
[0002] In the Linux kernel, when the system memory is insufficient, memory needs to be reclaimed. To ensure that the system can reclaim memory, each module needs to register the shrinker in the global linked list first. When reclaiming memory, the shrinkers in the global linked list will be traversed. The arrangement order of the shrinkers in the global linked list is related to the registration order of the shrinkers. Based on this, the efficiency of the system's memory recovery will be affected because the shrinkers of each module are registered in a global linked list.
[0003] Therefore, how to improve the efficiency of system memory recovery and thus improve the user experience is a problem that needs to be solved urgently. Summary of the invention
[0004] The present application provides a method, device and electronic device for managing memory, which can improve the efficiency of system memory recovery, thereby improving the user experience.
[0005] In a first aspect, a method for managing memory is provided, comprising: determining at least two sub-linked lists, the at least two sub-linked lists being related to a global linked list; registering the recycler of the first module in a first sub-linked list according to information of a recycler of a first module, the first sub-linked list belonging to one of the at least two sub-linked lists, the information of the recycler of the first module including a seek value of the recycler of the first module, the seek value being used to determine the amount of reclaimed memory; and traversing the at least two sub-linked lists according to the priorities of the at least two sub-linked lists.
[0006] Illustratively, the at least two sub-link lists may include at least two of shrinker_seeks_zero_list, shrinker_seeks_default_list, and shrinker_other_value_list.
[0007] It can be understood that at least two sub-linked lists are obtained by splitting the global linked list. The sub-linked lists obtained by splitting the global linked list include but are not limited to the above three sub-linked lists. According to actual conditions, the global linked list can be split into any other number (at least two) of sub-linked lists.
[0008] Among them, the seek value of the recycler of the first module can also be used to indicate the speed of the system reclaiming memory. For example, the modules with seeks value of 0 are mostly file systems based on random access memory (RAM). The slab memory reclaiming process of such modules does not involve disk input / output (I / O) operations, the amount of memory reclaimed at a single time is large, and the speed of memory reclaiming is fast. The modules with seeks value of non-0 are mostly file systems based on disk. The slab memory reclaiming process of such modules involves disk I / O operations, the amount of memory reclaimed at a single time is related to the system reclaiming priority, and the speed of memory reclaiming is slow.
[0009] It should be noted that the shrinkers of different modules can be registered in the corresponding sub-lists according to the values of the shrinker's seeks.
[0010] Based on the above scheme, the global linked list is divided into at least two sub-linked lists according to the different seeks values. When the module is initialized, the shrinker is registered in the corresponding sub-linked list according to the seeks value of the shrinker. When the system reclaims memory, it can traverse the sub-linked list according to the priority of the sub-linked list to improve the efficiency of the system in reclaiming memory.
[0011] In combination with the first aspect, in certain implementations of the first aspect, the method further includes: calling a memory recovery function corresponding to the recyclers in the at least two sub-linked lists to recycle memory.
[0012] That is to say, while traversing at least two sub-lists, the system calls the memory recovery function corresponding to the recycler in the sub-list, so as to realize the memory recovery of the shrinker module registered in the sub-list.
[0013] For example, the memory recovery function of "B_shrinker seeks=0" is called to realize the recovery of the module memory.
[0014] In combination with the first aspect, in certain implementations of the first aspect, the at least two sub-linked lists include at least two of the following: a sub-linked list of a first priority, a sub-linked list of a second priority, and a sub-linked list of a third priority; wherein the first priority is higher than the second priority, the second priority is higher than the third priority, the seek value of the recycler in the sub-linked list of the first priority is a first value, the seek value of the recycler in the sub-linked list of the second priority is a second value, and the seek value of the recycler in the sub-linked list of the third priority is a third value, wherein the third value is different from the first value, and the third value is different from the second value.
[0015] It should be noted that the first value may be 0, the second value may be a default value of 2, and the third value may be another positive integer that is neither 0 nor 2.
[0016] Exemplarily, the first priority sub-list is shrinker_seeks_zero_list, the second priority sub-list is shrinker_seeks_default_list, and the third priority sub-list is shrinker_other_value_list. The priority of the sub-list reflects the order of the system traversal. It is understandable that the system can traverse the sub-list with a higher priority first.
[0017] It should be noted that the seeks of the shrinker in at least two sub-lists are different. It can be understood that the global list is divided into at least two sub-lists according to the seeks value. Among them, the seeks value of the shrinker in the sub-list of the first priority is 0, the seeks value of the shrinker in the sub-list of the second priority is the default value (2), and the seeks value of the shrinker in the sub-list of the third priority is other values (for example, 4 or 8). Among them, the seeks value of the global list should be understood as the final value of the module.
[0018] Based on the above method, the global linked list is divided into at least two sub-linked lists according to the seeks value. Moreover, since the memory recovery speed of the module with a seeks value of 0 is fast, the priority of the sub-linked list registered with the shrinker with a seeks value of 0 is set to the highest, so that when the system recycles memory, the sub-linked list can be traversed first, so that the system can end the low memory state as soon as possible, providing users with a good user experience.
[0019] In combination with the first aspect, in certain implementations of the first aspect, when the sought value of the recycler of the first module is the first value, the first sub-linked list is the sub-linked list of the first priority.
[0020] In combination with the first aspect, in certain implementations of the first aspect, when the sought value of the recycler of the first module is the second value, the first sub-linked list is the sub-linked list of the second priority.
[0021] In combination with the first aspect, in certain implementations of the first aspect, when the sought value of the recycler of the first module is the third value, the first sub-linked list is a sub-linked list of the third priority.
[0022] For example, when the shrinker of the first module is "B_shrinker seeks=0", the first sub-link list is shrinker_seeks_zero_list; when the shrinker of the first module is "A_shrinker seeks=2", the first sub-link list is shrinker_seeks_default_list; when the shrinker of the first module is "D_shrinker seeks=4", the first sub-link list is shrinker_other_value_list.
[0023] Based on the above scheme, the shrinker is registered in the corresponding sub-list according to the seeks value of the shrinker of the first module, so that the subsequent system can traverse the shrinker in the corresponding sub-list according to the priority of the sub-list, thereby completing memory recovery.
[0024] In combination with the first aspect, in certain implementations of the first aspect, traversing the at least two sub-linked lists according to the priorities of the at least two sub-linked lists includes: traversing the sub-linked list of the first priority at a first moment, wherein the number of concurrent processes traversing the sub-linked list of the first priority at the first moment is less than or equal to a first threshold; traversing the sub-linked list of the second priority at a second moment, the second moment being later than the first moment, wherein the total recovery ratio of concurrent processes traversing the sub-linked list of the second priority at the second moment is less than or equal to a second threshold.
[0025] That is, when the system reclaims memory, in order to prevent concurrent processes from excessively reclaiming memory, it is necessary to limit the number of concurrent processes that simultaneously traverse the shrinker_seeks_zero_list and reclaim memory to be less than or equal to the first threshold, and limit the total recovery ratio of concurrent processes that simultaneously traverse the shrinker_seeks_default_list and reclaim memory to be less than or equal to the second threshold.
[0026] Exemplarily, the first threshold may be 2.
[0027] It should be noted that for the module with seeks value of 0, the amount of slab memory recovered by a single process at a time is half of the total amount of slab memory. When the number of concurrent processes is 2, the amount of slab memory recovered by two concurrent processes at a time is the total amount of slab (each process recovers half of the total amount of memory at a time, and two concurrent processes recover the total amount of memory at a time). It can be seen that when the number of concurrent processes is 2, it can be guaranteed that the memory recovery ratio does not exceed 100%, where 2 can be understood as the first threshold of the number of concurrent processes for memory recovery on shrinker_seeks_zero_list.
[0028] Exemplarily, the second threshold may be 1 (ie, 100%).
[0029] It should be noted that for modules with a seeks value of 2, the amount of slab memory reclaimed by a single process at a time is the product of the total amount of slab memory and the recovery ratio. The recovery ratio is (1 / 2)^[(priority-2)*seeks]. When the seeks value is 2, the recovery ratio is (1 / 2)^[(priority-1)]. When there are multiple concurrent processes reclaiming the module memory, the sum of the amount of slab memory reclaimed by the multiple concurrent processes at a time needs to be less than or equal to the total amount of slab memory of the module. In other words, the sum of the recovery ratios of the multiple concurrent processes is the total recovery ratio. The total recovery ratio needs to be less than or equal to 100%. 100% can be understood as the second threshold of the total recovery ratio.
[0030] Based on the above method, the concurrent processes traversing the sub-linked list of the first priority and the sub-linked list of the second priority are limited to prevent excessive memory recovery caused by too many concurrent processes recovering memory at the same time, which wastes the system's power consumption and affects the system's efficiency in recovering memory.
[0031] In combination with the first aspect, in certain implementations of the first aspect, the method also includes: in response to the first process traversing the sub-linked list of the first priority, the number of concurrent processes is increased by one; in response to the first process traversing the sub-linked list of the second priority, the total recovery ratio of the concurrent processes is increased by the recovery ratio of the first process; wherein the recovery ratio of the first process is related to the system memory recovery priority of the first process at the second moment, and the system memory recovery priority is used to determine the amount of reclaimed memory.
[0032] In combination with the first aspect, in certain implementations of the first aspect, the method also includes: in response to the completion of memory reclaiming by the first process, the number of concurrent processes is reduced by one; in response to the completion of memory reclaiming by the first process, the total reclaim ratio of the concurrent processes is reduced by the reclaim ratio of the first process; wherein the reclaim ratio of the first process is related to the system memory reclaim priority of the first process at the second moment, and the system memory reclaim priority is used to determine the amount of reclaimed memory.
[0033] It should be noted that when the number of concurrent processes is less than the first threshold, newly added concurrent processes are allowed to reclaim the slab memory of modules with a seeks value of 0; when the number of concurrent processes is greater than or equal to the first threshold, newly added concurrent processes are not allowed to reclaim the slab memory of modules with a seeks value of 0. When the total recovery ratio of concurrent processes is less than the second threshold, newly added concurrent processes are allowed to reclaim the slab memory of modules with a seeks value of 2; when the total recovery ratio of concurrent processes is greater than or equal to the second threshold, newly added concurrent processes are not allowed to reclaim the slab memory of modules with a seeks value of 2.
[0034] Based on the above scheme, the number of concurrent processes and the total recycling ratio of concurrent processes can increase with whether a new process starts to traverse the linked list, and can also decrease with whether a certain process has finished recycling memory. The system can determine whether to allow new concurrent processes to recycle memory based on the dynamic changes in the number of concurrent processes and the total recycling ratio of concurrent processes, which can effectively prevent excessive memory recycling, reduce system power consumption, and improve the efficiency of system memory recycling.
[0035] In combination with the first aspect, in some implementations of the first aspect, the method further includes: traversing the sub-linked list of the third priority at a third moment, the third moment being later than the second moment.
[0036] Based on the above scheme, the system can continue to traverse the sub-list of the third priority level after traversing the sub-list of the first priority level and the sub-list of the second priority level based on the priorities of at least two sub-lists, so that the system can reclaim more memory and improve the efficiency of the system in reclaiming memory.
[0037] In a second aspect, a device for managing memory is provided, comprising: a processing unit, used to determine at least two sub-linked lists, the at least two sub-linked lists being related to a global linked list; the processing unit, further used to register a recycler of the first module in a first sub-linked list according to information of the recycler of the first module, the first sub-linked list belonging to one of the at least two sub-linked lists, the information of the recycler of the first module including a seek value of the recycler of the first module, the seek value being used to determine the amount of reclaimed memory; the processing unit, further used to traverse the at least two sub-linked lists according to the priorities of the at least two sub-linked lists.
[0038] In combination with the second aspect, in certain implementations of the second aspect, the processing unit is further used to call a memory recovery function corresponding to the recycler in the at least two sub-linked lists to recycle memory.
[0039] In combination with the second aspect, in certain implementations of the second aspect, the at least two sub-linked lists include at least two of the following: a sub-linked list of a first priority, a sub-linked list of a second priority, and a sub-linked list of a third priority; wherein the first priority is higher than the second priority, the second priority is higher than the third priority, the seek value of the recycler in the sub-linked list of the first priority is a first value, the seek value of the recycler in the sub-linked list of the second priority is a second value, and the seek value of the recycler in the sub-linked list of the third priority is a third value, wherein the third value is different from the first value, and the third value is different from the second value.
[0040] In combination with the second aspect, in certain implementations of the second aspect, when the sought value of the recycler of the first module is the first value, the first sub-linked list is the sub-linked list of the first priority.
[0041] In combination with the second aspect, in certain implementations of the second aspect, when the sought value of the recycler of the first module is the second value, the first sub-list is the sub-list of the second priority.
[0042] In combination with the second aspect, in certain implementations of the second aspect, when the sought value of the recycler of the first module is the third value, the first sub-list is a sub-list of the third priority.
[0043] In combination with the second aspect, in certain implementations of the second aspect, the processing unit is specifically used to traverse the sub-linked list of the first priority at a first moment, wherein the number of concurrent processes traversing the sub-linked list of the first priority at the first moment is less than or equal to a first threshold; and traverse the sub-linked list of the second priority at a second moment, the second moment being later than the first moment, wherein the total recovery ratio of the concurrent processes traversing the sub-linked list of the second priority at the second moment is less than or equal to the second threshold.
[0044] In combination with the second aspect, in certain implementations of the second aspect, the processing unit is further used to, in response to the first process traversing the sub-linked list of the first priority, increase the number of concurrent processes by one; the processing unit is further used to, in response to the first process traversing the sub-linked list of the second priority, increase the total recovery ratio of the concurrent processes by the recovery ratio of the first process; wherein the recovery ratio of the first process is related to the system memory recovery priority of the first process at the second moment, and the system memory recovery priority is used to determine the amount of reclaimed memory.
[0045] In combination with the second aspect, in certain implementations of the second aspect, the processing unit is further used to, in response to the completion of memory reclaiming by the first process, reduce the number of concurrent processes by one; the processing unit is further used to, in response to the completion of memory reclaiming by the first process, reduce the total reclaim ratio of the concurrent processes by the reclaim ratio of the first process; wherein the reclaim ratio of the first process is related to the system memory reclaim priority of the first process at the second moment, and the system memory reclaim priority is used to determine the amount of reclaimed memory.
[0046] In combination with the second aspect, in some implementations of the second aspect, the processing unit is further used to traverse the sub-linked list of the third priority at a third moment, and the third moment is later than the second moment.
[0047] In a third aspect, an electronic device is provided, comprising: one or more processors; one or more memories; the one or more memories storing one or more computer programs, the one or more computer programs comprising instructions, which, when executed by the one or more processors, enable the electronic device to execute the method in the above-mentioned first aspect and any possible implementation thereof.
[0048] In a fourth aspect, a device for managing memory is provided, comprising: a processor coupled to a memory, the memory being used to store a computer program, the processor being used to run the computer program, so that the device for managing memory executes the method in the first aspect and any possible implementation thereof.
[0049] In combination with the fourth aspect, some implementations of the fourth aspect also include one or more of the memory and the transceiver, and the transceiver is used to receive signals and / or send signals.
[0050] In a fifth aspect, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a computer, the computer executes the above-mentioned first aspect and any method for managing memory that can be implemented in the first aspect.
[0051] In a sixth aspect, a computer program product comprising instructions is provided, which, when executed on a computer, enables the computer to execute the above-mentioned first aspect and any method for managing memory that can be implemented in the first aspect.
[0052] It should be noted that the above-mentioned computer program code can be stored in whole or in part on the first storage medium, wherein the first storage medium can be packaged together with the processor or separately packaged with the processor, and the embodiments of the present application do not specifically limit this.
[0053] In a seventh aspect, a chip is provided, which includes a processor and a data interface, and the processor reads instructions stored in a memory through the data interface to execute the above-mentioned first aspect and any method for managing memory that can be implemented in the first aspect.
[0054] In combination with the seventh aspect, in one possible implementation, the processor is coupled to the memory via an interface.
[0055] In combination with the seventh aspect, in a possible implementation, the chip system also includes a memory, in which a computer program or computer instructions are stored. BRIEF DESCRIPTION OF THE DRAWINGS
[0056] Figure 1 It is a structural diagram of an electronic device.
[0057] Figure 2 It is a software structure block diagram of the electronic device provided in the embodiment of the present application.
[0058] Figure 3 It is a schematic flowchart of a method for managing memory provided in an embodiment of the present application.
[0059] Figure 4 This is a schematic diagram of a chain representation provided by an embodiment of the present application.
[0060] Figure 5 It is a schematic diagram of traversing a linked list provided in an embodiment of the present application.
[0061] Figure 6 This is a comparison chart of system memory recovery speed provided by an embodiment of the present application.
[0062] Figure 7 It is a schematic flowchart of a method for managing memory provided in an embodiment of the present application.
[0063] Figure 8 This is a schematic diagram of a chain representation provided by an embodiment of the present application.
[0064] Fig. 9 It is a schematic flowchart of shrinker registration provided in an embodiment of the present application.
[0065] Fig.10 It is a schematic flowchart of traversing a linked list provided in an embodiment of the present application.
[0066] Fig.11 It is a schematic flowchart of a method for managing memory provided in an embodiment of the present application.
[0067] Fig.12 It is a schematic block diagram of a memory management device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0068] The technical solution in this application will be described below in conjunction with the accompanying drawings.
[0069] The terms used in the following embodiments are only for the purpose of describing specific embodiments, and are not intended to be used as limitations on the present application. As used in the specification and the appended claims of the present application, the singular expressions "one", "a kind of", "said", "above", "the" and "this" are intended to also include expressions such as "one or more", unless there is a clear contrary indication in the context. It should also be understood that in the following embodiments of the present application, "at least one", "one or more" refer to one, two or more. The term "and / or" is used to describe the association relationship of associated objects, indicating that three relationships may exist; for example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone, where A and B can be singular or plural. The character " / " generally indicates that the objects associated before and after are in an "or" relationship.
[0070] References to "one embodiment" or "some embodiments" etc. described in this specification mean that a particular feature, structure or characteristic described in conjunction with the embodiment is included in one or more embodiments of the present application. Thus, the phrases "in one embodiment", "in some embodiments", "in some other embodiments", "in some other embodiments", etc. that appear at different places in this specification do not necessarily refer to the same embodiment, but mean "one or more but not all embodiments", unless otherwise specifically emphasized in other ways. The terms "including", "comprising", "having" and their variations all mean "including but not limited to", unless otherwise specifically emphasized in other ways.
[0071] The following describes an electronic device, a user interface for such an electronic device, and embodiments for using such an electronic device. In some embodiments, the electronic device may be a portable electronic device that also includes other functions such as a personal digital assistant and / or a music player function, such as a mobile phone, a tablet computer, a wearable electronic device with wireless communication function (such as a smart watch), etc. Exemplary embodiments of portable electronic devices include but are not limited to devices equipped with Or a portable electronic device with other operating systems. The portable electronic device may also be other portable electronic devices, such as a laptop computer, etc. It should also be understood that in some other embodiments, the electronic device may not be a portable electronic device, but a desktop computer.
[0072] For example, Figure 1The schematic diagram of the structure of the electronic device 100 is shown. The electronic device 100 may include a processor 110, an audio module 120, a display screen 130, a camera 140, an internal memory 150, an external memory interface 160, a sensor module 170, an antenna 1, an antenna 2, a mobile communication module 180, and a wireless communication module 190. The sensor module 170 may include a gyroscope sensor 170A, a magnetic sensor 170B, a fingerprint sensor 170C, a touch sensor 170D, etc. 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, a graphics processor (graphics processing unit, GPU), an image signal processor (image signal processor, ISP), a controller, a memory, a video codec, a digital signal processor (digital signal processor, DSP), a baseband processor, and / or a neural network processor (neural-network processing unit, NPU), etc.
[0073] It is to be understood that the structure illustrated in the embodiment of the present application does not constitute a specific limitation on the electronic device 100. In other embodiments of the present application, the electronic device 100 may include more or fewer components than shown in the figure, or combine some components, or split some components, or arrange the components differently. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.
[0074] Figure 2 1 is a software structure diagram of the electronic device 100 of the embodiment of the present application. Exemplarily, the operating system of the electronic device 100 may be an Android system, a Linux system, a Microsoft Windows system, an Apple mobile operating system (iOS) or a Harmony OS. The method provided in the present application is particularly applicable to a Linux system or other systems derived from a Linux system.
[0075] The layered architecture 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 embodiments, the Android system is divided into four layers, from top to bottom, namely, the application layer, the application framework layer, the Android runtime (Android runtime) and the system library, and the kernel layer. The application layer can include a series of application packages.
[0076] like Figure 2As shown, the application layer may include camera, settings, skin module, user interface (UI), third-party applications, etc. Among them, the third-party applications may include gallery, calendar, call, map, navigation, WLAN, Bluetooth, music, video, short message, etc.
[0077] The application framework layer provides an application programming interface (API) and a programming framework for the applications in the application layer. The application framework layer may include some predefined functions.
[0078] like Figure 2 As shown, the application framework layer may include a window manager, a content provider, a view system, a phone manager, a resource manager, a notification manager, and the like.
[0079] The window manager is used to manage window programs. The window manager can obtain the display screen size, determine whether there is a status bar, lock the screen, capture the screen, etc. The content provider is used to store and obtain data and make this data accessible to applications. This data can include video, images, audio, calls made and received, browsing history and bookmarks, phone books, etc.
[0080] The view system includes visual controls, such as controls for displaying text, controls for displaying images, etc. The view system can be used to build applications. The display interface can be composed of one or more views.
[0081] The phone manager is used to provide communication functions of the electronic device 100, such as management of call status (including connecting, hanging up, etc.).
[0082] The resource manager provides various resources for applications, such as localized strings, icons, images, layout files, video files, and so on.
[0083] The notification manager enables applications to display notification information in the status bar. It can be used to convey notification-type messages and can disappear automatically after a short stay without user interaction. For example, the notification manager is used to notify download completion, message reminders, etc. The notification manager can also be a notification that appears in the system top status bar in the form of a chart or scroll bar text, such as notifications of applications running in the background, or a notification that appears on the screen in the form of a dialog window. For example, a text message is displayed in the status bar, a prompt sound is emitted, an electronic device vibrates, an indicator light flashes, etc.
[0084] Android runtime includes core libraries and virtual machines. Android runtime is responsible for scheduling and management of the Android system.
[0085] The core library consists of two parts: one is the function that needs to be called by the Java language, and the other is the Android core library.
[0086] The application layer and the application framework layer run in a virtual machine. The virtual machine executes the Java files of the application layer and the application framework layer as binary files. The virtual machine is used to perform functions such as object life cycle management, stack management, thread management, security and exception management, and garbage collection.
[0087] The system library may include multiple functional modules, such as surface manager, media libraries, 3D graphics processing library (such as OpenGL ES), 2D graphics engine (such as SGL), etc.
[0088] The surface manager is used to manage the display subsystem and provide the fusion of 2D and 3D layers for multiple applications.
[0089] The media library supports playback and recording of a variety of commonly used audio and video formats, as well as static image files, etc. The media library can support a variety of audio and video encoding formats, such as: MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, etc.
[0090] The 3D graphics processing library is used to implement 3D graphics drawing, image rendering, compositing, and layer processing.
[0091] A 2D graphics engine is a drawing engine for 2D drawings.
[0092] In addition, the system library may also include status monitoring service modules, such as a physical status recognition module for analyzing and identifying user gestures; a sensor service module for monitoring sensor data uploaded by various sensors at the hardware layer to determine the physical status of the electronic device 100.
[0093] The kernel layer is a layer between hardware and software. The kernel layer includes at least display driver, camera driver, audio driver, sensor driver and memory management. The method for managing memory provided in the present application can be implemented in the kernel layer, more specifically, in the memory management of the kernel layer.
[0094] The hardware layer can include various types of sensors, such as Figure 1 Various types of sensors introduced in.
[0095] Combined with the above Figure 1 and Figure 2The electronic device introduced in the embodiment of the present application may be a smart phone, a tablet computer, a smart screen or other smart device. The physical devices involved in the electronic device 100 mainly include hardware components such as sensors, decision support systems (DSS) display chips, touch screens, fingerprint recognition modules, etc.; as well as kernel software layers such as screen management modules, display drivers, fingerprint drivers, and anti-mistouch; application framework layer functions such as anti-mistouch input, screen control, always on display (AOD) services, and power management; as well as application layer services such as special adaptation applications (camera), third-party applications, system sleep, and AOD.
[0096] Before introducing the technical solutions in the embodiments of the present application, some related concepts in the embodiments of the present application are first introduced.
[0097] Slab: A memory allocation mechanism in the Linux kernel. It mainly works on objects that are frequently allocated and released (for example, process descriptors). In Linux, memory is generally managed and allocated through the buddy system in units of pages (for example, 4KB). However, in actual applications, most objects that need to be allocated and released are in bytes, and the size of these objects is generally small. If they are allocated and released through the buddy system, it will cause a certain waste of memory and the processing speed will be slow. For example, if you need to apply for 20 bytes, allocating a page for 20 bytes will waste a certain amount of memory. The slab manager manages objects based on the above-mentioned bytes. The slab manager classifies objects of the same type into one category. Whenever such an object is to be applied, the slab manager will allocate a unit of the corresponding size from a slab list, and when it needs to be released, it will be saved in the slab list again. The slab allocator does not break away from the buddy system, but further subdivides the large memory allocated by the buddy system into small memory allocations.
[0098] Shrinker: A structure in the Linux kernel. The structure contains slab memory recovery functions (scan objects) implemented by each module. The Linux kernel calls the memory recovery function to recover the recyclable slab memory of each module.
[0099] Seeks: A parameter in the Linux structure that can be used to determine the amount of slab memory reclaimed each time. Seeks can have different values. For example, the system can set the default value of seeks to 2, and most modules can use the default value of seeks. Of course, different modules can set different seeks values. For example, memory-based file systems (e.g., procfs file systems, sysfs file systems, cgroup file systems) can set seeks to 0; anonymous memory modules (e.g., ashmem modules) can set seeks to 8.
[0100] It should be noted that there is a corresponding relationship between modules and file systems. File systems belong to modules, but modules do not necessarily belong to file systems. For example, the file system type corresponding to the procfs module is procfs, the file system type corresponding to the sysfs module is sysfs, and the file system type corresponding to the cgroup module is cgroup.
[0101] It should be noted that the process file system (procfs) is used to access process information through the kernel. The system file system (sysfs) is a virtual file system that provides a mechanism for managing device kernel parameters in the Linux system. The control groups (cgroup) file system is a function of the Linux kernel that is used to limit, control, and separate the resources of a process group. The anonymous shared memory module (ashmem) can be used to assist the memory management system in effectively managing unused memory blocks, and can also achieve memory sharing between processes through the Binder process communication mechanism.
[0102] In the Linux kernel, each module caches slab memory. When the system free memory is lower than a certain threshold, it can be considered that the system memory is insufficient and memory needs to be reclaimed to avoid affecting the operating efficiency of electronic devices or causing electronic devices to freeze and affect the user experience due to insufficient system memory.
[0103] In order to ensure that the system can recycle slab memory, each module first needs to register its own shrinker in the global linked list (shrinker list). When the module is initialized, the shrinker needs to be registered on the global linked list; when the module is uninstalled, the shrinker needs to be deleted from the global linked list. The global linked list (shrinker list) is a linked list in the Linux system written in C language.
[0104] Since the global linked list includes the shrinkers of each module, when the system reclaims memory, it first needs to traverse the global linked list, access the shrinkers registered by each module in the global linked list in turn, and call the recycling function in the shrinker to reclaim the slab memory of each module.
[0105] Since the shrinkers of each module are registered in the same global linked list, the arrangement order of the shrinkers in the global linked list is related to the order in which the shrinkers are registered, and the earliest registered one is closest to the front of the linked list. When the system reclaims memory and traverses the global linked list, it will also reclaim the slab memory of each module in the order of registration. Based on this, the efficiency of the system's memory recovery will be affected because the shrinkers of each module are registered in a global linked list.
[0106] Specifically, the following introduces several situations in which the shrinkers of various modules are registered in a global linked list and affect the efficiency of system memory recovery.
[0107] In the first case, the shrinkers of each module are registered in a global linked list, which affects the speed at which the system reclaims memory and thus affects the efficiency of the system reclaiming memory.
[0108] The amount of slab memory that each module reclaims each time is related to the total amount of slab memory of the module, the priority of the system to reclaim memory, and the seeks parameter of the module shrinker. When the seeks parameter is 0, the amount of slab memory that each module reclaims each time is half of the total amount of slab memory (the amount of slab memory that each module reclaims each time = the total amount of slab memory / 2). When the seeks parameter is non-zero, the amount of slab memory that each module reclaims each time = the total amount of slab memory / (2^(priority-2)*seeks). For example, when the total amount of slab memory of a module is 4096, the current system priority for reclaiming memory is 10, and the seeks parameter value is 2, the amount of slab memory that the module reclaims this time is 4096 / (210-2*2)=8.
[0109] It should be noted that when the system memory is insufficient, the kernel needs to reclaim memory. Usually, the priority of system memory reclaiming starts from 12. If the system memory is still insufficient after the current reclaiming, the priority of system memory reclaiming is reduced by one, that is, the priority of system memory reclaiming is set to 11, and the amount of slab memory to be reclaimed next time is calculated. And so on, until the priority of system memory reclaiming is reduced to 0.
[0110] The shrinkers of each module in the current Linux kernel will be registered in a unique global linked list, which may include multiple types of shrinkers in the global linked list, and the seeks parameters of each shrinker may also be different. It is understandable that the shrinker types of each module can be different, and there is a one-to-one correspondence between the module and the shrinker type. The kernel reclaims the slab memory of each module in the same order as the order in which the modules register the shrinkers in the global linked list. That is to say, the module that first registers the shrinker in the global linked list is at the front of the global linked list. When the system reclaims the slab memory, it will first traverse the shrinker at the front of the global linked list.
[0111] It should be noted that the current Linux kernel does not limit the number of concurrent processes that can reclaim slab memory. Any process can try to reclaim memory when memory is insufficient.
[0112] For example, when a process is running and needs to apply for memory, the system will start traversing from the front-most shrinker in the global linked list and reclaim memory; when multiple processes are running concurrently and need to apply for memory, the system will also start traversing from the front-most shrinker in the global linked list and reclaim memory. In other words, when multiple processes are running concurrently, there may be a situation where multiple processes traverse the shrinker of the same module and reclaim memory at the same time.
[0113] It should be noted that the speeds at which each module reclaims slab memory may be different. Generally speaking, when the system is low on memory, in order to allow the system to end the low memory state as soon as possible and avoid more processes being stuck due to insufficient memory, memory recovery can be performed on modules with fast slab memory recovery speeds first. For example, when a user starts the camera of an electronic device, a large amount of memory will be requested, which will cause insufficient system memory and the kernel will need to reclaim slab memory. At this point, the user triggers the start of an application. If the kernel reclaims slab memory slowly, the system is still in a low memory state, and the startup process of the application will be stuck due to insufficient system memory; if the kernel reclaims slab memory quickly, the system has ended the low memory state, and the application can start smoothly.
[0114] In the current Linux kernel, most modules with seeks of 0 are file systems based on random access memory (RAM). The slab memory recovery process of memory-based file systems does not involve disk input / output (I / O) operations, and the recovery speed is fast, and the number of single recovery is large. Most modules with non-0 seeks are disk-based file systems. The slab memory recovery process of disk-based file systems involves disk I / O operations, and the recovery speed is slow. The number of single recovery is related to the system recovery priority. Therefore, in order to end the low memory state of the system as soon as possible when the system memory is insufficient, the system can give priority to recovering the slab memory of modules with seeks of 0.
[0115] According to the above introduction, any process can try to reclaim memory when the memory is insufficient. Therefore, there may be multiple concurrent processes reclaiming memory at the same time. If there is no limit on the number of concurrent processes that reclaim memory at the same time, there will be a problem of excessive slab memory reclaiming. For example, for a module with seeks of 0, the system reclaims half of the total slab memory of the module at a time. At the same time, there are 4 concurrent processes reclaiming the slab memory of the module at the same time. In this case, the 4 concurrent processes will try to reclaim 200% of the total slab memory, resulting in excessive reclaiming.
[0116] In view of this, the embodiments of the present application provide a method, device and electronic device for managing memory, which optimizes the slab memory recovery speed while solving the problem of excessive recovery that may be caused when multiple concurrent processes recover slab memory at the same time, thereby improving the efficiency of system memory recovery. Figure 2 It is implemented in the kernel layer shown.
[0117] like Figure 3 As shown, the main steps of a memory management method provided in an embodiment of the present application are shown. The method 300 can be applied to the slab memory management of the Linux kernel. The method 300 is specifically introduced below.
[0118] S301, split the global linked list.
[0119] Exemplarily, the global linked list is split into at least two linked lists, such as shrinker_seeks_zero_list, shrinker_seeks_default_list, and shrinker_other_value_list.
[0120] It should be noted that the global linked list is split into three linked lists. The seeks values of the registered shrinkers in these three linked lists are different. The seeks values of the shrinkers registered in the same linked list can be the same or different.
[0121] like Figure 4 As shown, a schematic diagram of the global linked list and the split linked list is shown.
[0122] Figure 4 (a) in FIG. 1 shows a global linked list (shrinker_list), which may include shrinkers of different modules. That is, the global linked list may include shrinkers with different seeks values. For example, Figure 4 (a) shows the B shrinker and C shrinker with seeks value of 0, the A shrinker and E shrinker with seeks value of 2, the D shrinker with seeks value of 4, and the F shrinker with seeks value of 8.
[0123] Exemplarily, the seeks value of the shrinker registered in the global linked list by the procfs file system may be 0, and the seeks value of the shrinker registered in the global linked list by the ashmem module may be 8.
[0124] It should be noted that the default seeks value of the shrinker is 2, and the seeks value of most modules can be set to the default value. After registering the shrinker in the global linked list, some modules may modify the seeks value to a more appropriate value. For example, for the procfs file system, the file system is based on memory, so the seeks value of the file system can be modified from the default value to 0.
[0125] That is to say, various seeks value modules register shrinker in the global linked list. For example, Figure 4"A_shrinker seeks=2" shown in (a) indicates the shrinker registered by module A in the global linked list, and the seeks value is 2; "B_shrinker seeks=0" indicates the shrinker registered by module B in the global linked list, and the seeks value is 0; "C_shrinker seeks=0" indicates the shrinker registered by module C in the global linked list, and the seeks value is 0; "D_shrinker seeks=4" indicates the shrinker registered by module D in the global linked list, and the seeks value is 4; "E_shrinker seeks=2" indicates the shrinker registered by module E in the global linked list, and the seeks value is 2; "F_shrinker seeks=8" indicates the shrinker registered by module F in the global linked list, and the seeks value is 8.
[0126] For example, the order of shrinkers in the global linked list can be arranged according to the time when each module registers the shrinker. The shrinker registered earliest is ranked first. Figure 4 In the global linked list shown in (a), the order of shrinker arrangement is "A_shrinker seeks=2", "B_shrinker seeks=0", "C_shrinker seeks=0", "D_shrinker seeks=4", "E_shrinker seeks=2", and "F_shrinker seeks=8". The earliest registered shrinker is "A_shrinker seeks=2".
[0127] Based on Figure 4 When reclaiming memory, the system can also traverse the shrinkers of each module in the order shown in (a).
[0128] Figure 4 (b) in FIG. 1 shows the linked lists after the splitting, for example, shrinker_seeks_zero_list, shrinker_seeks_default_list, and shrinker_other_value_list.
[0129] Illustratively, shrinker_seeks_zero_list includes "B_shrinker seeks=0" and "C_shrinker seeks=0", shrinker_seeks_default_list includes "A_shrinker seeks=2" and "E_shrinker seeks=2", and shrinker_other_value_list includes "D_shrinker seeks=4" and "F_shrinker seeks=8".
[0130] In other words, the global linked list can be divided into the following types according to the value of shrinker: Figure 4 The three linked lists shown in (b) in the shrinker_seeks_zero_list have a seeks value of 0, the shrinker_seeks_default_list have a seeks value of 2, and the shrinker_other_value_list have seeks values other than 0 and 2 (for example, 4 or 8).
[0131] Exemplarily, the arrangement order of the shrinkers in the split linked list can be arranged according to the time when each module registers the shrinker. The shrinker registered earliest is arranged at the front. For example, the order of the shrinkers in shrinker_seeks_zero_list is "B_shrinker seeks=0", "C_shrinker seeks=0"; the order of the shrinkers in shrinker_seeks_default_list is "A_shrinker seeks=2", "E_shrinker seeks=2"; the order of the shrinkers in shrinker_other_value_list is "D_shrinker seeks=4", "F_shrinker seeks=8".
[0132] Based on Figure 4 When reclaiming memory, the system can also traverse the shrinkers of each module in the order shown in (b).
[0133] It should be noted that Figure 4The arrangement order of the shrinkers in each linked list after the split shown in (b) can be adjusted. When the arrangement order of the shrinkers is adjusted, the order of traversing the shrinkers during system recycling will also be adjusted accordingly. Specifically, the adjustment can be made according to the slab memory recycling speed.
[0134] For example, in shrinker_seeks_default_list, the seeks value of the shrinker registered by each module is 2. Although the seeks value is the same, the recovery speed of slab memory may be different. The seeks value of the graphics processing unit (gpu) module shrinker is 2, and the recovery speed of slab memory is faster. The shrinker of the gpu module can be placed at the front of shrinker_seeks_default_list, so that when reclaiming slab memory, the memory of the gpu module can be reclaimed first, so that the system ends the low memory state as soon as possible.
[0135] S302, register shrinker.
[0136] It should be noted that, according to the seeks value of the shrinker of the module, the shrinker of each module is registered in each linked list split in the above step S301.
[0137] It should be noted that, according to the above record in step S301, the value of seeks can be modified after registering the shrinker. Based on this, if the method for managing memory provided in the embodiment of the present application registers the shrinker in the corresponding linked list according to the seeks value before modification, the shrinker's seeks value may change later, and the shrinker registered in the linked list will not meet the requirements of the linked list for the seeks value. Therefore, in the method for managing memory provided in the embodiment of the present application, it is necessary to register the shrinker in the corresponding linked list according to the final seeks value of the shrinker of the module.
[0138] For example, the system defaults the seeks value of the procfs file system to be 2. If the shrinker is registered according to the default value of seeks of the procfs file system, the shrinker of that file system will be in the shrinker_seeks_default_list. Since the procfs file system is memory-based, the final seeks value of the shrinker of that file system is 0 and should be registered in the shrinker_seeks_zero_list. It can be seen that in order to avoid some file systems modifying the seeks parameter value after registering the shrinker, thereby causing the shrinker to be registered in the wrong linked list, in the memory management method provided in the embodiment of the present application, it is necessary to register according to the final seeks value of the shrinker of the file system.
[0139] For example, Figure 4 As shown in (b), the shrinker with a seeks value of 0 is registered in shrinker_seeks_zero_list, the shrinker with a seeks value of 2 is registered in shrinker_seeks_default_list, and the shrinker with a seeks value other than 0 and 2 is registered in shrinker_other_value_list. In shrinker_seeks_zero_list, "B_shrinker seeks = 0" is registered before "C_shrinker seeks = 0", in shrinker_seeks_default_list, "A_shrinker seeks = 2" is registered before "E_shrinker seeks = 2", and in shrinker_other_value_list, "D_shrinker seeks = 4" is registered before "F_shrinkerseeks = 8".
[0140] S303, traverse the corresponding linked list.
[0141] It should be noted that when the system reclaims slab memory, it needs to traverse the linked list, access the shrinker registered by each module in the linked list in turn, and call the recycling function in the shrinker to reclaim the slab memory of each module.
[0142] When the linked list is Figure 4 When the global linked list is shown in (a) in the figure, the system needs to traverse the shrinkers registered in the global linked list; when the linked list is as follows Figure 4When the three linked lists shown in (b) are used, the system needs to traverse the shrinkers registered in the three linked lists one by one.
[0143] It should be noted that the system needs to be Figure 5 The order shown is traversed in sequence as follows Figure 4 The three linked lists shown in (b) in .
[0144] S501, traverse shrinker_seeks_zero_list.
[0145] S502, traverse shrinker_seeks_default_list.
[0146] S503, traverse shrinker_other_value_list.
[0147] That is to say, when the system reclaims slab memory, it first traverses the shrinker_seeks_zero_list, then the shrinker_seeks_default_list, and finally the shrinker_other_value_list.
[0148] It is understandable that each time the system reclaims slab memory, it will follow the following Figure 5 The three linked lists are traversed in the order shown. After completing the traversal of shrinker_other_value_list, the system can determine whether the current free memory of the system is sufficient. If the current free memory of the system is insufficient and the system is still in a low memory state, the system will start the next traversal, also in the same way as shown. Figure 5 The order shown is traversed.
[0149] It should be noted that before reclaiming memory, the system can traverse the linked list according to the priority.
[0150] It can be considered that the priorities of the linked lists from high to low are shrinker_seeks_zero_list, shrinker_seeks_default_list, and shrinker_other_value_list.
[0151] For ease of understanding, Figure 6 As shown, a comparison chart of system memory recovery speed is shown.
[0152] like Figure 6 As shown in the figure, the horizontal axis represents time, and the vertical axis represents the amount of available memory in the system. The N points on the vertical axis represent the sufficient amount of memory in the system. Figure 6The solid line in the figure shows the change of the amount of available memory in the system over time when the system traverses the global linked list. Figure 6 The dotted line in the figure shows the change of the amount of available system memory over time when the system traverses the shrinker_seeks_zero_list first. It can be understood that the slope of the solid line or the dotted line indicates the speed at which the system reclaims memory. In the time period from t0 to t1, the amount of available system memory decreases. At time t1, the system is in a low memory state and memory reclaim is required. When the system traverses the shrinker_seeks_zero_list first, at time t2, the amount of available system memory reaches the amount of sufficient system memory; when the system traverses the global linked list, at time t3, the amount of available system memory reaches the amount of sufficient system memory. It can be seen that when the system traverses the shrinker_seeks_zero_list first, the speed of memory reclaiming will be faster. Since there is a shrinker with a seeks value of 0 registered in the shrinker_seeks_zero_list, that is, when the system traverses the shrinker module with a seeks value of 0 first, the memory reclaim speed is faster.
[0153] It should be noted that when the method for managing memory provided by the embodiment of the present application is adopted, the global linked list is divided into multiple linked lists according to different seeks values, and the shrinkers of the modules are respectively registered in the divided linked lists, and according to different priorities of the linked lists, the linked lists with seeks values of 0 are preferentially traversed, so as to ensure that the system preferentially traverses the shrinkers with seeks values of 0 during the memory recovery process, so that the system can end the low memory state as soon as possible. If each module registers the shrinker in a global linked list, according to the principle of first registration first traversal, when the system recycles memory, it cannot be guaranteed that the system preferentially traverses the shrinkers with seeks values of 0, and therefore cannot guarantee a faster memory recovery speed.
[0154] S304, reclaiming memory.
[0155] It should be noted that after the system completes traversal of the above linked list, it can reclaim the memory of the module in which the shrinker is registered in the linked list.
[0156] On the one hand, for memory recycling on shrinker_seeks_zero_list, the number of concurrent processes is limited.
[0157] According to the introduction above, for a module with seeks value of 0, the amount of slab memory reclaimed by a single process at a time is half of the total amount of slab memory. When the number of concurrent processes is 2, the amount of slab memory reclaimed by two concurrent processes at a time is the total amount of slab memory (each process reclaims half of the total amount of memory at a time, and two concurrent processes reclaim the total amount of memory at a time). It can be seen that when the number of concurrent processes is 2, the memory recovery ratio can be guaranteed not to exceed 100%, where 2 can be understood as the threshold of the number of concurrent processes for memory recovery on shrinker_seeks_zero_list.
[0158] For example, when process A reclaims the slab memory, the number of processes increases by 1; when process A finishes reclaiming the slab memory, the number of processes decreases by one.
[0159] It should be noted that when the total number of concurrent processes is less than the threshold of the number of concurrent processes, new concurrent processes are allowed to reclaim the slab memory of modules whose seeks value is 0; when the total number of concurrent processes is greater than or equal to the threshold of the number of concurrent processes, new concurrent processes are not allowed to reclaim the slab memory of modules whose seeks value is 0.
[0160] On the other hand, for memory recycling on shrinker_seeks_default_list, there is a limit on the total concurrent recycling ratio.
[0161] According to the introduction above, for a module with a seeks value of 2, the amount of slab memory that a single process reclaims at a time is the product of the total amount of slab memory and the reclaim ratio. Among them, the reclaim ratio is (1 / 2)^[(priority-2)*seeks]. When the seeks value is 2, the reclaim ratio is (1 / 2)^[(priority-1)]. Since the priority value range is [0,12], and as the number of reclaims increases, the priority decreases successively. Therefore, when the priority value is 12, the reclaim ratio of slab memory that a single process reclaims at a time is the smallest (1 / 2 to the 11th power), which is 1 / 2048.
[0162] When there are multiple concurrent processes reclaiming the module memory, the sum of the number of slab memory reclaimed by multiple concurrent processes at a time needs to be less than or equal to the total slab memory of the module, that is, the sum of the recovery ratios of multiple concurrent processes is the total recovery ratio, and the total recovery ratio needs to be less than or equal to 100%, which can be understood as the threshold of the total recovery ratio. That is: ∑Pi≤1, where Pi represents the recovery ratio of process i, ∑Pi represents the total recovery ratio of concurrent processes, Pi=(1 / 2)^[(Wi-1)], where Wi represents the priority of the system to reclaim memory for process i.
[0163] Exemplarily, the concurrent processes include process A, process B, and process C. The system memory recovery priorities of multiple concurrent processes at a certain memory recovery time may be the same or different. The system memory recovery priority of process A is WA; the system memory recovery priority of process B is WB; and the system memory recovery priority of process C is WC. The recovery ratio PA of process A is (1 / 2)^[(WA-1)]; the recovery ratio PB of process B is (1 / 2)^[(WB-1)]; and the recovery ratio PC of process C is (1 / 2)^[(WC-1)]. The total recovery ratio of process A, process B, and process C is: (1 / 2)^[(WA-1)]+(1 / 2)^[(WB-1)]+(1 / 2)^[(WC-1)].
[0164] For example, at a certain moment, concurrent processes A, B, and C need to reclaim memory. The system memory reclaiming priority of process A is 12, the system memory reclaiming priority of process B is 11, and the system memory reclaiming priority of process C is 2. At this time, the total reclaiming ratio of processes A, B, and C is: (1 / 2)^[(12-1)]+(1 / 2)^[(11-1)]+(1 / 2)^[(2-1)]=1 / 2048+1 / 1024+1 / 2. Obviously, the total reclaiming ratio is less than or equal to 1, that is, 1 / 2048+1 / 1024+1 / 2≤1. Multiply both sides of the inequality by 2048 to obtain 1+2+1024≤2048, where 2048 can be understood as the threshold of the total reclaiming ratio for memory reclaiming on shrinker_seeks_default_list. It should be noted that if both sides of the above inequality are multiplied by a positive integer, the threshold of the total recovery ratio will change with the positive integer, and the positive integer is related to the maximum value (12) of the system's priority for memory recovery. In other words, the above inequality is multiplied by a positive integer twice to ensure that the recovery ratio of the process with the highest priority for system memory recovery can also be an integer. It can be seen that the maximum value of the positive integer is 2 to the power of 12 minus 1, which is 2048. When both sides of the inequality are multiplied by the largest positive integer 2048, the recovery ratio of any process with the priority for system memory recovery can be converted into an integer.
[0165] Exemplarily, by multiplying both sides of the above inequality by 2048, when process A reclaims slab memory, the total recovery ratio increases by 2^(12-WA), and when process A finishes reclaiming slab memory, the total recovery ratio decreases by 2^(12-WA). Similarly, when process B reclaims slab memory, the total recovery ratio increases by 2^(12-WB), and when process B finishes reclaiming slab memory, the total recovery ratio decreases by 2^(12-WB). When process C reclaims slab memory, the total recovery ratio increases by 2^(12-WC), and when process C finishes reclaiming, the total recovery ratio decreases by 2^(12-WC).
[0166] It should be noted that when the total recovery ratio is less than the threshold of the total recovery ratio (for example, 2048), new concurrent processes are allowed to reclaim the slab memory of modules with a seeks value of 2; when the total recovery ratio is greater than or equal to the threshold of the total recovery ratio (for example, 2048), new concurrent processes are not allowed to reclaim the slab memory of modules with a seeks value of 2.
[0167] For another example, concurrent processes A, B, and C need to reclaim memory, the system memory reclaiming priority of process A is 11, the system memory reclaiming priority of process B is 10, and the system memory reclaiming priority of process C is 9. At this time, the total reclaiming ratio of processes A, B, and C is: (1 / 2)^[(11-1)]+(1 / 2)^[(10-1)]+(1 / 2)^[(9-1)]=1 / 1024+1 / 512+1 / 256. Obviously, the total reclaiming ratio is less than or equal to 1, that is, 1 / 1024+1 / 512+1 / 256≤1. Multiply both sides of the inequality by 2048 to obtain 2+4+8≤2048, where 2048 can be understood as the threshold of the total reclaiming ratio for memory reclaiming on shrinker_seeks_default_list.
[0168] Exemplarily, by multiplying both sides of the above inequality by 2048, when process A reclaims slab memory, the total recovery ratio increases by 2^(12-WA), and when process A finishes reclaiming slab memory, the total recovery ratio decreases by 2^(12-WA). Similarly, when process B reclaims slab memory, the total recovery ratio increases by 2^(12-WB), and when process B finishes reclaiming slab memory, the total recovery ratio decreases by 2^(12-WB). When process C reclaims slab memory, the total recovery ratio increases by 2^(12-WC), and when process C finishes reclaiming, the total recovery ratio decreases by 2^(12-WC).
[0169] Furthermore, if the newly added process D needs to reclaim memory at this time, the system memory reclaiming priority value of process D is 12. The reclaiming ratio of process D is (1 / 2)^[(12-1)]=1 / 2048. At this time, the total recovery ratio of process A, process B, process C and process D is (1 / 2)^[(12-1)]+(1 / 2)^[(11-1)]+(1 / 2)^[(10-1)]+(1 / 2)^[(9-1)]=1 / 2048+1 / 1024+1 / 512+1 / 256. Obviously, the total recovery ratio is less than or equal to 1, that is, 1 / 2048+1 / 1024+1 / 512+1 / 256≤1. Multiplying both sides of the inequality by 2048, we get 1+2+4+8≤2048, where 2048 can be understood as the threshold of the total recovery ratio for memory recovery on shrinker_seeks_default_list.
[0170] It should be noted that the above steps S301 to S304 may be performed in the following manner: Figure 2 It is implemented in the kernel layer shown.
[0171] According to the above steps, the shrinkers of different modules can be registered in each linked list after the global linked list is split according to the seeks value. Therefore, when the module is uninstalled, the shrinker of the module needs to be uninstalled from the corresponding linked list. Among them, uninstalling the shrinker of a module from the corresponding linked list means deleting the shrinker from the corresponding linked list.
[0172] like Figure 7 , a schematic flow chart of a method for managing memory provided by an embodiment of the present application is shown, and the method can be implemented in the kernel layer of the software structure of an electronic device. The method 700 is described in detail below.
[0173] S701, determine at least two sub-link lists.
[0174] It should be noted that at least two sub-linked lists are related to the global linked list.
[0175] Exemplarily, the global linked list can be as follows Figure 4 In the shrinker_list shown in (a), at least two sublists can be as follows Figure 4 At least two of the shrinker_seeks_zero_list, shrinker_seeks_default_list and shrinker_other_value_list shown in (b).
[0176] It can be understood that at least two sub-linked lists are obtained by splitting the global linked list. The sub-linked lists obtained by splitting the global linked list include but are not limited to the above three sub-linked lists. According to actual conditions, the global linked list can be split into any other number (at least two) of sub-linked lists.
[0177] It should be noted that the priorities of different sub-chain lists are different. Exemplarily, at least two sub-chain lists include the following at least two items: a sub-chain list of the first priority, a sub-chain list of the second priority, and a sub-chain list of the third priority. Wherein, the first priority is higher than the second priority, and the second priority is higher than the third priority. The seek value (seeks) of the shrinker in the sub-chain list of the first priority is a first numerical value, the seek value of the shrinker in the sub-chain list of the second priority is a second numerical value, and the seek value in the sub-chain list of the third priority is a third numerical value. Wherein, the third numerical value can be a numerical value different from the first numerical value and the second numerical value.
[0178] Exemplarily, the first value is 0, the second value is a default value (2), and the third value is a positive integer that is not 0 and is not the default value.
[0179] For example, the first priority sub-link list is shrinker_seeks_zero_list, the second priority sub-link list is shrinker_seeks_default_list, and the third priority sub-link list is shrinker_other_value_list. The priority of the sub-link list reflects the order of the system traversal. It is understandable that the system can traverse the sub-link list with a high priority first.
[0180] It should be noted that the seeks of the shrinker in at least two sub-linked lists are different. It can be understood that, according to the seeks value, the global linked list is divided into at least two sub-linked lists. Among them, the seeks value of the shrinker in the sub-linked list of the first priority is the first value, the seeks value of the shrinker in the sub-linked list of the second priority is the second value, and the seeks value of the shrinker in the sub-linked list of the third priority is the third value (other values). Among them, the seeks value of dividing the global linked list should be understood as the final value of the module.
[0181] S702: Register the recycler of the first module in the first sub-link list according to the information of the recycler of the first module.
[0182] It should be noted that the first sub-list belongs to one of at least two sub-lists, and the information of the recycler of the first module includes the seek value of the recycler of the first module. The seek value is used to determine the amount of reclaimed memory and can also be used to indicate the speed at which the system recycles memory.
[0183] For example, most modules with seeks value of 0 are file systems based on RAM. The slab memory recovery process of such modules does not involve disk I / O operations, the amount of memory recovered at a time is large, and the memory recovery speed is fast. Most modules with seeks value of non-0 are file systems based on disk. The slab memory recovery process of such modules involves disk I / O operations, the amount of memory recovered at a time is related to the system recovery priority, and the memory recovery speed is slow.
[0184] It should be noted that the shrinkers of different modules can be registered in the corresponding sub-lists according to the values of the shrinker's seeks.
[0185] In one implementation, when the sought value of the recycler of the first module is a first value, the first sub-linked list is a sub-linked list of a first priority.
[0186] For example, the shrinker of the first module is as follows Figure 4 In (b) of FIG. 8 , “B_shrinker seeks=0” is shown, and the first sub-list is shrinker_seeks_zero_list.
[0187] In one implementation, when the sought value of the recycler of the first module is a second value, the first sub-linked list is a sub-linked list of the second priority.
[0188] For example, the shrinker of the first module is as follows Figure 4 In "A_shrinker seeks=2" shown in (b), the first sub-list is shrinker_seeks_default_list.
[0189] In one implementation, when the seeking value of the recycler of the first module is a third value, the first sub-linked list is a sub-linked list of the third priority.
[0190] For example, the shrinker of the first module is as follows Figure 4 In (b) shown in “D_shrinker seeks=4”, the first child linked list is shrinker_other_value_list.
[0191] S703: traverse at least two sub-lists according to their priorities.
[0192] In one implementation, the electronic device can call the memory recycling function corresponding to the recycler in at least two sub-linked lists to recycle memory. That is, after the system completes traversing at least two sub-linked lists, the memory of the module registered with the shrinker in the sub-linked list can be recycled.
[0193] For example, the memory recovery function of "B_shrinker seeks=0" is called to realize the recovery of the module memory.
[0194] When at least two sub-linked lists include a sub-linked list of a first priority, a sub-linked list of a second priority, and a sub-linked list of a third priority, the system traverses the sub-linked list of the first priority, the sub-linked list of the second priority, and the sub-linked list of the third priority in order from high to low priority.
[0195] For example, the sub-linked list of the first priority is traversed at the first moment, the sub-linked list of the second priority is traversed at the second moment, and the sub-linked list of the third priority is traversed at the third moment, wherein the second moment is later than the first moment, and the third moment is later than the second moment.
[0196] It should be noted that there may be multiple concurrent processes reclaiming memory at the same time, and multiple concurrent processes reclaiming memory at the same time may lead to the problem of excessive memory reclamation. Based on this, it is necessary to further limit the concurrent processes.
[0197] On the one hand, the number of concurrent processes traversing the sub-list of the first priority at the first moment is less than or equal to the first threshold.
[0198] That is, the number of concurrent processes that simultaneously traverse the shrinker_seeks_zero_list and reclaim memory is limited to be less than or equal to the first threshold.
[0199] Herein, the first threshold may be 2.
[0200] It should be noted that for the module with seeks value of 0, the amount of slab memory reclaimed by a single process at a time is half of the total slab memory. When the number of concurrent processes is 2, the amount of slab memory reclaimed by two concurrent processes at a time is the total slab memory (each process reclaims half of the total memory at a time, and the two concurrent processes reclaim the total memory at a time). It can be seen that when the number of concurrent processes is 2, it can be guaranteed that the memory recovery ratio does not exceed 100%, where 2 can be understood as the threshold of the number of concurrent processes for memory recovery on shrinker_seeks_zero_list.
[0201] It should be noted that when the number of concurrent processes is less than the first threshold, new concurrent processes are allowed to reclaim the slab memory of modules whose seeks value is 0; when the number of concurrent processes is greater than or equal to the first threshold, new concurrent processes are not allowed to reclaim the slab memory of modules whose seeks value is 0.
[0202] In one implementation, in response to the first process traversing the sub-list of the first priority, the number of concurrent processes increases by one; in response to the first process finishing memory reclaiming, the number of concurrent processes decreases by one.
[0203] On the other hand, at the second moment, the total recovery ratio of the concurrent processes traversing the sub-linked list of the second priority is less than or equal to the second threshold.
[0204] That is, the total recovery ratio of concurrent processes that simultaneously traverse shrinker_seeks_default_list and recover memory is limited to be less than or equal to the second threshold.
[0205] It should be noted that for modules with a seeks value of 2, the amount of slab memory reclaimed by a single process at a time is the product of the total amount of slab memory and the reclaim ratio. The reclaim ratio is (1 / 2)^[(priority-2)*seeks]. When the seeks value is 2, the reclaim ratio is (1 / 2)^[(priority-1)]. When there are multiple concurrent processes reclaiming the module memory, the sum of the amount of slab memory reclaimed by the multiple concurrent processes at a time needs to be less than or equal to the total amount of slab memory of the module. In other words, the sum of the reclaim ratios of the multiple concurrent processes is the total reclaim ratio, and the total reclaim ratio needs to be less than or equal to 100%. 100% can be understood as the threshold of the total reclaim ratio.
[0206] The second threshold value may be 1 (ie, 100%). Of course, for ease of understanding, the second threshold value may also be adjusted to 2048 according to the above description of step S304 in method 300. The specific content may be found in the above description of step S304, which will not be repeated here.
[0207] It should be noted that when the total recovery ratio of concurrent processes is less than the second threshold, new concurrent processes are allowed to reclaim the slab memory of modules with a seeks value of 2; when the total recovery ratio of concurrent processes is greater than or equal to the second threshold, new concurrent processes are not allowed to reclaim the slab memory of modules with a seeks value of 2.
[0208] In one implementation, in response to the first process traversing the second priority sub-list, the total recovery ratio of the concurrent processes increases the recovery ratio of the first process; in response to the first process finishing memory recovery, the total recovery ratio of the concurrent processes decreases the recovery ratio of the first recovery process. The recovery ratio of the first process is related to the priority of the system memory recovery of the first process at the second moment, and the priority of the system memory recovery is used to determine the amount of memory to be recovered.
[0209] For example, the first process is process A in the above step S304. When the system recycling priority of process A is 12, the recycling ratio of the first process is 2^(12-WA), where WA is the system recycling priority (12) of process A. For details, please refer to the description of step S304 above, which will not be repeated here.
[0210] In one implementation, when a module is uninstalled, the shrinker of the module needs to be uninstalled from the corresponding sub-link list. Uninstalling the shrinker of a module from the corresponding sub-link list means deleting the shrinker from the corresponding sub-link list.
[0211] It should be noted that the memory management method introduced above can be applied to smart devices, artificial intelligence products, etc., such as smartphones, tablets, smart screens, wearable devices, augmented reality (AR) / virtual reality (VR) devices, car terminals, etc.
[0212] The embodiments of the present application provide a method, device and electronic device for managing memory, which optimizes the speed of slab memory recovery by dividing sub-linked lists with different priorities. And by limiting concurrent processes, the problem of excessive recovery that may be caused when multiple concurrent processes recover slab memory at the same time is avoided, thereby improving the efficiency of system memory recovery.
[0213] In the second case, the shrinkers of each module are registered in a global linked list. When the system recycles memory, it will waste system power consumption to a certain extent, thereby affecting the efficiency of the system in reclaiming memory.
[0214] Based on the above description of the first case, it can be known that the amount of slab memory that each module recycles each time is related to the total amount of slab memory of the module and the priority of the system to recycle memory. In the second case, temporarily ignoring the restriction of seeks value on the amount of slab memory that the module recycles each time, it can be simply considered that the amount of slab memory that each module recycles each time = total amount of slab memory / 2^priority. It is understandable that, in the second case, the calculation formula for the amount of slab memory that each module recycles each time is a kind of schematic, used to indicate that the amount of slab memory that each module recycles each time is related to the priority of the system to recycle memory, and is not an accurate calculation formula.
[0215] It should be noted that the system will recycle the corresponding amount of memory only when the calculated amount of slab memory reclaimed by each module each time is greater than or equal to 1. When the calculated amount of slab memory reclaimed by each module each time is less than 1, the system will not recycle the memory.
[0216] For example, the total slab memory of a module is 4096, and the current priority value of memory recovery is 10. At this time, the module recovers 4096 / 2^10=4 of the slab memory.
[0217] It should be noted that when the system memory is insufficient, the kernel needs to reclaim memory. Usually, the priority of system memory reclaiming starts from 12. If the system memory is still insufficient after the current reclaiming, the priority of system memory reclaiming is reduced by one, and the priority of system memory reclaiming is set to 11 to obtain the amount of slab memory to be reclaimed next time. And so on, until the priority of system memory reclaiming is reduced to 0.
[0218] In the current Linux kernel, the shrinkers of each module will be registered in a unique global linked list, resulting in the possibility that the global linked list includes multiple types of shrinkers. It is understandable that the shrinker types of each module can be different, and there is a one-to-one correspondence between the module and the shrinker type. If different types of shrinkers are registered in a global linked list, when the system reclaims memory, system power consumption will be wasted to a certain extent.
[0219] For example, some modules, corresponding to the tmpfs file system, will not cache slab memory if the huge page function is not enabled, and will release the requested slab memory after use. In other words, the number of slabs corresponding to some modules is 0. When the system needs to reclaim slab memory, it traverses the global linked list and calls the shrinker registered by the module. There will be no slab memory to reclaim, which will cause a waste of system power consumption.
[0220] For another example, some modules correspond to the Ext4 file system, and the number of slabs cached by the module is related to the number of files in the disk partition corresponding to the file system. Take the loop partition of an Android phone as an example, which corresponds to the Ext4 file system. There are more than 20 loop partitions in the phone, and each loop partition has 20 to 200 preset files. Therefore, the number of slabs cached by each partition is at most 20 to 200. It can be seen that the number of slabs is at most 200, that is, no more than 2 to the 8th power (28=256). According to the above content, the number of slab memory recovered by each module each time is related to the priority of the system to recycle memory. When the total amount of slab memory is 200, and the priority of the system to recycle memory is 12, 11, 10, 9, 8, the calculated value is less than 1, and memory recovery will not be performed, which will cause a waste of system power consumption.
[0221] It should be noted that the memory management method in the second case can be applicable to a variety of modules, including but not limited to the above-mentioned tmpfs module and Ext4 module, and can also be applicable to F2fs module, erofs module, etc.
[0222] It is understandable that the temporary file system (tmpfs) is a memory-based file system with fast read and write speeds. When the large page function is not enabled, the slab memory is not cached and it can support the Linux system. The fourth extended file system (Ext4) is a log file system under the Linux system. The enhanced read-only file system (erofs) is a read-only file system under the Linux system, which is used to save storage space while ensuring the end-to-end performance of embedded devices. The flash-friendly file system (F2fs) is a new open source flash file system designed based on flash memory (not and, NAND) storage devices, which can support the Linux system. The enhanced read-only file system (erofs) is a read-only file system under the Linux system, which is used to save storage space while ensuring the end-to-end performance of embedded devices.
[0223] It should be noted that the memory management method in the second case is applicable to modules whose seeks value is the default value (2). The module whose seeks value is 2 belongs to the file system.
[0224] In view of this, the embodiments of the present application provide a method, device and electronic device for managing memory, which can optimize the system power consumption when the system recycles memory to a certain extent, improve the efficiency of system memory recycling, and improve system performance. Among them, the method for managing memory provided by the embodiments of the present application can be used in Figure 2 It is implemented in the kernel layer shown.
[0225] The following is a detailed introduction to the method for managing memory applicable to the second case above. The method 800 can be applied to slab memory management of the Linux kernel. The method 800 mainly includes the following steps S801 to S803.
[0226] S801, split the global linked list.
[0227] Exemplarily, the global linked list is split into at least two linked lists, for example, a shrinker_now linked list, a shrinker_delay linked list, and a shrinker_skip linked list.
[0228] It should be noted that the types of the file systems in which the shrinkers are registered in the three linked lists are different. It can be understood that the global linked list is divided into at least two linked lists according to the file system type.
[0229] like Figure 8 As shown, a schematic diagram of the global linked list and the split linked list is shown.
[0230] Figure 8 (a) in FIG. 1 shows a global linked list, which may include shrinkers of different file system types. For example, the file system types are F2fs, tmpfs, Ext4, etc. It should be noted that in the second case, each module corresponds to a different file system type. For example, the file system type corresponding to the F2fs module is F2fs; the file system type corresponding to the tmpfs module is tmpfs; and the file system type corresponding to the Ext4 module is Ext4.
[0231] It can be seen that various types of file systems are registered in the global linked list. For example, Figure 8 "F2fs_shrinker" shown in (a) indicates the shrinker registered by the F2fs file system in the global linked list; "tmpfs_shrinker" indicates the shrinker registered by the tmpfs file system in the global linked list; "Ext4_fs_shrinker / dev / block / sdd" indicates the shrinker registered by the Ext4 file system in the global linked list; "Ext4_fs_shrinker / dev / block / loop" indicates the shrinker registered by the Ext4 file system in the global linked list.
[0232] For example, the order of shrinkers in the global linked list can be sorted according to the time when each module registered the shrinker. The earliest registered shrinker is ranked first. Figure 8 In the global linked list shown in (a), the order of shrinkers is F2fs_shrinker, tmpfs_shrinker, Ext4_fs_shrinker / dev / block / sdd, and Ext4_fs_shrinker / dev / block / loop.
[0233] Figure 8(b) in FIG. 1 shows the split linked lists, for example, the shrinker_now linked list, the shrinker_delay linked list, and the shrinker_skip linked list.
[0234] For example, the order of the shrinkers in the split linked list can be sorted according to the time when each module registered the shrinker. The earliest registered shrinker is sorted first. Figure 8 In the shrinker_now linked list shown in (b), the order of shrinkers is F2fs_shrinker, Ext4_fs_shrinker / dev / block / sdd.
[0235] S802, register shrinker.
[0236] It should be noted that when each module registers the shrinker, it carries the additional information of the module. The additional information includes the type of file system, the disk partition path corresponding to the file system, the mount directory of the file system, etc. The system can register the shrinker of the module to the corresponding linked list according to the module's attachment information.
[0237] Specifically, you can follow the Fig. 9 The steps shown implement the process of registering shrinkers with various file systems. The process may include the following steps:
[0238] S901, the file system registers the shrinker.
[0239] S902: Determine whether the file system type is tmpfs.
[0240] S903, register the shrinker to the shrinker_skip linked list.
[0241] S904: Determine whether the disk device path corresponding to the file system starts with / dev / block / loop.
[0242] S905, register the shrinker to the shrinker_delay linked list.
[0243] S906, register the shrinker to the shrinker_now linked list.
[0244] It should be noted that Fig. 9 This is just an example of how to register a shrinker. Fig. 9The order of first determining the type of file system and then determining the disk device path corresponding to the file system is shown as only one of the orders. In the present application, the type of file system can also be determined first, and then the disk device path corresponding to the file system can be determined, so as to register the shrinker in the corresponding linked list.
[0245] In one implementation, Fig. 9 The specific steps of registering the shrinker shown may also include step S907. Fig. 9 Not shown.
[0246] S907, determine whether the mount directory of the file system starts with modem.
[0247] It should be noted that the execution order of step S907 is not limited, and it can be executed before the above step S904, or after the above step S905, or it can be executed optionally with step S904.
[0248] Exemplarily, when registering a shrinker in a file system, the type of the file system is first determined. When the type of the file system is tmpfs, the shrinker of the file system is registered to the shrinker_skip linked list obtained by splitting the global linked list in step S801. When the type of the file system is not tmpfs, step S904 is continued to be executed to further determine the disk device path corresponding to the file system. When the type of the file system is not tmpfs, and the disk device path corresponding to the file system is a string starting with / dev / block / loop, or step S907 is continued to be executed to further determine the mount directory of the file system, when the type of the file system is not tmpfs, and the mount directory of the file system is a string starting with modem, the shrinker of the file system is registered to the shrinker_delay linked list. Alternatively, when the disk device path corresponding to the file system is a string starting with / dev / block / loop, or the mount directory of the file system is a string starting with modem, or both of the above conditions are met, the shrinker of the file system is registered to the shrinker_delay linked list. When the file system type is not tmpfs, and the disk device path corresponding to the file system is not a string starting with / dev / block / loop, and the mount directory of the file system is not a string starting with modem, the shrinker of the file system is registered to the shrinker_now list. Among them, "dev" in the / dev / block / loop string represents the device directory, "block" represents the block device directory, and "loop" represents the disk device named loop.
[0249] For example, Figure 8 As shown in (b), the tmpfs type file system registers shrinker in the shrinker_skip list. The file system type is Ext4, and the disk device path corresponding to the file system starts with the string / dev / block / loop. The file system registers shrinker in the shrinker_delay list. The file system type is F2fs, and the disk path corresponding to the file system does not start with the string / dev / block / loop, and the mount directory of the file system does not start with modem. The file system registers shrinker in the shrinker_now list. Similarly, the file system type is Ext4, and the disk path corresponding to the file system does not start with the string / dev / block / loop, and the mount directory of the file system does not start with modem. The file system also registers shrinker in the shrinker_now list.
[0250] The type of the above-mentioned file system, the disk device path corresponding to the file system, and the mount directory of the file system can be understood as the conditions for the file system to register the shrinker to the corresponding linked list. These three conditions can be combined arbitrarily to determine the target linked list for the file system to register the shrinker.
[0251] S803, traverse the corresponding linked list.
[0252] like Fig.10 As shown, the specific process of traversing the corresponding linked list is shown.
[0253] S1001, traverse the shrinker_now linked list and recycle memory.
[0254] S1002: Determine whether the priority of the system to reclaim memory is lower than a threshold.
[0255] S1003, do not traverse the shrinker_delay linked list.
[0256] S1004, traverse the shrinker_delay linked list and recycle memory.
[0257] For example, when the priority of system memory recycling is greater than or equal to the threshold, the shrinker_now list is traversed and memory is recycled; when the priority of system memory recycling is lower than the threshold, the shrinker_now list and the shrinker_delay list are traversed and memory is recycled. The shrinker_skip list is skipped and the shrinker_skip list is not traversed. The threshold value can be 8.
[0258] That is to say, when the system reclaims memory, regardless of the type of file system and the priority of system memory reclaim, it will first traverse the shrinker_now list and call the memory reclaim function corresponding to each shrinker in the shrinker_now list to realize memory reclaim. Secondly, when the priority of system memory reclaim is greater than or equal to the threshold, it is not necessary to traverse the shrinker_delay list; when the priority of system memory reclaim is less than the threshold, it is necessary to traverse the shrinker_delay list again and call the memory reclaim function corresponding to each shrinker in the shrinker_delay list to realize memory reclaim.
[0259] It should be noted that steps S1001 to S1004 can be executed each time the linked list is traversed. For example, in the first traversal, the priority value of the system recycling is 12, in the second traversal, the priority value of the system recycling is 11, and so on. In the sixth traversal, the priority value of the system recycling is 7. According to the above step S1002, it is determined that the system recycling priority at this time is lower than the threshold value 8. After traversing the shrinker_now linked list, the shrinker_delay linked list can be traversed. In the previous first traversal to the fifth traversal, since the priority of the system recycling is higher than or equal to the threshold value 8, only the shrinker_now linked list is traversed, and the shrinker_delay linked list is not traversed.
[0260] It should be noted that if a certain file system does not cache slab memory and the number of slabs corresponding to the file system is 0 (for example, a tmpfs type file system), the shrinker will be registered in the shrinker_skip list. When the system reclaims memory, access to the shrinker of the file system can be skipped to save system power consumption. If the number of slabs cached by a certain file system is related to the corresponding disk path, and the preset files of the disk partition corresponding to the file system are limited, therefore, the number of slabs cached by the disk partition is also limited, the shrinker will be registered in the shrinker_delay list. When the system reclaims memory, it can start from the appropriate priority of the system to reclaim memory, avoiding the problem that the memory cannot be reclaimed when the number of slabs is small and the priority of the system to reclaim memory is high, which wastes system power consumption, and improves the efficiency of the system to reclaim memory to a certain extent.
[0261] Exemplarily, when the file system type is Ext4, and the disk path corresponding to the file system is a string beginning with / dev / block / loop, the module registers the shrinker to the shrinker_delay list. Since the preset files of the disk partition corresponding to the file system are at most 200, the number of slabs cached by the disk partition corresponding to the system file is at most 200, which does not exceed 2 to the 8th power (28=256). When the system recycles memory, it is determined that the priority of the system recycling is less than the threshold (the threshold is 8), then after traversing the shrinker_now list, the system will continue to traverse the shrinker_delay list, that is, continue to traverse the list corresponding to the shrinker registered by the file system. Call the memory recycling function corresponding to each shrinker in the shrinker_delay list, and start recycling memory from the priority value of 7 of the system memory recycling. If the memory recycling is completed this time and the system memory is still insufficient, the priority of the system recycling memory is reduced by one to obtain the amount of memory recycled next time. And so on, until the priority of the system recycling memory is reduced to 0.
[0262] According to the above steps, the shrinkers of different file systems (or modules) will be registered in each linked list after the global linked list is split. Therefore, when a file system (or module) is uninstalled, the shrinker of the file system needs to be uninstalled from the corresponding linked list. For example, the tmpfs file system is uninstalled from the shrinker_skip linked list. Among them, uninstalling the shrinker of a file system from the corresponding linked list means deleting the shrinker from the corresponding linked list.
[0263] It should be noted that the above is only an example of the specific way of registering shrinkers in different linked lists for various modules. The module with the tmpfs file system mentioned above may have the phenomenon of invalid memory recovery; the module with the Ext4 file system may have the phenomenon of inefficient memory recovery. It should be understood that if modules of other file system types also have similar invalid memory recovery or inefficient memory recovery as mentioned above, and cause system power consumption waste to a certain extent, when registering the shrinker, the shrinker can be registered in the corresponding linked list according to the module-specific additional information (for example, the type of file system, the disk partition path corresponding to the file system, etc.), so that when the system memory is insufficient, efficient memory recovery can be completed while saving system power consumption.
[0264] It should be noted that the memory management method introduced above can be applied to smart devices, artificial intelligence products, etc., such as smartphones, tablets, smart screens, wearable devices, augmented reality (AR) / virtual reality (VR) devices, car terminals, etc.
[0265] In the third case, the shrinkers of each module are registered in a global linked list. When the global linked list is read and written, it will affect the behavior of registering or unloading shrinkers of other modules, thereby affecting the efficiency of system memory recovery, resulting in poor system operation and performance degradation.
[0266] It should be noted that, based on the above introduction to system recycling, when a module is initialized, the shrinker corresponding to the module will be registered in the global linked list. When the module is uninstalled, the corresponding shrinker also needs to be uninstalled from the global linked list. Since the shrinkers of each module are registered in a global linked list, when the global linked list is read and written, it needs to be protected by the shrinker_rwsem lock. This will cause other modules to register or uninstall shrinkers (need to write shrinker_list) when the Linux kernel recycles slab memory (needs to read shrinker_list), which will affect the efficiency of system memory recycling.
[0267] In view of this, the embodiments of the present application provide a method, device and electronic device for managing memory, which can effectively reduce the probability of blocking memory recovery and registration / uninstallation operations, improve the efficiency of system memory recovery, and thus effectively improve the dynamic performance of the system in a low memory state.
[0268] like Fig.11 As shown, the main steps of a memory management method provided in an embodiment of the present application are shown. The method 1100 can be applied to the slab memory management of the Linux kernel. The method 1100 is specifically introduced below.
[0269] S1101, in response to a registration instruction, obtaining an activity index of a shrinker of a registration module.
[0270] It should be noted that, in some implementations, when the Linux kernel determines to reclaim memory, the modules that need to reclaim memory can be determined through the slab mechanism and the memory corresponding to each module can be reclaimed. If a module needs to reclaim memory but is not registered on linked list 1 or linked list 2, it is determined that the module needs to be registered. The Linux kernel generates a registration instruction to instruct the module to register.
[0271] Shrinker is a structure that stores a function for implementing the slab memory recovery of the corresponding module. The Linux kernel can recover the slab memory of the corresponding module by calling the recovery function in shrinker.
[0272] It should be noted that before registration, the activity index of the shrinker of the module can be obtained from a pre-stored global linked list based on the recycling function stored in the shrinker of the module. The global linked list can be stored in the cache, local storage or cloud in the form of an array. The global linked list includes multiple recycling functions and activity indexes corresponding to each recycling function. Each recycling function corresponds to a shrinker, and the activity index of the shrinker is the activity index corresponding to the recycling function corresponding to the shrinker. When each module is registered for the first time (that is, the recycling function corresponding to the module is not found in the pre-stored global linked list), the recycling function corresponding to the shrinker of the module will be written into the global linked list, and the activity index of the shrinker will be set to a preset value. For example, the preset value of the activity index corresponding to the recycling function can be set to 0.
[0273] Exemplarily, the recycling function corresponding to the shrinker (for example, written as gpu_shrinker) of the gpu module can be written as gpu_scan_objects. When the gpu module is registered for the first time (i.e., gpu_scan_objects is not found in the global linked list), gpu_scan_objects is written into the global linked list, and the activity index corresponding to gpu_scan_objects is set to 0. Whenever the gpu module is unloaded, the activity index corresponding to gpu_scan_objects in the global linked list can be increased by 1. When the gpu module is registered again, the corresponding activity index is searched in the global linked list according to gpu_scan_objects, and the activity index found is used as the activity index of gpu_shrinker.
[0274] S1102, save the activity index of the shrinker in the shrinker.
[0275] It should be noted that shrinker is a structure, which is composed of at least one data. Each data constituting the structure is called a "member" of the structure, that is, the structure includes at least one member.
[0276] Exemplarily, the shrinker may include members such as a recycling function and an active index (active_index). For example, in gpu_shrinker, two members, gpu_scan_objects and gpu_active_index, may be included. In step S1101, the active index of the gpu_shrinker found from the global linked list may be stored in gpu_active_index.
[0277] S1103: Determine whether the activity index of the shrinker is less than a threshold value 1.
[0278] It should be noted that, if the activity index of the shrinker is less than the threshold value 1, the subsequent step S1104 is executed; if the activity index of the shrinker is greater than or equal to the threshold value 1, the subsequent step S1105 is executed.
[0279] S1104, register the shrinker to linked list 1.
[0280] It should be noted that after the shrinker is registered in the linked list 1, the subsequent step S1110 can be executed.
[0281] S1105, register the shrinker to link table 2.
[0282] It should be noted that after the shrinker is registered in the linked list 2, the subsequent step S1110 can be executed.
[0283] It should be noted that the activity index of the shrinker registered in list 1 is less than the threshold value 1. List 1 can also be called the "cold shrinker list", and the shrinker modules registered in list 1 can be called "cold modules". Cold modules can be understood as modules with low unloading frequency in the Linux kernel, for example, f2fs modules and erofs modules. The activity index of shrinker is greater than or equal to threshold value 1. List 2 can be called the "hot shrinker list", and the shrinker modules registered in list 2 can be called "hot modules". Hot modules can be understood as modules with high unloading frequency in the Linux kernel, such as gpu modules.
[0284] Exemplarily, threshold 1 may be a preset value. For example, when the system is initialized, threshold 1 may be set to 20. Threshold 1 may also be updated from a server or cloud. For example, the system may obtain an updated threshold 1 from a server or cloud every 10 minutes, and the updated threshold 1 may be obtained by the server based on a global linked list uploaded by each electronic device.
[0285] It can be understood that in step S1104 and step S1105, registering the shrinker in linked list 1 or linked list 2 is worthwhile adding the shrinker to linked list 1 or linked list 2.
[0286] It should be noted that the arrangement order of the shrinkers in linked list 1 or linked list 2 is related to the registration order, and the shrinker registered first is arranged at the front of the linked list.
[0287] S1106 , in response to the uninstall instruction, obtaining an activity index of a shrinker in the shrinker corresponding to the module to be uninstalled.
[0288] S1107 , determining whether the activity index of the shrinker is less than a threshold of 1.
[0289] It should be noted that, if the activity index of the shrinker is less than the threshold value 1, the subsequent step S1108 is executed; if the activity index of the shrinker is greater than or equal to the threshold value 1, the subsequent step S1109 is executed.
[0290] S1108, unload the shrinker from linked list 1.
[0291] S1109, unload the shrinker from linked list 2.
[0292] It should be noted that when a module stops running, the shrinker corresponding to the module needs to be unloaded from linked list 1 or linked list 2. When unloading, the active index of the shrinker can be obtained from the active_index of the shrinker, and according to the active index of the shrinker, it is determined whether to unload the shrinker from linked list 1 or linked list 2. According to the above step S1101, after unloading a shrinker, the active index of the shrinker needs to be increased by 1. That is, the value of active_index is increased by 1, and at the same time, the corresponding active index is searched in the global linked list according to the recycling function in the shrinker, and the active index is increased by 1.
[0293] Among them, the execution method of step S1107 is similar to the execution method of step S1103, which will not be repeated here.
[0294] It should be noted that the way in which the module terminates operation may include the electronic device responding to the termination operation and terminating the corresponding module; or the module is destroyed after the task is completed, etc., and the embodiments of the present application are not limited to this.
[0295] It can be understood that unloading the shrinker from the linked list 1 or the linked list 2 refers to deleting the shrinker from the corresponding linked list.
[0296] It is understandable that the activity index of the cold module can be continuously increased with the continuous unloading and registration of the Linux kernel, until after the cold module is unloaded, the activity index corresponding to the cold module is greater than or equal to the threshold 1, then the cold module can be registered in the linked list 2, and the cold module is transformed into a hot module.
[0297] S1110, determining whether a memory reclaiming instruction is received.
[0298] It should be noted that if a memory reclaiming instruction is received, step S1111 is executed, otherwise, the process continues to wait for a memory reclaiming instruction.
[0299] S1111 , in response to a memory reclaiming instruction, reclaiming the memory of the shrinker registered in linked list 1 , and reclaiming the memory of the shrinker registered in linked list 2 .
[0300] For example, the system monitors the remaining memory, and when the Linux kernel receives a memory call request that is larger than the free memory, it will forcefully reclaim the memory. Alternatively, the Linux kernel can also reclaim the memory regularly to prepare for sudden memory calls.
[0301] Among them, when the system forcibly reclaims memory or reclaims memory regularly, it will send a memory reclaim instruction to the Linux kernel. After receiving the memory reclaim instruction, the Linux kernel responds to the memory reclaim instruction and reclaims the memory of the shrinker registered in linked list 1 and the memory of the shrinker registered in linked list 2.
[0302] It should be noted that the arrangement order of the shrinkers in linked list 1 or linked list 2 is related to the registration order. When the system reclaims memory, the order in which the reclaim function is read and executed is related to the arrangement order. For example, the shrinker that is registered first is at the front of linked list 1 or linked list 2. When the system reclaims memory, it will also read the shrinker at the front of linked list 1 or linked list 2 first and reclaim the slab memory of the module corresponding to the shrinker.
[0303] In the third case, S1101-S1105 are steps for registering the shrinker in the linked list, S1106-S1109 are steps for unloading the shrinker from the linked list, and S1110 and S1111 are steps for reclaiming the memory corresponding to the linked list. S1101-S1105, S1106-S1109, and S1110 and S1111 can be executed simultaneously or separately. For example, when registering a module to linked list 1, the module can also be unloaded from linked list 2. Alternatively, when reclaiming the slab memory of the module corresponding to linked list 1, the shrinker can be registered to linked list 2 or the module can be unloaded from linked list 2.
[0304] It should be noted that since linked list 1 and linked list 2 are independent of each other, registering / unloading linked list 2 will not affect the recovery of slab memory of the corresponding module of linked list 1, which can effectively reduce the probability of blocking memory recovery and registration / unloading operations, and effectively improve the dynamic performance of the system under low memory state.
[0305] In addition, more than two linked lists can be set, and the shrinker of the module can be registered in the corresponding linked list according to multiple thresholds. The method of determining module ownership, registering modules, unloading modules and reclaiming memory is similar to the above steps and will not be repeated here.
[0306] It should be understood that the order of execution of the above steps does not necessarily mean the order in which they are executed. The order in which each process is executed should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.
[0307] It should be noted that the memory management method introduced above can be applied to smart devices, artificial intelligence products, etc., such as smartphones, tablets, smart screens, wearable devices, augmented reality (AR) / virtual reality (VR) devices, car terminals, etc.
[0308] Based on the above content, three situations are introduced respectively, in which the method for managing memory provided by the embodiment of the present application is adopted to avoid the shrinkers of various modules in the Linux kernel being registered in the same global linked list, thereby affecting the efficiency of system memory recovery and affecting user experience.
[0309] Fig.12 is a schematic block diagram of a memory management device according to an embodiment of the present application. Fig.12The management memory device 1200 shown may include: a processor 1210, a transceiver 1220, and a memory 1230. The processor 1210, the transceiver 1220, and the memory 1230 are connected via an internal connection path, the memory 1230 is used to store instructions, the processor 1210 is used to execute the instructions stored in the memory 1230, and the transceiver 1220 receives / sends part of the parameters. Optionally, the memory 1230 may be coupled to the processor 1210 via an interface, or may be integrated with the processor 1210.
[0310] It should be noted that the transceiver 1220 may include but is not limited to a transceiver device such as an input / output interface to achieve communication between the management memory device 1200 and other devices or communication networks.
[0311] In the implementation process, each step of the above method can be completed by an integrated logic circuit of hardware in the processor 1210 or an instruction in the form of software. The method disclosed in conjunction with the embodiment of the present application can be directly embodied as a hardware processor for execution, or a combination of hardware and software modules in the processor for execution. The software module can be located in a mature storage medium in the art such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory or an electrically erasable programmable memory, a register, etc. The storage medium is located in the memory 1230, and the processor 1210 reads the information in the memory 1230 and completes the steps of the above method in conjunction with its hardware. To avoid repetition, it is not described in detail here.
[0312] It should also be understood that in the embodiment of the present application, the memory may include a read-only memory and a random access memory, and provide instructions and data to the processor. A portion of the processor may also include a non-volatile random access memory. For example, the processor may also store information about the device type.
[0313] The embodiment of the present application provides a computer program product, which, when executed on a device, enables the device to execute the technical solution in the above embodiment. Its implementation principle and technical effect are similar to those of the above method-related embodiments, and will not be described in detail here.
[0314] The embodiment of the present application provides a readable storage medium, which contains instructions. When the instructions are executed on a device, the device executes the technical solution of the above embodiment. The implementation principle and technical effect are similar and will not be repeated here.
[0315] The embodiment of the present application provides a chip, which is used to execute instructions. When the chip is running, the technical solution in the above embodiment is executed. The implementation principle and technical effect are similar and will not be repeated here.
[0316] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the embodiments of the present application.
[0317] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the above-described devices and units (modules) can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0318] In the several embodiments provided in the present application, it should be understood that the disclosed devices, apparatuses and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the unit is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0319] The units described above as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0320] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0321] If the above functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the embodiment of the present application is essentially or the part that contributes to the prior art or the part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk and other media that can store program code.
[0322] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A method for managing memory, characterized in that: include: Determine at least two sub-linked lists, wherein the at least two sub-linked lists are related to the global linked list; According to the information of the recycler of the first module, register the recycler of the first module in a first sub-chained list, the first sub-chained list belongs to one of the at least two sub-chained lists, the information of the recycler of the first module includes a seek value of the recycler of the first module, and the seek value is used to determine the amount of reclaimed memory; The at least two sub-linked lists are traversed according to the priorities of the at least two sub-linked lists.
2. The method according to claim 1, characterized in that The method further comprises: The memory recovery function corresponding to the recycler in the at least two sub-linked lists is called to recover the memory.
3. The method according to claim 1 or 2, characterized in that: The at least two sub-link lists include at least two of the following: The sub-linked list of the first priority, the sub-linked list of the second priority, and the sub-linked list of the third priority; Among them, the first priority is higher than the second priority, the second priority is higher than the third priority, the seeking value of the recycler in the sub-linked list of the first priority is a first value, the seeking value of the recycler in the sub-linked list of the second priority is a second value, and the seeking value of the recycler in the sub-linked list of the third priority is a third value, wherein the third value is different from the first value, and the third value is different from the second value.
4. The method according to claim 3, characterized in that When the sought value of the recycler of the first module is the first value, the first sub-link list is a sub-link list of the first priority.
5. The method according to claim 3, characterized in that: When the sought value of the recycler of the first module is the second value, the first sub-link list is a sub-link list of the second priority.
6. The method according to claim 3, characterized in that: When the sought value of the recycler of the first module is the third value, the first sub-link list is a sub-link list of the third priority.
7. The method according to any one of claims 3 to 6, characterized in that The traversing the at least two sub-chained lists according to the priorities of the at least two sub-chained lists comprises: Traversing the first priority sub-list at a first moment, wherein the number of concurrent processes traversing the first priority sub-list at the first moment is less than or equal to a first threshold; The second priority sub-list is traversed at a second time, the second time being later than the first time, wherein a total recovery ratio of concurrent processes traversing the second priority sub-list at the second time is less than or equal to a second threshold.
8. The method according to claim 7, characterized in that The method further comprises: In response to the first process traversing the first priority sub-list, the number of concurrent processes increases by one; In response to the first process traversing the sub-linked list of the second priority level, the total recycling ratio of the concurrent processes is increased by the recycling ratio of the first process; The recycling ratio of the first process is related to the priority of the system memory recycling of the first process at the second moment, and the priority of the system memory recycling is used to determine the amount of recycled memory.
9. The method according to claim 7 or 8, characterized in that: The method further comprises: In response to the first process completing memory reclaiming, the number of concurrent processes is reduced by one; In response to the first process finishing memory reclaiming, the total reclaim ratio of the concurrent processes is reduced by the reclaim ratio of the first process; The recycling ratio of the first process is related to the priority of the system memory recycling of the first process at the second moment, and the priority of the system memory recycling is used to determine the amount of recycled memory.
10. The method according to any one of claims 7 to 9, characterized in that The method further comprises: The sub-list of the third priority is traversed at a third time, and the third time is later than the second time.
11. An electronic device, comprising: one or more processors; one or more memories; The one or more memories store one or more computer programs, and the one or more computer programs include instructions, which, when executed by the one or more processors, enable the electronic device to perform the method of any one of claims 1 to 10.
12. A device for managing memory, characterized in that: include: A processor coupled to a memory, wherein the memory is used to store a computer program, and the processor is used to run the computer program, so that the device for managing memory performs the method according to any one of claims 1 to 10.
13. The memory management device according to claim 12, characterized in that: Also included is one or more of the memory and the transceiver, wherein the transceiver is used to receive signals and / or send signals.
14. A computer-readable storage medium, characterized in that: A computer program is stored thereon, and when the computer program is executed by a computer, the computer is caused to implement the method according to any one of claims 1 to 10.
15. A computer program product comprising instructions, characterized in that When the computer program product is run on a computer, the computer is caused to perform the method according to any one of claims 1 to 10.
16. A chip, characterized in that: The chip includes a processor and a data interface, and the processor reads instructions stored in a memory through the data interface to execute the method according to any one of claims 1 to 10.