Memory management method and apparatus, electronic device, and storage medium

By adopting a data table-based memory management method in real-time system and adjusting the memory block size and strategy in real-time, the problems of uncertain memory allocation time and fragmented memory waste are solved, and more efficient memory allocation is achieved.

WO2025044869A1PCT designated stage expired Publication Date: 2025-03-06Z-ONE TECH CO LTD

Patent Information

Application Number
PCT/CN2024/113704
Authority / Receiving Office
WO · WO
Patent Type
Applications
Current Assignee / Owner
Priority Date
2023-08-28
Filing Date
2024-08-21
Publication Date
2025-03-06

AI Technical Summary

Technical Problem

In the prior art, memory allocation in real-time systems has problems of time uncertainty and fragmented memory waste.

Method used

In the memory management method, the size of memory blocks and the segmentation and merging strategies are adjusted in real time based on the data table, and the upgrade allocation method is adopted to reduce the generation of fragmented memory and improve the memory allocation efficiency.

Benefits of technology

It effectively reduces the memory allocation time and the generation of fragmented memory, and improves the memory allocation efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN2024113704_06032025_PF_FP_ABST
    Figure CN2024113704_06032025_PF_FP_ABST
Patent Text Reader

Abstract

The present application provides a memory management method and apparatus, an electronic device, and a storage medium. The memory management method comprises: upon receipt of a memory application request, determining a first memory block on the basis of a first data table, and allocating the first memory block to an application corresponding to the memory application request; upon receipt of a memory release request, on the basis of a second data table, determining the number of times of allocation of a second memory block corresponding to the memory release request; when the number of times of allocation is greater than or equal to a first threshold, releasing the memory of the second memory block and storing memory block information of the second memory block in the first data table; and when the number of times of allocation is less than the first threshold, releasing the memory of the second memory block and combining the second memory block and a third memory block to obtain a fourth memory block. The memory block size and the strategy for segmenting and combining the memory blocks can be adjusted in real time on the basis of the usage of the memory blocks, thereby effectively reducing memory fragmentation and improving the memory allocation efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Memory management method, device, electronic device and storage medium

[0001] This application claims priority to the Chinese patent application filed with the China Patent Office on August 28, 2023, with application number CN202311087994.0 and application name “Memory Management Method, Device, Electronic Device and Storage Medium”, the entire contents of which are incorporated by reference into this application. Technical Field

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

[0003] Memory allocation in existing real-time systems is performed using, for example, the malloc function in a C library (e.g., glibc). The malloc function allocates memory in the following manner: a continuous space of length size is allocated in the dynamic storage area of ​​the memory. When the memory is no longer in use, the idle memory block is released and the dynamically requested memory is returned to the system. However, the malloc allocation algorithm is relatively complex, the required dynamic memory size varies, and the allocation time for each corresponding allocation process is uncertain. Furthermore, dynamic memory allocation is a process of continuously requesting and releasing memory, during which pointer alignment is required to achieve memory alignment, and memory alignment generates a lot of fragmented memory, resulting in memory waste. Therefore, existing memory management methods have the problem of uncertain memory allocation time, that is, sometimes the time required for memory allocation is too long, and a lot of fragmented memory is generated, resulting in memory waste.

[0004] Summary of the Invention

[0005] The present application provides a memory management method that, during the memory allocation process, can adjust the size of memory blocks and adjust the memory block splitting and merging strategies in real time according to the usage of memory blocks, effectively reducing the generation of fragmented memory, improving memory allocation efficiency, and adopting a hierarchical allocation method to effectively reduce the time required for memory allocation.

[0006] To solve the above technical problems, in a first aspect, an embodiment of the present application provides a memory management method, the method comprising: upon receiving a memory application request, determining a first memory block based on a first data table, and allocating the first memory block to an application corresponding to the memory application request, wherein the first memory block is a memory block determined to be in an idle state based on the first data table and having available memory greater than the memory requested by the memory application request, and the first data table stores memory block information of the memory block in an idle state, the memory block information including available memory information and address information; upon receiving a memory release request, determining an allocation count of a second memory block corresponding to the memory release request based on a second data table, and if the allocation count is greater than or equal to a first threshold, releasing the memory of the second memory block, and storing the memory block information of the second memory block in the first data table; and if the allocation count is less than the first threshold, releasing the memory of the second memory block, merging the second memory block and the third memory block to obtain a fourth memory block, and storing the memory block information of the fourth memory block in the first data table, wherein the third memory block is a memory block determined to be in an idle state based on the first data table and having an address adjacent to the address of the second memory block, and the second data table stores the allocation count information of the memory block.

[0007] In the implementation method of the present application, when a memory application request is received, a first memory block that is in an idle state and has available memory larger than the memory requested by the memory application request is allocated to the application corresponding to the memory application request, so that the allocated memory block can definitely meet the use of the application corresponding to the memory application request, avoiding the need to traverse the entire first data table to search for a memory block with available memory of the same size as the memory requested by the memory application request during the memory allocation process, which takes a lot of time, and effectively reduces the time required for memory allocation.

[0008] When a memory release request is received, if it is determined from the second data table that the number of allocations of the second memory block corresponding to the memory release request is greater than a first threshold, it indicates that the second memory block is frequently used. Therefore, the memory of the second memory block is released, and the memory block information of the second memory block is stored in the first data table. The second memory block is not merged with other memory blocks so that the second memory block can be directly used the next time it is needed for allocation. This reduces the number of memory merges and splits, effectively reduces the generation of fragmented memory, and improves memory allocation efficiency.

[0009] In a possible implementation of the first aspect above, the method also includes: when it is determined based on the second data table that the number of allocations of the fifth memory block is greater than or equal to a second threshold, dividing the sixth memory block to obtain a seventh memory block, the sixth memory block being a memory block that is in an idle state determined based on the first data table and whose usable memory is greater than the usable memory of the fifth memory block, and the usable memory of the seventh memory block is the same as the usable memory of the fifth memory block.

[0010] In the implementation of the present application, if the number of allocations of the fifth memory block is greater than or equal to the second threshold, it indicates that the fifth memory block is frequently used. Therefore, the sixth memory block is split to obtain a seventh memory block, so that the available memory of the seventh memory block is the same as that of the fifth memory block. This facilitates direct allocation of the seventh memory block the next time the available memory of the fifth memory block is the same as that of the fifth memory block, without the need to split the memory block again, effectively reducing memory allocation time.

[0011] In a possible implementation of the first aspect above, the method also includes: receiving a memory acquisition request; allocating a first memory according to the memory acquisition request; dividing the first memory into multiple memory blocks according to a first preset rule; obtaining a first data table based on memory block information of the multiple memory blocks, and obtaining a second data table based on allocation information of the multiple memory blocks, wherein the allocation information includes allocation count information.

[0012] In the implementation of the present application, a first data table and a second data table are obtained based on the memory block information of multiple memory blocks, so as to facilitate subsequent search for corresponding memory blocks according to the data in the first data table and the second data table.

[0013] In a possible implementation of the first aspect above, after allocating the first memory block to the application corresponding to the memory application request, the method also includes: determining a spare memory block for the first memory block; when the usable memory of the spare memory block is the same as the usable memory of the first memory block, storing the memory block information of the spare memory block to the storage location corresponding to the memory block information of the first memory block in the first data table; when the usable memory of the spare memory block is greater than the usable memory of the first memory block, obtaining an eighth memory block whose usable memory is the same as the usable memory of the first memory block according to the splitting of the spare memory block, and storing the memory block information of the eighth memory block to the storage location corresponding to the memory block information of the first memory block in the first data table.

[0014] In the implementation of the present application, after the first memory block is allocated to the application corresponding to the memory application request, the memory block information of the spare memory block or the eighth memory block whose available memory is the same as the available memory of the first memory block is stored in the storage location corresponding to the memory block information of the first memory block in the first data table, so that when the memory to be allocated next time is the same as the available memory of the first memory block, the spare memory block or the eighth memory block can be directly allocated without the need to perform the memory block splitting operation, thereby effectively reducing the memory allocation time.

[0015] In a possible implementation of the first aspect above, when a memory application request is received, if it is determined that multiple memory blocks corresponding to the first memory have been allocated, the method also includes: allocating a second memory; and determining, based on a second data table, memory blocks whose allocation times are greater than a third threshold, and determining that the usable memory of the memory blocks whose allocation times are greater than the third threshold is a second value; determining a first number of memory blocks whose usable memory is the second value in the memory blocks corresponding to the first memory; dividing the second memory into multiple memory blocks based on the second value and the first number to obtain a ninth memory block of the second number, the usable memory of the ninth memory block is the second value, and the second number is greater than the first number.

[0016] In the implementation of the present application, when multiple memory blocks corresponding to the first memory are allocated, the second memory is allocated, and the second memory is divided into multiple memory blocks based on the second value of the available memory of the memory blocks whose allocation times are greater than the third threshold value and the first number of memory blocks whose available memory is the second value, and the multiple memory blocks include multiple ninth memory blocks whose available memory is the second value, and the second number is greater than the first number. The number of allocations of the ninth memory block is greater than the third threshold value, indicating that the memory blocks whose available memory is the second value are used very frequently. Therefore, when dividing the second memory, more ninth memory blocks whose available memory is the second value are divided out, so as to reduce the number of memory block division operations in subsequent memory allocations, effectively reducing the memory allocation time and improving the memory allocation efficiency.

[0017] In a possible implementation of the first aspect above, the memory block includes a memory header structure and available memory, and the memory block information further includes the memory header structure.

[0018] In a possible implementation of the first aspect above, the memory header structure includes: a front memory block header address, a rear memory block header address, a memory block size, a memory block status, a memory block address, a front same-size memory block header address, and a rear same-size memory block header address.

[0019] In the implementation of the present application, the front memory block header address, the rear memory block header address, the memory block size, the memory block status, the memory block address, the front memory block header address of the same size and the rear memory block header address of the same size are stored in the memory header structure, so as to facilitate searching the required memory block according to the memory block information in the first data table when allocating memory.

[0020] In a second aspect, an embodiment of the present application provides a memory management device for executing the memory management method described in the first aspect above, the device comprising: a first processing module for determining a first memory block based on a first data table upon receiving a memory application request, and allocating the first memory block to an application corresponding to the memory application request, the first memory block being a memory block in an idle state determined based on the first data table and having available memory greater than the memory requested by the memory application request, the first data table storing memory block information of the memory block in an idle state, the memory block information including available memory information and address information; a second processing module for determining a first memory block based on a first data table upon receiving a memory release request, and allocating the first memory block to an application corresponding to the memory application request, the first memory block being a memory block in an idle state determined based on the first data table and having available memory greater than the memory requested by the memory application request, the memory block information of the memory block in an idle state being stored in the first data table, the memory block information including available memory information and address information; In the case of a request, the allocation count of the second memory block corresponding to the memory release request is determined based on the second data table. When the allocation count is greater than or equal to the first threshold, the memory of the second memory block is released, and the memory block information of the second memory block is stored in the first data table. When the allocation count is less than the first threshold, the memory of the second memory block is released, the second memory block and the third memory block are merged to obtain a fourth memory block, and the memory block information of the fourth memory block is stored in the first data table. The third memory block is a memory block that is in an idle state determined based on the first data table and whose address is adjacent to the address of the second memory block. The second data table stores the allocation count information of the memory block.

[0021] In a third aspect, an embodiment of the present application provides an electronic device, comprising: a memory for storing a computer program, the computer program including program instructions; a processor for executing the program instructions so that the electronic device executes the memory management method provided by the above-mentioned first aspect and / or any possible implementation of the first aspect.

[0022] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, which stores a computer program. The computer program includes program instructions, and the program instructions are executed by an electronic device to execute the memory management method provided by the above-mentioned first aspect and / or any possible implementation of the first aspect.

[0023] In a fifth aspect, an embodiment of the present application provides a computer program product, including a computer program / instruction, which, when executed by a processor, implements the memory management method provided by the above-mentioned first aspect and / or any possible implementation method of the first aspect.

[0024] Beneficial effects of this application:

[0025] The memory management method provided by the present application, when receiving a memory application request, allocates a first memory block that is in an idle state and has available memory larger than the memory requested by the memory application request to the application corresponding to the memory application request, so that the allocated memory block can definitely meet the use of the application corresponding to the memory application request, avoiding the need to traverse the entire first data table to search for a memory block with available memory of the same size as the memory requested by the memory application request during the memory allocation process, and effectively reducing the time required for memory allocation.

[0026] When a memory release request is received, if it is determined from the second data table that the number of allocations of the second memory block corresponding to the memory release request is greater than a first threshold, it indicates that the second memory block is frequently used. Therefore, the memory of the second memory block is released, and the memory block information of the second memory block is stored in the first data table. The second memory block is not merged with other memory blocks so that the second memory block can be directly used the next time it is needed for allocation. This reduces the number of memory merges and splits, effectively reduces the generation of fragmented memory, and improves memory allocation efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0027] FIG1 is a schematic diagram showing a memory block header structure according to some implementations of the present application;

[0028] FIG2 is a flow chart illustrating a memory management method according to some implementations of the present application;

[0029] FIG3 is a schematic diagram illustrating dividing a memory into multiple memory blocks according to some implementations of the present application;

[0030] FIG4 is a schematic diagram illustrating a method of dividing a memory block and a spare memory block according to some implementations of the present application;

[0031] FIG5 is a flow chart illustrating another memory management method according to some implementations of the present application;

[0032] FIG6 is a schematic diagram illustrating a process of allocating a second memory after determining that multiple memory blocks corresponding to a first memory are allocated according to some implementations of the present application;

[0033] FIG7 is a schematic diagram showing a memory pool header structure according to some implementations of the present application;

[0034] FIG8 is a schematic diagram showing a memory management device according to some implementations of the present application;

[0035] FIG9 is a schematic structural diagram of an electronic device according to some implementations of the present application. DETAILED DESCRIPTION

[0036] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any modifications or substitutions that can be easily conceived by a person skilled in the art within the technical scope disclosed in the present invention should be included in the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be based on the scope of protection of the claims.

[0037] The technical solution of this application will be described in further detail below with reference to the accompanying drawings.

[0038] As mentioned above, the memory allocation of existing real-time systems has the problem of uncertain memory allocation time, that is, sometimes memory allocation takes too long, and a lot of fragmented memory is generated, resulting in memory waste.

[0039] Based on this, the present application provides a memory management method, which can adjust the size of memory blocks and the splitting and merging strategies of memory blocks in real time according to the usage of memory blocks during the memory allocation process, effectively reducing the generation of fragmented memory, improving memory allocation efficiency, and adopting a hierarchical allocation method to effectively reduce the time required for memory allocation.

[0040] Next, referring to Figures 1 to 7 , the implementation process and advantages of the memory management method of the present application are described in detail.

[0041] In one implementation of the present application, the memory management method includes:

[0042] When a memory application request is received, a first memory block is determined based on the first data table, and the first memory block is allocated to an application corresponding to the memory application request.

[0043] Among them, the first memory block is a memory block that is in an idle state and whose available memory is greater than the memory requested by the memory application request, determined based on the first data table. The first data table stores memory block information of the memory block in an idle state, and the memory block information includes available memory information and address information.

[0044] The available memory information (i.e., the size of the available memory) refers to the maximum capacity of data that the current memory block can store or process. The address information is used to identify the memory block. The memory block information in the first data table can be used to accurately locate the memory block required by the memory application request.

[0045] It should be noted that the first memory block is in an idle state and has available memory greater than the memory requested by the memory application request. In this way, the allocated memory block can satisfy the application usage corresponding to the memory application request, effectively reducing the time required for memory allocation.

[0046] In one implementation of the present application, in order to make the memory size of the first memory block meet the memory requirements without causing memory waste, the first memory block with the closest available memory and larger than the memory requested by the application can be selected. 3 , and the available memory corresponding to the memory block whose available memory is larger than the memory requested by the application is 2 3 +4,2 3 +8,2 4 , 2 5 , then the preferred option is to use 2 3 The memory block with +4 is used as the first memory block.

[0047] In one implementation of the present application, a memory block includes a memory header structure and available memory, and the memory block information in the first data table also includes the memory header structure. The first data table can also be called a free memory linked list, which is used to manage the headers of free memory blocks (i.e., memory block information), thereby simplifying the process of finding and locating free memory blocks.

[0048] In one implementation of the present application, as shown in Figure 1, the memory header structure includes: a front memory block header address, a rear memory block header address, a memory block size (i.e., usable memory information), a memory block status, a memory block address, a front memory block header address of the same size, and a rear memory block header address of the same size.

[0049] When a memory release request is received, the allocation count of the second memory block corresponding to the memory release request is determined based on the second data table; when the allocation count is greater than or equal to a first threshold, the memory of the second memory block is released, and the memory block information of the second memory block is stored in the first data table; when the allocation count is less than the first threshold, the memory of the second memory block is released, the second memory block and the third memory block are merged to obtain a fourth memory block, and the memory block information of the fourth memory block is stored in the first data table, the third memory block is a memory block that is in an idle state determined based on the first data table and whose address is adjacent to the address of the second memory block, and the second data table stores the memory block allocation count information.

[0050] It should be noted that the memory block in this application is obtained by first allocating the first memory (the first memory may also be referred to as the first memory pool) according to the memory acquisition request after receiving the memory acquisition request, and then dividing the first memory. Therefore, when a memory release request is received, the memory of the memory block is released, and the memory of the memory block can be released to the first memory (i.e., the first memory pool) rather than to the system.

[0051] The first threshold may be 1 or a value greater than 1, and this application does not impose any specific limitation.

[0052] Among them, storing the memory block information of the second memory block in the first data table can be inserting the memory block information of the second memory block to the end of the first data table, or inserting it to the storage location of the memory block information in the first data table when the second memory block is in an idle state, or other locations, which is not limited here.

[0053] When the second memory block and the third memory block are combined to obtain the fourth memory block, the third memory block can be a memory block in an idle state and having a memory block address identical to the front memory block address of the second memory block, or the third memory block can be a memory block in an idle state and having a memory block address identical to the back memory block address of the second memory block. The storage location of the memory block information of the fourth memory block can be the storage location of the memory block information of the second memory block in the first data table, or the storage location of the memory block information of the third memory block in the first data table, or the storage location of the memory block information of the fourth memory block in the first data table can be re-determined.

[0054] The memory management method, when receiving a memory application request, allocates a first memory block that is in an idle state and has available memory larger than the memory requested by the memory application request to an application corresponding to the memory application request, so that the allocated memory block can definitely meet the needs of the application corresponding to the memory application request, avoiding the need to traverse the entire first data table to search for a memory block with available memory of the same size as the memory requested by the memory application request during the memory allocation process, which consumes a lot of time, and effectively reduces the time required for memory allocation.

[0055] When a memory release request is received, if it is determined from the second data table that the number of allocations of the second memory block corresponding to the memory release request is greater than a first threshold, it indicates that the second memory block is frequently used. Therefore, the memory of the second memory block is released, and the memory block information of the second memory block is stored in the first data table. The second memory block is not merged with other memory blocks so that the second memory block can be directly used the next time it is needed for allocation. This reduces the number of memory merges and splits, effectively reduces the generation of fragmented memory, and improves memory allocation efficiency.

[0056] Before allocating and releasing memory blocks, a large memory block needs to be divided into multiple memory blocks according to the received memory acquisition request. In one implementation of the present application, the method includes the following steps:

[0057] S100: Receive a memory acquisition request.

[0058] S200: Allocate a first memory according to the memory acquisition request.

[0059] S300: Divide a first memory into a plurality of memory blocks according to a first preset rule.

[0060] S400: Obtain a first data table based on memory block information of a plurality of memory blocks, and obtain a second data table based on allocation information of the plurality of memory blocks, wherein the allocation information includes allocation count information.

[0061] Generally, when an electronic device's system executes an application, it dynamically allocates a certain amount of memory space for the application to use. During the first memory allocation, the size of the first memory corresponding to the application is pre-set. Upon receiving a memory acquisition request, the first memory is allocated to the application based on the pre-set value.

[0062] In one implementation of the present application, the first memory is divided into multiple memory blocks according to a first preset rule, as shown in FIG3 and FIG4 , including:

[0063] According to the first-level index, the first memory is roughly divided into powers of 2, namely: 2^5, 2^6, ..., 2^31. Each memory block corresponds to a bit in the bitmap mark (ie, bitmap).

[0064] Each memory block obtained by the coarse segmentation is then subdivided into the specified intervals based on the secondary index. Assuming the number of intervals is configured to be 8, each memory block obtained by the coarse segmentation is evenly divided into 8 intervals. For example, a memory block of size 2^5 to 2^6 is divided into the intervals (32-35), (36-39), (40-43), (44-47), (48-51), (52-55), (56-59), and (60-63), each of which corresponds to a bit in the bitmap.

[0065] Create an index linked list through the primary index and the secondary index. When the corresponding memory block in the index linked list is free, the position in the corresponding bitmap is set to 1, otherwise it is set to 0.

[0066] After the first memory is segmented using the primary index and the secondary index, multiple memory blocks are obtained, including memory block headers and available memory blocks, and then the memory block information of the memory blocks is stored in the first data table. It should be noted that the memory block information stored in the first data table is all memory block information of free memory blocks. When any memory block is allocated, the memory block information corresponding to the memory block is deleted from the first data table. After the memory of the memory block is released, the corresponding memory block information will be stored in the first data table again. The first data table determines whether the memory block is free based on the bitmap value corresponding to the memory block in the index linked list.

[0067] In one implementation of the present application, only when the bitmaps of the primary index and the secondary index corresponding to the memory block are both 1, it indicates that the memory block is in an idle state.

[0068] In one implementation of the present application, after allocating the first memory block to the application corresponding to the memory request, the method further includes:

[0069] A backup memory block for the first memory block is determined.

[0070] When the available memory of the spare memory block is the same as the available memory of the first memory block, the memory block information of the spare memory block is stored in a storage location corresponding to the memory block information of the first memory block in the first data table.

[0071] When the usable memory of the spare memory block is greater than the usable memory of the first memory block, an eighth memory block having the same usable memory as the first memory block is obtained by dividing the spare memory block, and the memory block information of the eighth memory block is stored in the storage location corresponding to the memory block information of the first memory block in the first data table.

[0072] Specifically, as shown in Figure 4, in one implementation of the present application, after the first memory is roughly divided into multiple memory blocks according to the first-level index, a spare memory block is also allocated to each memory block. It should be noted that the number of spare memory blocks corresponding to each memory block can be one, two, three, or can be set as needed. The size of the available memory of the spare memory block corresponding to each memory block can be the same as or different from the available memory size of the memory block. The memory blocks corresponding to 0-7 in Figure 4 are the memory blocks obtained by subdividing the memory blocks obtained by segmenting the first-level index through the secondary index (the segmentation interval of 8 is used as an example in the figure).

[0073] If the available memory of the spare memory block corresponding to a memory block is greater than the available memory of the memory block, the spare memory block needs to be divided to obtain an eighth memory block with the same available memory as the first memory block. In this case, if the available memory of the spare memory block corresponding to the memory block is a multiple of the available memory of the memory block, the spare memory block can be divided into multiple eighth memory blocks. If the available memory of the spare memory block corresponding to the memory block is not a multiple of the available memory of the memory block, the second memory block is divided from the spare memory block, and the remaining portion of the spare memory block is then divided into other memory blocks.

[0074] In one implementation of the present application, the available memory of the spare memory block corresponding to the memory block obtained by segmenting the first-level index is a multiple of the available memory of the memory block. For example, the available memory corresponding to the memory block obtained by segmenting the first-level index is 2 6, the available memory of the allocated spare memory block is 2 6 *n, where n is an integer. When n is 4, the available memory of the spare memory block is 256. After a memory block with a usable memory size of 64 is allocated, a memory block with a usable memory size of 64 (i.e., the eighth memory block) is separated from the spare memory block, and the memory block information of the memory block is stored in the first data table.

[0075] In one implementation of the present application, as shown in FIG5 , the memory management method includes the following steps:

[0076] S100': receiving a memory acquisition request.

[0077] S200': Allocate a first memory according to the memory acquisition request.

[0078] S300': dividing the first memory into a plurality of memory blocks according to a first preset rule.

[0079] S400 ′: Obtain a first data table and a second data table based on memory block information of a plurality of memory blocks.

[0080] S500': When a memory application request is received, a first memory block is determined based on a first data table, and the first memory block is allocated to an application corresponding to the memory application request, wherein the first memory block is a memory block that is in an idle state and has available memory greater than the memory requested by the memory application request and is determined based on the first data table. The first data table stores memory block information of the memory block in an idle state, and the memory block information includes available memory information and address information.

[0081] When a memory release request is received, the allocation count of the second memory block corresponding to the memory release request is determined based on the second data table; when the allocation count is greater than or equal to a first threshold, the memory of the second memory block is released, and the memory block information of the second memory block is stored in the first data table; when the allocation count is less than the first threshold, the memory of the second memory block is released, the second memory block and the third memory block are merged to obtain a fourth memory block, and the memory block information of the fourth memory block is stored in the first data table, the third memory block is a memory block that is in an idle state determined based on the first data table and whose address is adjacent to the address of the second memory block, and the second data table stores the memory block allocation count information.

[0082] In one implementation of the present application, when a memory application request is received, if it is determined that multiple memory blocks corresponding to the first memory have been allocated, as shown in FIG6 , the method further includes the following steps:

[0083] S510: Allocate a second memory.

[0084] S520: Determine, according to the second data table, memory blocks whose allocation times are greater than a third threshold, and determine that the available memory of the memory blocks whose allocation times are greater than the third threshold is a second value.

[0085] S530: Determine a first number of memory blocks having a second value of available memory in the memory blocks corresponding to the first memory.

[0086] S540: Divide the second memory into multiple memory blocks according to the second value and the first quantity to obtain a second number of ninth memory blocks, where the available memory of the ninth memory block is the second value, and the second number is greater than the first number.

[0087] The size of the second memory can be the same as or different from the size of the first memory. The third threshold is a pre-set value used to measure the frequency of use of the memory block. When the number of allocations of the memory block whose available memory size is the second value is greater than the third threshold, it indicates that the usage rate of this memory block is high during the current memory usage. Therefore, when the second memory is divided, some ninth memory blocks whose available memory size is the second value will be allocated for subsequent allocation and use, thereby improving the efficiency of memory allocation.

[0088] Specifically, the first memory and the second memory can also be referred to as the first memory pool and the second memory pool. In order to record the information of the memory blocks obtained by dividing the memory pool and the information of the extended memory pool, as shown in Figure 7, each memory pool is also provided with a corresponding memory pool header structure. The memory pool header structure includes the extended memory pool address, a two-dimensional index linked list, a free memory block array, and a memory block allocation record table. Among them, for the first memory, its corresponding extended memory pool address refers to the address of the second memory, the two-dimensional index linked list refers to the table composed of the above-mentioned first-level index and second-level index and the corresponding bitmap value, the free memory block array is also the above-mentioned first data table, and the memory block allocation record table is also the above-mentioned second data table.

[0089] In one implementation of the present application, the method also includes: when it is determined based on the second data table that the allocation count of the fifth memory block is greater than or equal to a second threshold, dividing the sixth memory block to obtain a seventh memory block, the sixth memory block is a memory block that is in an idle state determined based on the first data table and whose usable memory is greater than the usable memory of the fifth memory block, and the usable memory size of the seventh memory block is the same as the usable memory size of the fifth memory block.

[0090] The second threshold is a pre-set value, and the specific size is not limited in this application. The second threshold is a calibration value for the usage of the memory block, representing the frequency of use of a memory block of a certain size. Therefore, in one implementation of this application, the method further includes: within a first preset time, if it is determined based on the second data table that the number of allocations of the fifth memory block is greater than or equal to the second threshold, then dividing the sixth memory block to obtain a seventh memory block.

[0091] The first preset time may be 5 minutes, 6 minutes, 8 minutes or other preset time.

[0092] Among them, the sixth memory block is a memory block that is in an idle state and whose available memory is greater than the available memory of the fifth memory block. The available memory of the sixth memory block can be a multiple of the available memory of the fifth memory block, or it can be a non-multiple relationship with the available memory of the fifth memory block. When the available memory of the sixth memory block can be a multiple of the available memory of the fifth memory block, the sixth memory block can be divided into a plurality of memory blocks with the same available memory and a smaller number of the available memory of the fifth memory block. When the available memory of the sixth memory block is a non-multiple relationship with the available memory of the fifth memory block, the sixth memory block is divided to obtain the seventh memory block, and then the remaining part of the sixth memory block is divided into other memory blocks. Furthermore, in order to ensure that the sixth memory block has no impact on subsequent memory use after being divided, the sixth memory block with a lower usage frequency will be selected for division.

[0093] In one implementation of the present application, the memory management method provided by the present application can also be called a dynamic memory allocation method suitable for real-time systems. A free memory linked list (i.e., the first data table) is used to manage the headers of free memory blocks (i.e., memory block information), thereby simplifying the process of finding and locating free memory blocks.

[0094] An array (i.e., the second data table) is used to record the most recently allocated memory blocks, including the size of the memory block and the number of times that size has been allocated. Based on the memory allocation record table, a self-adjusting mechanism for increasing high-frequency memory blocks is provided to improve allocation efficiency in real-time systems.

[0095] A spare memory block mechanism is provided to fill the allocated memory blocks in time and improve the efficiency of memory allocation in real-time systems.

[0096] A memory self-learning initialization mechanism is provided to improve the efficiency of memory allocation in real-time systems.

[0097] The upgraded allocation method is used, that is, when applying for memory of a certain size, the memory is first checked to see if there is any free space in the interval one level larger than the size, which ensures the certainty of the allocation time.

[0098] The split and merge operations are adjusted according to the allocation frequency. That is, when releasing memory, if there is an allocation record of memory of this size in the most recent allocation record, it will not be merged first and will be directly put into the free list, thus reducing memory fragmentation.

[0099] Please refer to FIG8 , which shows a memory management device of the present application, which is used to execute the memory management method described above. The device includes:

[0100] The first processing module is used to determine a first memory block upon receiving a memory application request, and allocate the first memory block to an application corresponding to the memory application request, wherein the first memory block is a memory block that is in an idle state and has available memory greater than the memory requested by the memory application request, as determined based on a first data table. The first data table stores information on memory blocks in an idle state, and the memory block information includes available memory information and address information.

[0101] The second processing module is used to determine the allocation count of the second memory block corresponding to the memory release request through the second data table when a memory release request is received; if the allocation count is greater than or equal to the first threshold, release the memory of the second memory block and store the memory block information of the second memory block in the first data table; if the allocation count is less than the first threshold, release the memory of the second memory block, merge the second memory block and the third memory block to obtain a fourth memory block, and store the memory block information of the fourth memory block in the first data table, where the third memory block is a memory block that is in an idle state determined based on the first data table and whose address is adjacent to the address of the second memory block, and the second data table stores the allocation information of the memory block.

[0102] The specific operations that each processing module can perform are shown in the memory management method corresponding to Figure 2. Furthermore, according to the specific operation steps of the memory management method, the data processing device may include more or fewer processing modules for processing the contents of the memory management method.

[0103] Please refer to Figure 9, which shows a structural block diagram of an electronic device provided by an implementation of the present application. The electronic device may include one or more processors 1002, system control logic 1008 connected to at least one of the processors 1002, system memory 1004 connected to the system control logic 1008, non-volatile memory (NVM) 1006 connected to the system control logic 1008, and a network interface 1010 connected to the system control logic 1008.

[0104] The processor 1002 may include one or more single-core or multi-core processors. The processor 1002 may include any combination of general-purpose processors and specialized processors (e.g., graphics processors, application processors, baseband processors, etc.). In the implementation of this document, the processor 1002 may be configured to execute the aforementioned memory management method.

[0105] In some implementations, the system control logic 1008 may include any suitable interface controller to provide any suitable interface to at least one of the processors 1002 and / or any suitable device or component in communication with the system control logic 1008 .

[0106] In some implementations, the system control logic 1008 may include one or more memory controllers to provide an interface to the system memory 1004. The system memory 1004 may be used to load and store data and / or instructions. In some implementations, the system memory 1004 of the electronic device may include any suitable volatile memory, such as a suitable dynamic random access memory (DRAM).

[0107] NVM / memory 1006 may include one or more tangible, non-transitory computer-readable media for storing data and / or instructions. In some implementations, NVM / memory 1006 may include any suitable non-volatile memory such as flash memory and / or any suitable non-volatile storage device, such as at least one of a hard disk drive (HDD), a compact disc (CD) drive, and a digital versatile disc (DVD) drive.

[0108] NVM / storage 1006 may include a portion of storage resources installed on a device of an electronic device, or it may be accessible by the device but not necessarily a part of the device. For example, NVM / storage 1006 may be accessed over a network via network interface 1010 .

[0109] In particular, system memory 1004 and NVM / storage 1006 may include, respectively, a temporary copy and a permanent copy of instructions 1020. Instructions 1020 may include instructions that, when executed by at least one of processors 1002, cause the electronic device to implement the aforementioned memory management method. In some implementations, instructions 1020, hardware, firmware, and / or software components thereof may additionally or alternatively be located in system control logic 1008, network interface 1010, and / or processor 1002.

[0110] The network interface 1010 may include a transceiver for providing a radio interface for the electronic device, thereby communicating with any other suitable device (such as a front-end module, an antenna, etc.) via one or more networks. In some implementations, the network interface 1010 may be integrated into other components of the electronic device. For example, the network interface 1010 may be integrated into at least one of the processor 1002, the system memory 1004, the NVM / storage 1006, and a firmware device (not shown) having instructions. When at least one of the processors 1002 executes the instructions, the electronic device implements the aforementioned memory management method.

[0111] The network interface 1010 may further include any suitable hardware and / or firmware to provide a multiple-input multiple-output radio interface. For example, the network interface 1010 may be a network adapter, a wireless network adapter, a telephone modem, and / or a wireless modem.

[0112] In one implementation, at least one of the processors 1002 may be packaged with logic for one or more controllers of the system control logic 1008 to form a System in a Package (SiP). In one implementation, at least one of the processors 1002 may be integrated with logic for one or more controllers of the system control logic 1008 on the same die to form a System on Chip (SoC).

[0113] The electronic device may further include an input / output (I / O) device 1012. The I / O device 1012 may include a user interface to enable a user to interact with the electronic device; peripheral component interfaces may also be designed to enable peripheral components to interact with the electronic device. In some implementations, the electronic device may further include a sensor for determining at least one of environmental conditions and location information related to the electronic device.

[0114] In some implementations, the user interface may include, but is not limited to, a display (e.g., an LCD display, a touch screen display, etc.), a speaker, a microphone, one or more cameras (e.g., a still image camera and / or a video camera), a flashlight (e.g., an LED flash), and a keyboard.

[0115] In some implementations, the peripheral component interface can include, but is not limited to, a non-volatile memory port, an audio jack, and a power interface.

[0116] In some implementations, the sensors may include, but are not limited to, a gyroscope sensor, an accelerometer, a proximity sensor, an ambient light sensor, and a positioning unit. The positioning unit may also be part of or interact with the network interface 1010 to communicate with components of a positioning network (e.g., Global Positioning System (GPS) satellites).

[0117] It should be understood that the structures illustrated in the implementations of the present invention do not constitute specific limitations on the electronic device. In other implementations of the present application, the electronic device may include more or fewer components than shown, or may combine or separate certain components, or have different component arrangements. The illustrated components may be implemented in hardware, software, or a combination of software and hardware.

[0118] Program code can be applied to input instructions to perform the functions described herein and generate output information. The output information can be applied to one or more output devices in a known manner. For the purposes of the present application, a processing system includes any system having a processor such as, for example, a digital signal processor (DSP), a microcontroller, an application specific integrated circuit (ASIC), or a microprocessor.

[0119] Program code can be implemented with a high-level programming language or an object-oriented programming language to communicate with the processing system. Where necessary, program code can also be implemented with assembly language or machine language. In fact, the mechanism described herein is not limited to the scope of any particular programming language. In either case, the language can be a compiled language or an interpreted language.

[0120] One or more aspects of at least one implementation may be implemented by representative instructions stored on a computer-readable storage medium that represent various logic within a processor, which, when read by a machine, causes the machine to fabricate logic for performing the techniques described herein. These representations, known as "IP cores," may be stored on a tangible, computer-readable storage medium and supplied to various customers or manufacturing facilities to load into fabrication machines that actually make the logic or processor.

[0121] It should be noted that in the accompanying drawings, some structural or method features may be shown in a specific arrangement and / or order. However, it should be understood that such a specific arrangement and / or order may not be required. Rather, in some implementations, these features may be arranged in a manner and / or order different from that shown in the illustrative drawings. In addition, the inclusion of structural or method features in a particular figure does not imply that such features are required in all implementations, and in some implementations, these features may not be included or may be combined with other features.

[0122] It should be noted that the terms "first", "second", etc. are only used for distinction and description, and cannot be understood as indicating or implying relative importance.

[0123] It should be noted that in the accompanying drawings, some structural or method features may be shown in a specific arrangement and / or order. However, it should be understood that such a specific arrangement and / or order may not be required. Rather, in some embodiments, these features may be arranged in a manner and / or order different from that shown in the illustrative drawings. In addition, the inclusion of structural or method features in a particular figure does not imply that such features are required in all embodiments, and in some embodiments, these features may not be included or may be combined with other features.

[0124] Although the present application has been illustrated and described with reference to certain preferred embodiments of the present application, those skilled in the art will appreciate that the above descriptions are provided as further details of the present application in conjunction with specific embodiments, and that the specific implementation of the present application should not be limited to these descriptions. Those skilled in the art may make various changes in form and detail, including simple deductions or substitutions, without departing from the spirit and scope of the present application.

Claims

1. A memory management method, characterized in that: The method comprises: In the case of receiving a memory application request, determining a first memory block based on a first data table, and allocating the first memory block to an application corresponding to the memory application request, wherein the first memory block is a memory block in an idle state determined based on the first data table and whose available memory is greater than the memory requested by the memory application request, and the first data table stores memory block information of the memory block in an idle state, wherein the memory block information includes available memory information and address information; When a memory release request is received, the allocation count of a second memory block corresponding to the memory release request is determined based on a second data table; when the allocation count is greater than or equal to a first threshold, the memory of the second memory block is released, and the memory block information of the second memory block is stored in the first data table; when the allocation count is less than the first threshold, the memory of the second memory block is released, the second memory block is merged with the third memory block to obtain a fourth memory block, and the memory block information of the fourth memory block is stored in the first data table, the third memory block is a memory block that is in an idle state determined based on the first data table and whose address is adjacent to the address of the second memory block, and the second data table stores the memory block allocation count information.

2. The memory management method according to claim 1, characterized in that: The method further comprises: When it is determined based on the second data table that the allocation count of the fifth memory block is greater than or equal to the second threshold, the sixth memory block is divided to obtain a seventh memory block, the sixth memory block is a memory block that is in an idle state determined based on the first data table and whose available memory is greater than the available memory of the fifth memory block, and the available memory of the seventh memory block is the same as the available memory of the fifth memory block.

3. The memory management method according to claim 1 or 2, characterized in that: The method further comprises: Receive memory acquisition request; Allocate a first memory according to the memory acquisition request; Dividing the first memory into a plurality of memory blocks according to a first preset rule; The first data table is obtained based on the memory block information of the multiple memory blocks, and the second data table is obtained based on the allocation information of the multiple memory blocks, where the allocation information includes the allocation count information.

4. The memory management method according to claim 3, characterized in that: After allocating the first memory block to the application corresponding to the memory application request, the method further includes: determining a spare memory block for the first memory block; When the available memory of the spare memory block is the same as the available memory of the first memory block, storing the memory block information of the spare memory block in the storage location corresponding to the memory block information of the first memory block in the first data table; In the case where the usable memory of the backup memory block is greater than the usable memory of the first memory block, an eighth memory block whose usable memory is the same as that of the first memory block is obtained by dividing the backup memory block, and the memory block information of the eighth memory block is stored in the storage location corresponding to the memory block information of the first memory block in the first data table.

5. The memory management method according to claim 4, characterized in that: In the case of receiving the memory application request, if it is determined that the plurality of memory blocks corresponding to the first memory are allocated, the method further includes: allocating a second memory; and Determine the memory block whose allocation times are greater than the third threshold value according to the second data table, and determine the allocation times The available memory of the memory blocks whose number is greater than the third threshold is a second value; Determine a first number of memory blocks having usable memory of the second value in the memory blocks corresponding to the first memory; The second memory is divided into a plurality of memory blocks according to the second value and the first quantity to obtain a second number of ninth memory blocks, the usable memory of the ninth memory block is the second value, and the second number is greater than the first number.

6. The memory management method according to claim 5, characterized in that: The memory block includes a memory header structure and available memory, and the memory block information also includes the memory header structure.

7. The memory management method according to claim 6, characterized in that: The memory header structure includes: a front memory block header address, a rear memory block header address, a memory block size, a memory block status, a memory block address, a front same-size memory block header address, and a rear same-size memory block header address.

8. A memory management device, characterized in that: The device comprises: A first processing module is used for, when receiving a memory application request, determining a first memory block based on a first data table, and allocating the first memory block to an application corresponding to the memory application request, wherein the first memory block is a memory block in an idle state determined based on the first data table and whose available memory is greater than the memory requested by the memory application request, and the first data table stores memory block information of the memory block in an idle state, wherein the memory block information includes available memory information and address information; A second processing module is used to determine the allocation count of a second memory block corresponding to the memory release request based on a second data table when a memory release request is received, release the memory of the second memory block when the allocation count is greater than or equal to a first threshold, and store the memory block information of the second memory block in the first data table, release the memory of the second memory block when the allocation count is less than the first threshold, merge the second memory block and the third memory block to obtain a fourth memory block, and store the memory block information of the fourth memory block in the first data table, wherein the third memory block is a memory block that is in an idle state determined based on the first data table and whose address is adjacent to the address of the second memory block, and the second data table stores the memory block allocation count information.

9. An electronic device, characterized in that: include: A memory for storing a computer program, wherein the computer program includes program instructions; A processor is used to execute the program instructions so that the electronic device executes the memory management method as described in any one of claims 1-7.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, wherein the computer program includes program instructions, and the program instructions are executed by an electronic device to enable the electronic device to execute the memory management method according to any one of claims 1 to 7.

Citation Information

Patent Citations

  • Memory management method, system and device for embedded real-time system based on TLSF

    CN103885888A

  • Self-adaptive real-time memory allocation method and device, equipment and storage medium

    CN116501496A

  • Memory management method and device, electronic equipment and storage medium

    CN117130949A

  • Computer resource allocation system and method thereof

    US20070160080A1

Cited By

  • Memory allocation method and device, electronic equipment and storage medium

    CN120371724A

  • Memory allocation method, device, electronic device and storage medium

    CN120371724B