Method and device for optimizing fragmented memory

By recording and analyzing memory allocation in the Linux kernel and re-formulating memory allocation strategies, the memory fragmentation and waste problems caused by the kmalloc function interface are solved, and more efficient memory management is achieved.

CN119987983APending Publication Date: 2025-05-13SHENZHEN MICROBT ELECTRONICS TECH CO LTD
View PDF 0 Cites 2 Cited by

Patent Information

Application Number
CN202311498217.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-11-10
Publication Date
2025-05-13

AI Technical Summary

Technical Problem

The kmalloc function interface in the Linux kernel causes large internal fragmentation and waste of valuable memory resources during memory allocation.

Method used

By using the default memory allocation strategy to allocate the memory block size when the system is first started, the application and allocation status of the memory block size are recorded during the first run, and before the system shuts down, whether the memory allocation strategy needs to be re-formulated based on the recording situation to optimize memory allocation.

Benefits of technology

It effectively suppresses the generation of internal memory fragments, saves memory consumption, and automatically adjusts the memory allocation strategy according to the actual operation of the system, improving the refinement of the memory allocation strategy.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119987983A_ABST
    Figure CN119987983A_ABST
Patent Text Reader

Abstract

The invention discloses a fragmented memory optimization method, which comprises the following steps: using a default memory allocation strategy to allocate the size of a memory block in the first starting process of a system, recording the application and allocation conditions of the size of the memory block in the first running process after the first starting of the system is ended, and before the first shutdown of the system, recording the application and allocation conditions of the size of the memory block. And according to the recorded application and distribution conditions of the memory block sizes, judging whether a condition for reformulating a first memory distribution strategy is met, if so, reformulating the first memory distribution strategy as the first memory distribution strategy used in the next starting process of the system, and if not, reformulating the second memory distribution strategy. And taking the default memory allocation strategy as a second memory allocation strategy. The generation of memory fragments can be reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer memory management, and in particular, to a method and device for optimizing fragmented memory. Background Art

[0002] In the Linux kernel, memory allocation is usually performed through memory allocation interfaces for use by drivers, other kernel modules, etc. For example, the kmalloc function interface can be used to allocate continuous physical memory in the kernel space with a granularity of bytes, and the vmalloc function interface can be used to allocate discrete physical memory in the kernel space with a granularity of pages.

[0003] In the Linux kernel, kmalloc function interface is widely used to cope with the frequent allocation needs of general small memory. The kmalloc function interface is built based on the memory allocator (slab alloactor) system. Its essence is actually a general pre-allocated memory management mechanism of various specific sizes, such as slab cache. kmalloc supports the following two types of allocated memory block sizes:

[0004] One type is a memory block that is an integer multiple of bytes, such as 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096 bytes, with a maximum of 64MB;

[0005] The other type is a non-integer multiple of bytes memory block, which is of a specific size, for example, 96 bytes, 196 bytes.

[0006] The above two types of memory block sizes can use the allocation options in the allocation option set as memory allocation strategies.

[0007] In the actual use of the kmalloc function interface, according to the size of the memory block requested by the user, an allocation option that is close to the size of the memory block requested and larger is found in the allocation option set for allocation. For example, when using the kmalloc function interface to request a 560-byte memory block, 1024 bytes in the allocation option set will be selected, and the final allocated memory block size will be 1024 bytes; for example, when requesting 2080 bytes, 4096 bytes in the allocation option set will be selected, that is, the final allocated memory block size will be 4096 bytes. This leads to two defects: one is that it causes large internal fragmentation of the memory block, and the other is that it wastes precious memory resources.

[0008] For this problem, Linux actually supports creating slab cache to meet specific memory block allocation. For example, if a 2100-byte memory block is required, the user calls the slab-related interface to create a kmemcache memory of the specified size. In this way, the requested size is the same as the actual allocated size, which will not cause internal fragmentation or waste memory. However, due to the large number of kernel modules and the huge code, many mature kernel modules directly use the kmalloc function interface to allocate memory, and many kernel developers are accustomed to using the kmalloc function interface directly, which inevitably leads to the above defects. Summary of the invention

[0009] The present invention provides a method for optimizing fragmented memory to avoid large internal fragments of a memory block.

[0010] A first aspect of an embodiment of the present application provides a method for optimizing fragmented memory, the method comprising:

[0011] During the first system startup, the default memory allocation strategy is used to allocate the memory block size.

[0012] During the first run after the system is first started, record the application and allocation status of the memory block size.

[0013] Before the system is shut down for the first time, determine whether the conditions for re-formulating the first memory allocation strategy are met based on the recorded application and allocation status of the memory block size. If so, re-formulate the first memory allocation strategy as the first memory allocation strategy used during the next system startup process, and use the default memory allocation strategy as the second memory allocation strategy.

[0014] A second aspect of an embodiment of the present application provides a fragmented memory optimization device, the device comprising:

[0015] The memory allocation strategy selection module is used to allocate the memory block size using the default memory allocation strategy during the first system startup.

[0016] The recording module is used to record the application and allocation status of the memory block size during the first operation after the system is first started.

[0017] The memory allocation strategy formulation module is used to determine whether the conditions for re-formulating the first memory allocation strategy are met based on the recorded application and allocation status of the memory block size before the system is shut down for the first time. If so, the first memory allocation strategy is re-formulated as the first memory allocation strategy used during the next system startup process, and the default memory allocation strategy is used as the second memory allocation strategy.

[0018] The fragmented memory optimization method provided in the present application formulates a memory allocation strategy to be used in the next startup process according to the application and allocation status of the memory block size recorded during the first run, so that the memory allocation can be continuously and automatically adjusted as the system runs, and the allocation options in the memory allocation strategy are refined, which is conducive to suppressing the generation of internal memory fragments and effectively saving memory consumption. BRIEF DESCRIPTION OF THE DRAWINGS

[0019] Figure 1 A flowchart of a method for optimizing fragmented memory according to an embodiment of the present application.

[0020] Figure 2 A flowchart of a method for optimizing fragmented memory when the system of an embodiment of the present application is started for the first time.

[0021] Figure 3 A flowchart of a method for optimizing fragmented memory when the system of an embodiment of the present application is not started for the first time.

[0022] Figure 4 A schematic diagram of a fragmented memory optimization device according to an embodiment of the present application.

[0023] Figure 5 Another schematic diagram of the fragmented memory optimization device according to an embodiment of the present application. DETAILED DESCRIPTION

[0024] In order to make the objectives, technical means and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings.

[0025] The applicant's research found that the original memory allocation strategy of Linux cannot achieve on-demand allocation, because the kernel system is very large, with various small structures and various memory size requirements. It is impossible to create an allocation option for each memory size requirement, because each allocation option itself also consumes memory. Too many allocation options lead to excessive memory consumption.

[0026] An embodiment of the present application provides a method for optimizing fragmented memory. By recording the application and allocation status of the memory block size recorded during the first run, the memory allocation strategy used at the next startup is adjusted. This not only avoids memory fragmentation, but also automatically adjusts the memory allocation strategy according to the actual operation of the system, making the memory allocation strategy more refined.

[0027] See also Figure 1 As shown, Figure 1 A flowchart of a method for optimizing fragmented memory according to an embodiment of the present application is shown. The method includes:

[0028] Step 11: during the current system startup process, if a first memory allocation strategy is detected, the first memory allocation strategy is used to allocate the memory block size; otherwise, the second memory allocation strategy used in the last startup process is used to allocate the memory block size.

[0029] The first memory allocation strategy is the memory allocation strategy that was re-established before the system was shut down last time.

[0030] As an example, the detection of the first memory allocation strategy may be detected by means of a flag bit used to mark that the first memory allocation strategy is reformulated.

[0031] As an example, the detection of the first memory allocation strategy is triggered in response to a memory allocation request.

[0032] Step 12: During the current operation of the system after the current startup, record the application and allocation status of the memory block size.

[0033] As an example, the application and allocation status includes: the size of the memory block applied for and the number of times, the size of the memory block allocated and the number of times, so as to be used to formulate the first memory allocation strategy. Step 13, before the system is shut down this time, based on the recorded application and allocation status of the memory block size, it is determined whether the conditions for re-formulating the first memory allocation strategy are met.

[0034] If yes, the first memory allocation strategy is newly formulated and saved as the first memory allocation strategy used in the next system startup process, and the first memory allocation strategy used this time is used as the second memory allocation strategy.

[0035] Among them, this time is not the first time.

[0036] The method further comprises:

[0037] During the first system startup, in response to a memory allocation request, the default memory allocation strategy is used to allocate the memory block size.

[0038] During the first run after the system is first started, record the application and allocation status of the memory block size.

[0039] Before the system is shut down for the first time, it is determined whether the conditions for formulating the first memory allocation strategy are met based on the recorded application and allocation status of the memory block size. If so, the first memory allocation strategy is formulated as the first memory allocation strategy used during the next system startup process, and the default memory allocation strategy is used as the second memory allocation strategy.

[0040] The first time refers to the first power-on after the program is burned into the firmware.

[0041] As an example, the application of memory block size and the recording of allocation status are triggered in response to the current memory allocation application. The memory allocation application may be a kmalloc function interface. The memory allocation application may come from user input or from a kernel module.

[0042] As an example, the period before system shutdown may be any time period before receiving a system shutdown instruction, or may be a period between the moment when the system shutdown instruction is received and responded to and the moment when the system enters a shutdown state.

[0043] As an example, the conditions for formulating the first memory allocation strategy include:

[0044] The first condition is that the size of the allocated second memory block is not an integer multiple of the minimum unit of data transmission between the processor and the memory.

[0045] The second condition is that there is a situation where the difference between the applied first memory block size and the allocated second memory block size is greater than the set first difference threshold, and the number of times this situation occurs is greater than the set number threshold, so as to formulate a first memory allocation strategy for the situation where the same memory block size is applied for multiple times,

[0046] The third condition is that the difference between two adjacent first memory block sizes among each applied first memory block size is less than the set second difference threshold, and the difference between each applied first memory block size and its allocated second memory block size is greater than the set third difference threshold, so as to formulate a first memory allocation strategy for the situation of multiple adjacent memory block sizes applied.

[0047] It should be understood that the above-mentioned conditions for re-formulating the first memory allocation strategy may not be limited thereto, and may specifically be changed according to the recorded application and allocation status of the memory block size.

[0048] As an example, reformulating the first memory allocation strategy includes:

[0049] When the first condition is met, each memory block size is created according to different integer multiples of the minimum unit of data transmission between the processor and the memory, and each created memory block size is used as an allocation option in the allocation option set to ensure the performance of the processor.

[0050] When the second condition is met, a memory block size is created according to the first memory block size, and the created memory size is added as an allocation option to the allocation option set to avoid fragmented memory. Optionally, the created memory size is an integer multiple of the minimum unit of data transmission between the processor and the memory.

[0051] In the case of meeting the third condition, based on the largest memory block size among the adjacent first memory block sizes, the set first redundancy is increased to obtain the created memory block size, and the memory block size is added as an allocation option to the allocation option set, which not only reduces the number of allocation options and thus reduces memory consumption, but also reduces the generation of large fragmented memory. Optionally, the created memory block size is an integer multiple of the minimum unit of data transmission between the processor and the memory. For example, the created memory block size can be adjusted by the first redundancy so that the created memory block size is an integer multiple of the minimum unit of data transmission between the processor and the memory.

[0052] As an example, the priority of the first condition is greater than that of the second and third conditions, and the second and third conditions have the same priority. That is to say, when the above conditions are met at the same time, the first memory allocation strategy is reformulated according to the strategy corresponding to the first condition.

[0053] It should be understood that the above-mentioned re-formulation of the first memory allocation strategy may not be limited thereto, and may be specifically adjusted according to the recorded application and allocation status of the memory block size.

[0054] To avoid excessive number of allocation options in the allocation option set, reformulating the first memory allocation strategy further includes:

[0055] In the case that the number of allocation options in the newly created allocation option set is greater than the set number threshold, the allocation options in the allocation option set are adjusted so that the difference between the sizes of two adjacent memory blocks is greater than the set fourth difference threshold, and / or the allocation options created so that the applied first memory block size or the allocated second memory block size exists only once are deleted.

[0056] The fourth difference threshold is determined according to the memory size consumed by an allocation option and the set second redundancy.

[0057] As an example, adjusting the allocation options in the allocation option set can be performed as follows:

[0058] For each allocation option in an allocation option set,

[0059] Check whether the memory size of the allocation option in the allocation option set and the size of its adjacent memory block meet the critical condition. If so, check whether the two adjacent memory block sizes meet the optimization cost condition. If so, retain the larger memory block size of the two adjacent memory block sizes and delete the smaller memory block size of the two adjacent memory block sizes.

[0060] Check whether the reserved memory block size and another adjacent memory block size meet the above critical conditions and optimization cost conditions, and execute repeatedly until all allocation options in the allocation option set are checked. In this way, the number of allocation options in the allocation option set can be controlled and the precision of each allocation option is improved, which is conducive to suppressing memory fragmentation.

[0061] As an example, the critical condition is that the absolute value of the difference between the sizes of two adjacent memory blocks is smaller than the memory size consumed by the allocation option, which helps to avoid the sizes of the memory blocks of the allocation options being too close to each other.

[0062] The optimization cost condition is that the product of the absolute value of the difference between the sizes of two adjacent memory blocks and the number of allocation options in the current allocation option set is greater than or equal to the optimization benefit memory, so as to improve the overall optimization benefit and avoid new memory consumption caused by too many allocation options themselves.

[0063] As an example, the optimized benefit memory is determined based on the memory size consumed by the allocation options and the set expected benefit, the second redundancy is determined based on the set expected benefit, and the fourth difference threshold is: the ratio of the optimized benefit memory to the number of allocation options in the current allocation option set.

[0064] It should be understood that the adjustment of the above allocation options is not limited thereto, and may specifically vary according to the recorded application and allocation status of the memory block size, and the allocation options in the allocation option set.

[0065] The embodiment of the present application selectively re-formulates the memory allocation strategy for the next startup according to the recorded application and allocation status of the memory block size before the system is shut down, thereby achieving automatic adjustment of the memory size and greatly reducing the generation of large fragmented memory.

[0066] To facilitate the understanding of the present application, the following explanation is given using the kmalloc function interface in the Linux system to allocate memory block size. It should be understood that the present application is not limited to the kmalloc function interface in the Linux system, and may also be applicable to memory block size allocation of any computer system including but not limited to the Android system, servers, etc.

[0067] See also Figure 2 As shown, Figure 2 A flowchart of a method for optimizing fragmented memory when the system is first started according to an embodiment of the present application. It includes:

[0068] Step 21, during the first system startup, in response to the kmalloc function interface, the kmalloc function interface uses the default memory allocation strategy to allocate memory size.

[0069] Among them, the first boot refers to the first power-on boot after the code is burned into the firmware. The default memory allocation strategy is the original memory allocation strategy. For example, the allocation option set includes default allocation options, and each allocation option is a different memory block size. In this way, the kmalloc function interface selects an allocation option slightly larger than the requested memory block size in the allocation option set for allocation according to the requested memory block size.

[0070] Step 22, after the system is started for the first time and enters the first operation process, in response to the current kmalloc function interface, the memory application and allocation status of the current kmalloc function is recorded and saved to a specified file.

[0071] The first run refers to the run process after the first startup. The memory application and allocation status includes: the memory size applied for, the memory size actually allocated, the memory quantity applied for, the memory quantity actually allocated, etc.

[0072] Step 23, before the system is shut down for the first time, determine whether the conditions for formulating a new memory allocation strategy are met based on the recorded memory application and allocation status.

[0073] If yes, then execute step 24 to formulate a new memory allocation strategy.

[0074] Otherwise, execute step 26 to shut down the system.

[0075] As an example, when the system receives a shutdown instruction, it responds to the shutdown instruction and determines whether the conditions for formulating a new memory allocation strategy are met based on the recorded memory application and allocation status.

[0076] For ease of writing, the new memory allocation strategy is referred to as the first memory allocation strategy.

[0077] As an example, the determination of conditions for formulating the first memory allocation strategy includes:

[0078] If the size of the second memory block allocated is not an integer multiple of the minimum unit of data transmission between the processor and the memory, it is determined that the first condition for formulating the first memory allocation strategy is met. Taking ARM as an example, the cache line of ARM is generally 32 bytes. If the size of the second memory block allocated is not an integer multiple of 32 bytes, it is determined that the first condition for formulating the first memory allocation strategy is met.

[0079] If there is a situation where the difference between the applied first memory block size and the allocated second memory block size is greater than the set first difference threshold, and the number of times this situation occurs is greater than the set number threshold, it is determined that the second condition for formulating the first memory allocation strategy is met.

[0080] If the difference between two adjacent first memory block sizes among the applied first memory block sizes is less than the set second difference threshold, and the difference between each applied first memory block size and its allocated second memory block size is greater than the set third difference threshold, it is determined to meet the third condition for formulating the first memory allocation strategy.

[0081] Step 24, formulate different first memory allocation strategies according to different conditions,

[0082] As an example,

[0083] Under the first condition, each memory block size is created as an integer multiple of the minimum unit of data transmission between the processor and the memory. Taking ARM as an example, in order to ensure performance, it is created as an integer multiple of cacheline, and the size of each created memory block is used as an allocation option in the allocation option set;

[0084] Under the second condition, a memory block size is created according to the first memory block size, and the created memory size is added as an allocation option to the allocation option set. For example, if the recorded situation has multiple applications for the first memory size, such as 2080 bytes, but a memory block of the second memory size, such as 4096 bytes, is actually allocated, then an allocation option of the first memory size is added to the first memory allocation strategy, so that memory waste can be completely avoided;

[0085] Under the third condition, based on the maximum memory block size among the adjacent first memory block sizes, the set first redundancy is increased to obtain the created memory block size, and the memory block size is added as an allocation option to the allocation option set. In other words, if there are multiple applications for similar memory sizes in the recorded situation, but in fact all memory blocks of the same memory size are allocated, then based on the maximum memory size among the similar memory sizes, the set first redundancy is increased, and the obtained memory block size is used as an allocation option, which can minimize memory waste. For example, if there are multiple applications for 2080 bytes, 2090 bytes, and 2100 bytes, 12 bytes of redundancy are increased based on 2100 bytes, so that 2112 bytes are added as an allocation option to the allocation option set.

[0086] Step 25, considering that the allocation options themselves also need to consume memory, in order to control the number of allocation options in the allocation option set, when the number of allocation options in the created allocation option set is greater than the set number threshold, the allocation options in the allocation option set are adjusted so that the difference between the sizes of two adjacent memory blocks is greater than the set fourth difference threshold, and / or, the allocation options created so that the applied first memory block size or the allocated second memory block size only exists once are deleted; after the adjustment is completed, the current first memory allocation policy is saved, a flag bit for marking that the first memory allocation policy is re-formulated is set, and the default memory allocation policy is used as the second memory allocation policy, and then the system shutdown operation is performed to put the system into a shutdown state.

[0087] The fourth difference threshold is determined according to the memory size consumed by an allocation option and the set second redundancy.

[0088] For example, assuming that the memory size consumed by a new allocation option is S bytes, check whether the sizes of the two adjacent memory blocks created meet the critical conditions:

[0089] |X1-X2|

[0090] If the condition is satisfied, it means that the two allocation options of adjacent memory sizes have the possibility of being merged, but whether they can be merged depends on the optimization cost. If the critical condition is not satisfied, it means that the two allocation options of adjacent memory sizes do not have the possibility of being merged.

[0091] Therefore, check whether the sizes of the two adjacent memory blocks meet the optimization cost conditions:

[0092] |X1-X2|*N≥S(1+α%)

[0093] Among them, || represents taking the absolute value, X1 and X2 are the sizes of two adjacent memory blocks in the allocation option set, N is the current number of allocation options in the allocation option set, α% is the expected return set, which is set according to actual needs and is generally set to more than 100%. S(1+α%) represents the optimized return memory.

[0094] If the above optimization cost condition is met, the larger memory block size of the two adjacent memory block sizes is retained, and the smaller memory block size of the two adjacent memory block sizes is deleted. Then, it is checked whether the retained memory block size and another adjacent memory block size meet the above critical conditions and optimization cost conditions. This process is repeated until all two adjacent memory block sizes have been checked.

[0095] ​If |X1-X2|*N is only 1 greater than S, that is, only one byte can be optimized, the optimization cost benefit is not met. Step 26, do not set the flag bit used to mark that the first memory allocation strategy is re-formulated, use the default memory allocation strategy used in the first startup process as the second memory allocation strategy, and then perform a system shutdown operation to put the system into a shutdown state.

[0096] See also Figure 3 As shown, Figure 3 A flowchart of a method for optimizing fragmented memory when the system is not started for the first time according to an embodiment of the present application. It includes:

[0097] Step 31, during the current system startup process, in response to the kmalloc function interface, detecting whether there is a first memory allocation strategy,

[0098] If yes, the first memory allocation strategy is used to allocate memory through the kmalloc function interface.

[0099] Otherwise, the second memory allocation strategy used at the last startup is used.

[0100] This is not the first time, and the first memory allocation strategy is the memory allocation strategy that was re-established before the system was shut down last time.

[0101] The method of detecting whether the first memory allocation strategy exists may be to detect a flag bit used to mark that the first memory allocation strategy is re-formulated, and determine whether the first memory allocation strategy exists according to the flag bit.

[0102] Step 32, when the system is started up and enters the current runtime, the memory application and allocation status of the current kmalloc function interface is recorded according to the system operation status, and saved to the specified file.

[0103] The memory application and allocation status includes: the memory size applied for, the memory size allocated, the memory quantity applied for, the memory quantity allocated, and the like.

[0104] Step 33, before the system is shut down, judging whether the conditions for re-establishing the first memory allocation strategy are met according to the recorded memory application and allocation status,

[0105] If yes, the first memory allocation strategy is newly formulated and saved as the first memory allocation strategy used in the next system startup process, and a flag bit for marking that the first memory allocation strategy is newly formulated is set, and the first memory allocation strategy used in the current startup process is used as the second memory allocation strategy, and then the system shutdown operation is performed to make the system enter the shutdown state;

[0106] Otherwise, the flag bit for marking that the first memory allocation strategy is re-formulated is not set, the first memory allocation strategy used in the current startup process is used as the second memory allocation strategy, and then the system shutdown operation is performed to put the system into a shutdown state.

[0107] Among them, the method for judging whether the conditions for re-formulating the first memory allocation strategy are met is the same as step 23, and the method for re-formulating the first memory allocation strategy is the same as steps 24 and 25.

[0108] This embodiment can automatically and continuously adjust the memory allocation strategy according to the actual operation of the system, which can minimize the memory waste caused by the original allocation strategy, reduce internal memory fragmentation, and save memory consumption.

[0109] The memory optimization rate of this embodiment can reach more than 15%. The more complex the system, the greater the results achieved, such as Android system, large servers, etc.

[0110] See also Figure 4 As shown, Figure 4 This is a schematic diagram of a fragmented memory optimization device according to an embodiment of the present application. The device includes:

[0111] A memory allocation strategy selection module is used to allocate memory block size using the first memory allocation strategy when the first memory allocation strategy is detected during the current system startup process, and to allocate memory block size using the second memory allocation strategy used during the last startup process when the first memory allocation strategy is not detected, wherein the first memory allocation strategy is the memory allocation strategy re-formulated before the system was shut down last time,

[0112] The recording module is used to record the application and allocation status of the memory block size during the current operation after the system is started.

[0113] The memory allocation strategy formulation module is used to determine whether the conditions for re-formulating the first memory allocation strategy are met before the system is shut down this time, based on the recorded application and allocation status of the memory block size. If so, the first memory allocation strategy is re-formulated as the first memory allocation strategy used during the next system startup process, and the first memory allocation strategy used this time is used as the second memory allocation strategy.

[0114] Among them, this time is not the first time.

[0115] As an example, the memory allocation strategy selection module further includes, during the initial startup of the system, using a default memory allocation strategy to allocate a memory block size,

[0116] The recording module further includes recording the application and allocation status of the memory block size during the operation process after the system is first started.

[0117] The memory allocation strategy formulation module further includes, before the system is shut down for the first time, judging whether the conditions for re-formulating the first memory allocation strategy are met based on the recorded application and allocation status of the memory block size; if so, re-formulating the first memory allocation strategy as the first memory allocation strategy used during the next system startup process, and using the default memory allocation strategy as the second memory allocation strategy.

[0118] The memory allocation strategy formulation module is configured as follows:

[0119] If the allocated second memory block size is not an integer multiple of the minimum unit of data transmission between the processor and the memory, then create each memory block size according to different integer multiples of the minimum unit of data transmission between the processor and the memory, and use each created memory block size as an allocation option in the allocation option set.

[0120] If there is a situation where the difference between the applied first memory block size and the allocated second memory block size is greater than the set first difference threshold, and the number of times this situation occurs is greater than the set number threshold, then create a memory block size according to the first memory block size, and add the created memory size as an allocation option to the allocation option set,

[0121] If the difference between two adjacent first memory block sizes among the applied first memory block sizes is less than the set second difference threshold, and the difference between each applied first memory block size and its allocated second memory block size is greater than the set third difference threshold, then the set first redundancy is increased based on the maximum memory block size among the adjacent first memory block sizes to obtain the created memory block size, and the memory block size is added as an allocation option to the allocation option set.

[0122] See also Figure 5 As shown, Figure 5 Another schematic diagram of a fragmented memory optimization device according to an embodiment of the present application. The device includes a memory and a processor, the memory stores a computer program, and the processor is configured to execute the computer program to implement the steps of the fragmented memory optimization method according to the embodiment of the present application.

[0123] The memory may include a random access memory (RAM) or a non-volatile memory (NVM), such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.

[0124] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.

[0125] As for the apparatus / network-side device / storage medium embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.

[0126] In this article, relational terms such as first and second, etc. are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "comprise", "include" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the statement "comprise a ..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.

[0127] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.

Claims

1. A fragmented memory optimization method, characterized in that: The method includes: During the first system startup, the default memory allocation strategy is used to allocate the memory block size. During the first run after the system is first started, record the application and allocation status of the memory block size. Before the system is shut down for the first time, determine whether the conditions for re-formulating the first memory allocation strategy are met based on the recorded application and allocation status of the memory block size. If so, re-formulate the first memory allocation strategy as the first memory allocation strategy used during the next system startup process, and use the default memory allocation strategy as the second memory allocation strategy.

2. The optimization method according to claim 1, characterized in that: The method further comprises: During the current system startup process, if a first memory allocation strategy is detected, the first memory allocation strategy is used to allocate the memory block size; otherwise, the second memory allocation strategy used in the last startup process is used to allocate the memory block size, wherein the first memory allocation strategy is the memory allocation strategy re-formulated before the system was shut down last time. During the current operation after the system is started, the application and allocation status of the memory block size is recorded. Before the system is shut down this time, it is determined whether the conditions for re-formulating the first memory allocation strategy are met according to the recorded application and allocation status of the memory block size. If so, the first memory allocation strategy is re-formulated as the first memory allocation strategy used in the next system startup process, and the first memory allocation strategy used this time is used as the second memory allocation strategy. Among them, this time is not the first time.

3. The optimization method according to claim 1 or 2, characterized in that: The application and allocation status include: the size of the memory block applied for and the number of times, the size of the memory block allocated and the number of times; The step of judging whether the conditions for re-formulating the first memory allocation strategy are met according to the recorded application and allocation status of the memory block size includes: If the size of the allocated second memory block is not an integer multiple of the minimum unit of data transmission between the processor and the memory, it is determined that the first condition for re-formulating the first memory allocation strategy is met. If there is a situation where the difference between the applied first memory block size and the allocated second memory block size is greater than the set first difference threshold, and the number of times this situation occurs is greater than the set number threshold, it is determined that the second condition for re-formulating the first memory allocation strategy is met, or If the difference between two adjacent first memory block sizes among the applied first memory block sizes is less than the set second difference threshold, and the difference between each applied first memory block size and its allocated second memory block size is greater than the set third difference threshold, it is determined to meet the third condition for re-formulating the first memory allocation strategy.

4. The optimization method according to claim 3, characterized in that: The judgment priority of the first condition is higher than the judgment priority of the second condition and the third condition, and the judgment priority of the second condition is the same as the judgment priority of the third condition; The re-formulating the first memory allocation strategy comprises: When the first condition is met, each memory block size is created according to different integer multiples of the minimum unit of data transmission between the processor and the memory, and each created memory block size is used as an allocation option in the allocation option set. In the case where the second condition is met, a memory block size is created according to the first memory block size, and the created memory size is added as an allocation option to the allocation option set, or When the third condition is met, the maximum memory block size among the adjacent first memory block sizes is increased as a basis, the set first redundancy is increased to obtain the created memory block size, and the memory block size is added as an allocation option to the allocation option set.

5. The optimization method according to claim 4, characterized in that: When the second condition is met or the third condition is met, the size of the memory block created is an integer multiple of the minimum unit of data transmission between the processor and the memory; The re-formulating the first memory allocation strategy further comprises: In the case that the number of allocation options in the newly created allocation option set is greater than the set number threshold, the allocation options in the allocation option set are adjusted so that the absolute value of the difference between the sizes of two adjacent memory blocks is greater than the set fourth difference threshold, and / or the allocation options created so that the applied first memory block size or the allocated second memory block size exists only once are deleted. The fourth difference threshold is determined according to the memory size consumed by an allocation option and the set second redundancy.

6. The optimization method according to claim 5, characterized in that: The detection of the first memory allocation strategy is detected by a flag bit used to mark that the first memory allocation strategy is reformulated; The allocation of the memory block size is performed by using a memory allocation strategy through a kmalloc function interface; Adjust the allocation options in the allocation option set, including: For each allocation option in an allocation option set, Check whether the memory size of the allocation option in the allocation option set and the size of its adjacent memory block meet the critical condition. If so, check whether the two adjacent memory block sizes meet the optimization cost condition. If so, retain the larger memory block size of the two adjacent memory block sizes and delete the smaller memory block size of the two adjacent memory block sizes. Check whether the reserved memory block size and another adjacent memory block size meet the above critical conditions and optimization cost conditions, and repeat the process until all allocation options in the allocation option set are checked. The critical condition is: the absolute value of the difference between the sizes of two adjacent memory blocks is smaller than the memory size consumed by the allocation option; The optimization cost condition is: the product of the absolute value of the difference between the sizes of two adjacent memory blocks and the number of allocation options in the current allocation option set is greater than or equal to the optimization benefit memory, The optimized benefit memory is determined according to the memory size consumed by the allocation option and the set expected benefit.

7. The optimization method according to claim 6, characterized in that: The second redundancy is determined according to the set expected benefit. The fourth difference threshold is: the ratio of the optimized revenue memory to the number of allocation options in the current allocation option set.

8. The optimization method according to claim 2, characterized in that: The shutdown further includes: In response to the system shutdown instruction, the step of determining whether the conditions for re-formulating the first memory allocation strategy are met according to the recorded application and allocation status of the memory block size is executed, In the case where the conditions for re-formulating the first memory allocation strategy are met, executing the step of re-formulating the first memory allocation strategy as the first memory allocation strategy used in the next system startup process, and using the first memory allocation strategy used this time as the second memory allocation strategy, setting the flag bit, and then performing the system shutdown operation; or If the conditions for re-establishing the first memory allocation strategy are not met, the flag is not set, the first memory allocation strategy used in this startup process or the default memory allocation strategy used in the first startup process is used as the second memory allocation strategy, and then the system is shut down.

9. A fragmented memory optimization device, characterized in that: The device includes: The memory allocation strategy selection module is used to allocate the memory block size using the default memory allocation strategy during the first system startup. The recording module is used to record the application and allocation status of the memory block size during the first operation after the system is first started. The memory allocation strategy formulation module is used to determine whether the conditions for re-formulating the first memory allocation strategy are met based on the recorded application and allocation status of the memory block size before the system is shut down for the first time. If so, the first memory allocation strategy is re-formulated as the first memory allocation strategy used during the next system startup process, and the default memory allocation strategy is used as the second memory allocation strategy.

10. The optimization device according to claim 9, characterized in that: The memory allocation strategy selection module is also used to allocate the memory block size using the first memory allocation strategy when the first memory allocation strategy is detected during the current system startup process, and to allocate the memory block size using the second memory allocation strategy used in the previous startup process when the first memory allocation strategy is not detected, wherein: The first memory allocation strategy is the memory allocation strategy that was re-established before the system was shut down last time. The recording module is also used to record the application and allocation status of the memory block size during the current operation after the system is started up. The memory allocation strategy formulation module is also used to determine whether the conditions for re-formulating the first memory allocation strategy are met before the system is shut down this time, based on the recorded application and allocation status of the memory block size. If so, the first memory allocation strategy is re-formulated as the first memory allocation strategy used during the next system startup process, and the first memory allocation strategy used this time is used as the second memory allocation strategy. Among them, this time is not the first time.

Citation Information

Cited By

  • Video memory allocation method and device and storage medium

    CN121144052A

  • A method, device and storage medium for allocating video memory

    CN121144052B