Memory management method, electronic equipment and storage medium

By reserving a continuous physical memory as the first memory area in the target memory domain of the Linux system, it is used to allocate an immovable memory page, and the problem of memory fragmentation and sorting failure is solved, and more efficient memory sorting and allocation is achieved.

CN119988002APending Publication Date: 2025-05-13ZEBRED NETWORK TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411999796.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-31
Publication Date
2025-05-13

AI Technical Summary

Technical Problem

After the Linux system runs for a long time, the memory gradually becomes fragmented, resulting in the memory allocator being unable to allocate large chunks of continuous memory. The memory defragmentation function cannot effectively organize the unmovable memory pages, resulting in the failure to allocate continuous physical memory.

Method used

By reserving a continuous piece of physical memory in the target memory field as the first memory area, it is used to allocate an immovable memory page; if allocation from the first memory area fails, the memory page is allocated from the second memory area, so that the immovable memory page is concentrated in the first memory area, and the other areas are mainly removable memory pages and free memory pages.

Benefits of technology

It effectively reduces memory fragmentation, improves the efficiency and success rate of memory defragmentation, and can create more continuous free physical memory during sorting.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119988002A_ABST
    Figure CN119988002A_ABST
Patent Text Reader

Abstract

The invention discloses a memory management method, electronic equipment and a storage medium, and is applied to the field of computers.The memory management method comprises the steps that a memory allocation request of a target object is obtained; if the memory allocation request is used for applying for an immovable memory page, allocating a first memory page to a target object from a first memory region of a target memory domain; wherein the first memory area is a section of continuous physical memory which is reserved in the target memory domain and is used for distributing the immovable memory page; if the memory allocation to the target object from the first memory region fails, allocating a second memory page to the target object from a second memory region, the second memory region being a memory region except the first memory region in the target memory region. According to the invention, memory fragmentation is reduced to a certain extent.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a memory management method, an electronic device, and a storage medium. Background Art

[0002] As the Linux system continues to run for a long time, after a large amount of memory allocation and release operations, the continuous free page frames of the Linux system's partner system will gradually become fewer, and the memory will gradually become fragmented, resulting in the memory allocator being unable to allocate large blocks of continuous memory.

[0003] The memory defragmentation function of the Linux kernel is to migrate movable memory pages to other free locations, organize continuous free physical memory, and reduce memory fragmentation. The memory defragmentation function of the Linux kernel is to migrate movable memory pages to other free locations, organize continuous free physical memory, but memory defragmentation can only migrate movable memory pages, and non-movable memory pages cannot be migrated. If there are many non-movable memory pages and they are too scattered, memory defragmentation will fail. After defragmentation, the memory is still severely fragmented, so that the allocation of continuous physical memory fails. Summary of the invention

[0004] The embodiments of this specification provide a memory management method, an electronic device, and a storage medium, which are used to reduce memory fragmentation to a certain extent.

[0005] In a first aspect of the present specification, a memory management method is provided, comprising: obtaining a memory allocation request for a target object; if the memory allocation request is for applying for a non-movable memory page, allocating a first memory page to the target object from a first memory area of ​​a target memory domain, wherein the first memory area is a section of continuous physical memory reserved in the target memory domain for allocating non-movable memory pages; if memory allocation from the first memory area to the target object fails, allocating a second memory page to the target object from a second memory area, wherein the second memory area is a memory area in the target memory domain other than the first memory area.

[0006] In combination with the first aspect, in some embodiments, allocating a first memory page from a first memory area in a target memory domain to the target object includes: determining a migration type of the memory page requested by the memory allocation request; if it is determined that the migration type of the memory page requested by the memory allocation request is an immovable memory page, allocating the first memory page from a first memory area in the target memory domain to the target object through a partner system.

[0007] In combination with the first aspect, in some embodiments, before allocating the first memory page to the target object from the first memory area in the target memory domain through the partner system, it also includes: if the first memory area does not exist in the target memory domain, reserving the first memory area in the target memory domain through a page stealing operation.

[0008] In combination with the first aspect, in some embodiments, reserving the first memory area in the target memory domain through the page stealing operation includes: starting scanning from the starting page frame of the target memory domain with page blocks as scanning units; after each scan, if the current scanned page block meets the preset characteristic conditions, migrating the current scanned page block to the first immovable class, and the first immovable class is a migration type reserved for allocating immovable memory pages; when the scan stop condition is met, ending the scan of the target memory domain to obtain the first memory area.

[0009] In combination with the first aspect, in some embodiments, the currently scanned page block satisfies the following preset characteristic conditions: the migration type of the currently scanned page block is a movable class; all memory pages in the currently scanned page block are in the partner system; and all memory pages in the currently scanned page block have no retention flag set.

[0010] In combination with the first aspect, in some embodiments, the partner system includes a multi-level free memory page array; wherein, each level of the free memory page array includes a linked list array, and each level of the linked list array includes N free linked lists corresponding one by one to N migration types, and the N free linked lists include a first free linked list corresponding to a first immovable class and N-1 second free linked lists corresponding to other N-1 migration types, and the first free linked list of each level is used to manage free memory pages of the same level size in the first memory area, and the second free linked list of each level is used to manage free memory pages of the same level size of the corresponding migration type in the second memory area, and N is an integer greater than 2.

[0011] In combination with the first aspect, in some embodiments, migrating the currently scanned page block to the first immovable class includes: in the partner system, changing the migration type of the currently scanned page block from the movable class to the first immovable class, and migrating the free memory pages in the currently scanned page block from the second free linked list of the movable class to the first free linked list of the same order.

[0012] In combination with the first aspect, in some embodiments, allocating the first memory page to the target object from the first memory area in the target memory domain through a partner system includes: determining the target level of the memory page requested by the memory allocation request; in the first memory area, allocating the first memory page to the target object according to the first free linked list of the target level and above.

[0013] In combination with the first aspect, in some embodiments, allocating a second memory page from the second memory area to the target object includes: determining the target level of the memory page requested by the memory allocation request; in the second memory area, allocating the second memory page to the target object according to the second free linked list of the second immovable class whose migration type is the target level and above.

[0014] In combination with the first aspect, in some embodiments, after allocating the first memory page from the first memory area of ​​the target memory domain to the target object, it also includes: if the first memory page is a zero-order memory page, releasing the first memory page to the PCP linked list when the first memory page is reclaimed, and after the number of memory pages in the PCP linked list reaches a first number threshold, releasing the memory pages in the PCP linked list to the partner system; if the first memory page is a memory page greater than the zero order, releasing the first memory page to the partner system when the first memory page is reclaimed.

[0015] In combination with the first aspect, in some embodiments, releasing the memory pages in the PCP linked list to the partner system includes: respectively obtaining the migration type of the page block where each memory page in the PCP linked list is located; for each memory page in the PCP linked list, if the migration type of the page block where the memory page is located is the first immovable class, adding the memory page to the first free linked list of the corresponding order.

[0016] In the second aspect of this specification, an electronic device is provided, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the memory management method described in any embodiment of the first aspect when executing the computer program.

[0017] In a third aspect of the present specification, a computer-readable storage medium is provided, on which a computer program is stored, and when the program is executed by a processor, the memory management method described in any embodiment of the first aspect is implemented.

[0018] One or more technical solutions provided by the embodiments of this specification have at least the following technical effects or advantages:

[0019] In the case where the memory allocation request of the target object is used to apply for a non-removable memory page from the target memory domain, the embodiment of this specification allocates the first memory page to the target object by giving priority to the first memory area of ​​the target memory domain, wherein the first memory area is a section of continuous physical memory reserved in the target memory domain for allocating non-removable memory pages; if the memory allocation from the first memory area to the target object fails, the second memory page is allocated to the target object from the second memory area outside the first memory area, so that the non-removable memory pages are concentrated in the reserved first memory area, and most of the memory areas in the target memory domain are mainly removable memory pages and free memory pages. Therefore, after allocating memory using the memory management method of the embodiment of this specification, it is beneficial to organize more continuous free physical memory when the target memory domain is fragmented, thereby reducing memory fragmentation to a certain extent. Moreover, the efficiency and success rate of memory defragmentation can be improved. BRIEF DESCRIPTION OF THE DRAWINGS

[0020] Various other advantages and benefits will become apparent to those of ordinary skill in the art by reading the detailed description of the preferred embodiments below. The accompanying drawings are only for the purpose of illustrating the preferred embodiments and are not to be considered as limiting the present specification. Also, the same reference symbols are used throughout the accompanying drawings to represent the same components. In the accompanying drawings:

[0021] Figure 1 A flowchart of a memory management method provided according to an embodiment of this specification is shown;

[0022] Figure 2 A schematic diagram showing a first memory area and a second memory area in a target memory domain is shown;

[0023] Figure 3 A schematic diagram of a buddy system according to a memory management method in the related art is shown;

[0024] Figure 4 A schematic diagram showing a partner system according to the memory management method provided by the present invention is shown;

[0025] Figure 5a A schematic diagram of defragmentation according to related technologies is shown;

[0026] Figure 5b A schematic diagram showing defragmentation according to the memory management method provided by the present invention is shown;

[0027] Figure 6 A schematic diagram of the structure of an electronic device in an embodiment of this specification is shown. DETAILED DESCRIPTION

[0028] In order to better understand the above technical scheme, the technical scheme of the embodiments of this specification is described in detail below through the accompanying drawings and specific embodiments. It should be understood that the embodiments of this specification and the specific features in the embodiments are detailed descriptions of the technical scheme of the embodiments of this specification, rather than limitations on the technical scheme of this specification. In the absence of conflict, the embodiments of this specification and the technical features in the embodiments can be combined with each other.

[0029] The embodiment of the present invention provides a memory management method, which is applied to an electronic device whose operating system is a Linux kernel. Figure 1 FIG. 1 is a flowchart of a memory management method provided in an embodiment of this specification. Figure 1 As shown, the memory management method includes the following steps S101 to S103.

[0030] In step S101: a memory allocation request of a target object is obtained.

[0031] The memory management in the Linux kernel consists of structures such as nodes, memory zones, free memory page arrays (free_area), linked list arrays (free_list), and memory pages (page). The Linux kernel divides the physical memory into different types of memory zones, which represent logical partitions for memory management in the Linux kernel. The types of memory zones include: DMA (Direct Memory Access) memory zones, normal memory zones (ZONE_NORMAL), high-end memory zones (ZONE_HIGHMEM), and movable memory zones (ZONE_MOVABLE). Different types of memory zones have different properties and uses. The target object can be an application, the Linux kernel, and other objects.

[0032] It is understandable that, depending on the target object, the memory allocation request from the target object can be used to apply for memory pages from any of the above-mentioned types of memory domains. In the case where the memory allocation request is used to apply for memory from the target memory domain, memory pages of the requested migration type and order are allocated from the target memory domain to the target object that initiated the memory allocation request. It is understandable that when the target object is an application, the allocated memory pages are mainly movable memory pages, when the target object is the kernel itself, the allocated memory pages are mainly non-movable memory pages, and the inner pages allocated by the disk page cache are mainly reclaimable memory pages.

[0033] In step S102: if the memory allocation request is for applying for a non-movable memory page, a first memory page is allocated to the target object from a first memory area of ​​the target memory domain, wherein the first memory area is a section of continuous physical memory reserved in the target memory domain for allocating non-movable memory pages.

[0034] like Figure 2 As shown, the reserved first memory area is located in the target memory domain and close to the head position of the target memory domain, and the second memory area is located after the first memory area and close to the tail position of the target memory domain. The first memory area is smaller than the second memory area, and the first memory area is reserved for applying for non-movable memory pages. The second memory area is the main memory area in the target memory domain, which is used for applying for other migration types of memory pages including movable memory pages.

[0035] In the Linux kernel, there are at least three types of memory page migration:

[0036] 1. Movable memory page (MIGRATE_MOVABLE): indicates the memory page that can be moved at will, which is usually the memory page belonging to the application;

[0037] 2. Unmovable memory page (MIGRATE_UNMOVABLE): has a fixed location in the target memory domain and cannot be moved to other locations in the target memory domain. For example, the memory that the kernel itself needs to use belongs to this category;

[0038] 3. Reclaimable memory page (MIGRATE_RECLAIMABLE): cannot be moved directly, but can be reclaimed. The contents of the memory page can be read back or retrieved again. The most typical example is the object allocated using the slab mechanism.

[0039] There may also be: 4. MIGRATE_HIGHATOMIC: for high-order atomic allocation; 5. CMA memory page (MIGRATE_CMA, CMA's English full name: Contiguous Memory Allocator, Chinese translation: continuous memory allocator).

[0040] Furthermore, it should be noted that in the Linux kernel that uses the buddy system to manage memory, multiple consecutive memory pages form a page block, and the general size of a page block is 2M or 4M.

[0041] Because non-removable memory pages are mainly applied for in ZONE_NORMAL (normal memory domain), the target memory domain in the embodiments of this specification is the normal memory domain. In other words, a first memory area is reserved in the normal memory domain for applying for non-removable memory pages. In the case where the memory allocation request is used to apply for memory pages from the normal memory domain, any of the following migration types of memory pages can be applied to the normal memory domain: removable memory pages, non-removable memory pages, recyclable memory pages, and CMA memory pages, etc. Among them, the stack memory of the application layer is mainly removable memory pages, the memory pages allocated by the kernel are mainly non-removable memory pages, and the disk page cache is mainly recyclable memory pages.

[0042] In the case where the memory allocation request is used to apply for a memory page to the target memory domain, the migration type of the memory page applied for by the memory allocation request is determined. If the application is for an immovable memory page, a physical first memory page is allocated to the target object from the first memory area in the target memory domain through the buddy system. It should be noted that the buddy system is an architecture for managing the allocation and release of physical memory in the Linux kernel.

[0043] It can be understood that the first memory page is a memory page of a certain order from order 0 to order 10, which is determined according to the order of the non-movable memory page requested by the memory allocation request. In the case where the memory allocation request is used to request a non-movable memory page from the target memory domain, the target order of the memory page requested by the memory allocation request is determined, and the first memory page of the target order is allocated to the target object from the first memory area in the target memory domain through the buddy system.

[0044] It is understandable that when the linux kernel initializes the partner system, the migration type of all page blocks (pageblock) is the movable class by default. That is to say, when the linux kernel initializes the partner system, the free memory pages are all associated with the second free linked list whose migration type is the movable class. With the operation of the partner system, if there is a target object applying for memory pages of other migration types except the movable class, the linux kernel will perform a stealing page operation to migrate the migration types of some page blocks (pageblock) from the movable class to the migration type of the required application memory page. Therefore, in some embodiments, the first memory page is allocated to the target object from the first memory area in the target memory domain through the partner system, which may include: if the first memory area does not exist in the target memory domain, the first memory area is reserved in the target memory domain through the stealing page operation, and the first memory page is allocated to the above-mentioned target object from the reserved first memory area. If the first memory area already exists in the target memory domain, the first memory page is directly allocated to the target object from the first memory area.

[0045] In some embodiments, the first application for an immovable memory page after the buddy system is initialized triggers the first memory region to be reserved in the target memory domain through a page stealing operation, so that the first memory region exists in the target memory domain.

[0046] It should be understood that, in order to reserve a first memory area in the target memory domain through a page stealing operation, it is necessary to construct an M-order first free linked list of the first immovable class in the partner system. Figure 3 As shown, the M-order first free list specifically includes the first free lists (UNMOVABLE_RESERVED) from the [0]th order to the

[10] th order. Figure 4 As shown, each node (Node) in each order first free linked list (UNMOVABLE_RESERVED) is associated with an unmovable memory page of the corresponding order for allocation, for example: each node in the 0th order first free linked list is associated with an order-0 free memory page for allocation of an order-0 unmovable memory page, each node in the 1st order first free linked list is associated with an order-1 free memory page for allocation of an order-1 unmovable memory page... and so on, each node in the 10th order first free linked list is associated with an order-10 free memory page for allocation of an order-10 unmovable memory page.

[0047] It should be noted that in the partner system, the free memory page associated with each node in each first free list is 2 basic pages to the power of order (number of memory pages). Exemplary: in the first free list of order 0, the free memory page of order 0 associated with each node is a single basic page, and the free memory page of order 1 associated with each node in the first free list of order 1 is 2 basic pages, where the size of the basic page is fixed.

[0048] Because the memory defragmentation function of the Linux kernel scans in units of page blocks, therefore, in some embodiments, reserving a first memory area in the target memory domain by stealing pages may include: starting the scan from the starting page frame (PFN, page frame number) of the target memory domain with page blocks as the scanning unit, the target memory domain includes multiple page blocks, each page block includes multiple continuous basic pages, and each scan can obtain a page block with page blocks as the scanning unit. After each scan, determine whether the current scanned page block meets the preset characteristic conditions. If the current scanned page block meets the preset characteristic conditions, migrate the current scanned page block to the first immovable class; end the scan of the target memory domain when the scan stop condition is met to obtain the first memory area. In other embodiments, the target memory domain may also be scanned with other scanning units.

[0049] In some embodiments, the currently scanned page block needs to meet the following preset characteristic conditions: the migration type of the currently scanned page block is movable; all memory pages in the currently scanned page block are in the partner system; and all memory pages in the currently scanned page block have no retention flag set.

[0050] As the partner system runs, if a target object applies for memory pages of other migration types except the movable class, the partner system will perform a page stealing operation to migrate the migration type of some page blocks from the original migration type to the required migration type, and all free memory pages in the page block are migrated from the second free linked list of the movable class to one of the second free linked list of the second immovable class, the second free linked list of the recyclable class and the first free linked list of the first immovable class.

[0051] like Figure 4 As shown, the partner system includes a multi-level free memory page array free_area[0]-free_area

[10] , and the subscript index of the free memory page array corresponds to the level of the memory page. Among them, each level of the free memory page array includes a linked list array, which is used to manage free page arrays of the same level, and the subscript index of the linked list array corresponds to the migration type. Each level of the linked list array includes N free linked lists free_list corresponding to N migration types one by one, and the N free linked lists include a first free linked list corresponding to the first immovable class and N-1 second free linked lists corresponding to other N-1 migration types. The first free linked list of each level is used to manage free memory pages of the same level in the first memory area, and the second free linked list of each level is used to manage free memory pages of the same level in the corresponding migration type in the second memory area, and N is an integer greater than 2.

[0052] In some embodiments, migrating the currently scanned page block to the first immovable class includes: in a partner system, changing the migration type of the currently scanned page block from the movable class to the first immovable class, and migrating the free memory pages in the currently scanned page block from the second free linked list of the movable class to the first free linked list of the same order.

[0053] For example, Figure 4 As shown, in the implementation of this specification, it includes free memory page arrays of levels [0]-

[10] , and the linked list array in each free memory page array of levels [0]-

[10] includes at least a first free linked list of the first unmovable class: free_list[UNMOVABLE_RESERVED], and a second free linked list of the movable class in the following five migration types:

[0054] The second free list of the first unmovable class: free_list[UNMOVABLE];

[0055] The second free list of the movable class: free_list[MOVABLE];

[0056] The second free list of the recyclable class: free_list[RECLAIMABLE];

[0057] The second free list of the CMA class: free_list[CMA]; and

[0058] The second free list of the HIGHATOMIC class: free_list[HIGHATOMIC].

[0059] contrast Figure 3 and Figure 4 As shown, compared with the buddy system in the related art, the buddy system in the embodiment of this specification not only creates a second free list of various migration types in each level of free memory page array of the buddy system through the page stealing operation, which is used to manage the main memory area (second memory area) of the target memory domain, but also creates a first free list in each level of free memory page array to manage the free memory pages in the reserved first memory area. In the related art, only the second free list of various migration types is created in each level of free memory page array of the buddy system through the page stealing operation, which is used to manage the entire target memory domain, and no continuous physical memory segment and free list for managing this continuous physical memory segment are separately reserved.

[0060] In some embodiments, if any one of the following conditions is met first, it indicates that the scanning stop condition is met:

[0061] Condition 1: The number of memory pages in the first free list of order M reaches the set threshold;

[0062] Condition 2: Scan the last page frame of the target memory domain.

[0063] In some embodiments, regardless of fast allocation or slow allocation, the partner system determines the migration type of the memory page required by the target object and executes memory allocation by calling a function called _rmqueue, and is responsible for selecting and allocating the corresponding order of memory pages from the appropriate free list (free_list) to the target object that initiated the application.

[0064] Determine the target order of the memory page requested by the memory allocation request; in the first memory area, allocate the first memory page to the target object according to the first free list free_list[UNMOVABLE_RESERVED] of the target order and above. Specifically, allocate the first memory page to the target object from the first free list of the target order (for example: the kth order). When there is no k-order free memory page that can be allocated in the first free list of the kth order, the buddy system splits a k+1-order free memory page in the first free list of the k+1th order into two new k-order free memory pages for the first free list of the kth order. This process is performed recursively until a higher-order free memory page that can be split is found or there is no free memory page that can be split in the first free list of the highest order. For example: all the memory blocks of the 4th order have been allocated, and the buddy system splits two 4th-order free memory pages from a 5th-order free memory page for the first free list of the 4th order.

[0065] S103: If memory allocation from the first memory area to the target object fails, a second memory page is allocated to the target object from a second memory area, where the second memory area is a memory area in the target memory domain other than the first memory area.

[0066] If the memory is successfully allocated from the first memory area to the target object, the requested first memory page is returned to the target object. If the memory allocation from the first memory area to the target object fails, it means that there are no allocatable free memory pages in the first free linked lists of the target level and above, that is, there is no allocatable memory in the first memory area. For example, if the requested application is for a 2nd-level movable memory page and the application fails, then it means that there are no allocatable free memory pages in the first free linked lists of levels 2 to 10, that is, there are no consecutive 2 2 If a basic page exists, memory cannot be allocated from the first memory area to the target object, and memory can only be allocated from the second memory area to the target object.

[0067] like Figure 4 As shown, in some embodiments, the target level of the memory page requested by the memory allocation request is determined; in the second memory area, the second memory page is allocated to the target object in the second free list free_list[UNMOVABLE] of the second immovable class according to the migration type of the target level and above.

[0068] Through the above steps S101 to S103, it is achieved that the non-movable memory pages are allocated preferentially from the reserved first memory area, and the non-movable memory pages are concentrated in the reserved first memory area. Because the Linux kernel scans the target memory domain in units of page blocks when defragmenting the memory, a pointer is set at the head and tail of the target memory domain. The head pointer scans the movable memory pages from the head to the tail of the target memory domain, and the tail pointer scans the free memory pages from the tail to the head of the target memory domain. When the head pointer and the tail pointer meet, the defragmentation is terminated. Figure 5a As shown, through the technical means provided by the embodiment of the present invention, the main memory area in the target memory domain is mainly composed of movable memory pages and free memory pages, so more continuous free physical memory can be organized during defragmentation. In addition, the memory defragmentation efficiency can be improved, the memory defragmentation success rate can be improved, and the memory fragmentation of the Linux kernel can be reduced. Figure 5b As shown, in the related art, non-movable memory pages are numerous and scattered in the target memory domain, movable memory pages and non-movable memory pages are applied for in the same memory area of ​​the target memory domain, non-movable pages cannot be moved, and after memory defragmentation, there are still many memory fragments.

[0069] The memory page allocated from the first memory area is released to the first memory area when it is recycled. Therefore, in some embodiments, after allocating the first memory page from the first memory area of ​​the target memory domain to the target object, it also includes: when the first memory page is recycled, the first memory page is recycled to the first memory area.

[0070] In some embodiments, it is necessary to distinguish the recycling process of zero-order memory pages and non-zero-order memory pages: if the first memory page allocated to the target object is a zero-order memory page, in order to quickly obtain physical memory next time, when the first memory page is recycled, the first memory page is released to the PCP linked list; because the number of memory pages in the PCP linked list is limited, after the number of memory pages in the PCP linked list reaches a first number threshold, the memory pages in the PCP linked list are released to the partner system. The PCP linked list is a data structure that optimizes memory allocation. It provides a set of independent page cache lists for each CPU core for fast allocation and release of memory pages.

[0071] If the first memory page allocated to the target object is a memory page greater than the zero-order, that is, a non-zero-order memory page, then when the first memory page is recycled, the first memory page is released to the partner system, that is, when the non-zero-order first memory page is recycled, it does not involve entering the intermediate state of the PCP linked list, but directly releases the first memory page to the partner system.

[0072] It can be understood that releasing the first memory page to the PCP linked list (pcp_lists) is to add the first memory page to the PCP (percpu_pages, per CPU page frame cache) linked list. The pcp_lists linked list is a linked list array of the PCP linked list. The maximum subscript of the pcp_lists array is MIGRATE_PCPTYPES. The pcp_lists linked list only contains the free linked lists of the second non-movable class, movable class, and recyclable class. The memory pages of other migration types in the partner system are converted from these three types. In the scenario of multi-core processors, each CPU corresponds to a pcp_lists linked list.

[0073] Because there is no first free list of the first immovable class in the PCP linked list, when releasing the zero-order memory page to the PCP linked list, it is added to the free list pcp_lists[MIGRATE_UNMOVABLE] of the second immovable class in the PCP linked list, and all the free lists pcp_lists[MIGRATE_UNMOVABLE] are single pages (basic pages) of the immovable class. Therefore, in some embodiments, if the next memory allocation request obtained is for applying for a zero-order immovable memory page, memory is preferentially allocated from the free list of the second immovable class in the PCP linked list, without applying in the partner system, so as to enable the target object to quickly obtain physical memory. If the allocation of the zero-order immovable memory page from the PCP linked list to the target object fails, the zero-order immovable memory page is allocated to the target object from the first memory area of ​​the target memory domain.

[0074] In some embodiments, the step of releasing each memory page in the PCP linked list to the partner system may include: respectively obtaining the migration type of the page block where each memory page in the PCP linked list is located; for each memory page in the PCP linked list, according to the migration type of the page block where the memory page is located, releasing the memory page to the free linked list of the corresponding migration type in the partner system, wherein if the migration type of the page block where the memory page is located is the first immovable class, the first memory page is added to the first free linked list of the corresponding order.

[0075] When releasing the first memory page to the partner system, the Linux kernel detects whether the first memory page has a corresponding partner memory page (i.e., a memory page of the same size and physically continuous). If so, the first memory page and the partner memory page are merged into a higher-order memory page. This process is performed recursively until the merging cannot continue, and the merged memory page is added to the first free list of the corresponding order. For example: the first memory page is a 1st-order memory page (containing two pages), and it is merged with another 1st-order partner memory page (containing two pages) to form a 2nd-order memory page (containing 4 pages). If the merging cannot continue, it will be added to the first free list of the 2nd order.

[0076] It should be understood that the process of recycling the second memory page to the second memory area is similar to the process of recycling the first memory page to the first memory area, and also distinguishes between zero-order and non-zero-order second memory pages. For the sake of brevity of the specification, it will not be repeated here.

[0077] Based on the same inventive concept, an embodiment of this specification also provides an electronic device. Figure 6 Schematic diagram of the structure of the electronic device in the embodiment of this specification is shown. Figure 6 As shown, the electronic device includes a memory 604, a processor 602, and a computer program stored in the memory 604 and executable on the processor 602. When the processor 602 executes the computer program, the memory management method of any of the above embodiments is implemented.

[0078] Among them, Figure 6 In the embodiment of the present invention, a bus architecture (represented by bus 600) is shown, which may include any number of interconnected buses and bridges, and bus 600 connects various circuits including one or more processors represented by processor 602 and memory represented by memory 604. Bus 600 may also connect various other circuits such as peripherals, voltage regulators, and power management circuits, which are well known in the art and are not further described herein. Bus interface 605 provides an interface between bus 600 and receiver 601 and transmitter 603. Receiver 601 and transmitter 603 may be the same element, namely a transceiver, providing a unit for communicating with various other devices over a transmission medium. Processor 602 is responsible for managing bus 600 and general processing, while memory 604 may be used to store data used by processor 602 when performing operations.

[0079] Based on the same inventive concept, an embodiment of this specification further provides a computer-readable storage medium on which a computer program is stored. When the program is executed by a processor, the memory management method described in any of the above embodiments is implemented.

[0080] According to one or more embodiments provided in this specification, when the memory allocation request of the target object is used to apply for a non-removable memory page from the target memory domain, the first memory page is preferentially allocated to the target object from the first memory area of ​​the target memory domain, wherein the first memory area is a section of continuous physical memory reserved in the target memory domain for allocating non-removable memory pages; if the memory allocation from the first memory area to the target object fails, the second memory page is allocated to the target object from the second memory area outside the first memory area, so that the non-removable memory pages are concentrated in the reserved first memory area, and most of the memory areas in the target memory domain are mainly removable memory pages and free memory pages. Therefore, after allocating memory using the memory management method of the embodiment of this specification, it is beneficial to organize more continuous physical memory when defragmenting the target memory domain, thereby reducing memory fragmentation. Moreover, the efficiency and success rate of memory defragmentation can be improved.

[0081] This specification 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 this specification. 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 processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that has the functions specified in one or more boxes.

[0082] 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 including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0083] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process in the computer or other programmable device. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0084] Although the preferred embodiments of this specification have been described, those skilled in the art may make additional changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of this specification.

[0085] Obviously, those skilled in the art can make various changes and modifications to this specification without departing from the spirit and scope of this specification. Thus, if these modifications and variations of this specification fall within the scope of the claims of this specification and their equivalents, this specification is also intended to include these modifications and variations.

Claims

1. A memory management method, characterized in that: include: Get the memory allocation request of the target object; If the memory allocation request is for applying for a non-movable memory page, allocating a first memory page to the target object from a first memory area of ​​the target memory domain, wherein the first memory area is a section of continuous physical memory reserved in the target memory domain for allocating non-movable memory pages; If memory allocation from the first memory area to the target object fails, a second memory page is allocated to the target object from a second memory area, where the second memory area is a memory area in the target memory domain other than the first memory area.

2. The memory management method according to claim 1, characterized in that: The allocating a first memory page from a first memory area of ​​a target memory domain to the target object comprises: Determine the migration type of the memory page requested by the memory allocation request; If it is determined that the migration type of the memory page applied for by the memory allocation request is an immovable memory page, the first memory page is allocated to the target object from a first memory area in the target memory domain through a partner system.

3. The memory management method according to claim 2, characterized in that: Before allocating the first memory page from the first memory area in the target memory domain to the target object through the partner system, the method further includes: If the first memory area does not exist in the target memory domain, the first memory area is reserved in the target memory domain through a page stealing operation.

4. The memory management method according to claim 3, characterized in that: The step of reserving the first memory area in the target memory domain by using a page stealing operation includes: Scanning from a start page frame of the target memory domain using page blocks as scanning units; After each scan, if the currently scanned page block meets a preset characteristic condition, the currently scanned page block is migrated to a first immovable class, where the first immovable class is a migration type reserved for allocating immovable memory pages; When the scanning stop condition is met, the scanning of the target memory domain is terminated to obtain the first memory area.

5. The memory management method according to claim 4, characterized in that: The currently scanned page block meets the following preset characteristic conditions: The migration type of the currently scanned page block is movable; All memory pages in the currently scanned page block are in the partner system; and No reserved flag is set for all memory pages in the currently scanned page block.

6. The memory management method according to claim 5, characterized in that: In the buddy system, a multi-level array of free memory pages is included; Among them, the free memory page array at each level includes a linked list array, and the linked list array at each level includes N free linked lists corresponding one by one to N migration types, and the N free linked lists include a first free linked list corresponding to the first immovable class and N-1 second free linked lists corresponding to other N-1 migration types. The first free linked list at each level is used to manage free memory pages of the same level and size in the first memory area, and the second free linked list at each level is used to manage free memory pages of the same level and size of the corresponding migration type in the second memory area, and N is an integer greater than 2.

7. The memory management method according to claim 6, characterized in that: The step of migrating the currently scanned page block to the first immovable class includes: In the partner system, modifying the migration type of the currently scanned page block from the movable class to the first non-movable class; and The free memory pages in the currently scanned page block are migrated from the second free linked list of the movable class to the first free linked list of the same order.

8. The memory management method according to claim 6, characterized in that: Allocating the first memory page from a first memory area in the target memory domain to the target object through a partner system includes: Determine the target level of the memory page requested by the memory allocation request; In the first memory area, the first memory page is allocated to the target object according to the first free linked lists of the target level and above.

9. The memory management method according to claim 6, characterized in that: The allocating a second memory page from the second memory area to the target object comprises: Determine the target level of the memory page requested by the memory allocation request; In the second memory area, the second memory page is allocated to the target object according to the second free linked list of the second immovable class of migration types of the target level and above.

10. The memory management method according to claim 6, characterized in that: After the first memory page is allocated from the first memory area of ​​the target memory domain to the target object, the method further includes: If the first memory page is a zero-order memory page, releasing the first memory page to a PCP linked list when the first memory page is recycled, and after the number of memory pages in the PCP linked list reaches a first number threshold, releasing the memory pages in the PCP linked list to the partner system; If the first memory page is a memory page greater than the zero order, the first memory page is released to the partner system when the first memory page is recycled.

11. The memory management method according to claim 10, characterized in that: The releasing the memory pages in the PCP linked list to the partner system includes: Respectively obtain the migration type of the page block where each memory page in the PCP linked list is located; For each of the memory pages in the PCP linked list, if the migration type of the page block where the memory page is located is the first immovable type, the memory page is added to the first free linked list of the corresponding order.

12. An electronic device, characterized in that: The invention comprises a memory, a processor and a computer program stored in the memory and executable on the processor, wherein the processor implements the memory management method according to any one of claims 1 to 11 when executing the computer program.

13. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the memory management method described in any one of claims 1 to 11 is implemented.