Memory management method and device, storage medium and program product
Through the management method of the first memory pool and the second memory pool, the system performance fluctuation and fragmentation problems caused by unreasonable memory allocation are solved, and centralized management and efficient utilization of memory resources are achieved.
Patent Information
- Application Number
- CN202410383764.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-03-29
- Publication Date
- 2025-09-30
AI Technical Summary
The existing memory management mechanism does not consider data types and usage requirements when allocating memory, resulting in frequent memory swapping, system performance fluctuations and memory fragmentation.
A centralized management method is adopted for the first memory pool and the second memory pool. Memory pages are managed through the first memory pool management structure, divided into smaller memory blocks to create the second memory pool, and memory space is allocated at the memory block granularity to reduce system performance fluctuations and memory fragmentation.
It realizes the centralized management of memory resources, reduces the frequency of memory swapping, improves system performance and memory utilization, and enhances system stability.
Smart Images

Figure CN120723664A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of memory management technology, and in particular to a memory management method, device, storage medium, and program product. Background Art
[0002] Traditional operating systems (OSs) provide memory management mechanisms, such as the Buddy System. These mechanisms divide memory into pages of varying sizes and allocate and reclaim memory at the page level. This allows applications to request and obtain the appropriate amount of memory on demand, reducing memory fragmentation.
[0003] Although some existing memory management mechanisms use memory paging technology for memory allocation and recycling, which can reduce memory fragmentation, these memory management mechanisms still have the problem of unreasonable memory allocation, which easily leads to frequent memory swapping. In other words, it takes a lot of time to swap data out and into memory, causing system performance fluctuations. Summary of the Invention
[0004] Multiple aspects of the present application provide a memory management method, device, storage medium and program product for centralized management of memory resources through a first memory pool and a second memory pool, realizing a memory management method that is relatively independent of the operating system, reducing system performance fluctuations and improving system performance.
[0005] An embodiment of the present application provides a memory management method, including: using a first memory pool management structure to manage a first memory pool, the first memory pool includes at least one memory page requested from an operating system to provide allocatable memory space; using a second memory pool management structure to manage a second memory pool, the second memory pool includes memory blocks obtained by dividing a target memory space, and the target memory space is requested from the memory pages in the first memory pool; based on the second memory pool management structure, allocating memory space with the memory blocks in the second memory pool as the granularity, so that the data allocated to the memory space is distributed in the memory pages included in the first memory pool.
[0006] An embodiment of the present application provides a physical machine, the physical machine comprising hardware resources and a host operating system running on the hardware resources, a virtualization layer implemented between the hardware resources and the host operating system, the virtualization layer comprising a centralized memory management module;
[0007] The centralized memory management module is used to manage the first memory pool using a first memory pool management structure, where the first memory pool includes at least one memory page requested from the host operating system to provide allocatable memory space; manage the second memory pool using a second memory pool management structure, where the second memory pool includes memory blocks obtained by dividing the target memory space, where the target memory space is requested from the memory pages in the first memory pool; and allocate memory space to other functional modules in the virtualization layer based on the memory blocks in the second memory pool as the granularity, based on the second memory pool management structure, so that the data allocated to the memory space is distributed in the memory pages included in the first memory pool.
[0008] An embodiment of the present application further provides a physical machine, the physical machine comprising hardware resources and a host operating system running on the hardware resources, the operating system comprising: a centralized memory management module and a traditional memory management module;
[0009] The centralized memory management module is used to manage a first memory pool using a first memory pool management structure, wherein the first memory pool includes at least one memory page requested from the traditional memory management module to provide allocatable memory space; manage a second memory pool using a second memory pool management structure, wherein the second memory pool includes memory blocks obtained by dividing a target memory space, wherein the target memory space is requested from the memory pages in the first memory pool; and allocate memory space based on the second memory pool management structure with the memory blocks in the second memory pool as the granularity, so that the data allocated to the memory space is distributed in the memory pages included in the first memory pool.
[0010] An embodiment of the present application also provides an electronic device, comprising: a memory and a processor; the memory is used to store a computer program; the processor is coupled to the memory and is used to execute the computer program to implement the steps in the memory management method provided in the embodiment of the present application.
[0011] An embodiment of the present application also provides a computer-readable storage medium storing a computer program. When the computer program is executed by a processor, the processor implements the steps in the memory management method provided in the embodiment of the present application.
[0012] An embodiment of the present application also provides a computer program product, including a computer program / instruction. When the computer program / instruction is executed by a processor, the processor implements the steps in the memory management method provided in the embodiment of the present application.
[0013] The embodiment of the present application provides a memory management method, which manages the memory pages in the first memory pool through a first memory pool management structure, provides a relatively independent and allocable memory space through the first memory pool, allows the application of memory space from the memory pages in the first memory pool, and divides the applied memory space into smaller-granularity memory blocks to create a second memory pool, and manages the memory blocks in the second memory pool through a second memory pool management structure, so that the memory space can be allocated with the memory blocks in the second memory pool as the granularity. Among them, the first memory pool and the second memory pool can not only realize centralized memory management, avoid frequent swapping out of memory pages in the first memory pool, and reduce system performance fluctuations, but also allocate memory space based on memory blocks, which can also reduce memory fragmentation, improve memory utilization, and enhance system performance. BRIEF DESCRIPTION OF THE DRAWINGS
[0014] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0015] Figure 1 A flowchart of a memory management method provided by an exemplary embodiment of the present application;
[0016] Figure 2 A schematic diagram of a process for creating a second memory management structure and a second memory pool provided by an exemplary embodiment of the present application;
[0017] Figure 3a A schematic diagram of a process for creating a dedicated management structure provided by an exemplary embodiment of the present application;
[0018] Figure 3b A schematic diagram of the internal structure of a physical machine provided in an embodiment of the present application;
[0019] Figure 3c-Figure 3e A schematic diagram of the internal structure of another physical machine provided in an embodiment of the present application;
[0020] Figure 4 A schematic structural diagram of a memory management device provided by an exemplary embodiment of the present application;
[0021] Figure 5 A schematic structural diagram of an electronic device provided as an exemplary embodiment of the present application. DETAILED DESCRIPTION
[0022] To make the purpose, technical solutions, and advantages of this application more clear, the technical solutions of this application will be clearly and completely described below in conjunction with the specific embodiments of this application and the corresponding drawings. Obviously, the embodiments described are only part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of this application.
[0023] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of the relevant countries and regions, and provide corresponding operation portals for users to choose to authorize or refuse. In addition, the various models involved in this application (including but not limited to language models or large models) are in compliance with relevant laws and standards.
[0024] Currently, memory management mechanisms provided in operating systems include, but are not limited to, a buddy system and SLAB.
[0025] The buddy system is an algorithm used in operating system kernels (such as Linux or Unix) to manage physical memory. The buddy system divides physical memory into a series of equally sized blocks (also called buddy blocks), each of which is a power of two (e.g., 4KB or 2MB). These blocks are organized by granularity into a linked list of buddy blocks. The primary goal of the buddy system is to minimize memory fragmentation. When an application requests a block of memory of a specific size, the operating system kernel searches the linked list of buddy blocks that most closely matches the requested size and allocates an appropriate buddy block from that list. If no block of the exact same size exists, the kernel selects a buddy block of a larger granularity and splits it into two smaller buddy blocks. One of these blocks is allocated to the application, while the other is added to the linked list of the smaller buddy blocks. This method of splitting and merging buddy blocks efficiently utilizes memory and reduces memory fragmentation. When an application releases allocated memory, the kernel attempts to merge it with adjacent buddy blocks to form a larger buddy block for subsequent allocation.
[0026] SLAB is a memory allocation mechanism used in the operating system kernel for small memory blocks (such as 32-byte, 64-byte, or 128-byte blocks). By pre-allocating and managing memory blocks, it provides efficient memory allocation and deallocation operations. SLAB can reduce memory fragmentation, improve memory utilization, and provide a mechanism for object cache reuse to optimize system performance. SLAB is an efficient memory allocator designed to provide fast memory allocation and deallocation operations and reduce memory fragmentation. The core concept of SLAB is to provide an efficient mechanism for kernel object allocation and deallocation by pre-allocating and managing memory blocks. In SLAB, the kernel pre-allocates a certain number of memory blocks, which are organized into one or more linked lists to store kernel objects of a specific size. When an application needs to allocate a kernel object, SLAB selects a free memory block from the linked list of memory blocks of the corresponding granularity and allocates it to the application. When the application releases the allocated memory, SLAB returns the memory block to the free list for subsequent allocation.
[0027] Taking the two aforementioned memory management mechanisms as an example, existing memory management mechanisms use memory paging technology for memory allocation and recycling, which can reduce memory fragmentation. However, these memory management mechanisms still have the problem of irrational memory allocation, which can easily lead to frequent memory swapping. In other words, it takes a lot of time to swap data out and into memory, causing system performance fluctuations. Still using the two aforementioned memory management mechanisms as an example, we analyze the reasons why existing memory management mechanisms cause frequent memory swapping: the current memory management mechanism is mainly aimed at reducing memory fragmentation. When allocating memory, it does not consider the data type and data usage requirements. This may cause data to be stored in various places in the memory space. This scattered storage of data may have an adverse impact on memory swapping, causing frequent memory swapping and causing performance fluctuations. For example, the metadata in some application scenarios needs to be fixed in memory and is not suitable for memory swapping. However, if these metadata are stored in different physical pages in a dispersed manner, when counting the usage frequency of the physical pages, the usage frequency of these metadata may be low, resulting in a low usage frequency of the entire physical page, which in turn causes the physical page to be selected as a candidate page. Because these physical pages contain the above-mentioned metadata that is not suitable for memory swapping, conflicts arise with the page swapping logic, affecting the selection of candidate pages for memory swapping. If the candidate page is swapped out, it will be swapped in again due to the use of metadata, causing the physical page to be constantly swapped out and swapped in, causing system performance fluctuations.
[0028] To this end, the embodiments of the present application provide a new memory management method. This memory management method is an independent memory management mechanism, different from the memory management mechanism provided by the OS. It is mainly used to implement centralized management of memory allocation and recycling. It is suitable for various application scenarios that require centralized management of memory allocation and recycling, especially some data scenarios (such as metadata) that are not suitable for memory exchange, and scenarios that require independent memory isolation domains. These application scenarios can be application scenarios in traditional OSs or application scenarios in the follow mode of lightweight virtualization scenarios.
[0029] Specifically, for these application scenarios, the memory pages in the first memory pool are managed through the first memory pool management structure, and a relatively independent and allocable memory space is provided through the first memory pool, allowing the application of memory space from the memory pages in the first memory pool, and dividing the applied memory space into smaller-granularity memory blocks to create a second memory pool, and the memory blocks in the second memory pool are managed through the second memory pool management structure, so that the memory space can be allocated with the memory blocks in the second memory pool as the granularity. Among them, the first memory pool and the second memory pool can not only realize centralized memory management, but also the first memory pool serves as the basis for applying for memory space in the second memory pool, and the memory pages included therein can be less frequently swapped out, which is conducive to reducing system performance fluctuations; in addition, allocating memory space based on the memory blocks in the second memory pool can also reduce memory fragmentation, improve memory utilization, and enhance system performance.
[0030] The following describes in detail the technical solutions provided by various embodiments of the present application in conjunction with the accompanying drawings.
[0031] Figure 1 This is a flow chart of a memory management method provided by an exemplary embodiment of the present application. Figure 1 As shown, the method includes:
[0032] 101. Manage a first memory pool using a first memory pool management structure, where the first memory pool includes at least one memory page requested from an operating system to provide allocatable memory space.
[0033] 102. Manage a second memory pool using a second memory pool management structure, where the second memory pool includes memory blocks obtained by partitioning a target memory space, where the target memory space is allocated from memory pages in the first memory pool.
[0034] 103. Based on the second memory pool management structure, allocate memory space with memory blocks in the second memory pool as granularity, so that data allocated to the memory space is distributed in memory pages included in the first memory pool.
[0035] In this embodiment, the memory management method is an important part of managing memory resources in a computer system. Memory pools are a commonly used optimization technique used to improve the efficiency of memory allocation and access. Memory pools pre-allocate a certain amount of memory space and manage and allocate it according to a specific strategy. During initialization, memory pools pre-allocate a certain amount of memory space, reducing the number of memory requests and releases from the operating system, reducing the overhead of memory allocation and release, and improving program performance.
[0036] In this embodiment, a first memory pool can be created. For the sake of distinction and description, the memory pool that applies for and releases memory resources from the operating system is referred to as the first memory pool. The first memory pool includes at least one memory page applied for from the operating system to provide allocable and relatively independent memory space. Optionally, at least one memory page can be applied for from a partner system or SLAB provided by the operating system to constitute a first memory pool that is independently managed in an embodiment of the present application. There is no limit on the number of memory pages included in the first memory pool, which can be one or more, depending on the application scenario; in addition, the size of the memory page in the first memory pool (or page granularity) can be 4KB, 2MB or 1GB, etc., which is also not limited.
[0037] For the first memory pool, a first memory pool management structure is created, and the first memory pool is managed using the first memory pool management structure. The management here includes the application, allocation, recycling and release of memory pages in the first memory pool. The first memory pool management structure is a structure used to manage at least one memory page in the first memory pool. For example, the first memory pool management structure can record the address information of at least one memory page, record which memory pages are in an idle state and which memory pages are in use (i.e., have been allocated), or split the memory pages and manage the split sub-pages.
[0038] In this embodiment, a second memory pool can be created. For ease of distinction and description, the memory pool that applies for and releases memory resources from the first memory pool is referred to as the second memory pool. The second memory pool applies for memory space from the memory pages in the first memory pool. For ease of distinction and description, the memory space applied for by the second memory pool from the memory pages in the first memory pool is referred to as the target memory space. In the second memory pool, memory resources are managed at a relatively small granularity. Therefore, after applying for the target memory space, the target memory space can be divided into memory blocks, and the granularity of the memory blocks is smaller than the granularity of the memory pages. Among them, the target memory space can include one or more complete memory pages, or it can include incomplete memory pages (i.e., part of the space in a memory page is allocated to the second memory pool). Of course, the target memory space can include only complete memory pages, only incomplete memory pages, or both complete and incomplete memory pages. For example, if the memory page size is 2MB and the target memory space size is 6MB, then the target memory space can be provided by 3 complete memory pages, or it can also be provided by 2 complete memory pages and 2 incomplete memory pages, each providing 1MB.
[0039] In an embodiment of the present application, the granularity of the memory blocks divided from the target memory space and the number of memory block granularities are not limited. For example, the granularity of the memory blocks divided may include but is not limited to at least one of 32 bytes, 64 bytes, 128 bytes, 4KB, and 2MB. The granularity of the memory blocks in the second memory pool is less than or equal to the granularity of the memory pages in the first memory pool. For example, the granularity of the memory pages in the first memory pool is 2MB, and the granularity of the memory blocks in the second memory pool may be 32 bytes, 64 bytes, ..., 4KB, or 2MB.
[0040] A second memory pool management structure is created for the second memory pool and is used to manage the second memory pool. Management here includes the application, allocation, recycling, and release of memory blocks in the second memory pool. The second memory pool management structure is a structure used to manage memory blocks in the second memory pool. For example, the second memory pool management structure can divide memory pages into memory blocks of different sizes, record which memory blocks are idle, and record which memory blocks are in use (i.e., allocated).
[0041] Furthermore, based on the second memory pool management structure, various management such as allocation, recycling and release of memory space can be performed with the memory blocks in the second memory pool as the granularity. Among them, when the memory space is allocated with the memory blocks in the second memory pool as the granularity, the data allocated to the memory space can be distributed in the memory pages included in the first memory pool, and will not be distributed to other memory pages outside the memory pages included in the first memory pool. On the one hand, centralized management of data can be achieved. On the other hand, the first memory pool serves as the basis for applying for memory from the second memory pool, and the memory pages in the first memory pool are not easily swapped out. In this case, the performance fluctuation problem caused by memory swapping can be solved.
[0042] In an embodiment of the present application, the first memory pool and the second memory pool provide a memory application interface for a memory application object so that the memory application object can apply for memory space. The memory application object can be an application, driver, process, thread or other object that needs to apply for memory, etc., which is managed using the memory management method provided in the embodiment of the present application. These memory application objects either apply for memory space from the first memory pool or apply for memory space from the second memory pool. The first memory pool and the second memory pool have their own memory application objects, and the memory application objects of the two can be the same, partially the same or different, and this is not limited. The memory application objects of the first memory pool include at least the second memory pool, and the second memory pool can apply for target memory space from the first memory pool through the memory application interface provided by the first memory pool. In this way, when a memory application object applies for memory space from the second memory pool, the second memory pool can allocate memory space for the memory application object.
[0043] Of course, for a memory application object, you can apply for the required memory space from the first memory pool through the memory application interface provided by the first memory pool, or you can apply for the required memory space from the second memory pool through the memory application interface provided by the second memory pool. The specific memory pool to apply for memory from can be determined according to the size of the required memory space; if the memory application object requires a larger memory space, you can apply for the required memory space from the first memory pool through the memory application interface provided by the first memory pool; if the memory application object requires a smaller memory space, you can apply for the required memory space from the second memory pool through the memory application interface provided by the second memory pool.
[0044] Among them, if a memory application object requires a smaller amount of memory, a memory application can be initiated to the second memory pool through the memory application interface provided by the second memory pool; for the second memory pool, the second memory pool management structure can be queried according to the memory size requested to be allocated by the memory application object to determine the memory block that can be allocated to the memory application object, the determined memory block is allocated to the memory application object, and the information related to the memory block in the second memory pool management structure is updated, for example, the status of the memory block is updated from idle to in use. For example, in an operating system, there are some data objects (also known as kernel objects) that occupy a small amount of memory and frequently request and release memory, such as task structure (task_struct) objects, structure page (struct page) objects, structure file (struct file) objects, and socket (socket) objects. These data objects occupy a small amount of memory, and the memory blocks in the second memory pool are sufficient to be allocated to the above-mentioned data objects. When an application initiates a memory request from the second memory pool because it needs to use a certain data object, a memory block in the second memory pool that is adapted to the size of the data object can be allocated to the application, so that the application can store the data object in the allocated memory block and use the data object stored in the memory block. When the application releases the data object, the memory block used to store the data object will be recycled to the second memory pool.
[0045] The first and second memory pools provide memory release interfaces for memory application objects, allowing them to release memory space. A memory application object can call the memory release interface to release memory space. Memory space is released to the memory pool from which it was applied.
[0046] In an embodiment of the present application, for some scenarios that require centralized memory allocation and recycling, the memory pages in the first memory pool are managed through the first memory pool management structure, allowing application for memory space from the memory pages in the first memory pool, and dividing the applied memory space into smaller-granularity memory blocks to create a second memory pool, and the memory blocks in the second memory pool are managed through the second memory pool management structure, so that memory space can be allocated with memory blocks as the granularity. Among them, a relatively independent and allocable memory space is provided by the first memory pool, and the first memory pool serves as the basis for applying for memory space in the second memory pool. The memory pages included therein can be swapped out less frequently, which is conducive to reducing system performance fluctuations. Furthermore, the allocation, recycling and release of memory space are managed mainly based on the memory blocks in the second memory pool, which can reduce memory fragmentation, improve memory utilization and enhance system performance.
[0047] It is noted that the creation of the first memory pool management structure and the second memory pool management structure can be done once in the initial stage and then used directly each time memory is requested. In the embodiments of the present application, the implementation and creation methods of the first memory pool management structure and the second memory pool management structure are not limited. The implementation and creation methods of the first memory pool management structure and the second memory pool management structure are described below.
[0048] In this embodiment, the implementation method of the first memory pool management structure is not limited. The first memory pool management structure includes a first data structure for recording at least one memory page and a second data structure for recording the usage of at least one memory page. The first data structure may include but is not limited to: an array, a linked list, a tree or a bitmap, etc., and the second data structure may include but is not limited to: an array, a linked list, a tree or a bitmap, etc. The first data structure and the second data structure may be the same or different. In an optional embodiment, the first memory pool management structure is implemented by combining an array and a bitmap, which has a simple structure and saves memory space. The first memory pool management structure includes: an array for recording at least one memory page and a bitmap for recording the usage of at least one memory page.
[0049] The implementation method for creating the first memory pool management structure (denoted by mpool) is not limited. Optionally, a method for creating the first memory pool management structure includes: obtaining a first initial management structure (denoted as mpool-t), the first initial management structure including a first data structure (such as an array) and a second data structure (such as a bitmap); requesting at least one memory page from the operating system based on mpool-t, recording the first address of the at least one memory page into the first data structure (such as an array) of mpool-t, and initializing the second data structure (such as a bitmap) in mpool-t, specifically setting the usage status record information corresponding to each data page in the second data structure to a first value to indicate that each memory page is in an unused state (i.e., a free page); then, selecting a target memory page from the at least one requested memory page, and copying the first initial management structure to the target memory page to obtain the first memory pool management structure, i.e., mpool. It is noted that when the target memory page is used to store the first memory pool management structure, in order to ensure that there are still allocatable memory pages in the first memory pool, the number of memory pages requested from the operating system is preferably two or more. The target memory page may be any memory page in the first memory pool that is in an idle state.
[0050] Alternatively, another method for creating a first memory pool management structure includes: obtaining a first initial management structure (denoted as mpool-t), the first initial management structure including a first data structure (e.g., an array) and a second data structure (e.g., a bitmap); requesting at least one memory page from the operating system based on mpool-t, recording the first address of the at least one memory page into the first data structure (e.g., an array) of mpool-t, and initializing the second data structure (e.g., a bitmap) in mpool-t, specifically setting the usage status record information corresponding to each data page in the second data structure to a first value to indicate that each memory page is in an unused state (i.e., a free page); thereafter, when the second memory pool is created, a memory block can be allocated from the second memory pool, denoted as the first memory block, and the first initial management structure can be copied into the first memory block to obtain the first memory pool management structure. It is noted that when allocating the first memory block, a memory block of a size compatible with the first initial management structure can be selected as the first memory block, for example, a memory block of the same size as or slightly larger than the first initial management structure.
[0051] It should be noted that the first initial management structure is a temporary management structure corresponding to the first memory pool management structure and is a temporary local variable. After the contents of the first initial management structure are copied to the target memory page or first memory block (i.e., after the first memory pool management structure is obtained), the first initial management structure can be discarded. The first initial management structure can be predefined or temporarily created, without limitation.
[0052] Correspondingly, the implementation method of managing the first memory pool using the first memory pool management structure includes: when the second memory pool applies for the target memory space from the first memory pool, the usage status record information corresponding to the target memory space in the second data structure (such as a bitmap) is set to a second value, and the second value indicates that the memory page to which the target memory space belongs is in use. In the embodiment of the present application, the implementation method of the usage status record information is not limited, and can be determined according to the implementation method of the second data structure. For example, when the second data structure is implemented as an array, the usage status record information can be an element in the array, and the value of the element indicates whether the corresponding memory page is in an idle state or in use; for another example, when the second data structure is implemented as a bitmap, the usage status record information can be a bit in the bitmap, and the value of the bit indicates whether the corresponding memory page is in an idle state or in use. Among them, the first value and the second value are different. If the first value is 0, the second value can be 1; if the first value is 1, the second value can be 0.
[0053] In an optional embodiment, the memory page has a first page granularity. In order to facilitate maintenance and management, the memory page can be divided into sub-pages of smaller granularity, for example, the memory page can be divided into sub-pages of second page granularity. The first memory pool can allocate, recycle and release memory resources at the sub-page granularity. The second page granularity is smaller than the first page granularity. The sizes of the first page granularity and the second page granularity are not limited. For example, the first page granularity is 2MB and the second page granularity is 4KB. For another example, the first page granularity is 1GB and the second page granularity is 2MB or 4KB. In the case where the second data structure is implemented using a bitmap, the usage status record information corresponding to each memory page in the second data structure can be implemented as a bit corresponding to each sub-page, that is, the second data structure includes bits corresponding to each sub-page, wherein the sub-pages belonging to the same memory page correspond to continuous bits, and the corresponding bits between different memory pages are also continuous. For example, the first page granularity is 2MB, and the first memory pool includes multiple memory pages with 2MB granularity. Each memory page with 2MB granularity is divided into 512 sub-pages with 4KB granularity. The bitmap includes bits corresponding to each sub-page with 4KB granularity, as well as the serial number of each sub-page with 4KB granularity. In the bitmap, the serial numbers corresponding to the 512 sub-pages with 4KB granularity divided from the first memory page with 2MB granularity are 0-511, the serial numbers corresponding to the 512 sub-pages with 4KB granularity divided from the second memory page with 2MB granularity are 512 to 1023, and so on.
[0054] In addition, in addition to the first data structure (such as an array) and the second data structure (such as a bitmap), the first memory pool management structure may also include: a lock for managing at least one memory page in the array and various counting information for statistical memory page usage.
[0055] Further optionally, the implementation method of the second memory pool applying for the target memory space from the first memory pool is not limited, and a specific implementation method is provided below. In the case where the second memory pool applies for the target memory space, the search method varies depending on the size of the target memory space.
[0056] If the target memory space requires a memory page to be provided (for example, taking the memory page size as 2MB, the target memory space is 2M), then according to the usage of each memory page recorded in the bitmap, search for a free memory page in the first memory pool, allocate the free memory page as the target memory space to the second memory pool, and set the bit position corresponding to the memory page in the bitmap to the second value.
[0057] If the target memory space requires multiple memory pages to be provided (for example, taking the memory page size as 2MB and the target memory space is 6MB), then based on the usage of each memory page recorded in the bitmap, multiple free and continuous memory pages are searched in the first memory pool, and the multiple free and continuous memory pages are allocated as the target memory space to the second memory pool, and the bit positions corresponding to the multiple memory pages in the bitmap are set to the second value.
[0058] If the target memory space needs to be provided by at least one continuous sub-page in the same memory page (for example, taking the memory page size as 2MB and the target memory space as 1M), then according to the usage of each memory page recorded in the bitmap, search in the first memory pool for at least one continuous sub-page that is adapted to the target memory space size and is in an idle state (i.e., not in use). Adapting to the target memory space size means that the size of at least one continuous sub-page is the same as the target memory space size, or the difference between the size of at least one continuous sub-page and the target memory space size is less than a set threshold value, and the set threshold value is not limited and can be 4KB or 8KB, etc. Record the at least one continuous sub-page in the second memory pool management structure to allocate the at least one continuous sub-page as the target memory space to the second memory pool, and set the bit position corresponding to the at least one continuous sub-page in the bitmap to the second value.
[0059] In addition, the target memory space may also span memory pages, ie, at least one continuous subpage belongs to different memory pages. In this case, a search may also be performed on the bitmap, which will not be described in detail. Preferably, the target memory space includes at least one continuous subpage in the same memory page.
[0060] The method of searching the first memory pool for at least one free continuous subpage that matches the target memory space size based on the usage of each memory page recorded in the bitmap is not limited. The entire bitmap may be traversed to find at least one free continuous subpage that matches the target memory space size.
[0061] The following uses the example of searching for at least one consecutive subpage in a 2MB memory page as an example. An exemplary search method includes the following steps:
[0062] a) First, determine the start (start) and end (end) numbers of the search range. The start and end numbers are the start and end numbers of the 2MB memory page in the bitmap, respectively. For example, the start and end numbers can be 0 and 511, or 512 and 1023. Initialize the current offset (offset) position of the search to the start value. That is, the offset increases from the start number to the end number. Initialize the number of continuous sub-pages (free) to 0, and the starting position (last) of the current continuous free sub-page to the value of start. The minimum difference min_diff between the number of continuous sub-pages free and the size of the target memory space is initialized to 0, and the starting address min_idx of the continuous free sub-page corresponding to min_diff is initialized to 0.
[0063] b) According to the current offset, query the next position dix (ie, the sequence number of the next free subpage) in the bitmap with the first value (eg, 0).
[0064] c) Check whether the sequence number idx is equal to end. If not, the search for the 2MB memory page is not complete, so continue to the next step; otherwise, jump to step i.
[0065] d) Determine whether the position idx is the same as the current offset. If they are, it indicates that the search for a continuous free subpage is not complete. Free increases, offset also increases, and the process goes back to step b to continue determining whether the next subpage is free.
[0066] e) If the position idx is different from the current offset, the current continuous free subpage has ended and the next new free subpage has been found. Determine whether the size of the current continuous free subpage is equal to the target memory size (num). If so, return the current continuous free subpage as the target memory size.
[0067] f) If the size of the current contiguous free subpages is larger than the target memory size, to reduce contiguous memory fragmentation, the minimum contiguous memory that satisfies num is selected, from which num subpages are selected. The difference between the current number of contiguous free subpages, free, and num, is calculated, and this difference, diff, is compared with min_diff.
[0068] g) If min_diff has not recorded data, or the difference diff is less than the previously recorded min_diff, the current continuous free subpage is used as the candidate target memory space, and min_diff and min_dix are set to the current difference diff and the starting position (last) of the current continuous free subpage respectively.
[0069] h) Reset the mark of a new free memory segment and reset the number of consecutive free subpages free to 0. Set the starting position last of the current consecutive free subpages to the position idx of the next new free subpage.
[0070] i) At this point, the bitmap traversal of the entire 2MB memory page is complete. It is also possible that the last segment of continuous free subpages has not been compared with the previous candidate target memory space. In this case, steps f and g can be executed again.
[0071] j) If the search is successful, the starting position min_idx of the most suitable candidate target memory space is returned; otherwise, the search fails and an error is returned.
[0072] k) For the above search within a 2MB memory page, if the target memory space is not found within the 2MB memory page, the other 2MB memory pages in the bitmap may be traversed in sequence.
[0073] l) After confirming that the return value is a valid serial number, the return value serial number idx in the bitmap and the bit position within the num length can be set to 1, indicating that the sub-page is allocated.
[0074] In the embodiment of the present application, there is no limitation on the method of creating the second memory management structure and the second memory pool. The following example provides a method of creating the second memory management structure and the second memory pool. Figure 2 As shown, the method includes:
[0075] 201. Obtain a second initial management structure, the second initial management structure including at least one local management structure corresponding to a memory block granularity, and copy at least one pre-created kernel management structure corresponding to a memory block granularity into the corresponding local management structure.
[0076] 202. Based on the second initial management structure, apply for a first memory space from the first memory pool, and divide the first memory space into memory blocks according to at least one memory block granularity to obtain a second memory pool, wherein the first memory space belongs to a target memory space applied for by the second memory pool from the first memory pool.
[0077] 203. Copy the second initial management structure to the second memory block in the second memory pool to obtain a second memory pool management structure, and update the management object of the second memory pool from the second initial management structure to the second memory pool management structure.
[0078] In step 201, a kernel management structure is pre-created for each memory block granularity. The granularity of each memory block may include, but is not limited to, 32 bytes, 64 bytes, 128 bytes, and 4KB, depending on the size of the memory page. Developers can pre-implement the kernel management structure in the operating system kernel through the interface provided by the operating system kernel. The kernel management structure is a structure located within the operating system kernel for managing memory pools. In contrast, the local management structure has the same functions as the kernel management structure, differing in that the kernel management structure is located within the operating system kernel, while the local management structure is located within the local environment. The local environment, relative to the operating system kernel, is an application environment that uses the memory management method provided in the embodiments of this application for centralized memory management. For example, the local environment can be an application scenario requiring an independent kernel memory isolation domain under a traditional OS architecture, i.e., a scenario where memory isolation is performed on a component and the operating system is not required to participate in its memory allocation and management. The component can be a driver within the operating system or an application or process running on the operating system. Alternatively, the local environment can be the runtime environment of a lightweight virtualization scenario, referred to below as the virtualization mode. The lightweight virtualization scenario of this embodiment refers to the virtualization mode in which the application scenario in which the host operating system can be switched between non-virtualization mode and virtualization mode by the virtualization layer is switched to. The virtualization layer is located between the hardware resources and the host operating system, and is responsible for switching the host operating system from non-virtualization mode to virtualization mode. The memory management method of the embodiment of this application can be applied in the virtualization mode to allocate memory and reclaim memory for other functional modules running in the virtualization mode.
[0079] In addition to pre-creating a kernel management structure for each memory block granularity, a second initial management structure can also be obtained, and the second initial management structure includes at least one local management structure corresponding to the memory block granularity. Similar to the first initial management structure, the second initial management structure is a temporary management structure corresponding to the second memory pool management structure. It is a temporary local variable. After the content of the second initial management structure is copied to the second memory block (that is, after obtaining the second memory pool management structure), the second initial management structure can be discarded. Among them, the second initial management structure can be pre-defined or temporarily created, and there is no limitation on this. For example, if at least one memory block granularity includes 32 bytes, 64 bytes, and 128 bytes, etc., the second initial management structure includes a local management structure corresponding to 32 bytes, a local management structure corresponding to 64 bytes, and a local management structure corresponding to 128 bytes, etc.
[0080] It is explained here that the local management structures corresponding to the various memory block granularities included in the second initial management structure are empty, while the kernel management structure created in advance for each memory block granularity contains the content required to manage the memory blocks. In order to allocate, recycle and release memory blocks in the local environment, the kernel management structure created in advance for each memory block granularity can be copied to the corresponding local management structure respectively. At this time, the local management structure has the ability to allocate memory blocks.
[0081] In step 202, based on the second initial management structure, a request is made from the first memory pool for the first memory space. The first memory space is divided into memory blocks of different sizes based on at least one memory block granularity, with each memory block corresponding to a memory block granularity. The detailed implementation process of requesting the first memory space from the first memory pool based on the second initial management structure is the same or similar to the process of requesting the target memory space from the first memory pool in the above embodiment, and therefore will not be further described.
[0082] For step 203, when the second memory pool is obtained, the second initial management structure is copied to the second memory block in the second memory pool to obtain the second memory pool management structure. Like the second initial management structure, the second memory pool management structure includes: local management structures corresponding to the granularity of each memory block. Optionally, before copying the second initial management structure to the second memory block in the second memory pool, a memory block with a memory block granularity that is compatible with the second initial management structure can be selected from the second memory pool as the second memory block based on the size of the second initial management structure. For example, based on the size of the second initial management structure, a memory block with a memory block granularity that is the same as the size of the second initial management structure is preferentially selected from the second memory pool as the second memory block; if there is no memory block with the same size as the second initial management structure, a candidate memory block with a memory block granularity greater than the size of the second initial management structure is selected from the second memory pool, and the smallest memory block is selected from the candidate memory blocks as the second memory block.
[0083] Among them, the second memory pool is applied for by the second initial management structure from the first memory pool, and the management object of the second memory pool defaults to the second initial management structure. After the second initial management structure is copied to the second memory block to obtain the second memory pool management structure, the management object of the second memory pool also needs to be updated from the second initial management structure to the second memory pool management structure, so as to facilitate the subsequent allocation, recycling or release of memory blocks in the second memory pool through the second memory pool management structure.
[0084] In an embodiment of the present application, the implementation method of copying the kernel management structure corresponding to at least one pre-created memory block granularity into the corresponding local management structure in step 201 is not limited and may depend on the implementation structure of the local management structure. A specific implementation method is provided below: In this implementation method, the local management structure includes: a local information (local) field, an associated information field, and a kernel information (back) field. Based on this, copying the kernel management structure corresponding to at least one pre-created memory block granularity into the corresponding local management structure includes: recording the identification information of the kernel management structure corresponding to any memory block granularity into the kernel information field of the corresponding local management structure; copying the content of the kernel management structure corresponding to any memory block granularity into the local information field of the corresponding local management structure; and copying the associated structure information of the kernel management structure corresponding to any memory block granularity into the associated information field of the corresponding local management structure. The associated structure information of the kernel management structure includes but is not limited to: a next-level node (node) structure. The associated structure information plays a role when destroying the local management structure, and is used to simultaneously destroy the local management structure and the associated structure. Similarly, during the destruction process, in addition to cleaning up the local management structure, the kernel management structure can also be found based on the kernel information field and destroyed.
[0085] In an optional embodiment, Direct Memory Access (DMA) is a system that allows certain hardware subsystems to directly access the system's main memory without having to transfer data through the CPU. In computer technology, DMA operations can be used for data transfer between memory and devices, particularly in peripheral devices such as hard drives, network interfaces, and graphics cards. In some cases, memory blocks can be allocated for DMA operations. Therefore, the second memory pool can include memory blocks corresponding to DMA operations, can also include memory blocks accessed by the CPU, or can simultaneously include memory blocks corresponding to DMA operations and memory blocks accessed by the CPU.
[0086] Based on the above, in step 201, the kernel management structure corresponding to each memory block granularity includes a kernel management structure with a set flag corresponding to the memory block granularity and / or a kernel management structure without a set flag corresponding to the memory block granularity; accordingly, the local management structure corresponding to each memory block granularity includes: a local management structure for direct memory access corresponding to the memory block granularity and a local management structure for non-direct memory access corresponding to the memory block granularity. Based on this, the kernel management structure corresponding to at least one pre-created memory block granularity is copied to the corresponding local management structure, including: copying the kernel management structure with a set flag corresponding to at least one memory block granularity to the local management structure for direct memory access corresponding to at least one memory block granularity; and / or copying the kernel management structure without a set flag corresponding to at least one memory block granularity to the local management structure for non-direct memory access corresponding to at least one memory block granularity. The set flag can be a SLAB cache direct memory access (SLAB_CHACHE_DMA) flag.
[0087] In an alternative embodiment, Figure 2 Each memory block granularity can be implemented as a universal memory block granularity. The local management structures corresponding to each memory block granularity in the second memory pool management structure include: local universal management structures corresponding to each universal memory block granularity. The universal memory block granularity can be a pre-set memory block granularity of a certain size, generally a power of 2. For most memory blocks, this universal memory block size is sufficient. In practical applications, a universal memory block that is closest to a power of 2 can be allocated to the application or process requesting the memory block. In practical applications, the universal memory block allocated to an application or process may be larger than the actual memory space required. If this situation occurs frequently, it will result in a certain amount of memory waste. Therefore, in addition to the universal memory blocks corresponding to the universal memory block granularity, the second memory pool also includes dedicated memory blocks. Dedicated memory blocks are memory blocks with non-universal memory block granularity, i.e., memory block granularity that is not a power of 2, but rather 30 bytes, 48 bytes, or 60 bytes, etc. Among them, the dedicated memory block is determined according to the application requirements.
[0088] like Figure 3a As shown, the method provided in the embodiment of the present application also includes:
[0089] 301. Apply for a third memory block adapted to the local management structure granularity from a second memory pool according to the local management structure granularity.
[0090] 302. Copy the kernel management structure corresponding to the pre-created dedicated memory block into the third memory block to obtain a local dedicated management structure;
[0091] 303. Apply for a second memory space from the first memory pool based on the local dedicated management structure, and divide the second memory space into dedicated memory blocks, where the second memory space belongs to the target memory space.
[0092] 304. Add the local dedicated management structure to the linked list for managing the local dedicated management structure in the second memory pool management structure.
[0093] Among them, for the dedicated memory block, it is also necessary to build a local management structure to manage it. In the embodiment of the present application, since the second memory pool has been created, it is possible to directly apply for a third memory block adapted to the local management structure granularity from the second memory pool according to the local management structure granularity to store the local management structure for managing the dedicated memory block. In order to distinguish it from the local management structure for managing the general memory block (i.e., the local general management structure), in the embodiment of the present application, the local management structure for managing the dedicated memory block is referred to as the local dedicated management structure. Further, in order to save memory space and avoid waste, a general memory block that is larger than the local management structure granularity and closest to the local management structure granularity can be selected as the third memory block. For example, assuming that the local management structure granularity is 48 bytes, the general memory block granularity greater than 48 bytes includes: 64 bytes and 128 bytes, etc. In order to save memory space, a 64-byte general memory block is selected as the third memory block.
[0094] The kernel management structure corresponding to the pre-created dedicated memory block is copied to the third memory block to obtain a local dedicated management structure. The local dedicated management structure and the local general management structure have the same structure header, and the difference is that the memory blocks they manage are different. Creating the local dedicated management structure directly based on the granularity of the local management structure can simplify the process of creating the local dedicated management structure. In this embodiment, a kernel management structure is created in advance for each dedicated memory block, wherein each dedicated memory block can be a memory block of 30 bytes, 40 bytes, 60 bytes, etc. The kernel management structure corresponding to the dedicated memory block contains the content required to manage the dedicated memory block. In order to manage the allocation, recycling, and release of the dedicated memory block in the local environment, the kernel management structure created in advance for the dedicated memory block can be copied to the third memory block to obtain a local dedicated management structure. The local dedicated management structure has the ability to manage the dedicated memory block.
[0095] In this embodiment, if a dedicated memory block needs to be allocated based on the local dedicated management structure, the first memory pool can be requested for free subpages or free memory pages for initial allocation to obtain the dedicated memory block. For subsequent allocations, if a pre-split dedicated memory block exists, it is allocated directly; otherwise, the local dedicated management structure requests free memory pages or free subpages from the first memory pool for further segmentation.
[0096] In an embodiment of the present application, the first memory pool management structure can be located in the first memory pool or the second memory pool. To ensure the security of the first memory pool management structure and to prevent it from being swapped out, the first memory pool management structure can be copied to the first memory block in the second memory pool. In this way, the management structures involved in both the first and second memory pools are located in the second memory pool. While ensuring that the first memory pool is not swapped out, frequent memory swapping is avoided, thereby improving system performance.
[0097] In an optional embodiment, when initializing metadata for some special functions, a large memory block can be allocated for the metadata. For example, a large memory block can be a memory block larger than 2MB. The large memory block can be allocated using the virtual memory allocation (vmalloc) function. After the allocation is complete, the large memory block can be added to the first memory pool to ensure that it will not be swapped out during subsequent memory swaps.
[0098] Based on this, a specific memory page is requested from the operating system in advance, and the page granularity of the specific memory page is larger than the page granularity of at least one memory page. For example, the specific memory page refers to a memory page with a larger page granularity. For example, the page granularity of the specific memory page can be but not limited to 1GB, and can also be 2GB or even larger; in order to facilitate the relatively independent and centralized management of memory, the specific memory page can also be added to the first memory pool, and the first memory pool management structure is used to manage the specific memory page.
[0099] In an optional embodiment, based on the second memory pool management structure, memory space is recycled with memory blocks in the second memory pool as the granularity. Alternatively, based on the second memory pool management structure, memory space is released with memory blocks in the second memory pool as the granularity. Recycling memory space with memory blocks in the second memory pool as the granularity means recycling allocated memory blocks to the second memory pool; releasing memory space with memory blocks in the second memory pool as the granularity means releasing subpages belonging to free memory blocks in the second memory pool to the first memory pool.
[0100] For example, when an application releases a memory block, the released memory block can be provided to the second memory pool management structure for recycling, specifically, provided to the local management structure corresponding to the granularity of the memory block for recycling. As the number of memory blocks recycled by the second memory pool management structure increases, there may be a free subpage. If the number of free subpages exceeds a set first quantity threshold, these free subpages can be released back to the first memory pool. For example, the first quantity threshold can be 3, 10, or 50, etc.
[0101] Optionally, after the free sub-page is released to the first memory pool again, the local dedicated management structure or the local general management structure is no longer used, and the local dedicated management structure or the local general management structure may be destroyed.
[0102] The following is a method for releasing memory pages back to the first memory pool:
[0103] a) Based on the first address (vaddr) and length (num) of the released memory page, calculate the first address (vaddr2) of the 2M aligned memory page to which the released memory page belongs. vaddr2 is the first address of the 2M memory page.
[0104] b) Based on vaddr2, determine the sequence number of the 2M memory page in the first bitmap.
[0105] c) Based on the difference between vaddr2 and vaddr and the sequence number of vaddr2 in the first bitmap, calculate the sequence number dix of vaddr in the first bitmap.
[0106] d) Clear the released memory page to zero, that is, set the values corresponding to the sequence number from idx to idx to num in the first bit map to the first value (e.g., 0).
[0107] In an optional embodiment, based on the first memory pool management structure, memory space is allocated, reclaimed, or released for objects other than the second memory pool at the granularity of memory pages in the first memory pool. Allocating memory space at the granularity of memory pages in the first memory pool refers to allocating memory pages to the memory application object that applied for the memory pages; reclaiming memory space at the granularity of memory pages in the first memory pool refers to reclaiming memory pages allocated to the memory application object; and releasing memory space at the granularity of memory pages in the first memory pool refers to returning free pages in the first memory pool to the operating system.
[0108] The following is an example of creating a second memory pool and a second memory management structure.
[0109] 1. First, define a second memory pool management structure (abbreviated as cpool) to manage the local management structures corresponding to the granularity of each locally created memory block. cpool includes a cache array and a direct memory access cache (dma_caches) array. Different cache arrays correspond to different general memory block granularity, and different dma_caches arrays also correspond to different general memory block granularity. The difference is that one is used to allocate ordinary memory blocks (memory blocks accessed by the CPU) and the other is used to allocate DMA-related memory blocks. Furthermore, cpool also contains a linked list for managing local dedicated management structures.
[0110] 2. The caches array is used to establish local general management structures corresponding to general memory blocks with granularity ranging from 32 bytes to 2MB. The size of each general memory block is a power of 2, for example, 32 bytes, 64 bytes, 128 bytes, and so on. Each caches array creates a local general management structure with the granularity of a general memory block. One caches array corresponds to the structure of a second memory pool management structure item (cpool_item). The cpool_item structure contains a local information (local) field, a next-level node (node) field, and a kernel information (back) field. Among them, the local field is a local memory cache (kmem_cache) structure, the node field is the memory cache node (kmem_cache_node) structure of the next-level node corresponding to the local field, and the back field is a corresponding memory cache (kmem_cache) structure in the associated kernel.
[0111] 3. When creating the second memory pool, first create a local temporary memory pool management structure (cpool_t). This cpool_t is a static variable used to assist in the creation of the second memory pool and the second memory pool management structure. This cpool_t can also be called the second initial management structure, and the structure of cpool_t is the same as the cpool defined above.
[0112] 4. Get the local management structure t corresponding to the memory block of the common memory block granularity from cpool_t, that is, the cpool_item structure mentioned above. For each common memory block granularity, it is named after the common memory block granularity. The memory block granularity corresponds to a pre-created kernel management structure s. The address of the kernel management structure s is recorded in the back field of the local management structure t.
[0113] 5. Copy the contents of kernel management structure s into the local field of local management structure t, and copy the next section structure node of kernel management structure s into the node field of local management structure t. Simultaneously, initialize the linked lists and lock structures involved in local and node. The node field is used to store the next-level node structure information associated with kernel management structure s. This next-level node structure information is merely an example of other structure information associated with kernel management structure s and is not limited thereto.
[0114] 6. Similarly, similar flags will be applied to the local memory management structure of the DMA of the dedicated memory block. The difference is that when the kernel management structure s is created, it can be marked with SLAB_CHACHE_DMA.
[0115] 7. After the cpool_item structures for each memory block size are built, cpool_t is able to allocate memory blocks. At this point, the temporary cpool_t can be replaced with the official second memory pool management structure (cpool). To do this, based on the size of the cpool, a memory block with a cpool_item structure that is closest to the size of the cpool and that meets this size can be allocated to the official cpool.
[0116] 8. During the first allocation, since cpool_t has not yet been associated with a formal memory page, a certain number of subpages (pages) can be allocated from the above mpool according to the size of cpool_t through the cache growth (cache_grow) path.
[0117] 9. After obtaining the allocated subpage, the allocated subpage can be split into multiple memory blocks according to the SLAB algorithm, for example, general memory blocks of various common sizes, and a memory block is selected from them to store the cpool.
[0118] 10. Copy the local management structure and other structures recorded in cpool_t to the memory block used to store cpool to obtain cpool; at the same time, initialize the lock variables involved in the cpool structure.
[0119] 11. Update the association between the sub-pages (page) and management objects (management structures) allocated by the cache_grow path. The SLAB cache (slab_cache) field in the page structure of each sub-page can record the management structure that manages it. It was originally temporarily managed by cpool_t, and its associated local management structure was also in cpool_t. After obtaining the formal management structure cpool, the management structure of the sub-page can be updated to the local management structure in the formal cpool.
[0120] In the above embodiment, the various objects involved in the memory management method (such as the first memory pool, the first memory pool management structure, the second memory pool, the second memory pool management structure, etc.) and the implementation process of the entire method are introduced and explained in detail, and the application scenarios of the memory management method are introduced and explained. In order to facilitate a clearer understanding of the memory management method of the embodiment of the present application, the following description is made in conjunction with the deployment and implementation of this solution in the physical machine architecture. Among them, according to the different application scenarios of this solution, the deployment and implementation of this solution in the physical machine architecture will also be different.
[0121] In an optional embodiment, this solution is applied to application scenarios that require independent kernel memory isolation domains under traditional OS architecture. Figure 3b As shown, the physical machine under the traditional OS architecture includes, from bottom to top: hardware resources, an operating system running on the hardware resources, and applications running on the operating system. Optionally, the hardware resources include: a central processing unit (CPU), a graphics processing unit (GPU), a network card and a physical storage medium, and the physical storage medium includes memory, disk, etc. In addition, the hardware resources on the physical machine may also include: IO devices, communication components, displays, power components, audio components and other components, which are not shown in the figure. In the embodiments of the present application, there is no limitation on the operating system and application programs.
[0122] Further, if Figure 3b As shown, this solution runs in OS, specifically corresponding to Figure 3b The centralized memory management module in the embodiment of the present application is responsible for executing the memory management method provided by the embodiment of the present application, wherein the centralized memory management module is different from the traditional kernel management module in the OS, and is responsible for providing relatively independent memory isolation domains for memory application objects such as some applications, drivers or processes in the operating system in addition to the memory management provided by the traditional kernel management module, that is, memory allocation, recycling and release can be performed for these applications, drivers or processes in the first memory pool and / or the second memory pool without being affected by the traditional kernel management module. Figure 3b In the figure, an application program in the application layer applies for memory from the first memory pool and / or the second memory pool as an example.
[0123] Among them, the centralized memory management module can apply for at least one memory page from the traditional memory management module, and create a first memory pool and a first memory pool management structure based on the applied memory page, and use the first memory pool management structure to manage the first memory pool; and create a second memory pool management structure and a second memory pool, and use the second memory pool management structure to manage the second memory pool; and based on the second memory pool management structure, allocate memory space with the memory block in the second memory pool as the granularity; and / or based on the first memory pool management structure, allocate memory space with the memory page in the first memory pool or the sub-page divided by the memory page as the granularity.
[0124] Specifically, for applications, drivers or processes, when a relatively independent memory isolation domain is required, it can be determined to apply for memory from the first memory pool or the second memory pool based on the required memory size, and initiate a memory application to the centralized memory management module through the memory application interface provided by the first memory pool or the second memory pool for the memory application object; when the centralized management module receives the memory application, it allocates a free memory page or memory block from the first memory pool or the second memory pool based on the first memory pool management structure or the second memory pool management structure, and updates the corresponding state of the memory page or memory block in the first memory pool management structure or the second memory pool management structure to be in use. Among them, applications, drivers or processes can apply for one or more memory blocks, or one or more memory pages.
[0125] Furthermore, when an application, driver or process does not need memory resources, memory can be released through the memory release interface provided by the first memory pool structure or the second memory pool structure. For the release-related description, please refer to the above embodiment and will not be repeated here.
[0126] In another optional embodiment, this solution is applied to a lightweight virtualization scenario. The lightweight virtualization scenario in the embodiment of this application is introduced as follows:
[0127] Figure 3c This is a structural diagram of another physical machine provided by an exemplary embodiment of the present application. Figure 3c As shown, the physical machine includes: hardware resources 10 and a host operating system 20 running on the hardware resources, and a virtualization layer 30 is implemented between the hardware resources 10 and the host operating system 20. Furthermore, an application layer 40 is also included above the host operating system 20. The application layer 40 includes various application programs. For an introduction to hardware resources, please refer to Figure 3b In the illustrated embodiment, no limitation is imposed on the implementation of the host operating system 20 and the application program.
[0128] In this embodiment, the host operating system 20 operates in both a non-virtualized mode and a virtualized mode. In the non-virtualized mode, the host operating system 20 has direct access to the hardware resources 10. In the virtualized mode, the virtualization layer 30 virtualizes the hardware resources 10 and schedules and accesses the virtualized resources on behalf of the host operating system 20. In the virtualized mode, the virtualization layer 30 has limited access to the hardware resources 10. Accordingly, the host operating system 20 in the virtualized mode can also be referred to as a client operating system.
[0129] In this embodiment, a lightweight, powerful, and efficient virtualization layer 30 is inserted between the hardware resources 10 and the host operating system 20. The virtualization layer 30 can be pre-developed and inserted between the hardware resources 10 and the host operating system 20. The method for inserting the virtualization layer 30 is not limited. For example, the virtualization layer 30 can be inserted after the host operating system 20 is installed on the physical machine and the host operating system 20 is in normal operation.
[0130] Among them, the virtualization layer 30 has the ability to provide virtual resources, and can at least provide virtual resources corresponding to the resources that the host operating system 20 cannot provide in the non-virtualization mode due to the unactivated or missing system functions. That is to say, the resources that the host operating system 20 cannot provide in the non-virtualization mode due to the unactivated or missing system functions can be provided with corresponding virtual resources by the virtualization layer 30, so as to run the corresponding applications. Specifically, the virtualization layer 30 can switch the running host operating system 20 from the non-virtualization mode to the virtualization mode, and provide at least one virtual resource in the virtualization mode, and run at least one application on the at least one virtual resource. Without reinstalling the operating system, the virtual resource provision function can be flexibly implemented for the running host operating system, solving the problem that the host operating system 20 cannot provide corresponding resources to run the corresponding applications in the non-virtualization mode due to the unactivated or missing system functions. It can support the operation of more applications and is conducive to expanding the capabilities of the host operating system. Furthermore, for the sake of compatibility with the host operating system, in order to reduce the frequent mode switching of the host operating system, in the virtualization mode, not only can corresponding virtual resources be provided for the system functions that are not enabled or missing in the non-virtualization mode of the host operating system 20, but corresponding virtual resources can also be provided for the system functions that are enabled in the non-virtualization mode of the host operating system 20, thereby supporting more functions of the host operating system 20.
[0131] For example, at least one target function implemented in the virtualization layer 30 includes but is not limited to: memory management function, file management function, virtualization function, scheduling optimization function, etc. The memory management function, file management function, scheduling optimization function, etc. here can be a newly expanded function that is not originally supported by the host operating system, or a function that is originally supported by the host operating system but is not enabled during the installation of the host operating system. Regardless of whether it is a newly expanded function or an unenabled function, the memory management function specifically includes but is not limited to: memory page fault management, memory swap management, memory mapping management, etc.; the file management function specifically includes but is not limited to: directory management, permission management, file backup, creation, query, deletion, modification, etc.; the virtualization function includes but is not limited to: virtualization of various elastic resources (such as CPU, GPU, memory, network, etc.); the scheduling and tuning function includes but is not limited to: scheduling of physical computing resource objects and scheduling of various virtualized resources, etc.
[0132] further, Figure 3c The figure shows the overall architecture of the physical machine. Figure 3d and Figure 3e The internal architecture of a physical machine operating in non-virtualized mode and virtualized mode is shown. Before the mode switch is performed, the host operating system operates in non-virtualized mode. In this non-virtualized mode, Figure 3d As shown, the internal architecture of the running physical machine is hardware resources 10, host operating system 20 and application layer 40 from bottom to top; after the mode is switched, it works in virtualization mode. In this virtualization mode, Figure 3e As shown, the architecture of the running physical machine is hardware resources 10, virtualization layer 30, client operating system and application layer 40 from bottom to top. Among them, the client operating system is the name of the host operating system 20 running in virtualization mode.
[0133] In an embodiment of the present application, two operating modes are provided for a host operating system in a running state, namely a non-virtualization mode and a virtualization mode. A virtualization layer is inserted between the hardware resources and the host operating system, and the virtualization layer implements at least the virtualization resource provision function. The virtualization layer switches the running host operating system from a non-virtualization mode to a virtualization mode, provides virtual resources in the virtualization mode, and runs at least applications on the virtual resources that the host operating system cannot run in the non-virtualization mode due to insufficient resources. Without reinstalling the host operating system, the virtual resource provision function can be flexibly implemented for the running host operating system, supporting the operation of more applications and expanding the capabilities of the host operating system.
[0134] It is explained here that, from the perspective of program code, the implementation codes such as "the function of providing virtual resources" and "switching from non-virtualization mode to virtualization mode" involved in the embodiments of the present application belong to the virtualization layer 30, and these codes are located under the host operating system; however, from the perspective of the running state, before the mode switching is actually completed, at least part of the program code in the virtualization layer 30 is executed during the operation of the host operating system. In other words, at least part of the program code in the virtualization layer 30 is executed in the non-virtualization mode, wherein the program code executed in the non-virtualization mode in the virtualization layer 30 at least includes: program code for switching from non-virtualization mode to virtualization mode, such as the upper half of the switching function mentioned in the following embodiments. Accordingly, after the mode switching is actually completed, part of the program code will be executed in the virtualization mode, such as the lower half of the switching function mentioned in the following embodiments. In addition, the program code responsible for providing virtual resources in the virtualization layer 30 also runs in non-virtualization mode, but this part of the code is run after the switch is completed; the virtualization layer 30 also provides some interface codes to the outside world, some of which will run in virtualization mode, and some need to be switched to non-virtualization mode to run, depending on the function of the interface code.
[0135] When this solution is applied to lightweight virtualization scenarios, such as Figure 3d As shown, the virtualization layer 30 includes a centralized memory management module responsible for executing the memory management method of this embodiment, as well as other functional modules required by the virtualization layer 30. The centralized memory management module is used to provide centralized memory allocation, recycling, or release management for other functional modules in the virtualization layer in the virtualization mode.
[0136] Specifically, in non-virtualization mode, the various kernel management structures involved in the above-mentioned embodiments can be implemented in the host operating system kernel by calling the kernel interface provided by the host operating system; then, when switching to virtualization mode, the integrated memory management module is run to: apply for at least one memory page from the traditional memory management module located in the client operating system, create a first memory pool and a first memory pool management structure based on the applied memory page, and use the first memory pool management structure to manage the first memory pool; and create a second memory pool management structure and a second memory pool, and use the second memory pool management structure to manage the second memory pool; then, based on the second memory pool management structure, memory space is allocated with the memory block in the second memory pool as the granularity; and / or, based on the first memory pool management structure, memory space is allocated with the memory page in the first memory pool or the sub-page divided by the memory page as the granularity.
[0137] Specifically, for other functional modules in the virtualization layer, it can determine to apply for memory from the first memory pool or the second memory pool based on the required memory size, and initiate a memory application to the centralized memory management module through the memory application interface provided by the first memory pool or the second memory pool for the memory application object; when the centralized management module receives the memory application, it allocates an idle memory page or memory block from the first memory pool or the second memory pool based on the first memory pool management structure or the second memory pool management structure, and updates the corresponding status of the memory page or memory block in the first memory pool management structure or the second memory pool management structure to be in use. Among them, other functional modules in the virtualization layer can apply for one or more memory blocks, or one or more memory pages.
[0138] Furthermore, when an application, driver or process does not need memory resources, memory can be released through the memory release interface provided by the first memory pool structure or the second memory pool structure. For the release-related description, please refer to the above embodiment and will not be repeated here.
[0139] The detailed implementation and beneficial effects of each step in the method of this embodiment have been described in detail in the aforementioned embodiments and will not be elaborated here.
[0140] It should be noted that in some of the processes described in the above embodiments and the accompanying drawings, multiple operations that appear in a specific order are included, but it should be clearly understood that these operations may not be executed in the order in which they appear in this article or may be executed in parallel. The sequence numbers of the operations, such as 301, 302, etc., are only used to distinguish between different operations, and the sequence numbers themselves do not represent any execution order. In addition, these processes may include more or fewer operations, and these operations may be executed in sequence or in parallel. It should be noted that the descriptions of "first", "second", etc. in this article are used to distinguish different messages, devices, modules, etc., and do not represent a sequential order, nor do they limit "first" and "second" to being different types.
[0141] Figure 4 This is a schematic diagram of the structure of a memory management device provided by an exemplary embodiment of the present application. Figure 4 As shown, the device includes: a creation module 41, a management module 42 and an allocation module 43.
[0142] A management module 42 is used to manage a first memory pool using a first memory pool management structure, where the first memory pool includes at least one memory page requested from an operating system to provide allocatable memory space; and to manage a second memory pool using a second memory pool management structure, where the second memory pool includes memory blocks obtained by dividing a target memory space, where the target memory space is requested from the memory pages in the first memory pool.
[0143] The allocation module 43 is configured to allocate memory space based on the second memory pool management structure and with memory blocks in the second memory pool as granularity, so that data allocated to the memory space is distributed in memory pages included in the first memory pool.
[0144] In an alternative embodiment, if Figure 4 As shown, the device further includes: a creation module 41, which is used to create a first memory pool management structure, and is also used to create a second memory pool management structure and a second memory pool.
[0145] In an optional embodiment, when creating the first memory pool management structure, the creation module 41 is specifically used to: obtain a first initial management structure, which includes a first data structure and a second data structure; apply for at least one memory page from the operating system to form the first memory pool; record the first address of the at least one memory page in the first data structure, and initialize the usage status record information corresponding to each memory page in the second data structure to a first value to indicate that each memory page is in an idle state; copy the first initial management structure to a target memory page or a first memory block to obtain the first memory pool management structure; wherein the target memory page is a memory page in the first memory pool, and the first memory block is a memory block in the second memory pool.
[0146] Accordingly, when the management module uses the first memory pool management structure to manage the first memory pool, it includes: when the second memory pool applies for the target memory space from the first memory pool, setting the usage status record information corresponding to the target memory space in the second data structure to a second value to indicate that the memory page belonging to the target memory space is in use.
[0147] Optionally, the memory page has a first page granularity, and the memory page is divided into sub-pages of a second page granularity, and the second page granularity is smaller than the first page granularity; the usage status record information corresponding to each memory page in the second data structure includes bits corresponding to each sub-page, and the sub-pages belonging to the same memory page correspond to continuous bits; for any bit, if the value is the first value, it indicates that the corresponding sub-page is in an idle state, and if the value is the second value, it indicates that the corresponding sub-page is in use.
[0148] Further optionally, the device also includes: a search module and a recording module; the search module is used to search in the first memory pool for at least one continuous sub-page that is adapted to the size of the target memory space and is in an idle state according to the usage of each memory page recorded in the second data structure when the second memory pool applies for the target memory space; the recording module is used to record the at least one continuous sub-page into the second memory pool management structure to allocate the at least one continuous sub-page as the target memory space to the second memory pool, and set the bit position corresponding to the at least one continuous sub-page in the second data structure to the second value.
[0149] In an optional embodiment, when creating the second memory pool management structure and the second memory pool, the creation module is specifically used to: obtain a second initial management structure, the second initial management structure includes at least one local management structure corresponding to a memory block granularity, and copy the pre-created kernel management structure corresponding to the at least one memory block granularity to the corresponding local management structure; based on the second initial management structure, apply for a first memory space from the first memory pool, and divide the first memory space into memory blocks according to the at least one memory block granularity to obtain the second memory pool, and the first memory space belongs to the target memory space; copy the second initial management structure to the second memory block in the second memory pool to obtain the second memory pool management structure, and update the management object of the second memory pool from the second initial management structure to the second memory pool management structure.
[0150] Further optionally, the local management structure includes: a local information field, an associated information field and a kernel information field; when the creation module copies the kernel management structure corresponding to the at least one pre-created memory block granularity into the corresponding local management structure, it includes: recording the identification information of the kernel management structure corresponding to any memory block granularity into the kernel information field in the corresponding local management structure; copying the content in the kernel management structure corresponding to any memory block granularity into the local information field in the corresponding local management structure; copying the associated structure information of the kernel management structure corresponding to any memory block granularity into the associated information field in the corresponding local management structure.
[0151] In an optional embodiment, when the creation module copies the pre-created kernel management structure corresponding to the at least one memory block granularity to the corresponding local management structure, it includes: copying the kernel management structure with a set mark corresponding to the at least one memory block granularity to the local management structure for direct memory access corresponding to the at least one memory block granularity; and / or copying the kernel management structure without a set mark corresponding to the at least one memory block granularity to the local management structure for non-direct memory access corresponding to the at least one memory block granularity.
[0152] In an optional embodiment, at least one memory block granularity includes at least one universal memory block granularity, and the local management structure corresponding to the at least one memory block granularity includes the local universal management structure corresponding to the at least one universal memory block granularity. The device also includes: a determination module, an application module, a segmentation module, and an addition module. The determination module is used to apply for a third memory block adapted to the local management structure granularity from the second memory pool according to the local management structure granularity; the creation module is also used to copy the kernel management structure corresponding to the pre-created dedicated memory block to the third memory block to obtain a local dedicated management structure; the application module is used to apply for a second memory space from the first memory pool based on the local dedicated management structure; the segmentation module is used to segment the second memory space to obtain dedicated memory blocks, and the second memory space belongs to the target memory space; the addition module is used to add the local dedicated management structure to the linked list in the second memory pool management structure for managing local dedicated management structures.
[0153] In an optional embodiment, the application module is also used to: apply for a specific memory page from the operating system in advance, and the page granularity of the specific memory page is larger than the page granularity of at least one memory page; the adding module is also used to: add the specific memory page to the first memory pool, and use the first memory pool management structure to manage the specific memory page.
[0154] In an optional embodiment, the device further includes a processing module. The processing module is used to reclaim and / or release memory space based on the second memory pool management structure and at the granularity of memory blocks in the second memory pool; wherein, reclaiming memory space based on the granularity of memory blocks in the second memory pool means recycling allocated memory blocks to the second memory pool; releasing memory space based on the granularity of memory blocks in the second memory pool means releasing subpages belonging to free memory blocks in the second memory pool to the first memory pool. The processing module is also used to allocate, reclaim or release memory space for other objects other than the second memory pool based on the first memory pool management structure and at the granularity of the memory pages in the first memory pool.
[0155] The effects and detailed implementation of the above device embodiments can be found in the aforementioned embodiments and will not be described in detail here.
[0156] Figure 5 A schematic diagram of the structure of an electronic device provided by an exemplary embodiment of the present application is shown in FIG. Figure 5 As shown, the device includes: a memory 54 and a processor 55.
[0157] The memory 54 is used to store computer programs and can be configured to store various other data to support operations on the electronic device. Examples of such data include instructions for any application or method operating on the electronic device.
[0158] The memory 54 may be implemented by any type of volatile or non-volatile memory device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk, or optical disk.
[0159] The processor 55 is coupled to the memory 54 and is used to execute a computer program in the memory 54, so as to: manage the first memory pool by using a first memory pool management structure, wherein the first memory pool includes at least one memory page requested from an operating system to provide allocatable memory space; manage the second memory pool by using a second memory pool management structure, wherein the second memory pool includes memory blocks obtained by dividing a target memory space, wherein the target memory space is requested from the memory pages in the first memory pool; and allocate memory space based on the second memory pool management structure, with the memory blocks in the second memory pool as the granularity, so that the data allocated to the memory space is distributed in the memory pages included in the first memory pool.
[0160] In an optional embodiment, the processor 55 is also used to: obtain a first initial management structure, the first initial management structure including a first data structure and a second data structure; apply for at least one memory page from the operating system to form the first memory pool; record the first address of the at least one memory page in the first data structure, and initialize the usage status record information corresponding to each memory page in the second data structure to a first value to indicate that each memory page is in an idle state; copy the first initial management structure to a target memory page or a first memory block to obtain the first memory pool management structure; wherein the target memory page is a memory page in the first memory pool, and the first memory block is a memory block in the second memory pool.
[0161] Optionally, when the processor uses the first memory pool management structure to manage the first memory pool, it includes: when the second memory pool applies for the target memory space from the first memory pool, setting the usage status record information corresponding to the target memory space in the second data structure to a second value to indicate that the memory page belonging to the target memory space is in use.
[0162] Further optionally, the memory page has a first page granularity, and the memory page is divided into sub-pages of a second page granularity, and the second page granularity is smaller than the first page granularity; the usage status record information corresponding to each memory page in the second data structure includes bits corresponding to each sub-page, and the sub-pages belonging to the same memory page correspond to continuous bits; for any bit, if the value is the first value, it indicates that the corresponding sub-page is in an idle state, and if the value is the second value, it indicates that the corresponding sub-page is in use.
[0163] Further optionally, the processor 55 is also used to: when the second memory pool applies for target memory space, search in the first memory pool for at least one continuous sub-page that is adapted to the size of the target memory space and is in a space state according to the usage of each memory page recorded in the second data structure; record the at least one continuous sub-page in the second memory pool management structure to allocate the at least one continuous sub-page as the target memory space to the second memory pool, and set the bit position corresponding to the at least one continuous sub-page in the second data structure to the second value.
[0164] In an optional embodiment, the processor 55 is also used to create a second memory management structure and a second memory pool. When creating the second memory management structure and the second memory pool, the processor 55 is specifically used to: obtain a second initial management structure, the second initial management structure includes at least one local management structure corresponding to a memory block granularity, and copy the pre-created kernel management structure corresponding to the at least one memory block granularity to the corresponding local management structure; based on the second initial management structure, apply for a first memory space from the first memory pool, and divide the first memory space into memory blocks according to the at least one memory block granularity to obtain the second memory pool, the first memory space belongs to the target memory space; copy the second initial management structure to the second memory block in the second memory pool to obtain the second memory pool management structure, and update the management object of the second memory pool from the second initial management structure to the second memory pool management structure.
[0165] Optionally, the local management structure includes: a local information field, an associated information field and a kernel information field; when the processor 55 copies the kernel management structure corresponding to the at least one pre-created memory block granularity into the corresponding local management structure, it is specifically used to: record the identification information of the kernel management structure corresponding to any memory block granularity into the kernel information field in the corresponding local management structure; copy the content in the kernel management structure corresponding to any memory block granularity into the local information field in the corresponding local management structure; copy the associated structure information of the kernel management structure corresponding to any memory block granularity into the associated information field in the corresponding local management structure.
[0166] In an optional embodiment, when the processor 55 copies the kernel management structure corresponding to the at least one pre-created memory block granularity into the corresponding local management structure, it is specifically used to: copy the kernel management structure with a set mark corresponding to the at least one memory block granularity to the local management structure for direct memory access corresponding to the at least one memory block granularity; and / or copy the kernel management structure without a set mark corresponding to the at least one memory block granularity to the local management structure for non-direct memory access corresponding to the at least one memory block granularity.
[0167] In an optional embodiment, at least one memory block granularity includes at least one universal memory block granularity, and the local management structure corresponding to the at least one memory block granularity includes a local universal management structure corresponding to the at least one universal memory block granularity. The processor 55 is also used to: apply for a third memory block adapted to the local management structure granularity from the second memory pool according to the local management structure granularity; copy the kernel management structure corresponding to the pre-created dedicated memory block to the third memory block to obtain a local dedicated management structure; based on the local dedicated management structure, apply for a second memory space from the first memory pool, and divide the second memory space to obtain dedicated memory blocks, the second memory space belonging to the target memory space; and add the local dedicated management structure to the linked list in the second memory pool management structure for managing local dedicated management structures.
[0168] In an optional embodiment, the processor 55 is further used to: pre-apply for a specific memory page from the operating system, the page granularity of the specific memory page being greater than the page granularity of at least one memory page; add the specific memory page to the first memory pool, and use the first memory pool management structure to manage the specific memory page.
[0169] In an optional embodiment, the processor 55 is further configured to: based on the second memory pool management structure, reclaim and / or release memory space at the granularity of memory blocks in the second memory pool; wherein reclaiming refers to reclaiming allocated memory blocks back to the second memory pool; and releasing refers to releasing subpages belonging to free memory blocks in the second memory pool to the first memory pool. The processor 55 is further configured to: based on the first memory pool management structure, allocate, reclaim, or release memory space for objects other than the second memory pool at the granularity of memory pages in the first memory pool.
[0170] The detailed introduction and effect description of the electronic device can be found in the aforementioned embodiments, which will not be repeated here.
[0171] Further, if Figure 5 As shown, the electronic device also includes: a communication component 56, a display 57, a power component 58, an audio component 59 and other components. Figure 5 Only some components are shown schematically, which does not mean that the electronic device only includes Figure 5 In addition, Figure 5 The components in the dotted box are optional components, not mandatory components, and the specific components may depend on the product form of the electronic device. The electronic device of this embodiment can be implemented as a terminal device such as a desktop computer, a laptop computer, a smart phone or an IOT device, or a server device such as a conventional server, a cloud server or a server array. If the electronic device of this embodiment is implemented as a terminal device such as a desktop computer, a laptop computer, a smart phone, etc., it may include Figure 5 If the electronic device of this embodiment is implemented as a conventional server, cloud server or server array and other server-side devices, it may not include Figure 5 Components within the dotted box.
[0172] Accordingly, an embodiment of the present application further provides a computer-readable storage medium storing a computer program, which, when executed, can implement the steps that can be executed by the processor in the above method embodiment.
[0173] Accordingly, an embodiment of the present application also provides a computer program product, including a computer program / instruction. When the computer program / instruction is executed by a processor, the processor is caused to implement the steps that can be executed by the processor in the above method embodiment.
[0174] The above-mentioned memory can be implemented by any type of volatile or non-volatile memory device or a combination thereof, such as static random-access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk.
[0175] The above-mentioned communication component is configured to facilitate wired or wireless communication between the device where the communication component is located and other devices. The device where the communication component is located can access a wireless network based on a communication standard, such as WiFi, 2G, 3G, 4G / LTE, 5G and other mobile communication networks, or a combination thereof. In an exemplary embodiment, the communication component receives a broadcast signal or broadcast-related information from an external broadcast management system via a broadcast channel. In an exemplary embodiment, the communication component also includes a near field communication (NFC) module to facilitate short-range communication. For example, the NFC module can be implemented based on radio frequency identification (RFID) technology, infrared data association (IrDA) technology, ultra wide band (UWB) technology, Bluetooth (BT) technology and other technologies.
[0176] The above-mentioned display includes a screen, which may include a liquid crystal display (LCD) and a touch panel (TP). If the screen includes a touch panel, the screen may be implemented as a touch screen to receive input signals from the user. The touch panel includes one or more touch sensors to sense touch, slide, and gestures on the touch panel. The touch sensor can not only sense the boundary of the touch or slide action, but also detect the duration and pressure associated with the touch or slide operation.
[0177] The power supply assembly provides power to various components of the device in which the power supply assembly is located. The power supply assembly may include a power management system, one or more power supplies, and other components associated with generating, managing, and distributing power to the device in which the power supply assembly is located.
[0178] The above-mentioned audio component can be configured to output and / or input audio signals. For example, the audio component includes a microphone (MIC), and when the device where the audio component is located is in an operating mode, such as call mode, recording mode, and voice recognition mode, the microphone is configured to receive external audio signals. The received audio signal can be further stored in a memory or sent via a communication component. In some embodiments, the audio component also includes a speaker for outputting audio signals.
[0179] Those skilled in the art will appreciate that the embodiments of the present application can be provided as methods, systems, or computer program products. Therefore, the present application can adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application can adopt the form of a computer program product implemented on one or more computer-readable storage media (including but not limited to magnetic disk storage, compact disc read-only memory (CD-ROM), optical storage, etc.) that contain computer-usable program code.
[0180] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the steps in the process. Figure 1 a process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0181] These computer program instructions may also be stored in a computer readable memory that can direct a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 a process or multiple processes and / or boxes Figure 1 The function specified in one or more boxes.
[0182] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operational steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing the instructions executed on the computer or other programmable device for implementing the process. Figure 1 a process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.
[0183] In a typical configuration, a computing device includes one or more processors (Central Processing Unit, CPU), input / output interfaces, network interfaces, and memory.
[0184] Memory may include non-permanent storage in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. Memory is an example of a computer-readable medium.
[0185] Computer-readable media includes permanent and non-permanent, removable and non-removable media that can be used to store information using any method or technology. Information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase-change random access memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassettes, magnetic disk storage or other magnetic storage devices, or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer-readable media does not include transitory computer-readable media such as modulated data signals and carrier waves.
[0186] It should also be noted that the terms "comprises," "includes," or any other variations thereof are intended to encompass non-exclusive inclusion, such that a process, method, commodity, or apparatus that includes a series of elements includes not only those elements but also other elements not explicitly listed, or includes elements inherent to such process, method, commodity, or apparatus. In the absence of further limitations, an element defined by the phrase "comprises a ..." does not exclude the presence of other identical elements in the process, method, commodity, or apparatus that includes the element.
[0187] The above are merely embodiments of the present application and are not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principles of the present application should all be included within the scope of the claims of the present application.
Claims
1. A memory management method, characterized in that: include: Managing a first memory pool using a first memory pool management structure, wherein the first memory pool includes at least one memory page requested from an operating system to provide allocatable memory space; Managing a second memory pool using a second memory pool management structure, wherein the second memory pool includes memory blocks obtained by segmenting a target memory space, wherein the target memory space is requested from the memory page in the first memory pool; Based on the second memory pool management structure, memory space is allocated with the memory blocks in the second memory pool as granularity, so that data allocated to the memory space is distributed in the memory pages included in the first memory pool.
2. The method according to claim 1, characterized in that Also includes: Acquire a first initial management structure, where the first initial management structure includes a first data structure and a second data structure; Applying at least one memory page from the operating system to form the first memory pool; Recording the first address of the at least one memory page into the first data structure, and initializing the usage status record information corresponding to each memory page in the second data structure to a first value to indicate that each memory page is in an idle state; The first initial management structure is copied to a target memory page in the first memory pool or a first memory block in the second memory pool to obtain the first memory pool management structure.
3. The method according to claim 2, characterized in that Managing the first memory pool using the first memory pool management structure includes: When the second memory pool applies for the target memory space from the first memory pool, the usage status record information corresponding to the target memory space in the second data structure is set to a second value to indicate that the memory page belonging to the target memory space is in use.
4. The method according to claim 3, characterized in that The memory page has a first page granularity, and the memory page is divided into sub-pages of a second page granularity, the second page granularity being smaller than the first page granularity; The usage status record information corresponding to each memory page in the second data structure includes bits corresponding to each sub-page, and the sub-pages belonging to the same memory page correspond to continuous bits; for any bit, if the value is the first value, it indicates that the corresponding sub-page is in an idle state; if the value is the second value, it indicates that the corresponding sub-page is in use.
5. The method according to claim 4, characterized in that Also includes: When the second memory pool applies for a target memory space, searching the first memory pool for at least one free continuous subpage that matches the size of the target memory space according to usage of each memory page recorded in the second data structure; Record the at least one continuous sub-page into the second memory pool management structure to allocate the at least one continuous sub-page as the target memory space to the second memory pool, and set the bit position corresponding to the at least one continuous sub-page in the second data structure to the second value.
6. The method according to claim 1, characterized in that Also includes: Acquire a second initial management structure, the second initial management structure including at least one local management structure corresponding to a memory block granularity, and copy the pre-created kernel management structure corresponding to the at least one memory block granularity into the corresponding local management structure; Based on the second initial management structure, applying for a first memory space from the first memory pool, and dividing the first memory space into memory blocks according to the at least one memory block granularity to obtain the second memory pool, wherein the first memory space belongs to the target memory space; The second initial management structure is copied to the second memory block in the second memory pool to obtain the second memory pool management structure, and the management object of the second memory pool is updated from the second initial management structure to the second memory pool management structure.
7. The method according to claim 6, characterized in that The local management structure includes: local information field, associated information field and kernel information field; Copying the pre-created kernel management structure corresponding to the at least one memory block granularity into the corresponding local management structure respectively includes: Record the identification information of the kernel management structure corresponding to any memory block granularity into the kernel information field in the corresponding local management structure; Copy the contents of the kernel management structure corresponding to any memory block granularity to the local information field in the corresponding local management structure; The associated structure information of the kernel management structure corresponding to any memory block granularity is copied to the associated information field in the corresponding local management structure.
8. The method according to claim 6, characterized in that Copying the pre-created kernel management structure corresponding to the at least one memory block granularity into the corresponding local management structure respectively includes: Copying the kernel management structure with the set flag corresponding to the at least one memory block granularity to the local management structure for direct memory access corresponding to the at least one memory block granularity; and / or, The kernel management structure without a set flag corresponding to the at least one memory block granularity is copied to a local management structure for non-direct memory access corresponding to the at least one memory block granularity.
9. The method according to claim 6, characterized in that The at least one memory block granularity includes at least one universal memory block granularity, the local management structure corresponding to the at least one memory block granularity includes a local universal management structure corresponding to the at least one universal memory block granularity, and the method further includes: According to the granularity of the local management structure, applying for a third memory block adapted to the granularity of the local management structure from the second memory pool; Copying the kernel management structure corresponding to the pre-created dedicated memory block into the third memory block to obtain a local dedicated management structure; Based on the local dedicated management structure, applying for a second memory space from the first memory pool, and dividing the second memory space to obtain dedicated memory blocks, where the second memory space belongs to the target memory space; The local dedicated management structure is added to the linked list of the second memory pool management structure for managing local dedicated management structures.
10. The method according to any one of claims 1 to 9, characterized in that Also includes: Applying for a specific memory page from the operating system in advance, where the page granularity of the specific memory page is greater than the page granularity of the at least one memory page; The specific memory page is added to the first memory pool, and the specific memory page is managed by using the first memory pool management structure.
11. The method according to any one of claims 1 to 9, characterized in that Also includes at least one of the following operations: Reclaiming or releasing memory space based on the second memory pool management structure with the memory blocks in the second memory pool as granularity; Based on the first memory pool management structure, memory space is allocated, reclaimed or released for other objects except the second memory pool with the memory page in the first memory pool as the granularity.
12. A physical machine, characterized in that: The physical machine includes hardware resources and a host operating system running on the hardware resources, a virtualization layer is implemented between the hardware resources and the host operating system, and the virtualization layer includes a centralized memory management module; The centralized memory management module is configured to manage a first memory pool using a first memory pool management structure, wherein the first memory pool includes at least one memory page requested from the host operating system to provide allocatable memory space; A second memory pool is managed by a second memory pool management structure, wherein the second memory pool includes memory blocks obtained by dividing a target memory space, and the target memory space is requested from the memory pages in the first memory pool; based on the second memory pool management structure, memory space is allocated to other functional modules in the virtualization layer with the memory blocks in the second memory pool as the granularity, so that the data allocated to the memory space is distributed in the memory pages included in the first memory pool.
13. The physical machine according to claim 12, wherein: The centralized memory management module is further configured to: Acquire a second initial management structure, the second initial management structure including at least one local management structure corresponding to a memory block granularity, and copy the pre-created kernel management structure corresponding to the at least one memory block granularity into the corresponding local management structure; Based on the second initial management structure, applying for a first memory space from the first memory pool, and dividing the first memory space into memory blocks according to the at least one memory block granularity to obtain the second memory pool, wherein the first memory space belongs to the target memory space; The second initial management structure is copied to the second memory block in the second memory pool to obtain the second memory pool management structure, and the management object of the second memory pool is updated from the second initial management structure to the second memory pool management structure.
14. A physical machine, characterized in that: The physical machine includes hardware resources and a host operating system running on the hardware resources, wherein the operating system includes: a centralized memory management module and a traditional memory management module; The centralized memory management module is used to manage a first memory pool using a first memory pool management structure, wherein the first memory pool includes at least one memory page requested from the traditional memory management module to provide allocatable memory space; manage a second memory pool using a second memory pool management structure, wherein the second memory pool includes memory blocks obtained by dividing a target memory space, wherein the target memory space is requested from the memory pages in the first memory pool; and allocate memory space based on the second memory pool management structure with the memory blocks in the second memory pool as the granularity, so that the data allocated to the memory space is distributed in the memory pages included in the first memory pool.
15. An electronic device, characterized in that: include: memory and processor; The memory is used to store a computer program; the processor is coupled to the memory and is used to execute the computer program to implement the steps in the method according to any one of claims 1 to 11.
16. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the processor is caused to implement the steps of the method according to any one of claims 1 to 11.
17. A computer program product comprising a computer program / instructions, characterized in that When the computer program / instructions are executed by a processor, the processor is caused to implement the steps of the method according to any one of claims 1 to 11.
Citation Information
Patent Citations
Memory management method and device and computer storage medium
CN110245091A
Memory management method, device and equipment and machine readable storage medium
CN117743202A
Cited By
Operating system containerized kernel compatible method based on shadow structure mapping
CN121116502A