Memory release method and device, computer equipment and storage medium
By traversing the memory segment linked list in luajit's memory allocator and releasing free memory blocks, the problem that traditional operating systems cannot efficiently recycle memory is solved, efficient memory recycling and utilization is achieved, and memory waste is reduced.
Patent Information
- Application Number
- CN202411772473.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-04
- Publication Date
- 2025-02-28
AI Technical Summary
Under the memory allocator characteristics of the luajit scripting language, traditional operating systems cannot efficiently recycle memory, resulting in the system's idle memory being unable to be efficiently utilized and memory waste.
When the memory free rate is greater than the first preset threshold and the sum of free memory is greater than the second preset threshold, the memory segments are traversed, and the memory segments with free memory blocks are identified and released, and the free memory is released to the operating system.
It realizes efficient memory recycling when a specific threshold condition is reached, reduces the memory idle rate, improves memory recycling efficiency, enables free memory to be efficiently utilized, and reduces memory waste.
Smart Images

Figure CN119537026A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a memory release method, device, computer equipment, storage medium and computer program product. Background Art
[0002] When using the luajit scripting language, due to the memory allocator characteristics of luajit, the memory segment will be released to the operating system only when all the memory on the memory segment is free, that is, when there is only one free memory block. During business use, after luajit applies for the required maximum memory, it always maintains the required maximum memory and does not release the memory. For example, under normal circumstances, only 100M of memory is needed, and at a certain moment, an additional 900M is needed, and a total of 1000M of memory is required, corresponding to the memory blocks on two 500M memory segments. When the use is over, 900M of memory is released, and 100M of memory remains. However, at this time, the 100M used memory corresponds to a 50M used memory block on each of the two memory segments. At this time, the two 500M memory segments will not return the memory to the operating system, resulting in 1000M of occupied memory, but only 100M is actually used, and the memory usage rate is only 10%, which is a high waste of memory.
[0003] Therefore, the operating system in the traditional method cannot efficiently reclaim memory, resulting in the problem that the system's idle memory cannot be efficiently utilized. Summary of the invention
[0004] Based on this, it is necessary to provide a memory release method, device, computer equipment, computer-readable storage medium and computer program product that can efficiently recycle memory in response to the above technical problems.
[0005] A memory release method, comprising:
[0006] When the memory idle rate is greater than a first preset threshold and the total free memory is greater than a second preset threshold, traverse each memory segment in the memory segment linked list and use a memory segment with at least one free memory block as a target memory segment;
[0007] According to the preset free memory block release rule, a free memory block release operation is performed on each target memory segment, and a memory segment splitting result of each target memory segment after the free memory block release operation is performed is obtained;
[0008] For any target memory segment, according to the memory segment splitting result of the target memory segment, each sub-memory segment corresponding to the target memory segment is linked through a linked list to obtain the sub-memory segment link information corresponding to the target memory segment;
[0009] The memory segment link information corresponding to each target memory segment in the memory segment linked list is replaced with the sub-memory segment link information corresponding to each target memory segment, so as to release the free memory of each target memory segment to the operating system.
[0010] In one embodiment, the method further comprises:
[0011] Traverse the memory segment linked list to obtain the memory size of each memory segment, and obtain the free memory size of each memory segment; the free memory size on the memory segment is obtained by adding the memory sizes of each free memory block on the memory segment;
[0012] Determine the total memory amount according to the memory size of each memory segment, and determine the total free memory amount according to the free memory size of each memory segment;
[0013] Determine the memory idle rate based on the total memory and the total free memory.
[0014] In one embodiment, according to a preset free memory block release rule, a free memory block release operation is performed on each target memory segment, including:
[0015] When the first memory block on the target memory segment is a free memory block, determine the first address of the target memory segment and the last address of the first memory block;
[0016] According to the tail address of the first memory block and the first address of the target memory segment, determine the tail address of the memory block to be released;
[0017] When the memory length obtained by subtracting the first address of the target memory segment from the tail address of the memory block to be released is greater than the preset memory page length, the memory release function is called to release the memory block to be released.
[0018] In one embodiment, determining the tail address of the memory block to be released according to the tail address of the first memory block and the head address of the target memory segment includes:
[0019] Subtract the first address of the target memory segment from the tail address of the first memory block, and then perform memory alignment according to the preset memory page length to obtain the initial tail address of the memory block to be released;
[0020] When the length obtained by subtracting the initial tail address of the memory block to be released from the tail address of the first memory block is greater than 0 and less than the length of the minimum block, the result obtained by subtracting the preset memory page length from the initial tail address is used as the tail address of the memory block to be released.
[0021] In one embodiment, according to a preset free memory block release rule, a free memory block release operation is performed on each target memory segment, including:
[0022] When the first memory block on the target memory segment is not a free memory block, determining the first free memory block of the target memory segment;
[0023] Determine the first address of the target memory segment, the last address of the target memory segment, the first address of the first free memory block, and the last address of the first free memory block;
[0024] Determine the first address of the memory block to be released according to the first address of the first free memory block and the first address of the target memory segment, and determine the tail address of the memory block to be released according to the tail address of the first free memory block and the first address of the target memory segment;
[0025] When the length obtained by subtracting the head address of the memory block to be released from the tail address of the memory block to be released is greater than the preset memory page length, the memory release function is called to release the memory block to be released.
[0026] In one embodiment, determining the first address of the memory block to be released according to the first address of the first free memory block and the first address of the target memory segment includes:
[0027] Subtract the first address of the target memory segment from the first address of the first free memory block, perform memory alignment according to the preset memory page length, and then add the first address of the target memory segment to obtain the initial first address of the memory block to be released;
[0028] Subtracting the preset hidden segment length and the first address of the first free memory block from the initial first address of the memory block to be released to obtain a first length;
[0029] When the first length is greater than 0 and less than the length of the minimum block, the result obtained by adding the initial first address to the preset memory page length is used as the first address of the memory block to be released.
[0030] In one embodiment, determining the tail address of the memory block to be released according to the tail address of the first free memory block and the head address of the target memory segment includes:
[0031] Subtract the first address of the target memory segment from the tail address of the first free memory block, perform memory alignment according to the preset memory page length, and then add the first address of the target memory segment to obtain the initial tail address of the memory block to be released;
[0032] Subtract the initial tail address of the memory block to be released from the tail address of the first free memory block to obtain the second length;
[0033] When the second length is greater than 0 and less than the length of the minimum block, a result obtained by subtracting a preset memory page length from the initial tail address of the memory block to be released is used as the tail address of the memory block to be released.
[0034] A memory release device, comprising:
[0035] A traversal module, configured to traverse each memory segment in the memory segment linked list and select a memory segment having at least one free memory block as a target memory segment when the memory idle rate is greater than a first preset threshold and the total free memory is greater than a second preset threshold;
[0036] A release module is used to perform a free memory block release operation on each target memory segment according to a preset free memory block release rule, and obtain a memory segment splitting result of each target memory segment after performing the free memory block release operation;
[0037] A link module is used for linking the sub-memory segments corresponding to any target memory segment through a linked list according to the memory segment splitting result of the target memory segment, so as to obtain the sub-memory segment link information corresponding to the target memory segment;
[0038] The replacement module is used to replace the memory segment link information corresponding to each target memory segment in the memory segment linked list with the sub-memory segment link information corresponding to each target memory segment, so as to release the free memory of each target memory segment to the operating system.
[0039] A computer device includes a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of the above method when executing the computer program.
[0040] A computer-readable storage medium stores a computer program, which implements the steps of the above method when executed by a processor.
[0041] The above-mentioned memory release method, device, computer equipment, storage medium and computer program product, by traversing each memory segment in the memory segment linked list when the memory idle rate is greater than a first preset threshold value and the total idle memory is greater than a second preset threshold value, taking the memory segment with at least one idle memory block as the target memory segment; performing the idle memory block release operation on each target memory segment according to the preset idle memory block release rule, and obtaining the memory segment splitting result of each target memory segment after performing the idle memory block release operation; for any target memory segment, according to the memory segment splitting result of the target memory segment, linking each sub-memory segment corresponding to the target memory segment through a linked list to obtain the sub-memory segment link information corresponding to the target memory segment; replacing the memory segment link information corresponding to each target memory segment in the memory segment linked list with the sub-memory segment link information corresponding to each target memory segment, so as to release the idle memory of each target memory segment to the operating system; in this way, when both the memory idle rate and the total idle memory reach the threshold value, each memory segment in the memory segment linked list can be traversed, and even if there is used block memory on the memory segment, the idle block memory can be released to the operating system, thereby reducing the memory idle rate, improving the memory recycling efficiency, and making the idle memory be efficiently utilized. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the related technologies, the drawings required for use in the embodiments or the related technical descriptions are briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0043] FIG1 is an application environment diagram of a memory release method in one embodiment;
[0044] Figure 2 A schematic diagram of a flow chart of a memory release method in an embodiment;
[0045] Figure 3 A schematic diagram of memory block occupancy of a target memory segment in one embodiment;
[0046] Figure 4 A schematic diagram of a flow chart showing the memory block occupancy of a target memory segment in another embodiment;
[0047] Figure 5 A schematic diagram of a memory recycling result in one embodiment;
[0048] Figure 6 is a flow chart of a memory release method in another embodiment;
[0049] Figure 7 is a structural block diagram of a memory release device in one embodiment;
[0050] Figure 8 FIG. 4 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION
[0051] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0052] The memory release method of the present application can be applied to the memory recovery process of the luajit memory allocator. Luajit is a scripting language written in C language, using JIT real-time compilation technology, with the characteristics of light weight and high efficiency, can be easily called by C / C++ code, and is widely used in game development, web plug-in development and other fields.
[0053] When using luajit, due to the memory allocator characteristics of luajit, only when all the memory on the memory segment is free, that is, when there is only one free memory block, will the memory segment be released to the operating system. During business use, the following situation is found: after luajit applies for the required maximum memory, it always maintains the required maximum memory and does not release the memory. For example, under normal circumstances, only 100M of memory is needed, and at a certain moment, an additional 900M is needed, and a total of 1000M of memory is required, corresponding to the memory blocks on two 500M memory segments. When the use is over, 900M of memory is released, and 100M of memory remains. However, at this time, the 100M used memory corresponds to 50M used memory blocks on each of the two memory segments. At this time, the two 500M memory segments will not return the memory to the operating system. Luajit occupies 1000M of memory, and actually uses 100M. The memory usage rate is only 10%, which is a high waste of memory.
[0054] When the web server uses luajit to handle business, since the web server uses multiple processes to handle link requests according to the number of CPU cores, luajit is used on each process. On machines with more CPU cores, memory waste will be more serious. Furthermore, when the web server starts a new process to load a new configuration method, the web server will start a new batch of processes to handle new links, and let the old batch of processes wait for the client to close the old link before exiting. If the old link continues for a period of time, such as 12 hours, it will be forced to exit the old process. There will be a period of double process phenomenon, which will last up to 12 hours, which will double the machine's memory usage and may exhaust the machine's memory, causing the process to be killed by the operating system and affecting the business.
[0055] In actual applications, the luajit memory allocator mainly applies for memory through mmap, and will return the first address of the successful application. The returned size is returned as an integer multiple of the operating system page size on the machine. You can release the integer multiple of the operating system page size at any first address of this memory segment + the location of the integer multiple of the operating system page size. In this way, part of this memory segment is released. For example, if you apply for 128k memory, divide it into 3 segments of 100k, 20k, and 8k respectively. You can release the second segment of 20k first, and continue to use the first segment of 100k and the third segment of 8k.
[0056] The memory release method of this application allows the luajit memory allocator to reclaim the free block memory on the memory segment even if there is used block memory on the memory segment. Reduce the memory usage rate as much as possible. For example, when the memory occupied by luajit is 1000M, 100M is actually used, and 900M is idle, the native luajit cannot recycle it. The memory release method of this application recycles the idle 900M, and then reduces the memory idle rate from 90% to 0% as much as possible. Reduce memory exhaustion caused by using luajit.
[0057] The memory release method provided in the embodiment of the present application can be applied to the server 102 shown in FIG. 1 . The server 102 can be implemented as an independent server or a server cluster composed of multiple servers.
[0058] In an exemplary embodiment, Figure 2 As shown, a memory release method is provided, which is described by taking the method applied to the server 102 in FIG. 1 as an example, and includes the following steps S202 to S206. Among them:
[0059] Step S202, when the memory idle rate is greater than a first preset threshold and the total free memory is greater than a second preset threshold, traverse each memory segment in the memory segment linked list and use a memory segment with at least one free memory block as a target memory segment.
[0060] The memory idle rate may refer to the ratio of the idle memory to the total memory.
[0061] The total free memory may refer to the total of all free memory blocks.
[0062] Among them, the first preset threshold value can be a memory idle rate threshold value set for the memory idle rate, and the first preset threshold value is used to determine whether a corresponding memory release operation is required; the second preset threshold value can be a free memory sum threshold value set for the free memory sum, and the second preset threshold value is used to determine whether a corresponding memory release operation is required. In actual applications, when the memory idle rate reaches the first preset threshold value and the free memory sum reaches the second preset threshold value, memory recycling is triggered. In addition, when the process is in the exit state, memory recycling is also triggered.
[0063] The memory segment linked list is a linked list, and each node on the memory segment linked list points to a memory segment.
[0064] A memory segment refers to a section of memory requested from the operating system through mmap. The size of a memory segment is an integer multiple of the system operation page. It is divided into an available segment and a hidden segment. The hidden segment is at the end of the memory segment and is only 80 bytes on an x86 machine. The hidden segment stores the first address and segment length of the memory segment, as well as the memory segment address of the next node in the memory segment linked list.
[0065] In memory application, when the upper layer of Luajit applies for memory from the Luajit memory allocator, the Luajit memory allocator splits blocks of the required size from suitable free memory blocks, marks them as used blocks, and then returns the address of the used blocks to the upper layer. For example, a memory segment with a length of 128k currently has 120k free memory blocks and 8k used memory blocks. When the upper layer needs to use 20k of memory, the free memory block 120k is divided into 20k and the address of the 20k memory is returned to the upper layer. After the division, the memory is distributed into 120k free memory blocks, 20k used memory blocks, and 8k used memory blocks. If the required memory cannot find a suitable free block to split, a new memory segment is applied, the new memory segment is added to the memory segment linked list, and is split from the new free block on the new memory segment.
[0066] Among them, free memory blocks refer to unused memory blocks on the memory segment, which are memory blocks relative to used memory blocks. In actual applications, a memory segment may include free memory blocks and used memory blocks, which are obtained by dividing the memory segment. A newly applied memory segment initially has only one free memory block, and the free block size is equal to the available segment size in the memory segment.
[0067] The target memory segment refers to a memory segment where free memory blocks exist.
[0068] Optionally, taking the luajit memory allocator as an example, the luajit memory allocator is checked periodically by a timer. When the memory idle rate is greater than a first preset threshold and the total free memory is greater than a second preset threshold, the method of forced recovery of free memory blocks in the luajit memory allocator can be called to recover the free memory of luajit as much as possible. First, traverse the memory segments in the memory segment linked list and take the memory segment with free memory blocks as the target memory segment.
[0069] Step S204: according to the preset free memory block release rule, a free memory block release operation is performed on each target memory segment, and a memory segment splitting result of each target memory segment after the free memory block release operation is performed is obtained.
[0070] Among them, the preset free memory block release rule may refer to a pre-set memory recycling rule for the free memory block, which is used to determine whether the free memory block can be recycled, to determine how the free memory block should be recycled, and to determine how the memory segment linked list should be updated after the free memory block is recycled.
[0071] The free memory block release operation refers to an operation corresponding to memory recycling of the free memory blocks according to a preset free memory block release rule.
[0072] It should be noted that memory release means that when the luajit upper layer releases memory, the luajit memory allocator will find the corresponding used memory block according to the released address, mark the used memory block as a free memory block, and merge the adjacent free memory blocks into one free memory block. For example, a memory segment with a length of 128k may be divided into free memory block 100k, used memory block 20k and used memory block 8k. When the used memory block 20k is released, 20k and the previous free memory block will be merged to become free memory block 120k and used memory block 8k. This step only marks the released memory of the upper layer as free, and does not actually release the memory to the operating system. Memory segment release means that when the luajit upper layer releases memory to the luajit memory allocator for a certain number of times, the luajit memory allocator will traverse the memory segment linked list. If there is no used memory block on the available segment of the memory segment on the linked list, the memory segment will be removed from the memory segment linked list and released to the operating system. Regarding memory fragmentation, since the used memory blocks are not all on the same memory segment, the memory of the memory segment cannot be reclaimed. Assume that there are two memory segments, both of which are 128k, and the used memory blocks are 28k in total. There are two situations at this time. The first situation is that there are 100k free memory blocks on the first memory segment, 20k used memory blocks, 8k used memory blocks, and 128k free memory blocks on the second memory segment. In this case, the second memory block 128k can be reclaimed; the second situation is that there are 108k free memory blocks on the first memory segment and 20k used memory blocks, and there are 120k free memory blocks on the second memory segment and 8k used memory blocks. In this case, both memory segments cannot be reclaimed.
[0073] The memory splitting result of the target memory segment refers to the result obtained after the free memory blocks that can be reclaimed in the target memory segment are reclaimed.
[0074] Optionally, a free memory block release operation can be performed on each of the target memory segments according to a preset free memory block release rule, so that the free memory blocks in each of the target memory segments that meet the memory release conditions are released, and the memory segment splitting results of each target memory segment after the free memory blocks are released are obtained. For example, after the free memory blocks are released, the target memory segment a is split into sub-memory segment a1, sub-memory segment a2 and sub-memory segment a3, wherein the target memory segment a is split into three sub-memory segments because the memory between sub-memory segment a1 and sub-memory segment a2 is released, and the memory between sub-memory segment a2 and sub-memory segment a3 is released.
[0075] Step S206, for any target memory segment, according to the memory segment splitting result of the target memory segment, each sub-memory segment corresponding to the target memory segment is linked through a linked list to obtain sub-memory segment link information corresponding to the target memory segment.
[0076] The sub-memory segment corresponding to the target memory segment refers to a small memory segment obtained after the free memory blocks of the target memory segment are recycled and the remaining part of the target memory segment is split.
[0077] Optionally, when the free memory blocks of the target memory segment a are reclaimed, the target memory segment a is split into sub-memory segment a1, sub-memory segment a2, ..., sub-memory segment an, and the sub-memory segment a1, sub-memory segment a2, ..., sub-memory segment an are linked through a linked list to generate a memory segment linked list "memory segment a1->memory segment a2->...->memory segment an" corresponding to the target memory segment a.
[0078] Step S208: Replace the memory segment link information corresponding to each target memory segment in the memory segment linked list with the sub-memory segment link information corresponding to each target memory segment, so as to release the free memory of each target memory segment to the operating system.
[0079] Optionally, when the target memory segment a is split into sub-memory segment a1, sub-memory segment a2, ..., sub-memory segment an, the target memory segment b is split into sub-memory segment b1, sub-memory segment b2, ..., sub-memory segment bm, and the target memory segment c is split into sub-memory segment c1, sub-memory segment c2, ..., sub-memory segment cj, the original memory segment linked list "memory segment a->memory segment b->memory segment c" is replaced with "memory segment a1->memory segment a2->...->memory segment an->memory segment b1->memory segment b2->...->memory segment bm->memory segment c1->memory segment c2->...->memory segment cn".
[0080] In the above-mentioned memory release method, when the memory idle rate is greater than a first preset threshold value and the total amount of free memory is greater than a second preset threshold value, each memory segment in the memory segment linked list is traversed, and a memory segment with at least one free memory block is used as a target memory segment; according to a preset free memory block release rule, a free memory block release operation is performed on each target memory segment, and a memory segment splitting result of each target memory segment after the free memory block release operation is performed is obtained; for any target memory segment, according to the memory segment splitting result of the target memory segment, each sub-memory segment corresponding to the target memory segment is linked through a linked list to obtain sub-memory segment link information corresponding to the target memory segment; the memory segment link information corresponding to each target memory segment in the memory segment linked list is replaced with the sub-memory segment link information corresponding to each target memory segment, so as to release the free memory of each target memory segment to the operating system; in this way, when both the memory idle rate and the total amount of free memory reach the threshold value, each memory segment in the memory segment linked list can be traversed, and even if there is used block memory on the memory segment, the free block memory can be released to the operating system, thereby reducing the memory idle rate, improving the memory recycling efficiency, and making the free memory efficiently utilized.
[0081] In an exemplary embodiment, the method also includes: traversing the memory segment linked list to obtain the memory size of each memory segment, and obtaining the free memory size of each memory segment; the free memory size on the memory segment is obtained by adding the memory sizes of each free memory block on the memory segment; determining the memory sum according to the memory size of each memory segment, and determining the free memory sum according to the free memory size of each memory segment; determining the memory idle rate according to the memory sum and the free memory sum.
[0082] Optionally, first, traverse the memory segment linked list on the memory allocator malloc_state structure, add the size of each memory segment, determine the total memory of all memory segments, and while obtaining the size of each memory segment, traverse all memory blocks on the memory segment. If the memory block is a free memory block, add the free block memory sizes to calculate the total free memory blocks, then divide the total free memory blocks by the total memory of all memory segments to obtain the memory free rate, and finally return the calculated memory free rate and the total free memory.
[0083] In this embodiment, the memory idle rate can be accurately determined by traversing the memory segment linked list to determine the total memory and the total free memory.
[0084] In an exemplary embodiment, according to a preset free memory block release rule, a free memory block release operation is performed on each target memory segment, including: when the first memory block on the target memory segment is a free memory block, determining the first address of the target memory segment and the last address of the first memory block; determining the last address of the memory block to be released according to the last address of the first memory block and the first address of the target memory segment; when the memory length obtained by subtracting the first address of the target memory segment from the last address of the memory block to be released is greater than the preset memory page length, calling a memory release function to release the memory block to be released.
[0085] Optionally, determine whether the first memory block on the target memory segment a is a free memory block. If it is a free memory block and the reclaimable memory in the free memory exceeds one memory page, delete the free memory block from the memory block index. Figure 3For example, the preset memory page length in the record server is sys_len. When the first memory block on the target memory segment is a free memory block, the first address of the target memory segment is determined to be s1, and the tail address of the free memory block is c2=s1+free memory block length. Then, according to the tail address c2 of the first memory block and the first address s1 of the target memory segment, the tail address f2 of the memory block to be released (i.e., the free tail address f2) is determined. When the memory length f2-s1 obtained by subtracting the first address s1 of the target memory segment from the tail address f2 of the memory block to be released is greater than the preset memory page length sys_len, the memory release function munmap is called to release the memory block to be released. Then, the first address of the memory segment is moved from s1 to f2, and the length of the memory segment becomes "the original memory segment length-(f2-s1)". If the length of c2-f2 is greater than 0 at this time, it means that there is still a small free memory block, and this free memory block is inserted into the memory block index. Among them, memory block index means that luajit adds free memory blocks with a size greater than 248 bytes to the index tree, and adds those less than or equal to 248 bytes to the index array, so that when the upper layer applies for memory, it can find the most suitable memory block address at the fastest speed.
[0086] In this embodiment, when the first memory block on the target memory segment is a free memory block, the first address of the target memory segment and the tail address of the first memory block are determined; the tail address of the memory block to be released is determined according to the tail address of the first memory block and the first address of the target memory segment; when the memory length obtained by subtracting the first address of the target memory segment from the tail address of the memory block to be released is greater than the preset memory page length, the memory release function is called to release the memory block to be released; in this way, when the first memory block on the target memory segment is a free memory block, it can be quickly determined whether the free memory block meets the memory release condition, which is conducive to fast and accurate memory release.
[0087] In an exemplary embodiment, the tail address of the memory block to be released is determined based on the tail address of the first memory block and the first address of the target memory segment, including: subtracting the first address of the target memory segment from the tail address of the first memory block and performing memory alignment according to a preset memory page length to obtain an initial tail address of the memory block to be released; when the length obtained by subtracting the initial tail address of the memory block to be released from the tail address of the first memory block is greater than 0 and less than the length of the minimum block, the result obtained by subtracting the preset memory page length from the initial tail address is used as the tail address of the memory block to be released.
[0088] Among them, the minimum block refers to the space used to store metadata. Since each memory block requires 2 size_t and 2 pointer spaces to store metadata, such as the addresses of the previous and next memory blocks, whether the current memory block is used, etc., the total size of each memory block must be greater than the minimum block size. If memory allocation is required, a large free memory block is split into a used memory block and a small free memory block. When the length of the small free memory block is less than the minimum block, no splitting is performed, and the large free memory block is directly marked as a used memory block and returned.
[0089] Optional, with Figure 3 For example, let the preset memory page length in the server be sys_len. When the first memory block on the target memory segment is a free memory block, the first address of the target memory segment is determined to be s1, and the tail address of the free memory block is c2=s1+free memory block length. The specific process of "determining the tail address of the memory block to be released according to the tail address of the first memory block and the first address of the target memory segment" is as follows: first, the tail address c2 of the first memory block is subtracted from the first address s1 of the target memory segment, and then aligned according to the preset memory page length sys_len to obtain the initial tail address f2' of the memory block to be released. When the length obtained by subtracting the initial tail address f2 of the memory block to be released from the tail address c2 of the first memory block (i.e., c2-f2') is greater than 0 and less than the length of the minimum block, the initial tail address f2' is subtracted from the preset memory page length sys_len, and the result (i.e., f2=f2'-sys_len) is used as the tail address f2 of the memory block to be released. Among them, the lower alignment means: the length of 5k is aligned to 4k, and 4k is returned.
[0090] In this embodiment, the initial tail address of the memory block to be released is obtained by subtracting the first address of the target memory segment from the tail address of the first memory block and then performing memory alignment according to a preset memory page length; when the length obtained by subtracting the initial tail address of the memory block to be released from the tail address of the first memory block is greater than 0 and less than the length of the minimum block, the result obtained by subtracting the preset memory page length from the initial tail address is used as the tail address of the memory block to be released; in this way, when the first memory block of the target memory segment is a free memory block, the tail address of the memory block to be released can be quickly and accurately determined, which is conducive to accurately releasing the memory to be released.
[0091] In an exemplary embodiment, according to a preset free memory block release rule, a free memory block release operation is performed on each target memory segment, including: when the first memory block on the target memory segment is not a free memory block, determining the first free memory block of the target memory segment; determining the first address of the target memory segment, the last address of the target memory segment, the first address of the first free memory block, and the last address of the first free memory block; determining the first address of the memory block to be released according to the first address of the first free memory block and the first address of the target memory segment, and determining the last address of the memory block to be released according to the last address of the first free memory block and the first address of the target memory segment; when the length obtained by subtracting the first address of the memory block to be released from the last address of the memory block to be released is greater than the preset memory page length, calling a memory release function to release the memory block to be released.
[0092] Optionally, traverse the memory blocks on the target memory segment a, and if the first memory block is not a free memory block, determine whether the subsequent memory blocks are free memory blocks, and if a free memory block is found and it is determined that the length of the free memory that can be released is greater than a system page, that is, greater than a preset memory page length, release a section of memory in the free memory block. Figure 4 For example, the detailed process is as follows: let the preset memory page length in the server be sys_len, and the first memory block is not a free memory block of the target memory segment, then find the first free memory block of the target memory segment, determine the first address s1 of the target memory segment, the last address s2 of the target memory segment, the first address c1 of the first free memory block, and the last address c2 of the first free memory block; then, according to the first address c1 of the first free memory block and the first address s1 of the target memory segment, the first address f1 of the memory block to be released can be determined, and, according to the last address c2 of the first free memory block and the first address s1 of the target memory segment, the last address f2 of the memory block to be released can be determined; then, when the length obtained by subtracting the first address f1 of the memory block to be released from the last address f2 of the memory block to be released (i.e., f2-f1) is greater than the preset memory page length sys_len, call the memory release function munmap to release the memory of f1-f2.
[0093] Subsequently, the memory segment from s1 to f1 is set to a1, the hidden segment data is written before f1, the segment head address is set to s1, the segment length is set to f1-s1, and if the remaining free block length (i.e. "f1-hidden segment length-c1" is greater than 0, it means that there are still free memory blocks in memory segment a1, and it is added to the memory block index. The memory segment from f2 to s2 is set to a2, the segment length of a2 is set to s2-f2, and the segment head address is f2. If the remaining free blocks c2-f2 of memory segment a2 are greater than 0, it is added to the memory block index. Memory segments a1 and a2 are connected through a linked list. If s2-c2 on memory segment a2 is a hidden segment on the original memory segment a, all memory segment a2 is released. Memory segment a2 is released in the above manner until no suitable free memory block is found on the memory segment for release, and then a memory segment linked list is generated: memory segment a1->memory segment a2->...->memory segment an.
[0094] In this embodiment, when the first memory block on the target memory segment is not a free memory block, the first free memory block of the target memory segment is determined; the first address of the target memory segment, the tail address of the target memory segment, the first address of the first free memory block, and the tail address of the first free memory block are determined; the first address of the memory block to be released is determined according to the first address of the first free memory block and the first address of the target memory segment, and the tail address of the memory block to be released is determined according to the tail address of the first free memory block and the first address of the target memory segment; when the length obtained by subtracting the first address of the memory block to be released from the tail address of the memory block to be released is greater than the preset memory page length, the memory release function is called to release the memory block to be released; in this way, it is possible to accurately and quickly determine whether each free memory block on the target memory segment meets the memory release condition, which is conducive to quickly and accurately releasing the memory.
[0095] In an exemplary embodiment, the first address of the memory block to be released is determined according to the first address of the first free memory block and the first address of the target memory segment, including: subtracting the first address of the target memory segment from the first address of the first free memory block, and adding the first address of the target memory segment after memory alignment according to a preset memory page length to obtain the initial first address of the memory block to be released; subtracting the preset hidden segment length and the first address of the first free memory block from the initial first address of the memory block to be released to obtain a first length; when the first length is greater than 0 and less than the length of the minimum block, adding the preset memory page length to the initial first address is used as the result of the first address of the memory block to be released.
[0096] Optionally, Figure 4For example, let the preset memory page length in the server be sys_len. If the first memory block is not a free memory block of the target memory segment, then find the first free memory block of the target memory segment, determine the first address s1 of the target memory segment, the last address s2 of the target memory segment, the first address c1 of the first free memory block, and the last address c2 of the first free memory block. The specific process of "determining the first address f1 of the memory block to be released according to the first address c1 of the first free memory block and the first address s1 of the target memory segment" is as follows: subtract the first address s1 of the target memory segment from the first address c1 of the first free memory block, and align it according to the preset memory page length sys_len and add the first address c1 of the target memory segment. Address s1, get the initial first address f1' of the memory block to be released, that is, f1' is: the result obtained by subtracting s1 from c1 is aligned to sys_len and then added to s1; the initial first address f1' of the memory block to be released is subtracted from the preset hidden segment length and the first address c1 of the first free memory block to get the first length, which is: f1'-hidden segment length-c1. When the first length "f1'-hidden segment length-c1" is greater than 0 and less than the length of the smallest block, the result obtained by adding the preset memory page length sys_len to the initial first address f1' is used as the first address of the memory block to be released, and the first address of the memory block to be released is: f1=f1'+sys_len. Among them, the upper alignment means that the length of 5k is aligned to 4k and 8k is returned.
[0097] In this embodiment, the initial first address of the memory block to be released is obtained by subtracting the first address of the target memory segment from the first address of the first free memory block, and adding the first address of the target memory segment after memory alignment according to the preset memory page length; the first length is obtained by subtracting the preset hidden segment length and the first address of the first free memory block from the initial first address of the memory block to be released; when the first length is greater than 0 and less than the length of the minimum block, the result obtained by adding the preset memory page length to the initial first address is used as the first address of the memory block to be released; in this way, the first address of each memory block to be released in the target memory segment can be accurately determined, which is conducive to accurately releasing each memory to be released.
[0098] In an exemplary embodiment, the tail address of the memory block to be released is determined according to the tail address of the first free memory block and the first address of the target memory segment, including: subtracting the first address of the target memory segment from the tail address of the first free memory block, and adding the first address of the target memory segment after memory alignment according to a preset memory page length to obtain an initial tail address of the memory block to be released; subtracting the initial tail address of the memory block to be released from the tail address of the first free memory block to obtain a second length; when the second length is greater than 0 and less than the length of the minimum block, subtracting the preset memory page length from the initial tail address of the memory block to be released as the tail address of the memory block to be released.
[0099] Optionally, Figure 4 For example, let the preset memory page length in the server be sys_len. If the first memory block is not a free memory block in the target memory segment, then find the first free memory block in the target memory segment, determine the first address s1 of the target memory segment, the last address s2 of the target memory segment, the first address c1 of the first free memory block, and the last address c2 of the first free memory block. The specific process of "determining the last address of the memory block to be released based on the last address of the first free memory block and the first address of the target memory segment" is: subtract the first address s1 of the target memory segment from the last address c2 of the first free memory block, and add the target memory segment after aligning it according to the preset memory page length sys_len. The first address s1 of the memory block to be released is used to obtain the initial tail address f2' of the memory block to be released, that is, f2' is: the result obtained by subtracting s1 from c2 is aligned to the bottom according to sys_len and then s1 is added; the tail address c2 of the first free memory block is subtracted from the initial tail address f2' of the memory block to be released to obtain the second length, that is, the second length is: c2-f2'; when the second length "c2-f2'" is greater than 0 and less than the length of the minimum block, the result obtained by subtracting the preset memory page length sys_len from the initial tail address f2' of the memory block to be released is used as the tail address of the memory block to be released, that is, the tail address of the memory block to be released is: f2=f2'-sys_len.
[0100] In this embodiment, the initial tail address of the memory block to be released is obtained by subtracting the first address of the target memory segment from the tail address of the first free memory block, and adding the first address of the target memory segment after memory alignment according to the preset memory page length; the initial tail address of the memory block to be released is subtracted from the tail address of the first free memory block to obtain a second length; when the second length is greater than 0 and less than the length of the minimum block, the result obtained by subtracting the preset memory page length from the initial tail address of the memory block to be released is used as the tail address of the memory block to be released; in this way, the tail address of each memory block to be released in the target memory segment can be accurately determined, which is conducive to accurately releasing each memory to be released.
[0101] Figure 5 The memory recovery result after using the memory release method of the present application is provided by way of example. Figure 5As shown, there are two memory segment nodes on the memory segment linked list. Among them, the total size of memory segment 1 is 152m+hidden segment, and from the beginning to the end, there are free block memory blocks of 50m, used block memory blocks of 2m, free block memory blocks of 100m, and hidden segments. After forced recovery of memory segment 1, the total size is 2m+4k, and free memory blocks of 50m, 100m-4k in free memory blocks, and hidden segments are released. Among them, the total size of memory segment 2 is 4k+130m+80+hidden segment, and from the beginning to the end, there are free block memory blocks of 2k, used block memory blocks of 2k, free block memory blocks of 100m+80, used block memory blocks of 30m, and hidden segments. Memory segment 2 is divided into two new memory segments, linked by linked lists, with total sizes of 8k and 80+30m+hidden segments, respectively, and 100m-4k in free block memory blocks of 100m+80 is released.
[0102] Before recycling, the total size of memory segments is 152m+hidden segment+4k+130m+80+hidden segment~=282m, the total free block memory is 50m+100m+2k+100m+80~=250m, and the idle rate is 250m / 282m~=88%. After recycling, the total size of memory segments is 2m+4k+8k+80+30m+hidden segment~=32780k, the total free block memory is (4k-hidden segment)+2k+(4k-hidden segment)+80~=10k, and the memory idle rate is 0.03%.
[0103] In another embodiment, Figure 6 As shown, a memory release method is provided, which is described by taking the method applied to the server 102 in FIG. 1 as an example, and includes the following steps:
[0104] Step S602, traverse the memory segment linked list, obtain the memory size of each memory segment, and obtain the free memory size of each memory segment; the free memory size on the memory segment is obtained by adding the memory sizes of each free memory block on the memory segment.
[0105] Step S604, determining the total memory according to the memory size of each memory segment, and determining the total free memory according to the free memory size of each memory segment.
[0106] Step S606, determining the memory idle rate according to the total memory and the total idle memory.
[0107] Step S608, when the memory idle rate is greater than the first preset threshold and the total free memory is greater than the second preset threshold, traverse each memory segment in the memory segment linked list and use the memory segment with at least one free memory block as the target memory segment.
[0108] Step S610: according to a preset free memory block release rule, a free memory block release operation is performed on each target memory segment, and a memory segment splitting result of each target memory segment after the free memory block release operation is performed is obtained.
[0109] Step S612: for any target memory segment, according to the memory segment splitting result of the target memory segment, each sub-memory segment corresponding to the target memory segment is linked through a linked list to obtain sub-memory segment link information corresponding to the target memory segment.
[0110] Step S614: replace the memory segment link information corresponding to each target memory segment in the memory segment linked list with the sub-memory segment link information corresponding to each target memory segment, so as to release the free memory of each target memory segment to the operating system.
[0111] It should be noted that the specific definition of the above steps can refer to the specific definition of a memory release method mentioned above.
[0112] It should be understood that, although the various steps in the flowcharts involved in the above-mentioned embodiments are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence according to the order indicated by the arrows. Unless there is a clear explanation in this article, the execution of these steps does not have a strict order restriction, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above-mentioned embodiments can include multiple steps or multiple stages, and these steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily to be carried out in sequence, but can be executed in turn or alternately with other steps or at least a part of the steps or stages in other steps.
[0113] Based on the same inventive concept, the embodiment of the present application also provides a memory release device for implementing the memory release method involved above. The implementation scheme for solving the problem provided by the device is similar to the implementation scheme recorded in the above method, so the specific limitations in one or more memory release device embodiments provided below can refer to the limitations on the memory release method above, and will not be repeated here.
[0114] In an exemplary embodiment, Figure 7 As shown, a memory release device is provided, including: a traversal module 702, a release module 704, a link module 706 and a replacement module 708, wherein:
[0115] A traversal module 702 is used to traverse each memory segment in the memory segment linked list and select a memory segment with at least one free memory block as a target memory segment when the memory idle rate is greater than a first preset threshold and the total free memory is greater than a second preset threshold;
[0116] A release module 704 is used to perform a free memory block release operation on each target memory segment according to a preset free memory block release rule, and obtain a memory segment splitting result of each target memory segment after performing the free memory block release operation;
[0117] A linking module 706 is used for linking the sub-memory segments corresponding to any target memory segment through a linked list according to the memory segment splitting result of the target memory segment, so as to obtain the sub-memory segment linking information corresponding to the target memory segment;
[0118] The replacement module 708 is used to replace the memory segment link information corresponding to each target memory segment in the memory segment linked list with the sub-memory segment link information corresponding to each target memory segment, so as to release the free memory of each target memory segment to the operating system.
[0119] In one embodiment, the traversal module 702 is specifically used to traverse the memory segment linked list, obtain the memory size of each memory segment, and obtain the free memory size of each memory segment; the free memory size on the memory segment is obtained by adding the memory sizes of each free memory block on the memory segment; the memory sum is determined according to the memory size of each memory segment, and the free memory sum is determined according to the free memory size of each memory segment; the memory idle rate is determined according to the memory sum and the free memory sum.
[0120] In one of the embodiments, the release module 704 is specifically used to determine the first address of the target memory segment and the tail address of the first memory block when the first memory block on the target memory segment is a free memory block; determine the tail address of the memory block to be released based on the tail address of the first memory block and the first address of the target memory segment; and call the memory release function to release the memory block to be released when the memory length obtained by subtracting the first address of the target memory segment from the tail address of the memory block to be released is greater than the preset memory page length.
[0121] In one embodiment, the release module 704 is specifically used to subtract the first address of the target memory segment from the tail address of the first memory block and then perform memory alignment according to a preset memory page length to obtain an initial tail address of the memory block to be released; when the length obtained by subtracting the initial tail address of the memory block to be released from the tail address of the first memory block is greater than 0 and less than the length of the minimum block, the result obtained by subtracting the preset memory page length from the initial tail address is used as the tail address of the memory block to be released.
[0122] In one of the embodiments, the release module 704 is specifically used to determine the first free memory block of the target memory segment when the first memory block on the target memory segment is not a free memory block; determine the first address of the target memory segment, the last address of the target memory segment, the first address of the first free memory block, and the last address of the first free memory block; determine the first address of the memory block to be released according to the first address of the first free memory block and the first address of the target memory segment, and determine the last address of the memory block to be released according to the last address of the first free memory block and the first address of the target memory segment; and call the memory release function to release the memory block to be released when the length obtained by subtracting the first address of the memory block to be released from the last address of the memory block to be released is greater than the preset memory page length.
[0123] In one embodiment, the release module 704 is specifically used to subtract the first address of the target memory segment from the first address of the first free memory block, and add the first address of the target memory segment after memory alignment according to the preset memory page length to obtain the initial first address of the memory block to be released; subtract the preset hidden segment length and the first address of the first free memory block from the initial first address of the memory block to be released to obtain a first length; when the first length is greater than 0 and less than the length of the minimum block, the result obtained by adding the preset memory page length to the initial first address is used as the first address of the memory block to be released.
[0124] In one embodiment, the release module 704 is specifically used to subtract the first address of the target memory segment from the tail address of the first free memory block, and add the first address of the target memory segment after memory alignment according to the preset memory page length to obtain the initial tail address of the memory block to be released; subtract the initial tail address of the memory block to be released from the tail address of the first free memory block to obtain a second length; when the second length is greater than 0 and less than the length of the minimum block, the result obtained by subtracting the preset memory page length from the initial tail address of the memory block to be released is used as the tail address of the memory block to be released.
[0125] Each module in the above-mentioned memory release device can be implemented in whole or in part by software, hardware or a combination thereof. Each module can be embedded in or independent of a processor in a computer device in the form of hardware, or can be stored in a memory in a computer device in the form of software, so that the processor can call and execute operations corresponding to each module.
[0126] In an exemplary embodiment, a computer device is provided. The computer device may be a server, and its internal structure diagram may be as shown in FIG. Figure 8As shown. The computer device includes a processor, a memory, an input / output interface (Input / Output, referred to as I / O) and a communication interface. The processor, the memory and the input / output interface are connected through a system bus, and the communication interface is connected to the system bus through the input / output interface. The processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store memory release data. The input / output interface of the computer device is used to exchange information between the processor and an external device. The communication interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a memory release method is implemented.
[0127] Those skilled in the art will understand that Figure 8 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.
[0128] In one embodiment, a computer device is provided, including a memory and a processor, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor executes the steps of the above-mentioned memory release method. The steps of the memory release method here may be the steps of the memory release method in each of the above-mentioned embodiments.
[0129] In one embodiment, a computer-readable storage medium is provided, which stores a computer program. When the computer program is executed by a processor, the processor executes the steps of the above-mentioned memory release method. The steps of the memory release method here can be the steps of the memory release method in each of the above-mentioned embodiments.
[0130] In one embodiment, a computer program product is provided, including a computer program, which, when executed by a processor, causes the processor to execute the steps of the above-mentioned memory release method. The steps of the memory release method here may be the steps of the memory release method in each of the above-mentioned embodiments.
[0131] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to the memory, database or other medium used in the embodiments provided in this application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetoresistive random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. As an illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM). The database involved in each embodiment provided in this application may include at least one of a relational database and a non-relational database. Non-relational databases may include distributed databases based on blockchains, etc., but are not limited to this. The processor involved in each embodiment provided in this application may be a general-purpose processor, a central processing unit, a graphics processor, a digital signal processor, a programmable logic device, a data processing logic device based on quantum computing, etc., but are not limited to this.
[0132] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are 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.
[0133] The above-described embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.
Claims
1. A memory release method, characterized in that: The method comprises: When the memory idle rate is greater than a first preset threshold and the total free memory is greater than a second preset threshold, traverse each memory segment in the memory segment linked list and use a memory segment with at least one free memory block as a target memory segment; According to a preset free memory block release rule, a free memory block release operation is performed on each of the target memory segments, and a memory segment splitting result of each of the target memory segments after the free memory block release operation is performed is obtained; For any of the target memory segments, according to the memory segment splitting result of the target memory segment, each sub-memory segment corresponding to the target memory segment is linked through a linked list to obtain sub-memory segment link information corresponding to the target memory segment; The memory segment link information corresponding to each of the target memory segments in the memory segment linked list is replaced with the sub-memory segment link information corresponding to each of the target memory segments, so as to release the free memory of each of the target memory segments to the operating system.
2. The method according to claim 1, characterized in that The method further comprises: Traversing the memory segment linked list, obtaining the memory size of each memory segment, and obtaining the free memory size of each memory segment; the free memory size on the memory segment is obtained by adding the memory sizes of each free memory block on the memory segment; Determine the total memory amount according to the memory size of each memory segment, and determine the total free memory amount according to the free memory size of each memory segment; The memory idle rate is determined according to the memory sum and the idle memory sum.
3. The method according to claim 1, characterized in that The performing a free memory block release operation on each target memory segment according to a preset free memory block release rule includes: In the case where the first memory block on the target memory segment is a free memory block, determining the first address of the target memory segment and the last address of the first memory block; Determine the tail address of the memory block to be released according to the tail address of the first memory block and the head address of the target memory segment; When a memory length obtained by subtracting a head address of the target memory segment from a tail address of the memory block to be released is greater than a preset memory page length, a memory release function is called to release the memory block to be released.
4. The method according to claim 3, characterized in that Determining the tail address of the memory block to be released according to the tail address of the first memory block and the head address of the target memory segment includes: Subtracting the first address of the target memory segment from the tail address of the first memory block, and performing memory alignment according to the preset memory page length to obtain an initial tail address of the memory block to be released; When the length obtained by subtracting the initial tail address of the memory block to be released from the tail address of the first memory block is greater than 0 and less than the length of the minimum block, the result obtained by subtracting the preset memory page length from the initial tail address is used as the tail address of the memory block to be released.
5. The method according to claim 1, characterized in that: The performing a free memory block release operation on each target memory segment according to a preset free memory block release rule includes: If the first memory block on the target memory segment is not a free memory block, determining a first free memory block of the target memory segment; Determine the first address of the target memory segment, the last address of the target memory segment, the first address of the first free memory block, and the last address of the first free memory block; Determine the first address of the memory block to be released according to the first address of the first free memory block and the first address of the target memory segment, and determine the tail address of the memory block to be released according to the tail address of the first free memory block and the first address of the target memory segment; When the length obtained by subtracting the head address of the memory block to be released from the tail address of the memory block to be released is greater than the preset memory page length, a memory release function is called to release the memory block to be released.
6. The method according to claim 5, characterized in that The step of determining the first address of the memory block to be released according to the first address of the first free memory block and the first address of the target memory segment comprises: Subtract the first address of the target memory segment from the first address of the first free memory block, perform memory alignment according to the preset memory page length, and then add the first address of the target memory segment to obtain an initial first address of the memory block to be released; Subtracting a preset hidden segment length and a first address of the first free memory block from an initial first address of the memory block to be released to obtain a first length; When the first length is greater than 0 and less than the length of the minimum block, the result obtained by adding the preset memory page length to the initial first address is used as the first address of the memory block to be released.
7. The method according to claim 5, characterized in that The step of determining the tail address of the memory block to be released according to the tail address of the first free memory block and the head address of the target memory segment comprises: Subtract the first address of the target memory segment from the tail address of the first free memory block, perform memory alignment according to a preset memory page length, and then add the first address of the target memory segment to obtain an initial tail address of the memory block to be released; Subtract the initial tail address of the memory block to be released from the tail address of the first free memory block to obtain a second length; When the second length is greater than 0 and less than the length of the minimum block, a result obtained by subtracting the preset memory page length from the initial tail address of the memory block to be released is used as the tail address of the memory block to be released.
8. A memory release device, characterized in that: The device comprises: A traversal module, configured to traverse each memory segment in the memory segment linked list and select a memory segment having at least one free memory block as a target memory segment when the memory idle rate is greater than a first preset threshold and the total free memory is greater than a second preset threshold; A release module, used to perform a free memory block release operation on each of the target memory segments according to a preset free memory block release rule, and obtain a memory segment splitting result of each of the target memory segments after performing the free memory block release operation; a linking module, configured to link the sub-memory segments corresponding to any of the target memory segments through a linked list according to the memory segment splitting result of the target memory segment, so as to obtain the sub-memory segment linking information corresponding to the target memory segment; A replacement module is used to replace the memory segment link information corresponding to each target memory segment in the memory segment linked list with the sub-memory segment link information corresponding to each target memory segment, so as to release the free memory of each target memory segment to the operating system.
9. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 7 are implemented.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.
Citation Information
Cited By
Multi-segment memory allocation method based on distance constraint under real-time system
CN121029424A
A multi-segment memory allocation method based on distance constraint under a real-time system
CN121029424B