Memory management method and device, computer device and storage medium

By dividing memory blocks into multiple memory segments and adopting two-level metadata management, the problems of memory waste within memory blocks and the need for small-block memory management are solved, achieving efficient memory utilization and flexible allocation.

CN115599544BActive Publication Date: 2026-01-13ALIBABA (CHINA) CO LTD +1
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202211248341.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-10-12
Publication Date
2026-01-13
Estimated Expiration
2042-10-12

AI Technical Summary

Technical Problem

Large-granularity memory management leads to wasted storage space within memory blocks, and existing technologies struggle to effectively manage scenarios with small memory requirements.

Method used

The memory block is divided into multiple memory segments, and a two-layer metadata management is adopted: block metadata records the allocation status of each memory segment, and total metadata records the number of unallocated memory segments. The allocation status of memory segments is adjusted and the metadata is updated through memory adjustment requests.

Benefits of technology

It reduces the waste of remaining space in large-granularity memory blocks, achieves efficient management of memory segments, and adapts to memory needs of different granularities.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115599544B_ABST
    Figure CN115599544B_ABST
Patent Text Reader

Abstract

Embodiments of the present specification provide a memory management method and device, computer equipment and a storage medium. The memory includes a plurality of memory blocks, each of which is divided into a plurality of memory segments. The memory is used to store total metadata and block metadata corresponding to each allocated memory block. The block metadata includes allocation state information of each memory segment in the allocated memory block. The total metadata includes quantity information of unallocated memory segments in each allocated memory block. The method includes: in response to a memory adjustment request, determining a target memory segment in a state requiring adjustment according to the total metadata and the block metadata; adjusting the allocation state of the target memory segment based on the memory adjustment type corresponding to the memory adjustment request; after adjusting the allocation state of the target memory segment, updating the allocation state information of the block metadata of the target memory block to which the target memory segment belongs, and updating the quantity information of the unallocated memory segments in the target memory block in the total metadata.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This specification relates to the field of computer technology, and in particular to memory management methods, apparatus, computer devices and storage media. Background Technology

[0002] Traditional memory management schemes divide memory into multiple memory pages, and metadata (e.g., struct page) needs to be created for each page to manage it. Since memory pages are usually small (e.g., 4KB), each 4KB memory page would require 64 bytes of metadata. In large memory scenarios, storing metadata would consume a lot of memory space, resulting in a large amount of metadata consuming memory.

[0003] In other solutions, to avoid large amounts of metadata consuming memory, a larger granularity is used to manage memory, such as dividing memory into 2MB blocks. However, with a larger granularity, each memory block may not fully store data, resulting in wasted storage space within the block. Furthermore, scenarios requiring smaller memory blocks, such as storing compressed data, also necessitate finer-grained management. Therefore, avoiding memory waste under a large granularity of management is a pressing technical problem that needs to be solved. Summary of the Invention

[0004] To overcome the problems existing in the related technologies, this specification provides a memory management method, apparatus, and computer device.

[0005] According to a first aspect of the embodiments of this specification, a memory management method is provided, wherein the memory includes a plurality of memory blocks, each memory block being divided into a plurality of memory segments; the memory is used to store total metadata and block metadata corresponding to each allocated memory block;

[0006] The block metadata includes: allocation status information for each memory segment in the allocated memory block;

[0007] The total metadata includes: information on the number of unallocated memory segments in each allocated memory block;

[0008] The method includes:

[0009] In response to a memory adjustment request, the target memory segment whose state needs to be adjusted is determined based on the total metadata and the block metadata;

[0010] Based on the memory adjustment type corresponding to the memory adjustment request, the allocation status of the target memory segment is adjusted;

[0011] After adjusting the allocation status of the target memory segment, update the allocation status information of the block metadata of the target memory block to which the target memory segment belongs, and update the number of unallocated memory segments in the target memory block in the total metadata.

[0012] According to a second aspect of the embodiments of this specification, a memory management device is provided, wherein the memory includes a plurality of memory blocks, and each memory block is divided into a plurality of memory segments;

[0013] The memory is used to store total metadata, as well as block metadata corresponding to each allocated memory block;

[0014] The block metadata includes: allocation status information for each memory segment in the allocated memory block;

[0015] The total metadata includes: information on the number of unallocated memory segments in each allocated memory block;

[0016] The device includes:

[0017] The determination module is configured to: in response to a memory adjustment request, determine the target memory segment whose allocation status needs to be adjusted based on the total metadata and the block metadata;

[0018] The adjustment module is used to: adjust the allocation status of the target memory segment based on the memory adjustment type corresponding to the memory adjustment request;

[0019] The update module is used to: after adjusting the allocation status of the target memory segment, update the allocation status information of the block metadata of the target memory block to which the target memory segment belongs, and update the number of unallocated memory segments in the target memory block in the total metadata.

[0020] According to a third aspect of the embodiments of this specification, a computer device is provided, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor, when executing the computer program, implements the steps of the method embodiments of the first aspect described above. According to a third aspect of the embodiments of this specification, a computer program product is provided, including a computer program, which, when executed by a processor, implements the steps of the method embodiments of the first aspect described above.

[0021] According to a fourth aspect of the embodiments of this specification, a computer-readable storage medium is provided, on which a computer program is stored, which, when executed by a processor, implements the steps of the method embodiments described in the first aspect above.

[0022] The technical solutions provided in the embodiments of this specification may include the following beneficial effects:

[0023] In this embodiment, the memory includes multiple memory blocks, and each memory block is divided into multiple memory segments. Therefore, the memory blocks can be designed with a larger granularity, thereby reducing the occupation of block metadata. Furthermore, the memory segments within a memory block can be specifically managed. This embodiment designs two layers of metadata, including total metadata and block metadata for each allocated memory block. The block metadata includes: allocation status information of each memory segment in the allocated memory block, used to determine the memory segments available for allocation within that allocated memory block. The total metadata includes: the number of unallocated memory segments in each allocated memory block, used to determine the allocatable memory blocks in the memory. When a memory adjustment request is received, the target memory segment whose allocation status needs adjustment can be determined. After adjusting the allocation status of the target memory segment, the allocation status information of the block metadata of the target memory block to which the target memory segment belongs is updated, as is the number of the target memory blocks in the total metadata. This allows for the allocation of memory segments, reducing the waste of remaining space in large-granularity memory blocks and enabling finer-granular management of memory blocks.

[0024] It should be understood that the above general description and the following detailed description are exemplary and explanatory only, and are not intended to limit this specification. Attached Figure Description

[0025] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this specification and, together with the description, serve to explain the principles of this specification.

[0026] Figure 1A and Figure 1B This is a schematic diagram illustrating a memory architecture according to an exemplary embodiment of this specification.

[0027] Figure 2A This is a schematic diagram illustrating the division of memory blocks into memory segments according to an exemplary embodiment of this specification.

[0028] Figure 2B This is a schematic diagram of a singly linked list illustrated in this specification according to an exemplary embodiment.

[0029] Figure 2C This is a schematic diagram of a doubly linked list illustrated in this specification according to an exemplary embodiment.

[0030] Figure 2D This is a schematic diagram of two doubly linked lists illustrated in this specification according to an exemplary embodiment.

[0031] Figure 2E This is a schematic diagram of a linked list array illustrated in this specification according to an exemplary embodiment.

[0032] Figure 2FThis is a schematic diagram illustrating the total metadata according to an exemplary embodiment of this specification.

[0033] Figures 2G to 2J These are schematic diagrams of memory management illustrated in this specification according to an exemplary embodiment.

[0034] Figure 3 This is a block diagram of a computer device in which a memory management device is located, according to an exemplary embodiment of this specification.

[0035] Figure 4 This is a block diagram illustrating a memory management device according to an exemplary embodiment. Detailed Implementation

[0036] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numerals in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this specification. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this specification as detailed in the appended claims.

[0037] The terminology used in this specification is for the purpose of describing particular embodiments only and is not intended to be limiting of this specification. The singular forms “a,” “the,” and “the” as used in this specification and the appended claims are also intended to include the plural forms unless the context clearly indicates otherwise. It should also be understood that the term “and / or” as used herein refers to and includes any and all possible combinations of one or more of the associated listed items.

[0038] It should be understood that although the terms first, second, third, etc., may be used in this specification to describe various information, this information should not be limited to these terms. These terms are only used to distinguish information of the same type from one another. For example, without departing from the scope of this specification, first information may also be referred to as second information, and similarly, second information may also be referred to as first information. Depending on the context, the word "if" as used herein may be interpreted as "when," "when," or "in response to determination."

[0039] In the field of memory management, memory metadata refers to data that records the status information of each managed unit of memory (such as a memory page or memory block) to facilitate memory management. Depending on specific management needs, metadata can record various types of status information. It can be understood that metadata is stored in memory during the operation of a computer device. As mentioned in the background section, fine-grained memory management can result in a large amount of metadata consuming memory, leading to high memory management overhead, such as in scenarios like virtual machines. Figure 1A The above is a schematic diagram illustrating the running of a virtual machine on a host machine according to an exemplary embodiment of this specification.

[0040] In this embodiment, the host machine refers to the physical computer used to install the virtual machine software; the host machine is a concept relative to the virtual machine.

[0041] In this embodiment, a virtual machine (VM) refers to a complete computer system simulated by software, possessing full hardware system functionality and running in a completely isolated environment. A VM can perform the tasks that a physical computer can. When creating a VM on a computer, a portion of the physical machine's hard drive and memory capacity is used as the VM's hard drive and memory capacity. Each VM has its own independent operating system and can be operated just like a physical machine. Common virtual machine software includes, but is not limited to, VMware (VMware ACE), VirtualBox, Virtual PC, or KVM (Kernel-based Virtual Machine), which can virtualize multiple computers within a physical machine system.

[0042] Combination Figure 1A As shown, a host machine (HOST) can run multiple virtual machines (VM1, VM2, ... VMn), using memory from the host's memory. Simultaneously, the host's kernel and other applications (applications 1 to 3 in the diagram) may also use this memory. This can lead to memory contention between the kernel / applications and the virtual machines during operation, resulting in uncertainty regarding the host's available memory. Especially in cases of severe memory shortages, this can cause virtual machine memory to be swapped out or even render the virtual machines unusable, thus impacting system performance and stability.

[0043] Based on this, a memory allocation architecture with reserved memory can be adopted, such as... Figure 1B The diagram shown is a schematic representation of a reserved memory scenario illustrated in this specification. In this architecture, the host machine's memory includes two storage spaces, such as... Figure 1BThe diagram illustrates two memory storage spaces using different filling methods: a non-reserved storage space (space a) used by the kernel (filled with diagonal lines) and a reserved storage space (space b) used by virtual machines (filled with vertical lines and grayscale). Specifically, non-reserved storage space a is used by the kernel, and applications running on the operating system (applications 1 to 3 in the example) can use this space. Reserved storage space b, on the other hand, is used by virtual machines (VMs), such as VM1 to VMn, a total of n virtual machines, as shown in the diagram. These two storage spaces can employ different management granularities; that is, the memory can be partitioned in different ways. Figure 1B For ease of illustration, the two storage spaces are shown as contiguous in the diagram. However, in practical applications, the two storage spaces may be non-contiguous.

[0044] Reserved memory space occupies a large portion of memory and is unavailable to the host kernel. A dedicated reserved memory module can be inserted into the operating system kernel for its management. To facilitate the management of this memory while avoiding the excessive consumption of metadata, and considering that virtual machine memory allocations are often at least several hundred MB (MByte), reserved memory modules typically manage reserved memory in larger granular units. For example, reserved memory can be divided into 2MB memory sections (ms). In some scenarios, large memory sizes are also commonly used; other granularities, such as 1GB (Gigabyte), are optional.

[0045] However, with a larger management granularity, each memory block may not fully store data, resulting in wasted storage space within the memory block. Furthermore, in memory compression scenarios, the compressed data is smaller than the memory block size. Therefore, how to avoid memory waste and efficiently manage the internal structure of memory blocks at a large management granularity is a pressing technical problem that needs to be solved.

[0046] Based on this, this specification provides a memory management method. The memory includes multiple memory blocks, and each memory block is divided into multiple memory segments. Therefore, the memory blocks can be designed with larger granularity, thereby reducing the occupation of block metadata. In addition, the memory segments within the memory blocks can be specifically managed. This embodiment designs two layers of metadata: in addition to the block metadata of each allocated memory block, it also includes total metadata. The block metadata includes: allocation status information of each memory segment in the allocated memory block, used to determine the memory segments available for allocation in the allocated memory block; the total metadata includes: the number of unallocated memory segments in each allocated memory block, used to determine the allocatable memory blocks in the memory. When there is a memory adjustment request, the target memory segment whose allocation status needs to be adjusted can be determined. After adjusting the allocation status of the target memory segment, the allocation status information of the block metadata of the target memory block to which the target memory segment belongs is updated, and the number information of the target memory block in the total metadata is updated. Thus, the allocation of memory segments can be realized, reducing the waste of remaining space in large-granularity memory blocks. The following is a detailed description of this embodiment.

[0047] The memory in this embodiment includes multiple memory blocks, and each memory block is divided into multiple memory segments. The size of the memory blocks can be flexibly configured as needed, such as the aforementioned 2MB or 1GB, etc., and this embodiment does not limit this. The memory blocks can be contiguous or non-contiguous.

[0048] like Figure 2A The illustration shows an embodiment of this specification illustrating the division of each memory block into multiple memory segments. The size of the memory segments can be flexibly configured as needed, and this embodiment does not limit this. For example, taking a 2MB memory block as an example, it can be divided into multiple smaller segments, such as 2048 memory segments with a granularity of 1KB.

[0049] Based on the above design of dividing the memory block into multiple memory segments, it is necessary to design a data structure to manage each memory segment. The data structure in this embodiment includes block metadata and total metadata.

[0050] This embodiment establishes metadata for each allocated memory block at the memory block level, referred to as block metadata (header). The block metadata includes allocation status information for each memory segment within the allocated memory block. In practical applications, the data structure of the block metadata can be flexibly implemented as needed, and there are various ways to implement the allocation status information; this embodiment does not limit this. It is understood that block metadata is only needed for memory blocks that have already been allocated, i.e., those that already contain stored data.

[0051] For example, the memory segments of a memory block can be numbered in a set order (e.g., by address from high to low bits or from low to high bits, etc.), and each number can be configured with an allocation status flag indicating whether it has been allocated. Alternatively, a bitmap can be used to represent the allocation status of each memory segment in the memory block. A bitmap is a data structure that includes at least one element, arranged in order, with each element using "0" or "1" to indicate whether its corresponding element exists or not. In this embodiment, the memory segments can be sorted in a set order, and two bits, 0 and 1, can be used to represent the two states of whether a memory segment is allocated. Thus, a single bitmap can represent the allocation status information of each memory segment within a memory block, resulting in low data consumption and facilitating rapid analysis of the occupancy of each segment within the memory block during subsequent processing. Of course, those skilled in the art will understand that various methods can be used to implement allocation status information in practical applications, and this embodiment does not limit this approach.

[0052] For example, the storage location of block metadata can be varied. For instance, if the block metadata occupies a small amount of memory, it can be stored within a memory segment of the memory block. Depending on the size of the block metadata and the size of the memory segment, the block metadata can occupy one or more memory segments. The memory segment storing the block metadata can be configured as needed, for example, starting from the first memory segment of the memory block or starting from the last memory segment of the memory block. In other examples, the block metadata can also be uniformly stored in other locations in memory; this embodiment does not limit this.

[0053] In a reserved memory scenario, the memory may include a first storage space (non-reserved memory) used by the operating system of the computer device and a second storage space (reserved memory) used by virtual machines. The second storage space includes the plurality of memory blocks. The first and second storage spaces can employ different management granularities. The first storage space can be managed by the first memory management module of the operating system, and the method of this embodiment can be applied to the second memory management module in the operating system that manages the second storage space. When the second memory management module uses the first storage space, it needs to initiate memory allocation to the first memory management module. If the block metadata changes frequently, the second memory management module needs to frequently interact with the first memory management module. Therefore, storing the block metadata in the memory segment of the memory block and managing it directly by the second memory management module can improve processing efficiency. Furthermore, the reserved memory blocks are relatively large in granularity and are often not fully used, while the space occupied by the block metadata is very limited and will not affect the use of the memory blocks. There is also no need to specifically establish a correspondence between the block metadata and the memory blocks; once the address of the memory block is determined, the block metadata of that memory block can be directly determined.

[0054] As an example, the size of a memory segment can be determined based on the size of the block metadata, and the size of the memory segment can be greater than or equal to the size of the block metadata, so that the block metadata is stored in a memory segment, such as the first memory segment, which makes it easier to manage and improves management efficiency.

[0055] For example, the block metadata may also include other information as needed, such as the physical address of the memory block ms, the number of free memory segments free, the maximum number of free segments (i.e., the largest number of consecutive unallocated memory segments in the allocated memory block) max_free, and the sequence number of the starting position of the largest consecutive free segment max, etc., to facilitate the subsequent allocation or release of memory segments.

[0056] This embodiment also establishes total metadata, which includes information on the number of unallocated memory segments in each allocated memory block, to determine the allocatable memory blocks in the memory.

[0057] The number of unallocated memory segments in each allocated memory block can include information related to each free memory segment in the allocated memory block, such as the number of free memory segments and / or the maximum number of free segments, where the maximum number of free segments represents the largest number of consecutive free memory segments in the memory block. For example, a memory block ms has 200 free memory segments, including two consecutive free memory segments, one with 50 segments and the other with 150 segments, resulting in a maximum number of 150 free segments. Allocated memory blocks exist in two types: memory blocks with free memory segments as in the example above; and fully occupied memory blocks, i.e., memory blocks with zero free memory segments. Using this information, when a memory allocation request is received, the total metadata can be used to quickly determine whether there are memory blocks in the allocated memory blocks that meet the allocation request. In practical applications, the implementation of the total metadata data structure can be flexibly configured as needed; this embodiment does not limit this.

[0058] For example, the storage location of the total metadata can be varied; for instance, in a reserved memory scenario, the total metadata can be stored in either the non-reserved memory space or the reserved memory space. For example, due to the size of the total metadata and the requirement in a reserved memory scenario to reserve memory as much as possible for virtual machines, the total metadata can be stored in the non-reserved memory space.

[0059] In some examples, the total metadata may include the address of each block metadata. After determining that there is at least one alternative memory block that satisfies the memory allocation request, the method further includes: reading the block metadata of the at least one alternative memory block according to the address of the block metadata of the at least one alternative memory block. Therefore, in this embodiment, the block metadata of the alternative memory block can be read quickly after the alternative memory block is determined.

[0060] To facilitate management and enable rapid allocation, since there are two types of allocated memory blocks, in some cases, the information on fully allocated memory blocks and the information on incompletely allocated memory blocks in the total metadata can be managed and stored separately.

[0061] In practical applications, the total metadata used to record the number of unallocated memory segments in each allocated memory block can be implemented in various ways. In some examples, a linked list can be used to store the number of unallocated memory segments in each allocated memory block from the total metadata.

[0062] A linked list is a non-contiguous, non-sequential storage structure. The logical order of data elements is achieved through the linking order of pointers within the list. A linked list consists of a series of nodes (each element in the list is called a node), which can be dynamically generated at runtime. Each node includes two parts: a data field that stores the data element and a pointer field that stores its address.

[0063] Linked lists include singly linked lists and doubly linked lists; for example Figure 2B The diagram shown is a schematic of a singly linked list according to one embodiment of this specification. The first node of the linked list includes a head pointer (head) with an empty data field, and the head pointer points to the data field of the next node. The next pointer of the last node points to the end (null). The end indicates that the linked list is a non-circular linked list. In other examples, the next pointer of the last node can also point to the head pointer, thus forming a circular linked list.

[0064] In a doubly linked list, each node's pointer field includes a previous pointer (`prev`, which points to the data field of the previous node) and a next pointer (`next`). Therefore, compared to a singly linked list, it allows for faster location of the previous node. Similarly, based on the pointer `next` of the last node, a doubly linked list can also include non-circular and circular doubly linked lists. For example... Figure 2CThe diagram shown is a schematic of a doubly linked list according to an embodiment of this specification. Taking a circular doubly linked list as an example, the pointer field (i.e., the head pointer) of the first node includes a front pointer and a back pointer. The data field `head` can be empty or store data as needed. Similarly, the pointer fields of other nodes all include a front pointer and a back pointer. The data fields in the diagram are a1, a2, and a3, respectively. For illustrative purposes, the diagram shows each node as a front pointer, data field, and back pointer in sequence. In practical applications, other implementation methods can be used as needed, such as a front pointer, back pointer, and data field in sequence. This embodiment does not limit this. In practical applications, the type of doubly linked list can be selected as needed; this embodiment does not limit this either.

[0065] In some examples, the total metadata includes one or more first linked lists, with different first linked lists corresponding to different quantity information; in practical applications, the first linked list can be a singly linked list or a doubly linked list as needed, and this embodiment does not limit it.

[0066] The first linked list includes at least one node, which stores the address of the block metadata of an allocated memory block, allowing for quick access to the block metadata of the memory block through the total metadata. The addresses of the block metadata of allocated memory blocks with the same amount of information are stored in different nodes of the first linked list.

[0067] like Figure 2D Two doubly linked lists are shown: List_a and List_b. The linked lists in this embodiment include a head node. In practical applications, it is optional to set a head node as needed. This embodiment does not limit this.

[0068] List_a is a doubly linked circular list. The pointer field (head pointer) of the first node includes a front pointer and a back pointer, as well as the data head_a. The back pointer points to the next node a1, the front pointer points to the last node a1, and the back pointer points to the next node a1. Similarly, the front pointer of node a1 points to head_a, and the back pointer points to head_a.

[0069] The linked list List_b is a doubly linked circular list. The pointer field (head pointer) of the first node includes a front pointer and a back pointer, as well as the data head_b. The back pointer points to the next node b1, and the front pointer points to the last node b2. The pointers of the other two nodes are similar, as shown in the attached figure.

[0070] In this embodiment, nodes can store the addresses of the block metadata headers of allocated memory blocks. As shown in the figure, a1, b1, and b2 respectively store the addresses of the corresponding allocated memory block metadata headers. The block metadata is stored in a memory segment of ms, such as the first memory segment, and the header of that memory block can be accessed through the node.

[0071] Node a1 in linked list List_a is used to connect to memory block m1. Nodes b1 and b2 in linked list List_b represent memory blocks m2 and m3 respectively. That is, the block metadata of m2 and the block metadata of m3 are linked in a linked list, indicating that these two allocated memory blocks have the same information about the amount of unallocated memory segments (e.g., max_free).

[0072] The block metadata of the allocated memory block corresponding to a1 and the block metadata of the allocated memory block corresponding to b1 use different linked lists. This means that the number of unallocated memory segments (e.g., max_free) of the allocated memory block corresponding to a1 is different from the number of unallocated memory segments (e.g., max_free) of the allocated memory block corresponding to b1.

[0073] In practical applications, there are often many memory segments, leading to numerous possibilities regarding the number of unallocated memory segments within each allocated memory block. For example, if a memory block has 2048 memory segments, and there are many memory blocks, there are 2048 possible numbers of unallocated memory segments within the allocated memory blocks. Each first linked list corresponds to one number of unallocated memory segments, meaning there may be multiple first linked lists. To facilitate querying allocable memory blocks using total data during memory allocation, in this embodiment, the total metadata includes a linked list array. Each element in the linked list array corresponds to a different quantity range. Each element is linked to one or more first linked lists, and the quantity corresponding to the linked first linked list is within the quantity range corresponding to that element. For example, the linked list array in the total metadata can be a separate metadata set used to manage the linked lists connected to each element in the array.

[0074] The number of memory segments can be divided according to the number of memory segments. There can be multiple number ranges, and each number range can be the same or different. For example, if there are 2048 memory segments, they can be divided into 16 parts, with segments 1 to 128 forming one number range, segments 129 to 256 forming another, and so on. Those skilled in the art will understand that there are many other ways to divide the memory in practical applications, and this embodiment does not limit this.

[0075] Based on this, n ranges of quantities are defined, resulting in n elements in the linked list array. Each element in the linked list array is a linked list. As an example, the linked list array partial[nlist] (where nlist indicates n linked list elements) includes 16 elements: partial[0] to partial

[15] . The first linked list with the number of unallocated memory segments in the range of "1 to 128" is linked to the first element partial[0] of the array, and so on.

[0076] like Figure 2E The diagram shown is a schematic representation of a linked list array according to an exemplary embodiment of this specification. Figure 2E The linked list array shown includes 16 elements, and the range of each element's quantity is shown in the figure. For example, assuming there are three allocated memory blocks in memory, the corresponding linked list could be based on the maximum number of free memory segments in these three allocated memory blocks:

[0077] Assuming the maximum number of free memory segments for memory block ms1 is 100, then the block metadata header for this memory block can be linked to... Figure 2D In the linked list List_a shown;

[0078] Assuming the maximum number of free memory segments (max_free) for memory blocks ms2 and ms3 is 120, then the block metadata headers for these two memory blocks can be linked in... Figure 2D The linked list List_b shown; where b1 represents the header of memory block ms2 and b2 represents the header of memory block ms3.

[0079] Since the maximum number of free memory segments in these three memory blocks is in the range of "1-128", they can be linked to the first element of the array, partial[0].

[0080] In practical applications, various linking methods can be used as needed. For example, each element corresponds to a main linked list and is used to store the head pointer of the corresponding main linked list; the head pointer of each first linked list is stored in the node of the main linked list corresponding to the element of the first linked list.

[0081] In a linked list array, each element is a linked list, and each element stores information about the first node of that linked list. For example, Figure 2EThe first element of the linked list array, partial[0], stores the information of the first node of the doubly linked circular list List_k. Specifically, the doubly linked circular list List_k under partial[0] includes: node head_k, which points to node k1, node k1 points to node k2, and the next pointer of k2 can point to head_k, thus forming a doubly linked circular list. That is, head_k, k1, and k2 form the linked list List_k. Unlike the linked list List_k, the other two linked lists List_a and List_b can be linked to the linked list List_k respectively. For example, node k1 can store the first node of the linked list List_a, and another node k2 can store the first node of the linked list List_b. Figure 2F As shown, k1 actually stores the information of the first node of List_a (highlighted by the dashed box), and k2 actually stores the information of the first node of List_b (highlighted by the dashed box), thus establishing the link between linked lists List_k, List_a, and List_b. For ease of understanding, the information within the dashed boxes is not included in k1 and k2 in the diagram.

[0082] For example, the above example involves two cases where max_free is 100 and 120. This max_free can also be stored in the linked list as needed. For example, it can be stored in the data field head_a of the first node of List_a and the data field head_b of the first node of List_b, respectively.

[0083] For example, the order of the two linked lists List_a and List_b linked by the linked list List_k can be flexibly configured as needed. For example, it can be sorted in ascending order of max_free, or in descending order, or other custom sorting. This embodiment does not limit this.

[0084] Since the maximum number of memory segments `max_free` can have multiple possibilities, in this embodiment, the corresponding first linked list can be created only when `max_free` occurs. For example, in the range "1-128" corresponding to the first element `partial[0]`, since `max_free` only has two cases, 100 and 120, only the linked list `List_a` corresponding to 100 and the linked list `List_b` corresponding to 120 are created, thereby reducing resource consumption. Accordingly, the linked list `List_k` includes nodes used to link these two linked lists. It can be understood that in practical applications, a corresponding linked list can be created for each type of `max_free`. For a case where there is no corresponding allocated memory block for a certain `max_free`, storing an empty value in the linked list corresponding to that `max_free` is also optional, and this embodiment does not limit this.

[0085] In practical applications, there exist fully allocated memory blocks, meaning all memory segments of the allocated memory block have been allocated, and the free memory segments of the allocated memory block are zero. In this case, as mentioned earlier, similar to the previous example, a first linked list representing that max_free is zero can be created. In other examples, fully allocated memory blocks can be managed separately. For example, another linked list can be created, referred to in this embodiment as a second linked list. This second linked list is not linked to the first linked list in the linked list array. The data field of the nodes in this second linked list can be used to store the address of the block metadata of the fully allocated memory block, thereby linking the block metadata headers of each fully allocated memory block, so that the block metadata headers of each fully allocated memory block can be mounted in the same linked list. In memory allocation scenarios, fully allocated memory blocks have no free memory segments and cannot be used for allocation. Based on this, separate management of fully allocated memory blocks can be implemented to improve the processing efficiency of memory allocation.

[0086] As can be seen from the above embodiments, the elements in the linked list array do not need to be directly linked to the block metadata header. There is an intermediate structure list (i.e., each first linked list), which can be allocated as needed according to the maximum number of free segments. For example, if there is only one ms containing 5 consecutive small segments corresponding to 1-128 of partial[0], then a linked list list is allocated so that its corresponding max_free is 5. Then this list is linked upward to partial[0] and downward to the header. Other maximum free segments have not yet appeared, so they are not allocated yet to avoid wasting metadata.

[0087] The aforementioned linked list data and the second linked list can be organized in a pool structure, which serves as the total metadata and can be used to manage all memory blocks in this embodiment. Optionally, the total metadata may also include other information, such as recording the number of memory blocks (ms, nr) it contains, the protection flag lock that protects linked list operations, and the cache pool that caches list metadata, etc. In practical applications, it can be flexibly configured as needed, and this embodiment does not limit it.

[0088] Based on the above metadata design, this specification also provides embodiments of memory management methods. For example... Figure 2G and Figure 2H The diagram shown is a flowchart illustrating a memory management method according to an exemplary embodiment of this specification. The method may include the following steps:

[0089] In step 202, in response to the memory adjustment request, the target memory segment whose allocation status needs to be adjusted is determined based on the total metadata and the block metadata.

[0090] In step 204, the allocation status of the target memory segment is adjusted based on the memory adjustment type corresponding to the memory adjustment request;

[0091] In step 206, after adjusting the allocation status of the target memory segment, the allocation status information of the block metadata of the target memory block to which the target memory segment belongs is updated, and the number of unallocated memory segments in the target memory block in the total metadata is updated.

[0092] The memory management method of this embodiment can be applied to any scenario that requires memory management, including but not limited to the aforementioned reserved memory scenario. In some examples, the method of this embodiment can manage all or part of the storage space of the internal memory; for example, in the reserved memory scenario, the internal memory has reserved storage space specifically for use by the virtual machine.

[0093] When applied to a reserved memory scenario, the memory may include a first storage space used by the operating system of the computer device and a second storage space used by a virtual machine, wherein the second storage space includes the plurality of memory blocks. The first storage space and the second storage space may employ different management units. The first storage space may be managed by the first memory management module of the operating system. The method of this embodiment is applied to the second memory management module of the operating system that manages the second storage space, that is, the solution of this embodiment can be used to manage the second storage space of the memory.

[0094] like Figure 2I and Figure 2J As shown, memory management typically involves two operations: memory allocation and memory release. These will be explained separately below. Taking the method of this embodiment applied to the memory management module as an example, in practical applications, memory allocation and memory release can be independent functions. When a memory allocation request 21 is input to the memory management module, it can execute step 211 of determining the target memory segment, and after adjusting the allocation state of the target memory segment, execute update step 212, which specifically includes updating the block metadata of the target memory block and updating the total metadata. Similarly, when a memory release request 22 is input to the memory management module, it can execute step 221 of determining the target memory segment, and after adjusting the allocation state of the target memory segment, execute update step 222, which specifically includes updating the block metadata of the target memory block and updating the total metadata.

[0095] In some examples, the memory adjustment request includes: a memory allocation request; determining the target memory segment whose allocation status needs to be adjusted based on the total metadata and the block metadata includes:

[0096] Based on the total metadata, determine whether there is at least one alternative memory block that satisfies the memory allocation request;

[0097] If so, based on the block metadata corresponding to at least one of the candidate memory blocks, a target memory block and a target memory segment in the target memory block for memory allocation are determined from the at least one candidate memory block.

[0098] In this embodiment, the memory allocation request can carry the size of the storage space to be allocated. In practical applications, the size of the storage space may be larger than or smaller than the size of a memory block. If it is smaller than the size of a memory block, the availability of a suitable free memory segment can be determined through the total metadata and block metadata mentioned above.

[0099] In some examples, the quantity information of unallocated memory segments includes the maximum number of free segments, which represents the largest consecutive unallocated memory segment in the allocated memory block; determining whether there is at least one alternative memory block that satisfies the memory allocation request based on the total metadata includes:

[0100] Determine the number of memory segments that need to be allocated to satisfy the memory allocation request;

[0101] Based on the total metadata, determine whether there is at least one candidate memory block whose maximum number of free segments is greater than or equal to the number of memory segments to be allocated.

[0102] For example, the storage space size (size) can be divided by the memory segment size and rounded up to obtain the number of memory segments (chunks) that need to be allocated.

[0103] Since the total metadata includes information on the number of unallocated memory segments in each allocated memory block, it is possible to determine whether there are any allocatable memory blocks in memory, and then query for allocatable memory segments. In some examples, the number of unallocated memory segments recorded in the total metadata may be the number of free memory segments, the storage space required for a single memory allocation request, or non-contiguous memory segments.

[0104] In other examples, the storage space required for a single memory allocation request can be a contiguous segment of memory. For instance, this embodiment, based on the design of the maximum number of free segments, can allocate a contiguous target memory segment in response to each memory allocation request, thereby reducing the complexity of memory management. The total metadata stores the max_free of each memory block, determines the range of the number of memory segments to be allocated (the chunk), and then queries the information stored in each element of the linked list array. If the element corresponding to the range of the number of chunks is not empty and is linked to the first linked list below, it can be determined that there is an allocatable memory block.

[0105] In some examples, determining a target memory block and a target memory segment within the target memory block for memory allocation, based on block metadata corresponding to at least one of the candidate memory blocks, includes:

[0106] If there is a candidate memory block with a maximum number of free segments equal to the number of memory segments to be allocated, the candidate memory block and the maximum free segment in the candidate memory block are determined as the target memory block and the target memory segment in the target memory block for allocation, based on the block data of the candidate memory block.

[0107] If the maximum number of free segments in at least one candidate memory block is greater than the number of memory segments to be allocated, the difference between the number of consecutive unallocated memory segments in the candidate memory block and the number of memory segments to be allocated is determined based on the block data of the at least one candidate memory block, and the target memory block and the target memory segment in the target memory block to be allocated are determined based on the difference.

[0108] In this embodiment, if the total data stores a max_free that is exactly equal to the chunk of memory segment to be allocated, then one or more memory blocks under the first linked list corresponding to max_free can be used as target memory blocks. If there are multiple targets, one can be flexibly selected as the target memory block as needed. For example, to facilitate the updating of metadata, the memory block to which the metadata of the last node in the first linked list corresponding to max_free belongs can be used as the target memory block, so that the node can be quickly removed from the first linked list, thereby realizing the rapid updating of the total metadata.

[0109] If there is no `max_free` that is exactly equal to the memory chunk to be allocated, other memory blocks corresponding to `max_free` can be selected as needed. For example, if the memory chunk to be allocated is 110, and `max_free` includes 120, 150, 200, etc. in ascending order, the allocated memory block corresponding to 120 can be selected, thereby minimizing memory fragmentation after the 120 memory segments of this memory block are allocated. Of course, selecting other allocated memory blocks corresponding to `max_free` is also optional, and this embodiment does not limit this.

[0110] Taking the selection of allocated memory blocks corresponding to a max_free value of 120 as candidate memory blocks as an example, there are multiple memory blocks corresponding to a max_free value of 120. Let's assume there are two: candidate memory block ms2 and candidate memory block ms3. Since the maximum number of contiguous free segments in ms2 and ms3 is greater than the required memory chunk, there may be smaller contiguous free memory segments under ms2 and ms3 that could potentially match the chunk. To reduce memory fragmentation, one memory block can be randomly selected as needed, and its block metadata can be traversed to determine if there are more suitable contiguous memory segments. Of course, in other examples, selecting multiple or all memory blocks and traversing the block metadata of each block is also an option, but this method incurs overhead when the system is busy. In practical applications, configuration can be flexible as needed; this embodiment does not limit this. For example, taking ms2 as an example, the block metadata of ms2 is read, and the allocation status information of each memory segment is traversed to finally determine the contiguous free memory segments required for the chunk. For example, by using the block metadata of ms2, it is determined that there is another contiguous free memory segment of 115. Since the difference between 115 and chunk is less than the difference between 118 and chunk, ms2 is determined as the target memory segment, and the found contiguous free memory segment of 115 is determined as the target memory segment.

[0111] Based on this, the target memory segment within the identified target memory block is the memory block used for this allocation, and the address of the target memory segment can be returned to the request. Furthermore, the allocation status of each target memory segment is adjusted, changing it from an unallocated state to an allocated state. Afterwards, the block metadata of the target memory block is updated, that is, the allocation status information of each memory segment of the target memory block is updated.

[0112] Furthermore, the number of unallocated memory segments in the target memory block is updated in the total metadata. For example, if the target memory block becomes a fully allocated memory block after adjusting the allocation status, the header of the target memory block is removed from the original first linked list and linked to the aforementioned second linked list representing fully allocated memory blocks. If the maximum number of free segments in the target memory block changes, the header of the target memory block is removed from the original first linked list, and a new maximum number of free segments is determined; if, after removal, the original first linked list no longer contains the header of the memory block, then the original first linked list is deleted, i.e., the list head metadata is deleted. If the new maximum number of free segments corresponds to a first linked list, it is added to that first linked list; otherwise, a first linked list is created and linked to the corresponding element in the linked list array.

[0113] Next, let's look at an example of memory allocation:

[0114] 1. Receive memory allocation request and determine the size of the storage space to be allocated; the information of the memory blocks to be allocated needs to be recorded in the total metadata pool; in this embodiment, based on the size and the granularity of the memory segments, it is converted into the number of memory segments (chunks) required.

[0115] 2. In the pool's linked list array partial[], check if there is a chunk that satisfies max_free. If so, obtain the address information of the target memory block's metadata header based on the corresponding linked list, return the address information of the found header, and jump to step 5. Otherwise, execute step 3.

[0116] 3. Since the available free memory blocks in the pool (ms) are insufficient to meet the allocation requirements, a new free memory block (ms) needs to be allocated. If there is insufficient memory space, the allocation will fail and the process will exit directly; otherwise, proceed to step 4.

[0117] 4. Allocate a new memory block ms, initialize the block metadata header of the memory block and return the address information of the header; in addition, other related processes such as establishing virtual address mapping can also be performed, which will not be described in detail in this embodiment.

[0118] 5. Based on the address information of the returned header, it can be determined that a block metadata header that meets the allocation requirements has been found. If max_free is equal to the required chunk, the starting allocation position sidx is set to the starting position max of the largest continuous segment in the block metadata header, and the process jumps directly to step 14.

[0119] 6. Otherwise, if max_free is greater than the required chunk (at this time, there may be multiple memory blocks with the same max_free), traverse the allocation bitmap recorded in the header and find the position idx of the first free segment.

[0120] 7. Increment the number of consecutive free segments by 1, and continue to check if the next segment is free. If it is, proceed to step 11; otherwise, proceed to step 8.

[0121] 8. Determine if the free segment is equal to the required chunk size. If they are equal, the allocation requirement is met, and proceed to step 13.

[0122] 9. If the free segment (free) is larger than the chunk, record the difference between them (diff) and compare it with the minimum difference (min_diff). If it is smaller than min_diff, record the starting position (min_idx) of this continuous segment and update min_diff.

[0123] 10. Determine if an end marker has been set. If yes, proceed to step 13; otherwise, proceed to step 11.

[0124] 11. Continue searching for the starting position of the next free memory segment.

[0125] 12. If the next segment is still a free memory segment, determine whether the traversal has ended (in the case of multiple memory blocks with the same max_free, one or more memory blocks' bitmaps can be traversed as needed). If so, set the end flag and jump to step 8; otherwise, jump back to step 7.

[0126] 13. At this point, the minimum difference min_diff is the target memory segment that needs to be allocated, and the starting position sidx is set to min_idx.

[0127] 14. Set the starting chunk size of the allocation location sidx to the allocated state.

[0128] 15. Return the virtual address handle where the sidx memory segment of this memory block is located. This location can be used to store small blocks of memory.

[0129] 16. If it is not a newly allocated memory block, remove the address information of the memory block header from the existing linked list; if the original linked list does not have a memory block header after removal, delete the original first linked list, that is, delete the metadata of the linked list header.

[0130] 17. Determine the number of free memory segments in the header at this time. If all segments are full, move them into the full linked list and jump to step 23; otherwise, execute step 18.

[0131] 18. Since the memory block is not completely full, first update the position (max) and size (max_free) of the largest contiguous memory segment in the header of the block metadata.

[0132] 19. Find the position of the partially full array containing max_free based on max_free.

[0133] 20. Traverse the head of the partial[i] linked list and check if there is a linked list node with max_free in the list below it; if not, proceed to step 21; otherwise, find the list and proceed to step 22.

[0134] 21. First, allocate a linked list node list and set its max_free value, then link it upwards to partial[i].

[0135] 22. Find the list that satisfies max_free, and link the header down to it.

[0136] 23. Complete the entire allocation process.

[0137] The following provides an embodiment of memory release. The memory adjustment request includes a memory release request, which carries the size of the memory to be released and the address of the memory to be released; determining the target memory segment to be adjusted based on the total metadata and the block metadata includes: determining the target memory block based on the size of the memory block and the address of the memory to be released; and determining the target memory segment to be released within the target memory block based on the size of the memory segment and the size of the memory to be released.

[0138] 1. In response to a memory release request, a small block of memory needs to be moved out of the pool; based on the memory release request, determine the location to be released (handler), whose size is size; based on the size of the memory segment, convert it into the number of memory segments to be allocated (chunk);

[0139] 2. Based on the address of the handler, determine the memory block in which it is located, and access the block metadata header of that memory block;

[0140] 3. Determine the handler's position (idx) in the header based on the block metadata header;

[0141] 4. Set the first 'chunk' memory segments starting with idx to a free state;

[0142] 5. Remove the header from the existing linked list; if the original linked list does not have a header linked to a memory block after removal, delete the original linked list, that is, delete the header metadata.

[0143] 6. Determine the number of free memory segments in the header at this time. If all segments are empty, release the memory block directly and return it to the reserved memory management system of the previous level, and jump to step 12; otherwise, execute step 7.

[0144] 7. If not completely empty, update the position (max) and size (max_free) of the largest contiguous memory segment in the header of the memory block (ms);

[0145] 8. Find the position of the element corresponding to max_free in the linked list array: partial[i].

[0146] 9. Traverse the head of the partial[i] linked list and check if there is a linked list node with max_free in the list below it; if not, proceed to step 11; otherwise, find the list and proceed to step 10.

[0147] 10. First, allocate a linked list node `list` and set its `max_free` value, then link it upwards to `partial[i]`;

[0148] 11. Find the list that satisfies max_free, and link the header down to it.

[0149] 12. Complete the entire allocation process.

[0150] Corresponding to the embodiments of the aforementioned memory management methods, this specification also provides embodiments of a memory management device and the computer equipment to which it is applied.

[0151] The embodiments of the memory management device described in this specification can be applied to computer devices, such as servers or terminal devices. The device embodiments can be implemented in software, hardware, or a combination of both. Taking software implementation as an example, as a logical device, it is formed by the processor reading the corresponding computer program instructions from non-volatile memory into memory for execution. From a hardware perspective, such as... Figure 3 The diagram shown is a hardware structure diagram of a computer device containing the memory management device described in this manual. (Except for...) Figure 3 In addition to the processor 310, memory 330, network interface 320, and non-volatile memory 340 shown, the computer device in which the memory management device 331 is located in the embodiment may also include other hardware depending on the actual function of the computer device, which will not be described in detail here.

[0152] like Figure 4 As shown, Figure 4 This is a block diagram illustrating a memory management device according to an exemplary embodiment of the present specification, wherein the memory includes a plurality of memory blocks, and each memory block is divided into a plurality of memory segments;

[0153] The memory is used to store total metadata, as well as block metadata corresponding to each allocated memory block;

[0154] The block metadata includes: allocation status information for each memory segment in the allocated memory block;

[0155] The total metadata includes: information on the number of unallocated memory segments in each allocated memory block;

[0156] The device includes:

[0157] Determining module 41 is configured to: in response to a memory adjustment request, determine the target memory segment whose allocation status needs to be adjusted based on the total metadata and the block metadata;

[0158] The adjustment module 42 is used to: adjust the allocation status of the target memory segment based on the memory adjustment type corresponding to the memory adjustment request;

[0159] The update module 43 is used to: after adjusting the allocation status of the target memory segment, update the allocation status information of the block metadata of the target memory block to which the target memory segment belongs, and update the number of unallocated memory segments in the target memory block in the total metadata.

[0160] In some examples, the memory adjustment request includes: a memory allocation request;

[0161] The determining module is further configured to:

[0162] Based on the total metadata, determine whether there is at least one alternative memory block that satisfies the memory allocation request;

[0163] If so, based on the block metadata corresponding to at least one of the candidate memory blocks, a target memory block and a target memory segment in the target memory block for memory allocation are determined from the at least one candidate memory block.

[0164] In some examples, the quantity information of the unallocated memory segments includes the maximum number of free segments, which represents the largest number of consecutive unallocated memory segments in the allocated memory block;

[0165] The determining module is further configured to:

[0166] Determine the number of memory segments that need to be allocated to satisfy the memory allocation request;

[0167] Based on the total metadata, determine whether there is at least one candidate memory block whose maximum number of free segments is greater than or equal to the number of memory segments to be allocated.

[0168] In some examples, the determining module is also used for:

[0169] If there is a candidate memory block with a maximum number of free segments equal to the number of memory segments to be allocated, the candidate memory block and the maximum free segment in the candidate memory block are determined as the target memory block and the target memory segment in the target memory block for allocation, based on the block data of the candidate memory block.

[0170] If the maximum number of free segments in at least one candidate memory block is greater than the number of memory segments to be allocated, the difference between the number of consecutive unallocated memory segments in the candidate memory block and the number of memory segments to be allocated is determined based on the block data of the at least one candidate memory block, and the target memory block and the target memory segment in the target memory block to be allocated are determined based on the difference.

[0171] In some examples, the total metadata also includes the address of each block metadata; the determining module is further configured to, after determining that there is at least one alternative memory block that satisfies the memory allocation request, read the block metadata of the at least one alternative memory block according to the address of the block metadata of the at least one alternative memory block.

[0172] In some examples, the total metadata includes one or more first linked lists, with different first linked lists corresponding to different quantity information;

[0173] The first linked list includes at least one node, each node being used to store the address of the block metadata of an allocated memory block, so as to access the block metadata of the candidate memory block after the candidate memory block is determined; wherein, the addresses of the block metadata of the same number of allocated memory blocks are stored in different nodes of the first linked list.

[0174] In some examples, the total metadata includes an array of linked lists, where each element corresponds to a different range of quantities;

[0175] Each element is used to link to one or more of the first linked lists, and the quantity information corresponding to the linked first linked list is within the quantity range corresponding to the element.

[0176] In some examples, each element corresponds to a main linked list and is used to store the head pointer of the corresponding main linked list;

[0177] The head pointer of each of the first linked lists is stored in the node of the main linked list corresponding to the element of the first linked list.

[0178] In some examples, the memory adjustment request includes a memory release request, which carries the size of the memory to be released and the address of the memory to be released.

[0179] The determining module is further configured to:

[0180] The target memory block is determined based on the size of the memory block and the address of the memory to be freed;

[0181] Based on the size of the memory segment and the size of the memory to be released, determine the target memory segment to be released within the target memory block.

[0182] In some examples, the memory includes a first storage space for use by the operating system of the computer device and a second storage space for use by a virtual machine, the second storage space including the plurality of memory blocks;

[0183] The first storage space is managed by the first memory management module of the operating system, and the device is applied to the second memory management module in the operating system for managing the second storage space;

[0184] The block metadata of the allocated memory block is stored in the memory segment of the memory block, and the total metadata is stored in the first storage space by calling the first memory management module.

[0185] The specific implementation process of the functions and roles of each module in the above memory management device can be found in the implementation process of the corresponding steps in the above memory management method, and will not be repeated here.

[0186] Accordingly, embodiments of this specification also provide a computer program product, including a computer program that, when executed by a processor, implements the steps of the aforementioned memory management method embodiments.

[0187] Accordingly, embodiments of this specification also provide a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the steps of the memory management method embodiment.

[0188] Accordingly, embodiments of this specification also provide a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the memory management method embodiments.

[0189] For the device embodiments, since they basically correspond to the method embodiments, the relevant parts can be referred to in the description of the method embodiments. The device embodiments described above are merely illustrative. The modules described as separate components may or may not be physically separate, and the components shown as modules may or may not be physical modules, that is, they may be located in one place or distributed across multiple network modules. Some or all of the modules can be selected to achieve the purpose of the solution in this specification according to actual needs. Those skilled in the art can understand and implement this without creative effort.

[0190] The above embodiments can be applied to one or more electronic devices. The electronic device is a device that can automatically perform numerical calculations and / or information processing according to pre-set or stored instructions. The hardware of the electronic device includes, but is not limited to, microprocessors, application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), digital signal processors (DSPs), embedded devices, etc.

[0191] The electronic device can be any electronic product that can interact with the user, such as a personal computer, tablet computer, smartphone, personal digital assistant (PDA), game console, interactive network television (IPTV), smart wearable device, etc.

[0192] The electronic device may also include network devices and / or user devices. The network devices include, but are not limited to, a single network server, a server group consisting of multiple network servers, or a cloud based on cloud computing consisting of a large number of hosts or network servers.

[0193] The networks in which the electronic devices are located include, but are not limited to, the Internet, wide area networks, metropolitan area networks, local area networks, and virtual private networks (VPNs).

[0194] The foregoing has described specific embodiments of this specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims may be performed in a different order than that shown in the embodiments and may still achieve the desired result. Furthermore, the processes depicted in the drawings do not necessarily require the specific or sequential order shown to achieve the desired result. In some embodiments, multitasking and parallel processing are possible or may be advantageous.

[0195] The steps of the various methods described above are only for clarity. In practice, they can be combined into one step or some steps can be split into multiple steps. As long as they include the same logical relationship, they are all within the scope of protection of this patent. Adding insignificant modifications or introducing insignificant designs to the algorithm or process, but without changing the core design of the algorithm and process, are also within the scope of protection of this application.

[0196] The terms "specific example" or "some examples," etc., refer to specific features, structures, materials, or characteristics described in connection with the embodiments or examples, which are included in at least one embodiment or example of this specification. In this specification, illustrative expressions of the above terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in one or more embodiments or examples.

[0197] Other embodiments of this specification will readily occur to those skilled in the art upon consideration of the specification and practice of the invention claimed herein. This specification is intended to cover any variations, uses, or adaptations that follow the general principles of this specification and include common knowledge or customary techniques in the art not claimed herein. The specification and examples are to be considered exemplary only, and the true scope and spirit of this specification are indicated by the following claims.

[0198] It should be understood that this specification is not limited to the precise structures described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this specification is limited only by the appended claims.

[0199] The above description is merely a preferred embodiment of this specification and is not intended to limit this specification. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this specification should be included within the scope of protection of this specification.

Claims

1. A memory management method, said memory comprising a plurality of memory blocks, each of said memory blocks being divided into a plurality of memory segments. The memory is used to store total metadata and block metadata corresponding to each allocated memory block; The block metadata includes allocation state information of each memory segment in the allocated memory block; the allocation state information represents whether the memory segment is allocated; The total metadata includes quantity information of unallocated memory segments in each allocated memory block; the quantity information of unallocated memory segments includes the number of unallocated memory segments and the maximum free segment number, the maximum free segment number represents the number of the largest continuous unallocated memory segment in the allocated memory block; The method comprises: In response to a memory adjustment request, determining a target memory segment requiring state adjustment according to the total metadata and the block metadata; Based on the memory adjustment type corresponding to the memory adjustment request, adjusting the allocation state of the target memory segment; After adjusting the allocation state of the target memory segment, updating the allocation state information of the block metadata of the target memory block to which the target memory segment belongs, and updating the quantity information of unallocated memory segments in the target memory block in the total metadata.

2. The method of claim 1, the memory adjustment request comprising: A memory allocation request; The determination of the target memory segment requiring state adjustment according to the total metadata and the block metadata comprises: According to the total metadata, determining whether there is at least one alternative memory block meeting the memory allocation request; If yes, determining a target memory block and a target memory segment in the target memory block for allocating memory according to the block metadata corresponding to at least one alternative memory block.

3. The method according to claim 2, wherein the determination of whether there is at least one alternative memory block meeting the memory allocation request according to the total metadata comprises: Determining the number of required allocated memory segments required to meet the memory allocation request; According to the total metadata, determining whether there is at least one alternative memory block with the maximum free segment number greater than or equal to the number of required allocated memory segments.

4. The method according to claim 3, wherein the determination of a target memory block and a target memory segment in the target memory block for allocating memory according to the block metadata corresponding to at least one alternative memory block comprises: If there is an alternative memory block with the maximum free segment number equal to the number of required allocated memory segments, determining the alternative memory block and the maximum free segment in the alternative memory block as the target memory block and the target memory segment for allocation according to the block metadata of the alternative memory block; If the maximum free segment numbers of the at least one alternative memory block are all greater than the number of required allocated memory segments, determining the difference between the number of continuous unallocated memory segments in the alternative memory block and the number of required allocated memory segments according to the block metadata of the at least one alternative memory block, and determining the target memory block and the target memory segment for allocation in the target memory block according to the difference.

5. The method of claim 2, wherein the total metadata further comprises an address of each of the block metadata; and after determining that there is at least one candidate memory block satisfying the memory allocation request, the method further comprises: Reading the block metadata of the at least one alternative memory block according to the address of the block metadata of the at least one alternative memory block. 6.The method of claim 5, wherein the total metadata comprises one or more first linked lists, and different first linked lists correspond to different quantity information. The first chain table includes at least one node, each node being used to store an address of block metadata of an allocated memory block to access the block metadata of the alternative memory block after the alternative memory block is determined; wherein, The addresses of the block metadata of the allocated memory blocks with the same quantity information are stored in different nodes of the first linked lists. 7.The method of claim 6, wherein the total metadata comprises an array of linked lists, and each element of the array of linked lists corresponds to a different quantity range. Each element is used to link to one or more first linked lists, and the linked first linked lists correspond to the quantity information within the quantity range corresponding to the element. 8.The method of claim 7, wherein each element corresponds to a total linked list, and is used to store a head pointer of the corresponding total linked list. The head pointer of each first linked list is stored in a node of the total linked list corresponding to the element corresponding to the first linked list.

9. The method of claim 1, the memory adjustment request comprising: a memory release request, the memory release request carrying: a size of memory to be released and an address of the memory to be released; the determining of the target memory segment with the adjusted state according to the total metadata and the block metadata comprises: determining a target memory block according to the size of the memory block and the address of the memory to be released; and determining the target memory segment to be released in the target memory block according to the size of the memory segment and the size of the memory to be released. 10.The method of any one of claims 1 to 9, wherein the memory comprises a first storage space for use by an operating system of a computer device and a second storage space for use by a virtual machine, and the second storage space comprises the plurality of memory blocks. The first storage space is managed by a first memory management module of the operating system, and the method is applied to a second memory management module in the operating system for managing the second storage space. The block metadata of the allocated memory block is stored in a memory segment of the memory block, and the total metadata is stored in the first storage space by invoking the first memory management module. 11.A memory management apparatus, wherein the memory comprises a plurality of memory blocks, and each memory block is divided into a plurality of memory segments; the memory is used to store total metadata and block metadata corresponding to each allocated memory block; the block metadata comprises allocation state information of each memory segment in the allocated memory block, and the allocation state information represents whether the memory segment is allocated; the total metadata comprises quantity information of unallocated memory segments in each allocated memory block, and the quantity information of the unallocated memory segments comprises: a number of unallocated memory segments and a maximum free segment number, and the maximum free segment number represents a number of the largest continuous unallocated memory segments in the allocated memory block; the apparatus comprises: a determining module, configured to: in response to a memory adjustment request, determine a target memory segment with an adjusted allocation state according to the total metadata and the block metadata; an adjusting module, configured to: based on a memory adjustment type corresponding to the memory adjustment request, adjust the allocation state of the target memory segment. An updating module is configured to update allocation state information of block metadata of a target memory block to which the target memory segment belongs, and update the number information of unallocated memory segments in the target memory block in the total metadata after adjusting the allocation state of the target memory segment.

12. A computer device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, wherein, The processor implements the steps of the method in any one of claims 1-10 when executing the computer program. 13.A computer readable storage medium having stored thereon a computer program, the computer program, when executed by a processor, implementing the steps of the method in any one of claims 1-10.

Citation Information

Patent Citations

  • Data management method, device and system, storage medium and electronic equipment

    CN108121813A

  • Memory management method, system and device and readable storage medium

    CN113010453A