A memory processing method, product, electronic device and medium
By dividing thread types according to the memory size applied by threads and allocating memory resources using a multi-level resource pool mechanism, the problem of low memory application and release efficiency in a multi-threaded environment is solved, program performance is improved and memory usage is optimized.
Patent Information
- Application Number
- CN202411996499.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-30
- Publication Date
- 2025-04-25
- Estimated Expiration
- 2044-12-30
AI Technical Summary
In a multi-threaded environment, the long time required for memory application and release, resulting in serious impact on program performance.
By obtaining the memory size currently applied by each thread, the thread of the first type and the second type is determined, and memory resources are allocated to each type of thread according to the distribution of memory blocks in the first resource pool and the second resource pool. The first resource pool corresponds one by one to one with the first type of thread, while the second resource pool is shared by the second type of thread.
It reduces the memory application and release time, improves program performance, and effectively utilizes memory resources, avoids resource waste.
Smart Images

Figure CN119376961B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of resource allocation, and in particular to a memory processing method, product, electronic equipment and medium. Background Art
[0002] With the development of computer technology, multithreaded programming has become one of the key technologies to improve computing efficiency and resource utilization. In order to meet the requirements of high efficiency and thread safety of memory management in a multithreaded environment, memory pool technology came into being. Threads can directly apply for and release memory from the memory pool without making system calls every time.
[0003] In the related memory application or release methods, multiple threads often access a single memory pool. When a thread calls the memory pool function to apply for or release memory, it needs to obtain a global mutex lock first to ensure that only one thread can apply for or release memory from the memory pool at the same time, thereby maintaining the consistency of the memory pool data. However, if a large number of threads apply for or release memory at the same time, due to the existence of the mutex lock, only one thread can access the memory pool at the same time, and other threads can only wait, resulting in a long time required for memory application and release, which seriously affects the performance of the entire program using the memory pool.
[0004] It can be seen that how to improve the efficiency of memory application and release, so as to ensure the performance of programs using the memory pool, is a technical problem that people in this field need to solve urgently. Summary of the invention
[0005] The purpose of the present invention is to provide a memory processing method, product, electronic device and medium to solve the technical problem that when a thread calls a memory pool function to apply for or release memory, the time required for memory application and release is long, which seriously affects the performance of the entire program using the memory pool.
[0006] In order to solve the above technical problems, the present invention provides a memory processing method, comprising:
[0007] Get the memory size currently requested by each thread;
[0008] Determine the first type of threads and the second type of threads according to the memory size currently applied for by each thread; wherein the memory size currently applied for by each thread in the first type of threads is less than or equal to a first preset value; and the memory size currently applied for by each thread in the second type of threads is greater than the first preset value;
[0009] Allocating memory resources from the first memory pool or the shared pool to the first type of threads according to the distribution of memory blocks in the first resource pool; wherein the first memory pool corresponds to the first type of threads one by one;
[0010] Memory resources are allocated to the second type of threads according to the distribution of memory blocks in the second resource pool; wherein the second resource pool is a resource pool shared by all the second type of threads.
[0011] On the one hand, allocating memory resources from the first memory pool or the shared pool to the first type of thread according to the distribution of memory blocks in the first resource pool includes:
[0012] After determining that the first current thread is a thread of the first type, obtaining distribution of memory blocks in a first memory pool corresponding to the first current thread;
[0013] If a memory block that satisfies the application of the first current thread is determined according to the distribution of the memory blocks in the first memory pool corresponding to the first current thread, memory resources are allocated to the first current thread from the first memory pool;
[0014] If a memory block that satisfies the request of the first current thread cannot be determined based on the distribution of memory blocks in the first memory pool corresponding to the first current thread, memory resources are applied for from the shared pool through the first memory pool corresponding to the first current thread, and memory resources are allocated to the first current thread using the applied memory.
[0015] On the other hand, the shared pool includes a second memory pool and a page cache pool;
[0016] The applying for memory resources from the shared pool through the first memory pool corresponding to the first current thread includes:
[0017] Applying for memory resources from the second memory pool through the first memory pool corresponding to the first current thread;
[0018] If a memory block that satisfies the application of the first current thread is not determined according to the distribution of the memory blocks in the second memory pool, memory resources are applied for from the page cache pool through the second memory pool.
[0019] On the other hand, the first memory pool and the second memory pool are represented by a free memory block array, each element of the free memory block array is a free memory block linked list of the same size, there is a corresponding relationship between the subscripts of the free memory block array and the free memory block linked list, and the memory block sizes in the free memory block linked list corresponding to different subscripts are different.
[0020] On the other hand, determining whether a memory block that satisfies or does not satisfy the application of the first current thread according to the distribution of memory blocks in the second memory pool or according to the distribution of memory blocks in the first memory pool corresponding to the first current thread includes:
[0021] Determine whether there is a subscript of the free memory block array corresponding to the memory size applied for by the first current thread according to the corresponding relationship between the subscript of the free memory block array and the free memory block linked list;
[0022] If not, determining that the distribution of memory blocks in the second memory pool or the distribution of memory blocks in the first memory pool corresponding to the first current thread does not satisfy the memory blocks applied for by the first current thread;
[0023] If so, when it is detected that there is a memory block under the subscript of the free memory block array corresponding to the memory size requested by the first current thread, it is determined that the distribution of memory blocks in the second memory pool or the distribution of memory blocks in the first memory pool corresponding to the first current thread meets the memory block requested by the first current thread; when it is detected that there is no memory block under the subscript of the free memory block array corresponding to the memory size requested by the first current thread, it is determined that the distribution of memory blocks in the second memory pool or the distribution of memory blocks in the first memory pool corresponding to the first current thread does not meet the memory block requested by the first current thread.
[0024] On the other hand, the page cache pool is represented by a free memory interval array, each element of the free memory interval array is a free memory interval linked list of the same size, there is a corresponding relationship between the subscript of the free memory interval array and the free memory interval linked list, and the memory interval sizes in the free memory interval linked list corresponding to different subscripts are different.
[0025] On the other hand, after applying for memory resources from the page cache pool through the second memory pool, the method further includes:
[0026] Determine whether memory resources are successfully applied for from the page cache pool;
[0027] If yes, use the applied memory resources to allocate memory resources to the first current thread;
[0028] If not, apply for memory resources from the board through the page buffer pool, and use the memory resources applied from the board to allocate memory resources to the first current thread.
[0029] On the other hand, the determining whether the memory resource is successfully applied for from the page cache pool includes:
[0030] Determining an initial index of the free memory interval array in the page cache pool according to the size of the memory requested by the first current thread;
[0031] Searching for a free memory interval in the free memory interval linked list starting from the free memory interval linked list corresponding to the initial subscript to the free memory interval linked list corresponding to the maximum subscript among all subscripts of the free memory interval array;
[0032] If it is detected that there is the idle memory interval, it is determined that the memory resource is successfully applied for from the page cache pool;
[0033] If it is detected that the idle memory interval does not exist, it is determined that the memory resource has not been successfully applied for from the page cache pool.
[0034] On the other hand, allocating memory resources to the first current thread using the memory resources applied for from the board includes:
[0035] The memory resources applied for from the board are divided into a first memory resource and a second memory resource; wherein the size of the first memory resource is the memory size applied for by the second memory pool;
[0036] storing the second memory resource as a new memory interval in the page cache pool;
[0037] The first memory resource is used as the memory resource allocated to the first current thread.
[0038] On the other hand, the second resource pool includes a third memory pool and a fourth memory pool; wherein the maximum memory value allowed to be applied for by the third memory pool is smaller than the maximum memory value allowed to be applied for by the fourth memory pool;
[0039] The allocating memory resources to the second type of threads according to the distribution of memory blocks in the second resource pool includes:
[0040] After determining that the second current thread is a thread of the second type, obtaining a memory size requested by the second current thread;
[0041] When it is detected that the memory size requested by the second current thread is less than or equal to the maximum memory value allowed to be requested by the third memory pool, memory resources are allocated to the second current thread according to the distribution of memory blocks in the third memory pool;
[0042] When it is detected that the memory size requested by the second current thread is greater than the maximum memory value allowed by the third memory pool, and less than or equal to the maximum memory value allowed by the fourth memory pool, memory resources are allocated to the second current thread according to the distribution of memory blocks in the fourth memory pool.
[0043] On the other hand, obtaining the memory size requested by the second current thread includes:
[0044] Get the first memory size requested by the second current thread;
[0045] Adjusting the first memory size to a second memory size; wherein the second memory size is larger than the first memory size;
[0046] Using the second memory size as the memory size requested by the second current thread;
[0047] Allocating memory resources to the second current thread according to the distribution of memory blocks in the third memory pool or the distribution of memory blocks in the fourth memory pool includes:
[0048] If multiple memory blocks greater than or equal to the second memory size are found from the third memory pool or the fourth memory pool, a memory block is randomly selected from all the memory blocks greater than or equal to the second memory size to allocate memory resources to the second current thread, or the smallest memory block is selected from all the memory blocks greater than or equal to the second memory size to allocate memory resources to the second current thread.
[0049] On the other hand, determining the second memory size includes:
[0050] Determine whether the first memory size is less than or equal to a preset byte;
[0051] If yes, determining that the second memory size is the preset bytes;
[0052] If not, adopting the first strategy or the second strategy to determine the second memory size;
[0053] Wherein, adopting the first strategy to determine the second memory size includes:
[0054] Obtaining a pre-established correspondence between the number of segments, the segment blocks, and the memory size threshold of the segment blocks, determining a target segment block corresponding to the first memory size according to the memory size threshold in the correspondence, and segmenting the target segment block according to the number of segments corresponding to a previous segment block of the target segment block; obtaining a sub-segment block where the first memory size is located in the target segment block after segmentation, and using the memory size threshold corresponding to the sub-segment block as the second memory size;
[0055] Adopting the second strategy to determine the second memory size includes:
[0056] Get the preset rounding size;
[0057] The second memory size is determined according to the first memory size and the rounded size.
[0058] On the other hand, the randomly selecting a memory block from all found memory blocks that are greater than or equal to the second memory size to allocate memory resources to the second current thread, or selecting the smallest memory block from all found memory blocks that are greater than or equal to the second memory size to allocate memory resources to the second current thread includes:
[0059] Randomly select a memory block from all found memory blocks that are greater than or equal to the second memory size, or select the smallest memory block from all found memory blocks that are greater than or equal to the second memory size, and determine whether the size of the selected memory block is greater than the second memory size;
[0060] If yes, the selected memory block is divided into a first part of memory and a second part of memory; the first part of memory is allocated to the second current thread to allocate memory resources, and the second part of memory is placed in the second resource pool; wherein the first part of memory is greater than or equal to the size of the second memory;
[0061] If not, memory resources are allocated to the second current thread using the selected memory block.
[0062] In another aspect, the method further comprises:
[0063] If no memory block greater than or equal to the second memory size is found from the third memory pool and the fourth memory pool, a memory block is requested from the board to obtain the selected memory block; and the step of determining whether the size of the selected memory block is greater than the second memory size is returned.
[0064] On the other hand, determining the size of the memory block requested from the board includes:
[0065] In the case where it is detected that the first strategy is in the enabled state, determining whether the second memory size is less than or equal to a second preset value;
[0066] If so, determining that the size of the memory block requested from the board is greater than the second preset value;
[0067] If not, if it is detected that the second memory size is smaller than the third preset value, it is determined that the size of the memory block applied for from the board is greater than the third preset value; if it is detected that the second memory size is greater than or equal to the third preset value, the second strategy is used to determine the size of the memory block applied for from the board.
[0068] On the other hand, determining the size of the memory block requested from the board includes:
[0069] In the case where it is detected that the first strategy is in the closed state, determining whether the second memory size is less than or equal to a fourth preset value; wherein the fourth preset value is greater than the first preset value and less than the second preset value;
[0070] If yes, determining that the size of the memory block requested from the board is greater than the fourth preset value;
[0071] If not, define the value of the variable corresponding to the second memory size as the second memory size;
[0072] In the case where it is detected that the third strategy is in the on state, the third strategy is adopted to obtain the value corresponding to the variable, and if it is detected that the value corresponding to the variable is less than or equal to the fifth preset value, it is determined that the size of the memory block applied for from the board is greater than the fifth preset value; if it is detected that the value corresponding to the variable is greater than the fifth preset value, the second strategy is adopted to determine the size of the memory block applied for from the board; wherein the fifth preset value is greater than the fourth preset value and less than the second preset value;
[0073] In the case where it is detected that the third strategy is in the off state, the step of detecting the relationship between the value corresponding to the variable and the fifth preset value is entered;
[0074] The adopting the third strategy to determine the size of the memory block to be applied for from the board includes:
[0075] Obtaining a maximum value among the second memory size, the free memory size, and the rated value of memory allocation;
[0076] The maximum value is used as the size of the memory block requested from the board.
[0077] In another aspect, the method further comprises:
[0078] Upon receiving a memory release request corresponding to the second resource pool, obtaining a memory pool of memory to be released;
[0079] Determine whether there is a memory block to be merged corresponding to the memory block to be released;
[0080] If yes, merging the memory block to be released with the memory block to be merged, and returning the merged memory block to the second resource pool;
[0081] If not, the memory block to be released is returned to the second resource pool.
[0082] In order to solve the above technical problem, the present invention also provides a computer program product, including a computer program / instruction, which implements the steps of the above memory processing method when executed by a processor.
[0083] In order to solve the above technical problem, the present invention further provides an electronic device, comprising:
[0084] Memory for storing computer programs;
[0085] A processor is used to implement the steps of the above-mentioned memory processing method when executing the computer program.
[0086] In order to solve the above technical problem, the present invention also provides a non-volatile storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the steps of the above memory processing method are implemented.
[0087] The beneficial effect of the present invention is that, in the method, firstly, for the first type of threads whose applied memory size is less than or equal to the first preset value, the first resource pool is used to allocate memory resources for the first type of threads. Since the first memory pool in the first resource pool corresponds one-to-one to the first type of threads, each thread in the first type of threads can find the first memory pool corresponding to its own thread and obtain memory resources from the first memory pool corresponding to its own thread. This operation does not require obtaining a mutex lock, which avoids the waiting problem caused by multiple threads competing for the same mutex lock, thereby reducing the long time for memory application and release, and ensuring the performance of programs using memory pools; secondly, since only the first memory pool corresponding to the thread is set for each thread in the first type of threads, each thread in the second type of threads shares the resource pool, instead of setting a one-to-one corresponding memory pool for all threads. Therefore, the method provided by the present invention will not cause a large amount of memory resource waste; thirdly, memory resources are allocated to the first type of threads considering the distribution of memory blocks in the first resource pool, and memory resources are allocated to the second type of threads considering the distribution of memory blocks in the second resource pool, so that the allocated memory resources are more appropriate; in addition, memory resources are allocated to the first type of threads from the first memory pool or the shared pool according to the distribution of memory blocks in the first resource pool, that is, the first resource pool is a hybrid resource pool. If the thread cannot obtain memory resources from the corresponding memory pool, it can obtain memory resources from the shared pool, thereby improving the success rate of the thread in obtaining memory resources.
[0088] In addition, after determining that the first current thread is a thread of the first type, the memory block that meets the application of the first current thread is first determined based on the distribution of memory blocks in the first memory pool corresponding to the first current thread, and memory resources are allocated to the first current thread from the first memory pool, thereby reducing the time for memory application and release; if the memory block that meets the application of the first current thread is not found in the first memory pool, memory resources are applied for from the shared memory pool, thereby ensuring as much as possible that memory resources can be allocated to the thread.
[0089] The shared pool in the first resource pool includes the second memory pool and the page cache pool, that is, the shared pool is also a hybrid resource pool. When the first current thread applies for memory resources from the shared pool, it first applies for memory resources from the second memory pool through the first memory pool corresponding to the first current thread. If the memory block that satisfies the application of the first current thread is not obtained from the second memory pool, it applies for memory resources from the page cache pool through the second memory pool, thereby ensuring that memory resources can be allocated to the thread as much as possible.
[0090] The first memory pool and the second memory pool are represented by an array of free memory blocks, and each element is a linked list of free memory blocks of the same size. The memory addresses are allocated to the requester in batches with the memory block as the unit, thereby reducing frequent memory allocation operations. And because there is a corresponding relationship between the subscript of the free memory block array and the free memory block linked list, the memory block sizes in the free memory block linked list corresponding to different subscripts are different, so when requesting a memory resource application from the first memory pool or the second memory pool, the corresponding array subscript can be found according to the memory size of the requested resource, and then the location of the memory resource requested from the first memory pool and the second memory pool can be quickly located.
[0091] The page cache pool is represented by a free memory interval array. Each element of the free memory interval array is a free memory interval linked list of the same size. Memory addresses are allocated to requesters in batches using memory intervals as units, reducing frequent memory allocation operations. And because there is a corresponding relationship between the subscripts of the free memory interval array and the free memory interval linked list, the sizes of memory intervals in the free memory interval linked list corresponding to different subscripts are different, so when requesting a memory resource from the page cache pool, the corresponding array subscript can be found according to the memory size of the requested resource, and then the initial position of the memory resource requested from the page cache pool can be quickly located. When it is detected that there is no free memory interval at the initial position, the free memory interval is searched from the free memory interval linked list corresponding to the initial subscript to the free memory interval linked list corresponding to the maximum subscript among all the subscripts of the free memory interval array, so as to ensure that memory resources can be allocated to the thread as much as possible.
[0092] When the memory resource is not successfully applied for from the page cache pool, the memory resource is applied for from the board through the page cache pool, and the memory resource applied for from the board is used to allocate the memory resource to the first current thread, thereby improving the success rate of the thread applying for the memory resource.
[0093] The memory resources applied for from the board are divided into the first memory resources and the second memory resources, one part is used as the memory resources allocated to the first current thread, and the other part is stored in the page cache pool as a new memory interval. This avoids allocating too much memory resources to the thread and reduces the waste of resources.
[0094] The second resource pool includes a third memory pool and a fourth memory pool, and the maximum memory value allowed to be applied for by the third memory pool is smaller than the maximum memory value allowed to be applied for by the fourth memory pool. When it is determined that the second current thread is a thread of the second type, a suitable memory pool is found according to the memory size applied for by the thread to allocate resources for it, thereby avoiding the situation where the applied memory resources cannot be met or too many memory resources are allocated, and ensuring that memory resources can be reasonably allocated to the thread as much as possible.
[0095] After obtaining the first memory size applied for by the second current thread, the first memory size is adjusted to the second memory size. Since the second memory size is larger than the first memory size, using the second memory size as the size of the applied memory can reduce the number of memory applications. If multiple memory blocks greater than or equal to the second memory size are found from the third memory pool or the fourth memory pool, the smallest memory block is selected from all the memory blocks greater than or equal to the second memory size found to allocate memory resources to the second current thread, thereby reducing waste of resources.
[0096] Different strategies (including the first strategy or the second strategy) are adopted to determine the second memory size, so as to realize flexible determination of the second memory size; and when the first strategy is adopted to determine the second memory size, the target segmentation block corresponding to the first memory size is segmented through the correspondence between the pre-established number of segmentation portions, the segmentation block and the memory size threshold of the segmentation block, and the sub-segmentation block where the first memory size is located in the target segmentation block after segmentation is obtained, and the memory size threshold corresponding to the sub-segmentation block is used as the second memory size. By segmenting the target segmentation block, it is realized to more accurately determine the second memory size to which the first memory size is to be adjusted.
[0097] When the size of the selected memory block is larger than the second memory size, the selected memory block is segmented, and a portion is allocated to the second current thread to allocate memory resources after switching, and the other portion is placed in the second resource pool, thereby avoiding waste of resources.
[0098] If no memory block greater than or equal to the second memory size is found from the third memory pool and the fourth memory pool, a memory block is applied for from the board to obtain the selected memory block, thereby ensuring that memory resources can be allocated to the thread.
[0099] The size of the memory block applied for by the board is judged against different preset values, and different strategies are adopted to determine the size of the memory block applied for the board, thereby reducing the time consumed for applying for memory from the board each time.
[0100] Before releasing a memory block, the previously split memory blocks are merged first and then released after merging, which reduces memory fragmentation. Merging free memory blocks can more effectively utilize memory space and avoid the problem of memory waste caused by small free memory blocks being unable to meet new memory requests.
[0101] In addition, the present invention also provides a computer program product, an electronic device, and a non-volatile storage medium, which have the same or corresponding technical features as the above-mentioned memory processing method and have the same effects as above. BRIEF DESCRIPTION OF THE DRAWINGS
[0102] In order to more clearly illustrate the embodiments of the present invention, the following briefly introduces the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0103] Figure 1 A schematic diagram of a multi-threaded access to a single memory pool provided by an embodiment of the present invention;
[0104] Figure 2 A schematic diagram of the distribution of a resource pool provided in an embodiment of the present invention;
[0105] Figure 3 A schematic diagram of an embodiment of the present invention in which each thread has its own first memory pool;
[0106] Figure 4 A flowchart of a memory processing method provided by an embodiment of the present invention;
[0107] Figure 5 A schematic diagram of a free memory block linked list corresponding to free memory block array subscripts from 0 to 71 in a first memory pool and a second memory pool provided by an embodiment of the present invention;
[0108] Figure 6 A schematic diagram of a free memory block linked list corresponding to free memory block array subscripts from 72 to 183 in a first memory pool and a second memory pool provided by an embodiment of the present invention;
[0109] Figure 7 A schematic diagram of a free memory block linked list corresponding to free memory interval array subscripts from 1 to 128 of a page cache pool provided in an embodiment of the present invention;
[0110] Figure 8 A memory management system architecture diagram provided by an embodiment of the present invention;
[0111] Fig. 9 A schematic diagram of a calculation mechanism of a segmentation strategy provided by an embodiment of the present invention;
[0112] Fig.10 A schematic diagram of a segmentation strategy provided by an embodiment of the present invention;
[0113] Fig.11A flowchart of a method for calculating a second memory size size2 by using a first memory size size1 provided by an embodiment of the present invention;
[0114] Fig.12 A flowchart of a method for calculating a memory size size3 applied to a board card by using a second memory size size2 provided by an embodiment of the present invention;
[0115] Fig.13 A flowchart of a memory application method of a memory allocator 2 provided in an embodiment of the present invention;
[0116] Fig.14 A flowchart of a method for releasing memory of a memory allocator 2 provided in an embodiment of the present invention;
[0117] Fig.15 A structural diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0118] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0119] The core of the present invention is to provide a memory processing method, product, electronic device and medium to solve the technical problem that when a thread calls a memory pool function to apply for or release memory, it takes a long time to apply for and release memory, which seriously affects the performance of the entire program using the memory pool.
[0120] There is no limitation on the scenarios of multi-threaded application of this application. For example, since most artificial intelligence training or reasoning programs are multi-threaded, the memory management optimization needs to consider the situation where multiple threads apply for and release memory at the same time. Figure 1 A schematic diagram of a multi-threaded access to a single memory pool provided by an embodiment of the present invention, such as Figure 1As shown, threads 1 to N all access the same memory pool to apply for and release memory. When the program is initialized, the memory pool will also be initialized accordingly. When a thread calls the memory pool function to apply for or release memory, it is necessary to first obtain a global mutex lock. The role of the mutex lock ensures that only one thread can apply for or release memory from the memory pool at the same time, thereby maintaining the consistency of the memory pool data. After the application or release of memory is completed, the mutex lock is released, and then other threads can apply for or release memory from the memory pool. In the method of multi-threaded access to a single memory pool, due to the existence of the mutex lock, if a large number of threads apply for or release memory at the same time, and only one thread can obtain the mutex lock at the same time, the other threads can only be in a waiting state, which will seriously affect the performance of the entire program. Therefore, the embodiment of the present invention proposes a new memory processing method to improve the efficiency of memory application and release, thereby ensuring the performance of the program using the memory pool.
[0121] In order to enable those skilled in the art to better understand the solution of the present invention, the present invention is further described in detail below in conjunction with the accompanying drawings and specific implementation methods. Figure 2 A schematic diagram of a resource pool distribution provided by an embodiment of the present invention is shown in FIG. Figure 2 As shown, the resource pool includes a first resource pool and a second resource pool. The first resource pool includes a first memory pool and a shared pool (a memory pool that can be accessed by multiple threads is called a shared pool). In addition, the shared pool includes a second memory pool and a page cache pool. The second resource pool is also a shared pool. The second resource pool includes a third memory pool and a fourth memory pool. In the embodiment of the present invention, each first memory pool is connected to a first memory pool. Figure 3 A schematic diagram of an embodiment of the present invention in which each thread has its own first memory pool is provided, taking three threads as an example. Figure 3 As shown, thread 1 corresponds to first memory pool one, thread 2 corresponds to first memory pool two, and thread 3 corresponds to first memory pool three. Figure 4 A flowchart of a memory processing method provided by an embodiment of the present invention is as follows: Figure 4 As shown, the method includes:
[0122] S10: Get the memory size currently requested by each thread;
[0123] S11: Determine the first type of thread and the second type of thread according to the memory size currently applied for by each thread; wherein the memory size currently applied for by each thread in the first type of thread is less than or equal to a first preset value; and the memory size currently applied for by each thread in the second type of thread is greater than the first preset value;
[0124] S12: Allocate memory resources from the first memory pool or the shared pool to the first type of threads according to the distribution of memory blocks in the first resource pool; wherein the first memory pool corresponds to the first type of threads one by one;
[0125] S13: Allocate memory resources to the second type of threads according to the distribution of memory blocks in the second resource pool; wherein the second resource pool is a resource pool shared by all the second type of threads.
[0126] Taking thread 1 and thread 2 applying for memory resources as an example, the memory size applied for by thread 1 in the current application is obtained, and the memory size applied for by thread 2 in the current application is obtained respectively. The relationship between the memory size currently applied for by each thread and the first preset value is judged, and then thread 1 and thread 2 are thread classified. The purpose of thread classification is to determine the resource pool that will be used to allocate memory resources to the thread. There is no limitation on the first preset value, such as 64KB. In an embodiment of the present invention, when it is judged that the memory size currently applied for by the thread is less than or equal to the first preset value, the thread is called a first type of thread; when it is judged that the memory size currently applied for by the thread is greater than the first preset value, the thread is called a second type of thread. For the first type of thread, the present invention uses the first resource pool to allocate memory resources for it. The first resource pool is a hybrid resource pool, including a first memory pool corresponding to the first type of thread and a shared pool.
[0127] In implementation, allocating memory resources from the first memory pool or the shared pool to the first type of thread according to the distribution of memory blocks in the first resource pool includes:
[0128] After determining that the first current thread is a thread of the first type, obtaining distribution of memory blocks in a first memory pool corresponding to the first current thread;
[0129] If a memory block that satisfies the application of the first current thread is determined according to the distribution of the memory blocks in the first memory pool corresponding to the first current thread, memory resources are allocated to the first current thread from the first memory pool;
[0130] If a memory block that satisfies the application of the first current thread is not determined according to the distribution of memory blocks in the first memory pool corresponding to the first current thread, memory resources are applied for from the shared pool through the first memory pool corresponding to the first current thread, and memory resources are allocated to the first current thread using the applied memory.
[0131] It is worth noting that the shared pool in the embodiment of the present invention also adopts a hybrid resource pool, which includes a second memory pool and a page cache pool (such as Figure 2 as shown).
[0132] Applying for memory resources from the shared pool through the first memory pool corresponding to the first current thread includes:
[0133] Apply for memory resources from the second memory pool through the first memory pool corresponding to the first current thread;
[0134] If a memory block that satisfies the application of the first current thread is not determined according to the distribution of the memory blocks in the second memory pool, memory resources are applied for from the page cache pool through the second memory pool.
[0135] The first memory pool and the second memory pool are represented by a free memory block array, each element of the free memory block array is a linked list of free memory blocks of the same size, there is a corresponding relationship between the subscripts of the free memory block array and the free memory block linked list, and the memory block sizes in the free memory block linked list corresponding to different subscripts are different. Figure 5 A schematic diagram of a free memory block linked list corresponding to free memory block array subscripts from 0 to 71 in a first memory pool and a second memory pool provided by an embodiment of the present invention, such as Figure 5 As shown, the correspondence between the array subscript and the corresponding array element (memory block linked list) is as follows: 1. When the array subscript is [0,15], the memory block size in the memory block linked list corresponding to the array element increases by 8 bytes, that is, when the subscript is 0, the memory block size is 8 bytes; when the subscript is 1, the memory block size is 16 bytes; when the subscript is 2, the memory block size is 24 bytes; when the subscript is 15, the memory block size is 128 bytes. 2. When the array subscript is [16,71], the memory block size in the memory block linked list corresponding to the array element increases by 16 bytes, that is, when the subscript is 16, the memory block size is 144 bytes; when the subscript is 17, the memory block size is 160 bytes; when the subscript is 71, the memory block size is 1024 bytes. Figure 6 A schematic diagram of a free memory block linked list corresponding to free memory block array subscripts from 72 to 183 in a first memory pool and a second memory pool provided by an embodiment of the present invention, such as Figure 6 As shown in the figure, 1. When the array subscript is [72,127], the memory block size in the memory block linked list corresponding to the array element increases by 128 bytes, that is, when the subscript is 72, the memory block size is 1152 bytes; when the subscript is 73, the memory block size is 1280 bytes; when the subscript is 127, the memory block size is 8192 bytes. 2. When the array subscript is [128,183], the memory block size in the memory block linked list corresponding to the array element increases by 1024 bytes, that is, when the subscript is 128, the memory block size is 9216 bytes; when the subscript is 129, the memory block size is 10240 bytes; when the subscript is 183, the memory block size is 65536 bytes.
[0136] Based on the data representation of the first memory pool and the second memory pool, determining the memory blocks that meet or do not meet the application of the first current thread according to the distribution of the memory blocks in the second memory pool or according to the distribution of the memory blocks in the first memory pool corresponding to the first current thread includes:
[0137] According to the correspondence between the subscript of the free memory block array and the free memory block linked list, it is determined whether there is a subscript of the free memory block array corresponding to the memory size requested by the first current thread;
[0138] If not, determining that the distribution of the memory blocks in the second memory pool or the distribution of the memory blocks in the first memory pool corresponding to the first current thread does not satisfy the memory blocks requested by the first current thread;
[0139] If so, when it is detected that there is a memory block under the subscript of the free memory block array corresponding to the memory size requested by the first current thread, it is determined that the distribution of memory blocks in the second memory pool or the distribution of memory blocks in the first memory pool corresponding to the first current thread meets the memory block requested by the first current thread; when it is detected that there is no memory block under the subscript of the free memory block array corresponding to the memory size requested by the first current thread, it is determined that the distribution of memory blocks in the second memory pool or the distribution of memory blocks in the first memory pool corresponding to the first current thread does not meet the memory block requested by the first current thread.
[0140] Taking the requested memory size of 64KB as an example, if there is no memory block under subscript 183 in the first memory pool corresponding to the first current thread (because it has been occupied, there is no memory block), it is determined that the distribution of memory blocks in the first memory pool corresponding to the first current thread does not meet the memory blocks requested by the first current thread.
[0141] When the memory block that satisfies the application of the first current thread is not determined according to the distribution of the memory blocks in the second memory pool, the memory resource is applied to the page cache pool through the second memory pool. The page cache pool is represented by a free memory interval array, each element of the free memory interval array is a free memory interval linked list of the same size, and there is a corresponding relationship between the subscript of the free memory interval array and the free memory interval linked list, and the memory interval sizes in the free memory interval linked list corresponding to different subscripts are different. In the embodiment of the present invention, the free memory block applied by the second memory pool from the page cache pool is called a "memory interval". Figure 7 A schematic diagram of a free memory block linked list corresponding to free memory interval array subscripts from 1 to 128 of a page buffer pool provided in an embodiment of the present invention. The size of the memory interval in the array element corresponding to array subscript 1 is 1 page (the size of 1 page depends on the board itself, for example, the memory page size of the board is 8KB), the size of the memory interval in the array element corresponding to array subscript 2 is 2 pages, and the size of the memory interval in the array element corresponding to array subscript 128 is 128 pages.
[0142] When applying for memory resources from the page cache pool through the second memory pool, the memory resources may be applied for successfully or unsuccessfully. Therefore, after applying for memory resources from the page cache pool through the second memory pool, the following steps are also required:
[0143] Determine whether memory resources are successfully applied for from the page cache pool;
[0144] If so, use the applied memory resources to allocate memory resources to the first current thread;
[0145] If not, memory resources are applied for from the board through the page buffer pool, and the memory resources applied for from the board are used to allocate memory resources to the first current thread.
[0146] Specifically, judging whether the memory resource is successfully applied from the page cache pool includes:
[0147] Determine the initial index of the free memory interval array in the page cache pool according to the size of the memory requested by the first current thread;
[0148] Search for a free memory interval in the free memory interval linked list starting from the free memory interval linked list corresponding to the initial subscript to the free memory interval linked list corresponding to the maximum subscript among all subscripts in the free memory interval array;
[0149] If an idle memory interval is detected, it is determined that the memory resource is successfully applied for from the page cache pool;
[0150] If it is detected that there is no free memory interval, it is determined that the memory resource has not been successfully applied for from the page cache pool.
[0151] When the second memory pool requests memory from the page cache pool, the page cache pool will search for a memory interval of appropriate size and return it to the second memory pool. When searching, the page cache pool will convert the requested memory size into an integer multiple of the memory page size (round up). For example, if n memory pages are needed in the end, the memory interval will be searched from the element with index n in the free memory interval array. If not found, the memory interval will be searched from the array element with index n+1. If still not found, the memory interval will be searched from the array element with index n+2.
[0152] In the case where no memory block is found from the page cache pool, in the embodiment of the present invention, memory resources are further applied to the board through the page cache pool, and memory resources are allocated to the first current thread using the memory resources applied from the board. When applying for memory resources from the board, the memory size applied for each time is 1MB. In order to avoid wasting memory resources, in implementation, using the memory resources applied from the board to allocate memory resources to the first current thread includes:
[0153] The memory resources applied for from the board are divided into a first memory resource and a second memory resource; wherein the size of the first memory resource is the memory size applied for by the second memory pool;
[0154] storing the second memory resource as a new memory interval in the page cache pool;
[0155] The first memory resource is used as the memory resource allocated to the first current thread.
[0156] There is no limitation on the first memory resource and the second memory resource, but it is necessary to ensure that the first memory resource allocated to the first current thread meets the memory size currently requested by the first current thread.
[0157] The above describes a scenario in which the first resource pool is used to allocate memory resources to the first type of thread. In practice, the first resource pool can be managed by memory allocator 1, and the second resource pool can be managed by memory allocator 2. Since the second resource pool includes the third memory pool and the fourth memory pool; the maximum memory value allowed to be applied for by the third memory pool is less than the maximum memory value allowed to be applied for by the fourth memory pool, the third resource pool can be referred to as a small memory pool and the fourth resource pool can be referred to as a large memory pool in the embodiment of the present invention. Figure 8 A memory management system architecture diagram provided by an embodiment of the present invention, such as Figure 8 As shown, the memory management system mainly includes a memory application and release forwarder and a memory allocator. The memory allocator includes a memory allocator 1 and a memory allocator 2. The memory allocator 1 includes a thread memory management instance, a thread memory pool (since the first memory pool corresponds to the first type of thread one by one, the first memory pool is directly referred to as the thread memory pool here), a shared memory pool (since the first memory pool is a memory pool shared by the first type of threads, the second memory pool is directly referred to as the shared memory pool here) and a page cache pool. Figure 8 Three thread memory management instances and corresponding three thread memory pools are listed in the figure. Thread memory management instance 1 is connected to thread memory pool 1, thread memory management instance 2 is connected to thread memory pool 2, and thread memory management instance 3 is connected to thread memory pool 3. Memory allocator 2 includes memory manager, large memory, small memory and configuration center.
[0158] Specifically, each part is described below.
[0159] Memory request and release forwarder: forwards the memory request to different memory allocators according to the memory size requested; accordingly, the memory request and release forwarder determines whether memory allocator 1 or memory allocator 2 will release the memory based on the memory pointer passed into the memory release function. In order to forward the request to the correct memory allocator when releasing the memory, it maintains a mapping between memory pointer and memory size. Every time the memory is released, it will search for the pointer passed in in this mapping. After finding it, it will know which memory allocator to forward the memory release request to based on the corresponding memory size.
[0160] Memory allocator: The memory allocator is responsible for specific memory application and release operations. The memory management system contains two memory allocators, which are listed as follows:
[0161] Memory allocator 1: When the memory requested or released is less than or equal to 64KB, the memory request and release forwarder will forward the memory request or release operation to memory allocator 1. That is, memory allocator 1 is responsible for the memory request and release of 64KB or less. Memory allocator 1 contains the following modules:
[0162] Thread memory management instance: The multi-strategy memory management system will create a thread memory management instance for each user thread. It is responsible for the memory application and release operations of the corresponding thread. Each thread memory management instance is connected to a thread memory pool. When receiving a memory application request, the thread memory management instance will search for a free memory block of the appropriate size in the thread memory pool and return it. When receiving a memory release request, the thread memory management instance will return the memory to the thread memory pool.
[0163] Thread memory pool: The thread memory pool contains an array of free memory blocks (such as Figure 5 and Figure 6 As shown in the figure). When applying for memory, the memory size passed by the memory management instance is mapped to the subscript of the array by the thread memory pool according to the memory size passed by the thread memory management instance, and the free memory block is searched. If it is found, it is returned directly; if it is not found, the thread memory pool will apply for memory from the shared memory pool and return it to the thread memory management instance. When releasing memory, if there are too many free memory blocks in the thread memory pool, the excess free memory blocks will be returned to the shared memory pool.
[0164] Shared memory pool: It is a memory pool shared by all threads. Only when there is no suitable memory block in the thread memory pool will it be allocated from the shared memory pool. If there is no suitable free memory block in the shared memory pool, the shared memory pool will apply for memory from the page cache pool. The free memory block requested by the shared memory pool from the page cache pool is called a "memory interval". The memory interval is cut into small blocks and provided to the thread memory pool. If all small blocks of the same memory interval are subsequently returned to the shared memory pool, then this memory interval is in an idle state. At this time, this memory interval will be returned to the page cache pool. Like the thread memory pool, the shared memory pool also contains an array of free memory blocks. Each element of the array is a linked list of free memory blocks of the same size. The correspondence between the array subscript and the corresponding array element (memory block linked list) is the same as that of the thread memory pool, which will not be repeated here.
[0165] Page Cache Pool: The page cache pool contains an array of free memory intervals (such as Figure 7 (As shown in the figure) When the shared memory pool requests memory from the page cache pool, the page cache pool will search for a memory interval of appropriate size and return it to the shared memory pool. If it is not found, the page cache pool will call the runtime memory request function provided by the board to request memory of a certain size. This memory will then be divided into the size of the memory requested by the shared memory pool and returned to the shared memory pool. The remaining memory after the division becomes another memory interval and is placed in the page cache pool. When releasing memory, the shared memory pool may return the memory interval to the page cache pool, and the returned memory interval will be cached in the page cache pool for subsequent use.
[0166] The above text describes in detail how the first type of thread (i.e., thread whose currently applied memory size is less than or equal to the first preset value) uses the first resource pool to allocate memory resources to the thread. The following describes in detail how the second type of thread (i.e., thread whose currently applied memory size is greater than the first preset value) uses the second resource pool to allocate memory resources to the thread. The second resource pool includes a third memory pool and a fourth memory pool; wherein the maximum memory value allowed to be applied for by the third memory pool is less than the maximum memory value allowed to be applied for by the fourth memory pool (that is, the third memory pool is called a small memory pool, and the fourth memory pool is called a large memory pool). For example, the memory resources applied for by the third memory pool are between 64KB and 1MB, and the memory resources applied for by the fourth memory pool are greater than 1MB.
[0167] Allocating memory resources to threads of the second type according to the distribution of memory blocks in the second resource pool includes:
[0168] After determining that the second current thread is a thread of the second type, obtaining a memory size requested by the second current thread;
[0169] When it is detected that the memory size requested by the second current thread is less than or equal to the maximum memory value allowed to be requested by the third memory pool, memory resources are allocated to the second current thread according to the distribution of memory blocks in the third memory pool;
[0170] When it is detected that the memory size requested by the second current thread is greater than the maximum memory value allowed by the third memory pool, and less than or equal to the maximum memory value allowed by the fourth memory pool, memory resources are allocated to the second current thread according to the distribution of memory blocks in the fourth memory pool.
[0171] In order to reduce the number of times memory is requested, in implementation, obtaining the memory size requested by the second current thread includes:
[0172] Get the first memory size requested by the second current thread;
[0173] Adjusting the first memory size to a second memory size; wherein the second memory size is larger than the first memory size;
[0174] The second memory size is used as the memory size requested by the second current thread;
[0175] Allocating memory resources to the second current thread according to the distribution of memory blocks in the third memory pool or the distribution of memory blocks in the fourth memory pool includes:
[0176] If multiple memory blocks greater than or equal to the second memory size are found from the third memory pool or the fourth memory pool, a memory block is randomly selected from all the memory blocks greater than or equal to the second memory size to allocate memory resources to the second current thread, or the smallest memory block is selected from all the memory blocks greater than or equal to the second memory size to allocate memory resources to the second current thread.
[0177] In the embodiment of the present invention, the first memory size (ie, the memory size requested by the memory application from the second resource pool) is represented by size1, and the second memory size (ie, the rounded memory size) is represented by size2.
[0178] Determining the second memory size includes:
[0179] Determine whether the first memory size is less than or equal to a preset byte;
[0180] If yes, determining the second memory size to be a preset byte;
[0181] If not, the first strategy or the second strategy is adopted to determine the second memory size.
[0182] The preset bytes are, for example, 512 bytes. Wherein, adopting the first strategy to determine the second memory size includes:
[0183] Obtain a pre-established correspondence between the number of partitions, the partition blocks and the memory size threshold of the partition blocks, determine the target partition block corresponding to the first memory size according to the memory size threshold in the correspondence, and partition the target partition block according to the number of partitions corresponding to the previous partition block of the target partition block; obtain the sub-partition block of the first memory size in the target partition block after the partition, and use the memory size threshold corresponding to the sub-partition block as the second memory size.
[0184] The first strategy is referred to herein as the division strategy. Fig. 9 A schematic diagram of a calculation mechanism of a segmentation strategy provided by an embodiment of the present invention is shown as follows: Fig. 9 As shown, the first line is the threshold, the second line is the number of shares, and the third line is the subscript. Each small grid is called a partition block. In the divisions strategy, an array is defined, which has a total of 16 elements, with subscripts from 0 to 15. The 0th element represents the divisions with more than 1MB of memory, the 1st element represents the divisions with more than 2MB of memory, and so on. As the array subscript increases by 1, the memory size represented by the corresponding element doubles, until the 15th element represents the divisions with more than 32GB of memory. Fig.10 A schematic diagram of a partitioning strategy provided by an embodiment of the present invention. If the memory size size1 applied by the user is 5000KB, size1 is between 4MB and 8MB, so 4MB of divisions is used, that is, the partition value divisions=4, and the difference between 8MB and 4MB is divided into divisions, that is, divided into 4 parts, each of which is 1MB in size. At this time, the value of size1 is within the first part, that is, 5000KB is less than 5120KB, and size2 is assigned 5120KB. This is the calculation mechanism of the divisions strategy.
[0185] In addition to the first strategy described above, the second memory size can also be determined by a second strategy. When the second strategy is used to determine the second memory size, a preset rounding size is obtained; and the second memory size is determined according to the first memory size and the rounding size.
[0186] In the embodiment of the present invention, the second strategy is called "round up strategy". There is no limitation on the selected rounding size. For example, the selected rounding size size_tmp is 512 bytes. When calculating size2 based on size1 (that is, the value of size1 needs to be rounded up to size_tmp), the calculation formula is: (size1 + size_tmp - 1) / size_tmp.
[0187] In order to enable those skilled in the art to better understand the above process of calculating the second memory size size2 by using the first memory size size1, it is described below with reference to the accompanying drawings and specific embodiments. Fig.11 A flowchart of a method for calculating a second memory size size2 by using a first memory size size1 is provided in an embodiment of the present invention, such as Fig.11 As shown, the method includes:
[0188] S14: The size of the memory application is the first memory size;
[0189] S15: Determine whether the first memory size is less than 512 bytes; if so, proceed to step S16; if not, proceed to step S17;
[0190] S16: Determine that the second memory size is 512 bytes;
[0191] S17: Determine whether the first strategy is enabled and whether the calculated segmentation value is greater than 0; if so, proceed to step S18; if not, proceed to step S19;
[0192] S18: Calculate the second memory size using the first strategy;
[0193] S19: Calculate the second memory size using the second strategy.
[0194] Specifically, for the memory application process, size2 is calculated based on size1. This calculation process can be described as follows:
[0195] 1. The size of the memory requested is size1;
[0196] 2. Determine whether size1 is less than or equal to 512 bytes. If so, go to step 3; if not, go to step 4;
[0197] 3. Round the memory size size2 = 512 bytes; go to step 7;
[0198] 4. Determine whether the "divisions strategy" is enabled and the calculated divisions is greater than 0. If yes, go to step 5; if no, go to step 6;
[0199] 5. Use the "divisions strategy" to calculate size2;
[0200] 6. Use the "round up strategy" to calculate size2 (size1 is rounded up to size_tmp, size_tmp = 512 bytes);
[0201] 7. The calculation is completed.
[0202] In order to avoid wasting memory resources, randomly selecting a memory block from all memory blocks found that are greater than or equal to the second memory size to allocate memory resources to the second current thread, or selecting the smallest memory block from all memory blocks found that are greater than or equal to the second memory size to allocate memory resources to the second current thread includes:
[0203] Randomly select a memory block from all found memory blocks that are greater than or equal to the second memory size, or select the smallest memory block from all found memory blocks that are greater than or equal to the second memory size, and determine whether the size of the selected memory block is greater than the second memory size;
[0204] If yes, the selected memory block is divided into a first memory portion and a second memory portion; the first memory portion is allocated to the second current thread to allocate memory resources, and the second memory portion is placed in a second resource pool; wherein the first memory portion is greater than or equal to the second memory size;
[0205] If not, the selected memory block is used to allocate memory resources for the second current thread.
[0206] In practice, it may happen that no memory block is obtained from the third memory pool and the fourth memory pool. In order to ensure that the second current thread can be allocated a memory block as much as possible, the memory processing method further includes:
[0207] If no memory block greater than or equal to the second memory size is found from the third memory pool and the fourth memory pool, a memory block is requested from the board to obtain the selected memory block; and the step of returning to determine whether the size of the selected memory block is greater than the second memory size is returned.
[0208] That is, in the case where no memory block is obtained from the third memory pool and the fourth memory pool, the present invention applies for memory from the board. In the embodiment of the present invention, the memory size applied to the board is referred to as size3. In implementation, determining the size of the memory block applied from the board includes:
[0209] When detecting that the first strategy is in the on state, determining whether the second memory size is less than or equal to a second preset value;
[0210] If so, determining that the size of the memory block requested from the board is greater than a second preset value;
[0211] If not, if it is detected that the second memory size is less than the third preset value, it is determined that the size of the memory block applied for from the board is greater than the third preset value; if it is detected that the second memory size is greater than or equal to the third preset value, the second strategy is used to determine the size of the memory block applied for from the board.
[0212] There is no limitation on the second preset value and the third preset value. For example, the second preset value is 16M and the third preset value is 32M.
[0213] In implementation, the size of the memory block requested from the board can also be determined by the following method. Specifically, when it is detected that the first strategy is in the off state, it is determined whether the second memory size is less than or equal to a fourth preset value; wherein the fourth preset value is greater than the first preset value and less than the second preset value;
[0214] If so, determining that the size of the memory block requested from the board is greater than a fourth preset value;
[0215] If not, define the value of the variable corresponding to the second memory size to be the second memory size;
[0216] In the case where it is detected that the third strategy is in the on state, the third strategy is used to obtain the value corresponding to the variable, and if it is detected that the value corresponding to the variable is less than or equal to the fifth preset value, it is determined that the size of the memory block applied for from the board is greater than the fifth preset value; if it is detected that the value corresponding to the variable is greater than the fifth preset value, the second strategy is used to determine the size of the memory block applied for from the board; wherein the fifth preset value is greater than the fourth preset value and less than the second preset value;
[0217] When it is detected that the third strategy is in the closed state, the step of detecting the relationship between the value corresponding to the variable and the fifth preset value is entered.
[0218] The third strategy for determining the size of the memory block requested from the board includes:
[0219] Get the maximum value among the second memory size, the free memory size and the rated value of the memory allocation;
[0220] The maximum value is used as the size of the memory block requested from the board.
[0221] There is no limitation on the fourth preset value and the fifth preset value, such as the selected fourth preset value is 1M, and the selected fifth preset value is 10M. In the embodiment of the present invention, the third strategy is referred to as the "remaining memory strategy". The variable is represented by malloc_size. If the remaining memory strategy is adopted, when calculating size3, the calculation formula is: max(size2, size_free / allocate_free_quota); max is a function that takes the maximum value, size_free is the free memory of the board, and allocate_free_quota is an integer constant that can be read from the configuration center. It is worth noting that when calculating size3 based on size2, the calculation formula of the second strategy (i.e., the rounding up strategy) is: (size2+ size_tmp- 1) / size_tmp, at this time size_tmp can be 2MB.
[0222] In order to enable those skilled in the art to better understand the above-mentioned process of calculating the second memory size size2 by the second memory size size2, it is described below with reference to the accompanying drawings and specific embodiments. Fig.12 A flowchart of a method for calculating a memory size size3 applied to a board card by using a second memory size size2 is provided in an embodiment of the present invention, such as Fig.12 As shown, the method includes:
[0223] S20: Determine the second memory size;
[0224] S21: Determine whether the first strategy is enabled; if so, proceed to step S22; if not, proceed to step S27;
[0225] S22: Determine whether the second memory size is less than or equal to 16MB; if so, proceed to step S23; if not, proceed to step S24;
[0226] S23: The memory size requested from the board is 32MB;
[0227] S24: Determine whether the second memory size is less than 32MB; if so, proceed to step S25; if not, proceed to step S26;
[0228] S25: The memory size requested from the board is 64MB;
[0229] S26: Calculate the memory size requested from the board using the second strategy;
[0230] S27: Determine whether the second memory size is less than or equal to 1MB; if so, proceed to step S28; if not, proceed to step S29;
[0231] S28: The memory size requested from the board is 2MB;
[0232] S29: Set the temporary variable to be equal to the second memory size;
[0233] S30: Determine whether the third strategy is enabled; if so, proceed to step S31; if not, proceed to step S32;
[0234] S31: Calculate temporary variables using the third strategy;
[0235] S32: Determine whether the temporary variable is less than or equal to 10MB; if so, proceed to step S33; if not, proceed to step S34;
[0236] S33: The memory size requested from the board is 20MB;
[0237] S34: Calculate the memory size requested from the board using the third strategy.
[0238] Specifically, for the memory application process, size3 is calculated based on size2. This calculation process can be described as follows:
[0239] 1. Round the memory size to size2;
[0240] 2. Determine whether the "divisions strategy" is enabled; if yes, go to step 3; if no, go to step 8;
[0241] 3. Determine whether size2 is less than or equal to 16MB; if so, go to step 4; if not, go to step 5;
[0242] 4. Apply for memory size size3 = 32MB from the board, and go to step 16;
[0243] 5. Determine whether size2 is less than or equal to 32MB; if so, go to step 6; if not, go to step 7;
[0244] 6. Apply for memory size size3 = 64MB from the board, and go to step 16;
[0245] 7. Use the "round up strategy" to calculate size3 (size2 is rounded up to size_tmp, size_tmp = 2MB), and go to step 16;
[0246] 8. Determine whether size2 is less than or equal to 1MB; if so, go to step 9; if not, go to step 10;
[0247] 9.size3 = 2MB, go to step 16;
[0248] 10. Define temporary variable malloc_size = size2;
[0249] 11. Determine whether the "remaining memory policy" is enabled; if yes, go to step 12; if no, go to step 13;
[0250] 12. Use the "remaining memory strategy" to calculate malloc_size;
[0251] 13. Determine whether malloc_size is less than or equal to 10MB; if so, go to step 14; if not, go to step 15;
[0252] 14.size3 = 20MB, go to step 16;
[0253] 15. Use the "round up strategy" to calculate size3 (malloc_size rounds up size_tmp, size_tmp = 2MB);
[0254] 16. The calculation is completed.
[0255] In order to make those skilled in the art better understand the method of applying for memory resources from the second resource pool of the present invention, Figure 8 , Fig.13 The second resource pool can be managed by a second memory allocator.
[0256] Memory allocator 2: When the memory requested or released is larger than 64KB, the memory request and release forwarder will forward the memory request or release operation to memory allocator 2, that is, memory allocator 2 is responsible for the memory request and release of memory larger than 64KB. Memory allocator 2 contains the following modules:
[0257] Memory Manager: The memory manager is responsible for memory allocation and release operations.
[0258] Large memory pool: When the memory requested by the user is larger than 1MB, it will be allocated from the large memory pool. When the memory is released, it will also be returned to the large memory pool.
[0259] Small memory pool: When the memory requested by the user is greater than 64KB and less than or equal to 1MB, it will be allocated from the small memory pool and returned to the small memory pool when the memory is released.
[0260] Configuration Center: The configuration center stores various configurations of memory allocator 2. It mainly includes the following configuration items:
[0261] 1.roundup_power2_divisions: The type is an array, used to calculate the rounded memory size size2. When judging whether the "divisions strategy" is enabled, the judgment standard is whether the 0th element of the roundup_power2_divisions array is greater than 0. If yes, it means that the "divisions strategy" is enabled; if no, it means that the "divisions strategy" is disabled;
[0262] 2.allocate_free_quota: The type is an integer value, which is used to calculate the memory size applied to the board. When judging whether the "remaining memory policy" is enabled, the judgment standard is whether allocate_free_quota is greater than 0. If it is, it means that the "remaining memory policy" is enabled; if not, it means that the "remaining memory policy" is disabled.
[0263] Fig.13 A flowchart of a memory application method of a memory allocator 2 provided in an embodiment of the present invention, such as Fig.13 As shown, the method includes:
[0264] S35: Obtain the first memory size of the memory request;
[0265] S36: adjusting the first memory size to the second memory size;
[0266] S37: Select memory pool;
[0267] S38: Calculate the memory size that needs to be applied to the board;
[0268] S39: searching for a memory block greater than or equal to the second memory size in the selected memory pool;
[0269] S40: Determine whether it is found; if not, proceed to step S41; if yes, proceed to step S44;
[0270] S41: Call the runtime function to apply for memory from the board;
[0271] S42: Determine whether the memory application to the board is successful; if not, proceed to step S43; if yes, proceed to step S44;
[0272] S43: print error information and exit the program;
[0273] S44: Determine whether the memory block needs to be split; if not, proceed to step S45; if yes, proceed to step S46;
[0274] S45: The memory block is returned to the caller as a whole as a segment;
[0275] S46: The memory block is divided into two blocks, block 1 and block 2;
[0276] S47: Block 1 is returned to the caller and block 2 is put back into the memory pool.
[0277] Specifically, the memory allocator 2 memory application process can be divided into the following steps:
[0278] 1. The memory request arrives at the memory manager, and the requested memory size is size1;
[0279] 2. The memory manager will adjust the size of the requested memory to size2 (size2>=size1);
[0280] 3. Select the memory pool, that is, determine whether to use a small memory pool or a large memory pool based on the size of size2;
[0281] 4. The memory manager calculates the memory size size3 that needs to be requested from the board (size3 >= size2);
[0282] 5. The memory manager searches the memory pool for a memory block that is greater than or equal to size2. If multiple blocks are found, the smallest one is selected.
[0283] 6. Determine whether it is found. If it is found, go to step 7; if not, go to step 11;
[0284] 7. Determine whether the memory block needs to be split. Since the found memory block may be relatively large, it is wasteful to return it directly to the user, so it needs to be split here; if it needs to be split, go to step 8; if it does not need to be split, go to step 10;
[0285] 8. Cut the memory block into two pieces;
[0286] 9. Block 1 is returned to the user, block 2 is put back into the memory pool, and go to step 14;
[0287] 10. The memory block is returned to the caller as a whole, and go to step 14;
[0288] 11. Call the runtime function to apply for memory from the board;
[0289] 12. Determine whether the memory application to the board is successful; if yes, go to step 7; if no, go to step 13;
[0290] 13. Print error information and exit the program;
[0291] 14. Memory application ends.
[0292] The above describes the memory application process of memory allocator 2. After allocating memory for a thread, the memory needs to be released. The memory processing method also includes:
[0293] When receiving a memory release request corresponding to the second resource pool, obtaining a memory pool of the memory to be released;
[0294] Determine whether there is a memory block to be merged corresponding to the memory block to be released;
[0295] If yes, the memory block to be released is merged with the memory block to be merged, and the merged memory block is returned to the second resource pool;
[0296] If not, the memory block to be released is returned to the second resource pool.
[0297] Fig.14 A flowchart of a method for releasing memory of a memory allocator 2 provided in an embodiment of the present invention is shown in FIG. Fig.14 As shown, the method includes:
[0298] S48: receiving a memory release request;
[0299] S49: Determine whether the memory pointer is valid, if so, proceed to step S50; if not, end;
[0300] S50: Select the memory pool;
[0301] S51: Determine whether it is necessary to merge with other memory blocks in the pool; if so, proceed to step S52; if not, proceed to step S53;
[0302] S52: merge memory blocks;
[0303] S53: The memory block is put back into the memory pool.
[0304] Specifically, the memory release process can be divided into the following steps:
[0305] 1. A memory release request arrives at the memory manager;
[0306] 2. Determine whether the memory pointer is valid; if yes, go to step 3; if no, go to step 7;
[0307] 3. Select the memory pool, that is, determine whether the memory belongs to the small memory pool or the large memory pool according to the memory pointer;
[0308] 4. Determine whether it needs to be merged with other memory blocks in the corresponding memory pool. Since the memory block may be split, it is necessary to determine whether it needs to be merged when the memory is released. If yes, go to step 5; if not, go to step 6;
[0309] 5. Merge memory blocks;
[0310] 6. Put the memory block back to the corresponding memory pool;
[0311] 7. Memory release is completed.
[0312] In the memory management system provided by the embodiment of the present invention, for memory allocator 1, a thread memory pool is created for each thread. Only when there is no suitable free memory block in the thread memory pool, memory will be applied from the shared memory pool. If there is no suitable free memory block in the shared memory pool, it will be applied from the page cache pool. The page cache pool is the module that actually calls the memory application and release functions during the board runtime to apply and release memory. Note that the memory allocator is only used to manage memory of 64KB and below. This is because for memory allocator 1, each thread has a thread memory pool. If the free memory blocks in the thread memory pool are too large, a large amount of memory will be cached, resulting in a waste of resources.
[0313] For the memory allocator 2, it has two memory pools, the difference between them is: the small memory pool is used to store memory equal to or less than 1MB; the large memory pool is used to store memory greater than 1MB. Multiple strategies (i.e., the system corresponding to the memory management optimization solution provided by the present invention, also known as a multi-strategy memory management system) are used to calculate the rounded memory size size2 and the memory size size3 applied to the board. The parameter value of each strategy is obtained from the configuration center, that is, it is configurable.
[0314] The method provided by the present invention, when applying for a memory less than or equal to 64KB, each thread first searches for a free memory block from a thread memory pool corresponding to the thread, and this operation does not need to obtain a mutex lock, thus avoiding a waiting problem caused by multiple threads competing for the same mutex lock, thereby improving the performance of board memory application and release, that is, reducing the time of board memory application and release, and improving performance; if the memory is not found, the shared memory pool will be searched, and the access to the shared memory pool needs to obtain a global mutex lock first; because a thread memory management instance and a thread memory pool will be created for each user thread only when applying for and releasing a memory less than or equal to 64KB, the board memory occupied by the program will not be significantly increased, and a large amount of memory waste will not be caused.
[0315] In the above embodiments, the memory processing method is described in detail, and the present invention also provides corresponding embodiments of a memory processing device and an electronic device. It should be noted that the present invention describes the embodiments of the device part from two perspectives, one is based on the functional module perspective, and the other is based on the hardware perspective.
[0316] The memory processing device provided in the embodiment of the present invention includes, based on the perspective of functional modules:
[0317] The first acquisition module is used to obtain the memory size currently requested by each thread;
[0318] A determination module, configured to determine a first type of thread and a second type of thread according to a memory size currently applied for by each thread; wherein the memory size currently applied for by each thread in the first type of thread is less than or equal to a first preset value; and the memory size currently applied for by each thread in the second type of thread is greater than the first preset value;
[0319] A first allocation module, configured to allocate memory resources from the first memory pool or the shared pool to threads of the first type according to the distribution of memory blocks in the first resource pool; wherein the first memory pool corresponds to the threads of the first type one by one;
[0320] The second allocation module is used to allocate memory resources to the second type of threads according to the distribution of memory blocks in the second resource pool; wherein the second resource pool is a resource pool shared by all the second type of threads.
[0321] In some embodiments, the memory processing device further includes:
[0322] A judgment module is used to judge whether the memory resource is successfully applied for from the page cache pool; if so, the third allocation module is triggered; if not, the application module is triggered;
[0323] A third allocation module, used to allocate memory resources to the first current thread using the applied memory resources;
[0324] The application module is used to apply for memory resources from the board through the page cache pool, and use the memory resources applied from the board to allocate memory resources to the first current thread.
[0325] Since the embodiments of the apparatus part correspond to the embodiments of the method part, please refer to the description of the embodiments of the method part for the embodiments of the apparatus part, which will not be repeated here.
[0326] Fig.15 This is a structural diagram of an electronic device provided by an embodiment of the present invention. This embodiment is based on the hardware perspective, such as Fig.15 As shown, the electronic equipment includes:
[0327] A memory 20, used for storing computer programs;
[0328] The processor 21 is used to implement the steps of the memory processing method mentioned in the above embodiment when executing a computer program.
[0329] Among them, the processor 21 may include one or more processing cores, such as a 4-core processor, an 8-core processor, etc. The processor 21 can be implemented in at least one hardware form of a digital signal processor (DSP), a field-programmable gate array (FPGA), and a programmable logic array. The processor 21 may also include a main processor and a coprocessor. The main processor is a processor for processing data in the awake state, also known as a central processing unit (CPU); the coprocessor is a low-power processor for processing data in the standby state. In some embodiments, the processor 21 may be integrated with a graphics processing unit (GPU), which is responsible for rendering and drawing the content to be displayed on the display screen. In some embodiments, the processor 21 may also include an artificial intelligence (AI) processor, which is used to process computing operations related to machine learning.
[0330] The memory 20 may include one or more computer-readable storage media, which may be non-transitory. The memory 20 may also include a high-speed random access memory, and a non-volatile memory, such as one or more disk storage devices, flash memory storage devices. In this embodiment, the memory 20 is at least used to store the following computer program 201, wherein, after the computer program is loaded and executed by the processor 21, it can implement the relevant steps of the memory processing method disclosed in any of the aforementioned embodiments. In addition, the resources stored in the memory 20 may also include an operating system 202 and data 203, etc., and the storage method may be temporary storage or permanent storage. Among them, the operating system 202 may include Windows, Unix, Linux, etc. The data 203 may include, but is not limited to, the data involved in the above-mentioned memory processing method, etc.
[0331] In some embodiments, the electronic device may further include a display screen 22 , an input / output interface 23 , a communication interface 24 , a power source 25 , and a communication bus 26 .
[0332] Those skilled in the art will understand that Fig.15 The structure shown in the figure does not constitute a limitation on the electronic device, and may include more or fewer components than shown in the figure.
[0333] The electronic device provided by the embodiment of the present invention includes a memory and a processor. When the processor executes the program stored in the memory, it can implement the following method: a memory processing method, the effect is the same as above.
[0334] An embodiment of the present invention further provides a computer program product, including a computer program / instruction, which implements the steps of the above-mentioned memory processing method when executed by a processor.
[0335] Finally, the present invention also provides an embodiment corresponding to a non-volatile storage medium. The non-volatile storage medium stores a computer program, and when the computer program is executed by a processor, the steps recorded in the above method embodiment are implemented.
[0336] It is understandable that if the method in the above embodiment is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium to execute all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk, etc. Various media that can store program codes.
[0337] The non-volatile storage medium provided by the present invention includes the above-mentioned memory processing method, and the effect is the same as above.
[0338] The above is a detailed introduction to a memory processing method, product, electronic device and medium provided by the present invention. The various embodiments in the specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same and similar parts between the embodiments can be referenced to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part description. It should be pointed out that for ordinary technicians in this technical field, without departing from the principle of the present invention, several improvements and modifications can be made to the present invention, and these improvements and modifications also fall within the scope of protection of the present invention.
[0339] It should also be noted that, in this specification, relational terms such as first and second, etc. are only used 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, an element defined by the statement "comprises a ..." does not exclude the presence of other identical elements in the process, method, article or device including the element.
Claims
1. A memory processing method, characterized in that: include: Get the memory size currently requested by each thread; Determine the first type of threads and the second type of threads according to the memory size currently applied for by each thread; wherein the memory size currently applied for by each thread in the first type of threads is less than or equal to a first preset value; and the memory size currently applied for by each thread in the second type of threads is greater than the first preset value; Allocating memory resources from the first memory pool or the shared pool to the first type of threads according to the distribution of memory blocks in the first resource pool; wherein the first memory pool corresponds to the first type of threads one by one; Allocating memory resources to the second type of threads according to the distribution of memory blocks in the second resource pool; wherein the second resource pool is a resource pool shared by all the second type of threads; The second resource pool includes a third memory pool and a fourth memory pool; wherein the maximum memory value allowed to be applied for by the third memory pool is smaller than the maximum memory value allowed to be applied for by the fourth memory pool; After determining that the second current thread is a thread of the second type, obtaining a memory size requested by the second current thread; allocating resources to the second current thread from the third memory pool or the fourth memory pool according to the memory size requested by the second current thread; The obtaining of the memory size requested by the second current thread includes: Get the first memory size requested by the second current thread; Adjusting the first memory size to a second memory size; wherein the second memory size is larger than the first memory size; The second memory size is used as the memory size requested by the second current thread.
2. The memory processing method according to claim 1, characterized in that: The allocating memory resources from the first memory pool or the shared pool to the first type of threads according to the distribution of memory blocks in the first resource pool includes: After determining that the first current thread is a thread of the first type, obtaining distribution of memory blocks in a first memory pool corresponding to the first current thread; If a memory block that satisfies the application of the first current thread is determined according to the distribution of the memory blocks in the first memory pool corresponding to the first current thread, memory resources are allocated to the first current thread from the first memory pool; If a memory block that satisfies the request of the first current thread cannot be determined based on the distribution of memory blocks in the first memory pool corresponding to the first current thread, memory resources are applied for from the shared pool through the first memory pool corresponding to the first current thread, and memory resources are allocated to the first current thread using the applied memory.
3. The memory processing method according to claim 2, characterized in that: The shared pool includes a second memory pool and a page cache pool; The applying for memory resources from the shared pool through the first memory pool corresponding to the first current thread includes: Applying for memory resources from the second memory pool through the first memory pool corresponding to the first current thread; If a memory block that satisfies the application of the first current thread is not determined according to the distribution of the memory blocks in the second memory pool, memory resources are applied for from the page cache pool through the second memory pool.
4. The memory processing method according to claim 3, characterized in that: The first memory pool and the second memory pool are represented by a free memory block array, each element of the free memory block array is a free memory block linked list of the same size, there is a corresponding relationship between the subscripts of the free memory block array and the free memory block linked list, and the memory block sizes in the free memory block linked list corresponding to different subscripts are different.
5. The memory processing method according to claim 4, characterized in that: Determining whether a memory block satisfies or does not satisfy the memory block requested by the first current thread according to the distribution of the memory blocks in the second memory pool or according to the distribution of the memory blocks in the first memory pool corresponding to the first current thread includes: Determine whether there is a subscript of the free memory block array corresponding to the memory size applied for by the first current thread according to the corresponding relationship between the subscript of the free memory block array and the free memory block linked list; If not, determining that the distribution of memory blocks in the second memory pool or the distribution of memory blocks in the first memory pool corresponding to the first current thread does not satisfy the memory blocks applied for by the first current thread; If so, when it is detected that there is a memory block under the subscript of the free memory block array corresponding to the memory size requested by the first current thread, it is determined that the distribution of memory blocks in the second memory pool or the distribution of memory blocks in the first memory pool corresponding to the first current thread meets the memory block requested by the first current thread; when it is detected that there is no memory block under the subscript of the free memory block array corresponding to the memory size requested by the first current thread, it is determined that the distribution of memory blocks in the second memory pool or the distribution of memory blocks in the first memory pool corresponding to the first current thread does not meet the memory block requested by the first current thread.
6. The memory processing method according to claim 3, characterized in that: The page buffer pool is represented by a free memory interval array, each element of the free memory interval array is a free memory interval linked list of the same size, there is a corresponding relationship between the subscript of the free memory interval array and the free memory interval linked list, and the memory interval sizes in the free memory interval linked list corresponding to different subscripts are different.
7. The memory processing method according to claim 6, characterized in that: After applying for memory resources from the page cache pool through the second memory pool, the method further includes: Determine whether memory resources are successfully applied for from the page cache pool; If yes, use the applied memory resources to allocate memory resources to the first current thread; If not, apply for memory resources from the board through the page buffer pool, and use the memory resources applied from the board to allocate memory resources to the first current thread.
8. The memory processing method according to claim 7, characterized in that: The determining whether the memory resource is successfully applied for from the page cache pool comprises: Determining an initial index of the free memory interval array in the page cache pool according to the size of the memory requested by the first current thread; Searching for a free memory interval in the free memory interval linked list starting from the free memory interval linked list corresponding to the initial subscript to the free memory interval linked list corresponding to the maximum subscript among all subscripts of the free memory interval array; If it is detected that there is the idle memory interval, it is determined that the memory resource is successfully applied for from the page cache pool; If it is detected that the idle memory interval does not exist, it is determined that the memory resource has not been successfully applied for from the page cache pool.
9. The memory processing method according to claim 7, characterized in that: The allocating memory resources for the first current thread by using the memory resources applied for from the board includes: The memory resources applied for from the board are divided into a first memory resource and a second memory resource; wherein the size of the first memory resource is the memory size applied for by the second memory pool; storing the second memory resource as a new memory interval in the page cache pool; The first memory resource is used as the memory resource allocated to the first current thread.
10. The memory processing method according to claim 1, characterized in that: The allocating memory resources to the second type of threads according to the distribution of memory blocks in the second resource pool includes: When it is detected that the memory size requested by the second current thread is less than or equal to the maximum memory value allowed to be requested by the third memory pool, memory resources are allocated to the second current thread according to the distribution of memory blocks in the third memory pool; When it is detected that the memory size requested by the second current thread is greater than the maximum memory value allowed by the third memory pool, and less than or equal to the maximum memory value allowed by the fourth memory pool, memory resources are allocated to the second current thread according to the distribution of memory blocks in the fourth memory pool.
11. The memory processing method according to claim 10, characterized in that: Allocating memory resources to the second current thread according to the distribution of memory blocks in the third memory pool or the distribution of memory blocks in the fourth memory pool includes: If multiple memory blocks greater than or equal to the second memory size are found from the third memory pool or the fourth memory pool, a memory block is randomly selected from all the memory blocks greater than or equal to the second memory size to allocate memory resources to the second current thread, or the smallest memory block is selected from all the memory blocks greater than or equal to the second memory size to allocate memory resources to the second current thread.
12. The memory processing method according to claim 11, characterized in that: Determining the second memory size includes: Determine whether the first memory size is less than or equal to a preset byte; If yes, determining that the second memory size is the preset bytes; If not, adopting the first strategy or the second strategy to determine the second memory size; Wherein, adopting the first strategy to determine the second memory size includes: Obtaining a pre-established correspondence between the number of segments, the segment blocks, and the memory size threshold of the segment blocks, determining a target segment block corresponding to the first memory size according to the memory size threshold in the correspondence, and segmenting the target segment block according to the number of segments corresponding to a previous segment block of the target segment block; obtaining a sub-segment block where the first memory size is located in the target segment block after segmentation, and using the memory size threshold corresponding to the sub-segment block as the second memory size; Adopting the second strategy to determine the second memory size includes: Get the preset rounding size; The second memory size is determined according to the first memory size and the rounded size.
13. The memory processing method according to claim 12, characterized in that: The randomly selecting a memory block from all the found memory blocks that are greater than or equal to the second memory size to allocate memory resources to the second current thread, or selecting the smallest memory block from all the found memory blocks that are greater than or equal to the second memory size to allocate memory resources to the second current thread includes: Randomly select a memory block from all found memory blocks that are greater than or equal to the second memory size, or select the smallest memory block from all found memory blocks that are greater than or equal to the second memory size, and determine whether the size of the selected memory block is greater than the second memory size; If yes, the selected memory block is divided into a first part of memory and a second part of memory; the first part of memory is allocated to the second current thread to allocate memory resources, and the second part of memory is placed in the second resource pool; wherein the first part of memory is greater than or equal to the size of the second memory; If not, memory resources are allocated to the second current thread using the selected memory block.
14. The memory processing method according to claim 13, characterized in that: The method further comprises: If no memory block greater than or equal to the second memory size is found from the third memory pool and the fourth memory pool, a memory block is requested from the board to obtain the selected memory block; and the step of determining whether the size of the selected memory block is greater than the second memory size is returned.
15. The memory processing method according to claim 14, characterized in that: Determining the size of the memory block requested from the board includes: In the case where it is detected that the first strategy is in the enabled state, determining whether the second memory size is less than or equal to a second preset value; If so, determining that the size of the memory block requested from the board is greater than the second preset value; If not, if it is detected that the second memory size is smaller than the third preset value, it is determined that the size of the memory block applied for from the board is greater than the third preset value; if it is detected that the second memory size is greater than or equal to the third preset value, the second strategy is used to determine the size of the memory block applied for from the board.
16. The memory processing method according to claim 14, characterized in that: Determining the size of the memory block requested from the board includes: In the case where it is detected that the first strategy is in a closed state, determining whether the second memory size is less than or equal to a fourth preset value; wherein the fourth preset value is greater than the first preset value and less than the second preset value; If yes, determining that the size of the memory block requested from the board is greater than the fourth preset value; If not, define the value of the variable corresponding to the second memory size as the second memory size; In the case where it is detected that the third strategy is in the on state, the third strategy is adopted to obtain the value corresponding to the variable, and if it is detected that the value corresponding to the variable is less than or equal to the fifth preset value, it is determined that the size of the memory block applied for from the board is greater than the fifth preset value; if it is detected that the value corresponding to the variable is greater than the fifth preset value, the second strategy is adopted to determine the size of the memory block applied for from the board; wherein the fifth preset value is greater than the fourth preset value and less than the second preset value; In the case where it is detected that the third strategy is in the off state, the step of detecting the relationship between the value corresponding to the variable and the fifth preset value is entered; The adopting the third strategy to determine the size of the memory block to be applied for from the board includes: Obtaining a maximum value among the second memory size, the free memory size, and the rated value of memory allocation; The maximum value is used as the size of the memory block requested from the board.
17. The memory processing method according to any one of claims 13 to 16, characterized in that: The method further comprises: Upon receiving a memory release request corresponding to the second resource pool, obtaining a memory pool of memory to be released; Determine whether there is a memory block to be merged corresponding to the memory block to be released; If yes, merging the memory block to be released with the memory block to be merged, and returning the merged memory block to the second resource pool; If not, the memory block to be released is returned to the second resource pool.
18. A computer program product, characterized in that It comprises a computer program / instruction, which, when executed by a processor, implements the steps of the memory processing method as described in any one of claims 1 to 17.
19. An electronic device, characterized in that: include: Memory for storing computer programs; A processor, configured to implement the steps of the memory processing method as claimed in any one of claims 1 to 17 when executing the computer program.
20. A non-volatile storage medium, characterized in that: The non-volatile storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the memory processing method according to any one of claims 1 to 17 are implemented.
Citation Information
Patent Citations
Memory management method and device for computing chip and computing chip
CN117311997A