Memory management method and apparatus, electronic device, and storage medium
By flexibly allocating memory blocks and employing various memory linked list management strategies, the problem of wasted space in memory management is solved, achieving efficient and precise memory management and improving memory utilization and management efficiency.
Patent Information
- Application Number
- CN202211705490.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-29
- Publication Date
- 2026-08-25
- Estimated Expiration
- 2042-12-29
AI Technical Summary
Existing technologies often waste memory space during memory management, especially when frequently requesting memory of different sizes. Since the size returned by the memory manager is always larger than the actual amount of memory used, memory space is wasted.
By detecting the memory request of the producer, the corresponding requested memory capacity and memory block address in the free memory list are obtained. The memory address offset is determined according to the requested memory capacity, and memory blocks are flexibly allocated to avoid fixed-size memory partitioning. Multiple memory list management strategies are adopted, such as the first free memory list, the first used memory list, the second free memory list, and the second used memory list, combined with the central memory block bitmap for memory management.
It effectively improves memory utilization, avoids memory fragmentation, achieves efficient and precise memory management, and improves the efficiency of memory allocation and release.
Smart Images

Figure CN116089321B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of memory management technology, and in particular to memory management methods, apparatus, electronic devices and storage media. Background Technology
[0002] In computer technology, caching techniques are commonly used. For example, intermediate computation results that cannot be processed immediately are temporarily stored in memory. Memory management typically divides the memory region into memory blocks of varying sizes. When memory is requested, the closest available memory block is searched for and its address is returned. If a suitable block is found, it is reserved; otherwise, a new memory block is requested, or a larger block is split into smaller blocks. However, when frequently requesting memory of varying sizes, this method often results in unused memory gaps (memory fragmentation) because the memory manager returns a size larger than the actual memory used, leading to wasted memory space.
[0003] Currently, no effective solution has been proposed to address the problem of wasted memory space during memory management in related technologies. Summary of the Invention
[0004] This application provides a memory management method, apparatus, electronic device, and storage medium to at least solve the problem of easily wasting memory space during memory management in related technologies.
[0005] In a first aspect, embodiments of this application provide a memory management method, the method comprising:
[0006] Detect the producer's current first memory request;
[0007] In response to the detected current first memory request, obtain the corresponding requested memory capacity and the current address of the first memory block to be used in the first free memory list, and use the first memory block to be used according to the current address;
[0008] The memory address offset of the first memory block to be used is determined based on the requested memory capacity, and the new address corresponding to the first memory block to be used in the first free memory list is obtained based on the current address and the memory address offset.
[0009] Detect the next first memory request from the producer; in response to the detected next first memory request, use the first memory block to be used according to the new address.
[0010] In some embodiments, the method further includes:
[0011] Based on the current address and the memory address offset, delete the first unused memory block from the first free memory list and add a first used memory block to the first used memory list.
[0012] In some embodiments, after obtaining the corresponding requested memory capacity, the method further includes:
[0013] If the requested memory capacity is detected to be less than or equal to a preset capacity value, the current address in the first free memory list is obtained, and the current unused memory block is used according to the current address;
[0014] If the requested memory capacity is detected to be greater than the preset capacity value, the available memory capacity and current address of the second available memory block in the second free memory list are obtained.
[0015] The second memory block to be used is used according to the requested memory capacity and the memory capacity to be used, and the second memory block to be used is deleted from the second free memory list according to the current address of the second memory block to be used, and a second used memory block is added to the second used memory list;
[0016] Wherein, the memory capacity of the second free memory linked list is greater than the memory capacity of the first free memory linked list, and the memory capacity of the second used memory linked list is greater than the memory capacity of the first used memory linked list.
[0017] In some embodiments, the method further includes:
[0018] The system detects a consumer's first memory release request and, in response to the detected first memory release request, sends the memory pool corresponding to the first memory release request to a preset memory pool cache list for caching.
[0019] Detect the memory pool release request corresponding to the memory pool cache linked list;
[0020] In response to the detected memory pool release request, if the number of memory blocks in the first free memory list is greater than the preset number of memory blocks, the first free memory list and the first used memory list are reset, and a new first free memory list and a new first used memory list are generated.
[0021] Based on the memory pool release request, the second free memory list and the second used memory list are reset, and a new second free memory list and a new second used memory list are generated.
[0022] In some embodiments, the method further includes:
[0023] Get the preset center memory block bitmap;
[0024] A second memory request corresponding to the memory pool is generated based on the requested memory capacity, and the central memory block bitmap is retrieved based on the second memory request; wherein, the memory pool includes the first free memory linked list;
[0025] If a contiguous unused memory region is found in the central memory block bitmap, the corresponding memory block is allocated to the memory pool according to the contiguous unused memory region.
[0026] If the search for the continuous unused memory region fails, a new central memory block bitmap is obtained, and the new central memory block bitmap is searched until the corresponding memory block is allocated to the memory pool.
[0027] In some embodiments, the method further includes:
[0028] Detect a second memory release request for the memory pool; wherein the second memory release request includes the memory address to be released and the memory capacity to be released;
[0029] In response to the detected second memory release request, a central memory block to be released is determined according to the memory address to be released, and the offset of the central memory block to be released corresponding to the central memory block in the central memory block bitmap is determined according to the memory capacity to be released.
[0030] The central memory block bitmap is zeroed according to the address offset to be released, resulting in a new central memory bitmap. The central memory block to be released is then released according to the new central memory bitmap.
[0031] In some embodiments, the method further includes:
[0032] Obtain the private memory pool application request sent by the producer, and based on the private memory pool application request, check whether there is an unused memory pool in the preset memory pool cache list;
[0033] If an unused memory pool is detected, the memory pool is allocated to the producer; if an unused memory pool is detected as missing, a new memory pool is obtained by creating the memory pool cache list and allocated to the producer.
[0034] Secondly, embodiments of this application provide a memory management device, the device comprising: a detection module, a first usage module, an address offset module, and a second usage module;
[0035] The detection module is used to detect the producer's current first memory request;
[0036] The first usage module is configured to, in response to the detected current first memory request, obtain the corresponding requested memory capacity and the current address of the first memory block to be used in the first free memory list, and use the first memory block to be used according to the current address;
[0037] The address offset module is used to determine the memory address offset of the first memory block to be used based on the requested memory capacity, and to obtain the new address corresponding to the first memory block to be used in the first free memory list based on the current address and the memory address offset.
[0038] The second usage module is configured to detect the producer's next first memory request; and in response to the detected next first memory request, to use the first memory block to be used according to the new address.
[0039] Thirdly, embodiments of this application provide an electronic device including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the memory management method as described in the first aspect above.
[0040] Fourthly, embodiments of this application provide a storage medium storing a computer program thereon, which, when executed by a processor, implements the memory management method as described in the first aspect above.
[0041] Compared to related technologies, the memory management method, apparatus, electronic device, and storage medium provided in this application detect the current first memory request from the producer, and in response to the detected current first memory request, obtain the corresponding requested memory capacity and the current address corresponding to the first unused memory block in the first free memory list, and use the first unused memory block according to the current address; determine the memory address offset of the unused memory block according to the requested memory capacity, and obtain the new address corresponding to the new first unused memory block in the first free memory list according to the current address and the memory address offset; detect the next first memory request from the producer; and in response to the detected next first memory request, use the first unused memory block according to the new address. Therefore, it can flexibly determine the size of the unused memory block according to the requested memory capacity, avoiding memory fragmentation caused by dividing memory into multiple fixed-size memory blocks in related technologies, effectively improving memory utilization, and thus solving the problem of easily wasting memory space during memory management, achieving an efficient and accurate memory management method.
[0042] Details of one or more embodiments of this application are set forth in the following drawings and description to make other features, objects and advantages of this application more readily apparent. Attached Figure Description
[0043] The accompanying drawings, which are included to provide a further understanding of this application and form part of this application, illustrate exemplary embodiments and are used to explain this application, but do not constitute an undue limitation of this application. In the drawings:
[0044] Figure 1 This is an application environment diagram of a memory management method according to an embodiment of this application;
[0045] Figure 2 This is a flowchart of a memory management method according to an embodiment of this application;
[0046] Figure 3 This is a flowchart of a memory pool memory allocation method according to an embodiment of this application;
[0047] Figure 4 This is a schematic diagram of the architecture of a memory management method according to a preferred embodiment of this application;
[0048] Figure 5 This is a structural block diagram of a memory management device according to an embodiment of this application;
[0049] Figure 6 This is a structural diagram of the internal structure of a computer device according to an embodiment of this application. Detailed Implementation
[0050] To make the objectives, technical solutions, and advantages of this application clearer, the application is described and illustrated below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the application. All other embodiments obtained by those skilled in the art based on the embodiments provided in this application without inventive effort are within the scope of protection of this application. Furthermore, it is understood that although the efforts made in such a development process may be complex and lengthy, for those skilled in the art related to the content disclosed in this application, modifications to design, manufacturing, or production based on the technical content disclosed in this application are merely conventional technical means and should not be construed as insufficient disclosure of the content of this application.
[0051] In this application, the reference to "embodiment" means that a specific feature, structure, or characteristic described in connection with an embodiment may be included in at least one embodiment of this application. The appearance of this phrase in various places in the specification does not necessarily refer to the same embodiment, nor is it a separate or alternative embodiment that is mutually exclusive with other embodiments. It will be explicitly and implicitly understood by those skilled in the art that the embodiments described in this application may be combined with other embodiments without conflict.
[0052] Unless otherwise defined, the technical or scientific terms used in this application shall have the ordinary meaning as understood by one of ordinary skill in the art to which this application pertains. The terms "a" and "b" used in this application...
[0053] Words such as “one,” “a,” and “the” do not indicate a quantity limitation and can indicate either a singular or plural number. The terms “including,” “comprising,” “having,” and any variations thereof used in this application are intended to cover…
[0054] Non-exclusive inclusion; for example, a process, method, system, product, or device that includes a series of steps or modules (units) is not limited to the listed steps or units, but may also include steps or units not listed, or may include other steps or units inherent to these processes, methods, products, or devices.
[0055] Yuan. The terms "connection," "linked," "coupled," and similar terms used in this application are not limited to physical or mechanical connections, but can include electrical connections, whether direct or indirect.
[0056] The term "multiple" in this application refers to two or more. "And / or" describes the relationship between related objects, indicating that three relationships can exist. For example, "A and / or B" can represent: A alone, A and B simultaneously, or B alone. The terms "first," "second," etc., used in this application...
[0057] The term "third" is merely used to distinguish similar objects and does not represent a specific ordering of objects.
[0058] The memory management methods provided in the five embodiments of this application can be applied to, for example... Figure 1 The application environment shown.
[0059] In this system, terminal device 102 communicates with server device 104 via a network. A data storage system can store the data that server 104 needs to process. The data storage system can be integrated onto server device 104 or located in the cloud or on another network server. The producer can send initial memory requests to terminal device 102.
[0060] The application request is sent and transmitted via terminal device 102 to server device 104. Server device 104, based on the current first memory application request, obtains the corresponding requested memory capacity and the current address of the first available memory block in the first memory free list. This current address is used by the current first available memory block. Server device 104 determines the memory address offset of the available memory block based on the requested memory capacity, and obtains the first available memory block in the first memory free list based on the current address and the memory address offset.
[0061] A free memory linked list summarizes the new addresses corresponding to the new first available memory blocks; the server device 1045 obtains the next first memory request sent by the producer via the terminal device 102, and based on the next...
[0062] The first memory allocation request uses the new first block of memory to be used based on the new address. The terminal device 102 can be, but is not limited to, various personal computers, laptops, smartphones, tablets, and portable wearable devices, such as smartwatches, smart bracelets, and head-mounted devices. The server device 104 can be implemented using a standalone server or a server cluster consisting of multiple servers.
[0063] It should be further noted that the execution entity in this embodiment can be a server device, operating system, memory management platform, or memory management system, etc. That is, the execution entity can be diverse and can be set, used, or changed as needed. Additionally, a third-party application can assist the execution entity in executing this embodiment.
[0064] This embodiment provides a memory management method. Figure 2 This is a flowchart of a memory management method according to an embodiment of this application, such as... Figure 2 As shown, the process includes the following steps:
[0065] Step S210: Obtain the producer's current first memory request.
[0066] The aforementioned first memory request refers to a request for memory space issued by a producer. For example, the producer can send this first memory request to a memory pool module deployed on a server device via the aforementioned terminal device, or directly to a central memory management module deployed on the server device. It is understood that there can be one or more producers.
[0067] Step S220: In response to the detected current first memory request, obtain the corresponding requested memory capacity and the current address of the first memory block to be used in the first free memory list, and use the first memory block to be used according to the current address.
[0068] Step S230: Determine the memory address offset of the memory block to be used based on the requested memory capacity, and obtain the new address corresponding to the first memory block to be used in the first free memory list based on the current address and the memory address offset.
[0069] In steps S220 to S230 above, after detecting the current first memory request, the current first memory request can be parsed and processed, and the size of the memory space required by the producer, i.e. the requested memory capacity, can be calculated. Then, the memory blocks in the first free memory list are used according to the requested memory capacity.
[0070] Specifically, it can be first determined whether the remaining capacity of the first available memory block at the head of the first free memory linked list meets the memory space size requested by the producer, that is, whether the remaining capacity of the current first available memory block is greater than or equal to the requested memory capacity. If it is, the memory address of the first available memory block is offset backward by the same length as the requested memory capacity. That is, the memory address offset of the available memory block is determined based on the requested memory capacity. The current address corresponding to the current first available memory block is offset backward according to the memory offset to obtain a new memory address. Then, the memory address of the first available memory block before the offset is returned to the producer, and the producer uses the first available memory block based on the memory address before the offset and puts the data to be stored into the first available memory block. At the same time, the requested memory capacity is subtracted from the remaining capacity of the first available memory block to obtain the new remaining capacity of the first available memory block. If the remaining capacity of the first memory block to be used is not sufficient to meet the memory space required by the producer, the memory pool module can request 4Kb of memory space from the central memory management module and add the 4Kb memory block to the first free memory list of the memory pool. At the same time, the remaining capacity of the first memory block to be used is initialized to 4Kb. Then, the remaining capacity after satisfying the request is calculated, and the memory address of the first memory block to be used is offset backward by the same length as the requested memory capacity. Finally, the memory address of the first memory block to be used before the offset is returned to the producer for use.
[0071] Step S240: Detect the next first memory request from the producer; in response to the detected next first memory request, use the first memory block to be used according to the new address.
[0072] In this process, after shifting the memory address of the current memory block in the first free memory list backward by the same length as the requested memory capacity through step S230, the first memory request from the producer is detected. If the next first memory request is detected, the above steps are repeated according to the next memory request to determine the new address and remaining capacity of the first memory to be used in the first free memory list, and the memory address before the shift is returned to the producer for its use.
[0073] Through steps S210 to S240, the requested memory capacity is determined based on the first memory request, and the memory address offset of the memory block to be used in the free memory list is determined according to the requested memory capacity. This avoids memory fragmentation caused by dividing memory into multiple fixed-size memory blocks in related technologies, effectively improves memory utilization, solves the problem of easily wasting memory space in the memory management process, and realizes an efficient and accurate memory management method.
[0074] The embodiments of this application will be described and illustrated below through preferred embodiments. Figure 3 This is a flowchart of a memory pool memory allocation method according to an embodiment of this application, such as... Figure 3 As shown, the process includes the following steps:
[0075] Step S301: Obtain the first memory request initiated by the producer.
[0076] Step S302: Based on the current first memory request, obtain the corresponding requested memory capacity and determine whether the requested memory capacity is greater than 4Kb.
[0077] Step S303: If the judgment result of step S302 is negative, then determine whether the memory block at the head of the first free memory linked list meets the above-mentioned memory capacity requirement. If the judgment result of step S303 is negative, then proceed to the next step S304; if the judgment result of step S303 is positive, then proceed directly to the next step S305.
[0078] Step S304: Request a 4Kb memory block from the central memory management module and add the 4Kb memory block to the first free memory linked list.
[0079] Step S305: Subtract the requested memory capacity from the remaining capacity of the memory block, and offset the starting address of the memory block backward by the same length as the requested memory capacity.
[0080] Step S306: If the judgment result of step S302 is yes, then directly apply to the central management module and add the memory block to the first memory usage list.
[0081] Step S307: Return the starting address of the memory block before the offset to the producer.
[0082] In some embodiments, the memory management method further includes the following steps: deleting the first unused memory block from the first free memory list based on the current address and the memory address offset, and adding a first used memory block to the first used memory list. Through these embodiments, the memory block requested by the producer is stored in the first used memory list according to its current address and memory address offset, so that when the consumer returns the memory block after using data, the memory can be quickly released from the first used memory list, thereby improving memory management efficiency.
[0083] In some embodiments, the memory pool further includes a second free memory list and a second used memory list; after obtaining the corresponding requested memory capacity, the memory management method further includes the following steps:
[0084] Step S251: If the requested memory capacity is detected to be less than or equal to a preset capacity value, obtain the current address in the first free memory list, and use the current unused memory block according to the current address.
[0085] The aforementioned preset capacity value can be set by the staff in advance. For example, the preset capacity value can be set to 4Kb, which is the smallest memory operation unit under the Linux operating system. When it is detected that the requested memory capacity is less than or equal to the preset capacity value, it means that the memory space requested by the producer is small. Therefore, by using the above steps S251 or S220 to S230, the current address of the free memory block to be used at the head of the first free memory linked list can be returned for use. The address of the memory block to be used is then offset backward by the same length as the requested memory capacity to obtain a new memory address and a new remaining capacity of the memory block to be used.
[0086] Step S252: If the requested memory capacity is detected to be greater than the preset capacity value, obtain the available memory capacity and current address of the second available memory block in the second free memory list.
[0087] Step S253: Use the second memory block to be used according to the requested memory capacity and the memory to be used capacity, delete the second memory block to be used from the second free memory list according to the current address of the second memory block to be used, and add a second used memory block to the second used memory list.
[0088] The second free memory list has a larger memory capacity than the first free memory list, and the second used memory list has a larger memory capacity than the first used memory list. For example, the first free memory list and the first used memory list may store memory blocks with a memory space value less than or equal to 4KB, while the second free memory list and the second used memory list may store memory blocks with a memory space value greater than 4KB.
[0089] In steps S252 and S253 above, each time a producer requests memory from the memory pool, the comparison result between the requested memory capacity and the preset capacity value is checked. If the requested memory space value is greater than the preset capacity value, it indicates that the producer needs a large amount of memory space. Therefore, the producer can search the second free memory list for a memory block with the same size as the requested memory capacity. If no memory block that meets the requested memory capacity requirement is found, the producer requests a memory block of the requested size from the central memory management module for use and adds the memory block to the second used memory list. If a second unused memory block that meets the requested memory capacity requirement (i.e., the unused memory capacity is greater than or equal to the requested memory capacity) is found in the second free memory list, the current address corresponding to the second unused memory block is returned to the producer, and the second unused memory block is removed from the second free memory list and added to the second used memory list.
[0090] In related technologies, large memory blocks are typically divided into smaller memory blocks for use. However, when requesting a new large memory block, it may be unavailable, necessitating the allocation of an additional large memory block. This leads to memory fragmentation and low memory utilization. This application, through steps S251 to S253, divides the memory pool into a first free memory list and a first used memory list with smaller memory spaces, and a second free memory list and a second used memory list with larger memory spaces. This allows for more efficient allocation of memory capacity.
[0091] When the requested memory size is small, a memory block matching the requested memory capacity can be retrieved from the first free memory list. When the requested memory capacity is large, a large block of memory can be retrieved from the second free memory list, thus effectively avoiding...
[0092] This eliminates memory fragmentation caused by the continuous splitting of large memory blocks, further improving memory utilization.
[0093] In some embodiments, the memory management method described above further includes the following steps:
[0094] Step S261: Detect the consumer's first memory release request, and in response to the detected first memory release request, send the memory pool corresponding to the first memory release request to the preset memory pool cache chain 0 table for caching.
[0095] The first memory release request mentioned above refers to a request sent by the consumer to release memory space. After processing the producer's memory data, the consumer can return the memory pool associated with that memory data to the memory pool linked list management module deployed on the aforementioned server device, which is used to cache the memory pool.
[0096] The memory pool linked list management module is used to manage the memory pool cache linked list. It's understood that this consumer can be one or multiple.
[0097] Step S262: Detect the memory pool release request corresponding to the memory pool cache linked list.
[0098] For example, when the memory pool linked list management module detects that the number of memory pools cached in the memory pool cache linked list exceeds a certain threshold, the memory pool linked list management module can generate a corresponding memory pool.
[0099] The release request is detected by the aforementioned server equipment. For example, when a consumer returns a memory pool, the memory pool linked list management module queries the number of currently managed cached memory pools. If the number exceeds a certain set threshold,
[0100] Then the general cache memory pool it maintains can be destroyed. The destroyed memory pool can be reset through subsequent steps to return all the memory blocks it manages to the aforementioned central memory management module.
[0101] Step S263: In response to the detected memory pool release request, if the number of memory blocks in the first free memory list is greater than the preset number of memory blocks, the first free memory list and the fifth used memory list are reset, and a new first free memory list and a new first used memory list are generated.
[0102] Memory linked list.
[0103] In response to the aforementioned memory pool release request, a reset operation is performed on the first free memory list and the first used memory list, which have relatively small memory spaces. Specifically, before resetting, the number of memory blocks in the first free memory list is checked. If the number of memory blocks in the first free memory list is greater than the preset number of memory blocks, all memory blocks managed by the first free memory list are returned to the central memory management module. Then, memory blocks in the first used memory list are deleted and correspondingly added to the first free memory list to generate new first free memory lists and first used memory lists.
[0104] Step S263: Based on the memory pool release request, reset the second free memory list and the second used memory list, and generate a new second free memory list and a new second used memory list.
[0105] In response to the aforementioned memory pool release request, a reset operation is performed on the second free memory list and the second used memory list, which have larger memory spaces. Specifically, all memory blocks managed by the second free memory list are returned to the central memory management module, and all memory blocks in the second used memory list are deleted and added to the second free memory list accordingly, generating new second free memory lists and second used memory lists. It is understandable that since larger memory blocks can be considered dedicated memory blocks—meaning only requests requesting memory capacity matching their size can use them—when resetting the memory pool, for memory blocks with large memory spaces managed by the second free memory list, it can be assumed that these blocks will no longer be needed for the current or next task, thus allowing for timely release and preventing excessive memory usage. Furthermore, the frequency of use for large memory blocks is typically lower than that for small memory blocks during application use; therefore, excessively occupying memory blocks need to be cleaned up promptly, while the reset scope for small memory blocks managed by the first free memory list can be gradually reduced.
[0106] Through steps S261 to S263 above, when a consumer releases a memory pool, the memory pool linked list management module caches the memory pool. The memory requested by the memory pool can be used by the next consumer, thereby avoiding repeated requests for memory from the system, improving memory allocation efficiency and reducing the risk of memory fragmentation.
[0107] In some embodiments, the memory management method described above further includes the following steps:
[0108] Step S271: Obtain the preset central memory block bitmap.
[0109] The aforementioned central memory block bitmap refers to a pre-generated bitmap corresponding to the central memory block in the central memory management module. For example, the central memory block is divided into multiple memory regions of 1KB in size, and each memory region corresponds one-to-one with 1 bit in the bitmap corresponding to the central memory block. The correspondence is usually in ascending order of memory address. Each bit is set to 1 to indicate that the corresponding memory region is used, and set to 0 to indicate that the corresponding memory region is not used.
[0110] Step S272: Generate a second memory request corresponding to the memory pool based on the requested memory capacity, and perform a retrieval process on the central memory block bitmap based on the second memory request; wherein, the memory pool includes the first free memory linked list.
[0111] Specifically, when it is detected that the free memory cached by the memory pool itself does not meet the memory space value requested by the current producer (i.e., the remaining free memory capacity of the memory pool is less than the requested memory capacity), a second memory request corresponding to the memory pool is generated and can be detected by the aforementioned server device. In response to the detected second memory request, the requested memory capacity is first aligned by 1Kb. Then, the aforementioned central memory management module traverses and searches the central blocks to determine if there exists a central memory block with consecutive bits of 0 corresponding to free memory that meets the aligned memory request size.
[0112] Step S273: If a contiguous unused memory region is found in the central memory block bitmap, the corresponding memory block is allocated to the memory pool according to the contiguous unused memory region.
[0113] Specifically, after retrieving, through step S272, a free memory region with consecutive bits set to 0 is found in the central memory block bitmap, and the size of the free memory region satisfies the estimated memory capacity requested, i.e., after detecting that there is a corresponding consecutive unused memory region in the central memory block bitmap, the consecutive bits corresponding to the consecutive unused memory region can be set to 1 in the central memory block bitmap, and the first memory address corresponding to the consecutive bits can be assigned to the memory pool module used to manage the memory pool, so that the memory pool can request a memory block with the corresponding space value from the central memory management module.
[0114] Step S274: If the search for the contiguous memory region to be used fails, obtain a new central memory block bitmap and perform search processing on the new central memory block bitmap until the corresponding memory block is allocated to the memory pool.
[0115] Specifically, if no continuous unused memory region is detected through step S273, the central memory management module can request a larger memory space from the operating system of the current application, such as 2Mb of memory. The central memory block obtained this time is aligned and added to the central memory block linked list. At the same time, a new central memory block bitmap corresponding to it is created and initialized to 0. Then, the consecutive bits in the new central memory block bitmap that satisfy the requested memory capacity are set to 1, and the first memory value corresponding to the consecutive bits is returned to the memory pool module.
[0116] Through steps S271 to S274, the central memory block is divided into fixed-size memory regions by the central memory management module and managed using a bitmap. This allows adjacent unused memory regions to be merged using the bitmap, unlike the buddy system in related technologies which actively searches for upstream and downstream memory regions before merging them. At the same time, the bitmap, composed of consecutive small memory regions, can improve CPU memory hit rate and computational efficiency, thereby effectively improving memory management efficiency.
[0117] In some embodiments, the memory management method described above further includes the following steps:
[0118] Step S281: Detect a second memory release request for the memory pool; wherein the second memory release request includes the memory address to be released and the memory capacity to be released.
[0119] When the memory pool releases memory to the central memory management module, it needs to pass not only the address of the memory to be released to the central memory management module, but also the size of the memory, i.e., the capacity of the memory to be released.
[0120] Step S282: In response to the detected second memory release request, determine the central memory block to be released based on the memory address to be released, and determine the address offset of the central memory block to be released in the central memory block bitmap based on the memory capacity to be released.
[0121] Step S283: Set the central memory block bitmap to zero according to the address offset to be released, obtain a new central memory bitmap, and release the central memory block to be released according to the new central memory bitmap.
[0122] In steps S282 and S283 above, in response to the second memory release request, the central memory management module determines which central memory block it manages belongs to based on the memory address to be released, and calculates the offset of the memory in the central memory block bitmap based on the starting memory address of the central memory block. Then, it sets the consecutive bits corresponding to the size of the memory to be released after the offset to 0. If all bits in the bitmap corresponding to the central memory block are 0, the central memory block can be further released to the operating system.
[0123] Through steps S281 to S283 above, memory is released to the central memory management module in a timely manner through the memory pool, which can effectively improve memory utilization. At the same time, since small blocks of memory are indirectly managed by the memory pool and large blocks of memory are managed by the central memory management module, memory fragmentation problems caused by repeated allocation and release of large and small blocks of memory are avoided or reduced.
[0124] In some embodiments, the memory management method described above further includes the following steps:
[0125] Step S201: Obtain the private memory pool application request sent by the producer, and based on the private memory pool application request, check whether there is an unused memory pool in the preset memory pool cache list.
[0126] Step S202: If an unused memory pool is detected, the memory pool is allocated to the producer; if an unused memory pool is detected as missing, a new memory pool is obtained through the memory pool cache list and allocated to the producer.
[0127] In steps S201 to S202 above, in the initial stage, the producer can apply for a private memory pool from the memory pool linked list management module, and the memory pool linked list management module can query whether there is an unused memory pool in the memory pool cache linked list it manages. If an unused memory pool is detected, the memory pool is returned to the producer corresponding to the private memory pool application request. If no unused memory pool is detected, a new memory pool can be created and allocated to the aforementioned producer.
[0128] Through steps S201 to S202 above, the memory pool in the cache list is allocated to the corresponding producer based on the detected private memory pool request initiated by the producer, so that each producer has a corresponding private memory pool. This avoids or reduces the competition when multiple producers request memory from the memory manager at the same time, which is conducive to further improving the efficiency and accuracy of memory management.
[0129] The embodiments of this application will be described in detail below with reference to practical application scenarios. Figure 4 This is a schematic diagram of the architecture of a memory management method according to a preferred embodiment of this application, such as... Figure 4 As shown, the system architecture of this memory management method mainly includes a memory pool module, a memory pool linked list management module, and a unique central memory management module. The memory pool module consists of a first free memory linked list, a first used memory linked list, a second free memory linked list, and a second used memory linked list, and provides memory allocation and reset functions. The memory pool linked list management module manages the memory pool and mainly consists of a memory pool cache linked list. The central memory management module internally contains a central memory block linked list to maintain multiple central memory blocks. Each central memory block can correspond to a bitmap; for example, a central memory block can be divided into multiple memory regions of 1KB in size, with each memory region corresponding one-to-one with 1 bit in the bitmap of the corresponding central memory block. The correspondence can be in ascending order of memory address, with each bit set to 1 indicating use and 0 indicating unused.
[0130] It should be noted that the steps shown in the above process or in the flowchart of the accompanying figures can be executed in a computer system such as a set of computer-executable instructions, and although a logical order is shown in the flowchart, in some cases the steps shown or described may be executed in a different order than that shown here.
[0131] This embodiment also provides a memory management device for implementing the above embodiments and preferred embodiments, and details already described will not be repeated. As used below, the terms "module," "unit," "subunit," etc., can refer to a combination of software and / or hardware that performs a predetermined function. Although the device described in the following embodiments is preferably implemented in software, hardware implementation, or a combination of software and hardware, is also possible and contemplated.
[0132] Figure 5 This is a structural block diagram of a memory management device according to an embodiment of this application, such as... Figure 5 As shown, the device includes: a detection module 52, a first usage module 54, an address offset module 56, and a second usage module 58.
[0133] The detection module 52 is used to detect the producer's current first memory request; the first usage module 54 is used to, in response to the detected current first memory request, obtain the corresponding requested memory capacity and the current address corresponding to the current first unused memory block in the first free memory list, and use the current first unused memory block according to the current address; the address offset module 56 is used to determine the memory address offset of the unused memory block according to the requested memory capacity, and obtain the new address corresponding to the new first unused memory block in the first free memory list according to the current address and the memory address offset; the second usage module 58 is used to detect the producer's next first memory request, and in response to the detected next first memory request, use the new first unused memory block according to the new address.
[0134] Through the above embodiments, the first usage module 54 determines the requested memory capacity based on the first memory request, and the address offset module 56 determines the memory address offset of the memory block to be used in the free memory list according to the requested memory capacity. This avoids memory fragmentation caused by dividing memory into multiple fixed-size memory blocks in related technologies, effectively improves memory utilization, solves the problem of easily wasting memory space in the memory management process, and realizes an efficient and accurate memory management method.
[0135] It should be noted that the above modules can be functional modules or program modules, and can be implemented through software or hardware. For modules implemented through hardware, the above modules can reside in the same processor; or the above modules can be located in different processors in any combination.
[0136] This embodiment also provides a computer device, which may be a server. Figure 6 This is a structural diagram of the internal structure of a computer device according to an embodiment of this application, such as... Figure 6 As shown, the computer device includes a processor, memory, network interface, and database connected via a system bus. The processor provides computing and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system, computer programs, and database. The internal memory provides an environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The database stores a memory pool. The network interface communicates with external terminals via a network connection. When the computer program is executed by the processor, it implements the aforementioned memory management method.
[0137] Those skilled in the art will understand that Figure 6 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0138] This embodiment also provides an electronic device, including a memory and a processor, wherein the memory stores a computer program and the processor is configured to run the computer program to perform the steps in any of the above method embodiments.
[0139] Optionally, the electronic device may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor and the input / output device is connected to the processor.
[0140] Optionally, in this embodiment, the processor can be configured to perform the following steps via a computer program:
[0141] S1 detects the producer's current first memory request.
[0142] S2, in response to the detected current first memory request, obtain the corresponding requested memory capacity and the current address of the current first memory block to be used in the first free memory list, and use the current first memory block to be used according to the current address.
[0143] S3. Determine the memory address offset of the memory block to be used based on the requested memory capacity, and obtain the new address corresponding to the new first memory block to be used in the first free memory list based on the current address and the memory address offset.
[0144] S4, in response to the detected next first memory request from the producer, uses the new first memory block to be used according to the new address.
[0145] It should be noted that the specific examples in this embodiment can refer to the examples described in the above embodiments and optional implementations, and will not be repeated here.
[0146] Furthermore, in conjunction with the memory management methods in the above embodiments, this application embodiment can provide a storage medium for implementation. This storage medium stores a computer program; when the computer program is executed by a processor, it implements any of the memory management methods in the above embodiments.
[0147] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. Any references to memory, storage, databases, or other media used in the embodiments provided in this application can include non-volatile and / or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in various forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), dual data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), Rambus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.
[0148] Those skilled in the art should understand that the technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments have been described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0149] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the invention patent. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this patent application should be determined by the appended claims.
Claims
1. A memory management method, characterized in that, The method includes: Detect the producer's current first memory request; In response to the detected current first memory request, obtain the corresponding requested memory capacity and the current address of the first memory block to be used in the first free memory list, and use the first memory block to be used according to the current address; The memory address offset of the first memory block to be used is determined based on the requested memory capacity, and the new address corresponding to the first memory block to be used in the first free memory list is obtained based on the current address and the memory address offset. Detect the next first memory request from the producer; in response to the detected next first memory request, use the first memory block to be used according to the new address; Get the preset center memory block bitmap; A second memory request corresponding to the memory pool is generated based on the requested memory capacity, and the central memory block bitmap is retrieved based on the second memory request; wherein, the memory pool includes the first free memory linked list; If a contiguous unused memory region is found in the central memory block bitmap, the corresponding memory block is allocated to the memory pool according to the contiguous unused memory region. If the search for the continuous unused memory region fails, a new central memory block bitmap is obtained, and the new central memory block bitmap is searched until the corresponding memory block is allocated to the memory pool. Detect a second memory release request for the memory pool; wherein the second memory release request includes the memory address to be released and the memory capacity to be released; In response to the detected second memory release request, a central memory block to be released is determined according to the memory address to be released, and the offset of the central memory block to be released corresponding to the central memory block in the central memory block bitmap is determined according to the memory capacity to be released. The central memory block bitmap is zeroed according to the address offset to be released, resulting in a new central memory bitmap. The central memory block to be released is then released according to the new central memory bitmap.
2. The memory management method according to claim 1, characterized in that, The method further includes: Based on the current address and the memory address offset, delete the first unused memory block from the first free memory list and add a first used memory block to the first used memory list.
3. The memory management method according to claim 2, characterized in that, After obtaining the corresponding requested memory capacity, the method further includes: If the requested memory capacity is detected to be less than or equal to a preset capacity value, the current address in the first free memory list is obtained, and the current unused memory block is used according to the current address; If the requested memory capacity is detected to be greater than the preset capacity value, the available memory capacity and current address of the second available memory block in the second free memory list are obtained. The second memory block to be used is used according to the requested memory capacity and the memory capacity to be used, and the second memory block to be used is deleted from the second free memory list according to the current address of the second memory block to be used, and a second used memory block is added to the second used memory list; Wherein, the memory capacity of the second free memory linked list is greater than the memory capacity of the first free memory linked list, and the memory capacity of the second used memory linked list is greater than the memory capacity of the first used memory linked list.
4. The memory management method according to claim 3, characterized in that, The method further includes: The system detects a consumer's first memory release request and, in response to the detected first memory release request, sends the memory pool corresponding to the first memory release request to a preset memory pool cache list for caching. Detect the memory pool release request corresponding to the memory pool cache linked list; In response to the detected memory pool release request, if the number of memory blocks in the first free memory list is greater than the preset number of memory blocks, the first free memory list and the first used memory list are reset, and a new first free memory list and a new first used memory list are generated. Based on the memory pool release request, the second free memory list and the second used memory list are reset, and a new second free memory list and a new second used memory list are generated.
5. The memory management method according to any one of claims 1 to 4, characterized in that, The method further includes: Obtain the private memory pool application request sent by the producer, and based on the private memory pool application request, check whether there is an unused memory pool in the preset memory pool cache list; If an unused memory pool is detected, the memory pool is allocated to the producer; if an unused memory pool is detected as missing, a new memory pool is obtained by creating the memory pool cache list and allocated to the producer.
6. A memory management device, characterized in that, The device includes: a detection module, a first usage module, an address offset module, and a second usage module; The detection module is used to detect the producer's current first memory request; The first usage module is configured to, in response to the detected current first memory request, obtain the corresponding requested memory capacity and the current address of the first memory block to be used in the first free memory list, and use the first memory block to be used according to the current address; The address offset module is used to determine the memory address offset of the first memory block to be used based on the requested memory capacity, and to obtain the new address corresponding to the first memory block to be used in the first free memory list based on the current address and the memory address offset. The second usage module is configured to detect the producer's next first memory request; and in response to the detected next first memory request, to use the first memory block to be used according to the new address. The memory management device is configured to: acquire a preset central memory block bitmap; generate a second memory request corresponding to the memory pool based on the requested memory capacity; and perform a retrieval process on the central memory block bitmap based on the second memory request; wherein the memory pool includes a first free memory list; if a contiguous unused memory region is found in the central memory block bitmap, the corresponding memory block is allocated to the memory pool based on the contiguous unused memory region; if the retrieval of the contiguous unused memory region fails, a new central memory block bitmap is acquired, and the new central memory block bitmap is retrieval processed until the corresponding memory block is allocated to the memory pool; The memory management device is further configured to detect a second memory release request of the memory pool; wherein the second memory release request includes a memory address to be released and a memory capacity to be released; in response to the detected second memory release request, a central memory block to be released is determined according to the memory address to be released, and the address offset of the central memory block to be released in the central memory block bitmap is determined according to the memory capacity to be released; the central memory block bitmap is zeroed according to the address offset to be released to obtain a new central memory bitmap, and the central memory block to be released is released according to the new central memory bitmap.
7. An electronic device comprising a memory and a processor, characterized in that, The memory stores a computer program, and the processor is configured to run the computer program to perform the memory management method according to any one of claims 1 to 5.
8. A storage medium, characterized in that, The storage medium stores a computer program, wherein the computer program is configured to execute the memory management method according to any one of claims 1 to 5 when running.
Citation Information
Patent Citations
Memory management method and device, electronic equipment and computer readable storage medium
CN112559165A
Memory management method, memory management unit and Internet of Things equipment
CN113535597A