Concurrent management method for multi-thread memory pool
By employing a multi-threaded memory pool concurrency management method in a multi-threaded environment, and utilizing the design of an independent memory pool and separate mutex locks, the problems of lock contention and memory fragmentation in traditional memory management are solved, thereby improving memory allocation efficiency and system performance.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-02-05
- Publication Date
- 2026-05-15
AI Technical Summary
Traditional memory management methods suffer from lock contention in multi-threaded environments, leading to performance degradation and memory fragmentation, which cannot effectively meet the needs of high-concurrency, low-latency embedded systems.
A multi-threaded memory pool concurrency management method is adopted, which maintains multiple independent memory pools, each corresponding to a preset memory block size. A singly linked list is used to manage memory block allocation and reclamation, and separate mutexes are used to protect the head pointer and tail pointer, thereby enabling concurrent operations under lock-free conditions.
It significantly reduces multi-threaded lock contention, reduces memory fragmentation, and improves memory allocation efficiency and system throughput, making it suitable for high-concurrency, low-latency embedded systems.
Smart Images

Figure CN122044870A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of memory management, specifically a concurrent management method for multi-threaded memory pools, suitable for high-concurrency, low-latency embedded systems or real-time systems. Background Technology
[0002] With the development of embedded technology, memory management plays a crucial role in embedded system and application development. However, with the increasing demands for multi-threaded applications and cross-platform development, traditional memory management methods and performance are facing bottlenecks and challenges.
[0003] Traditional static memory pools require the number of memory cells to be specified during initialization and cannot be expanded. In multi-threaded environments, they rely on locking mechanisms to ensure thread safety, leading to lock contention, wasting CPU resources, and causing performance degradation. Dynamic memory pools such as tcmalloc and jemalloc, while reducing mutex operations, cannot completely eliminate memory fragmentation. With the proliferation of multi-core processors and the increase in multi-threaded applications, these problems become even more pronounced for systems requiring efficient memory management. Summary of the Invention
[0004] To address the shortcomings of existing technologies, this invention provides a concurrent management method for multi-threaded memory pools. This method has a simple structure, can significantly reduce multi-threaded lock contention, and supports dynamic expansion.
[0005] To solve the aforementioned technical problem, the technical solution adopted by the present invention is: a concurrency management method for a multi-threaded memory pool, the method comprising: Maintain multiple independent memory pools, each corresponding to a preset memory block size. A memory block is the basic unit for storing user data. Each memory pool maintains a singly linked list consisting of free memory blocks, denoted as the free list. Each free list has a head pointer for memory block allocation and expansion, and a tail pointer for memory block reclamation. Memory block allocation is performed only by operating the head pointer from the head of the free list, and memory block reclamation is performed only by inserting into the tail of the free list, allowing memory block allocation and reclamation operations to be performed concurrently under lock-free conditions. When there is only one empty memory block left in the free list, an expansion operation is triggered, a new memory page is allocated, and its empty memory block is inserted into the head of the free list. Set separate first and second mutexes for each memory pool. The first and second mutexes protect concurrent modifications to the head pointer and tail pointer, respectively.
[0006] Furthermore, when a thread requests memory, a free memory block is taken from the head of the free list and allocated; when a thread releases memory, the released memory block is inserted into the tail of the free list; the expansion operation involves allocating new memory pages, linking all free memory blocks in the new memory pages, and then inserting them into the head of the free list.
[0007] Furthermore, there is only one first mutex and one second mutex. When multiple threads concurrently perform memory allocation or expansion operations, they compete for the first mutex. When multiple threads concurrently perform memory release operations, they compete for the second mutex.
[0008] Furthermore, multiple memory pools are created during system initialization. Each memory pool includes a memory pool header and multiple memory pages. A memory page includes a memory page header and multiple memory blocks. A memory block is the basic unit for storing user data. The size of the memory blocks in each memory pool is different. All empty memory blocks in the memory pool form a free linked list; the head of the memory pool has pointers to the first empty memory block, the last empty memory block, the first memory page, the last memory page, the memory block size, the first mutex, and the second mutex.
[0009] Furthermore, the memory pool creation process is as follows: create multiple memory pools according to user needs, initialize the memory pool, set the memory block size, establish memory pages, and configure the pointer of the memory pool head; The process of creating a memory page is as follows: allocate memory page space, form a free linked list of all memory blocks of the memory page, and store the linked list address in the starting space of the free memory block.
[0010] Furthermore, the memory pool allocation and expansion process is as follows: Based on the space size requested by the user, obtain the most suitable memory pool address, compare the pointers of the first and last empty memory blocks. If the pointers of the first and last empty memory blocks are not equal, it means that the number of remaining empty memory blocks is greater than 1. Temporarily store the pointer of the first empty memory block, modify the pointer of the first empty memory block to point to the second empty memory block, and then return the temporarily stored pointer of the first empty memory block to the user. If the pointers of the first and last empty memory blocks are equal, it means that the number of remaining empty memory blocks is equal to 1. Expand the memory pool by first creating a new memory page, the pointer of the last memory page pointing to the new memory page, the backward pointer of the last memory block in the new memory page pointing to the memory block where the pointer of the first empty memory block is located, the pointer of the first empty memory block pointing to the first memory block of the new memory page, and returning the first empty memory block to the user.
[0011] Furthermore, if multiple threads simultaneously request the same memory pool, the first mutex is acquired after obtaining the most suitable memory pool address, and the first mutex is released after returning the first empty memory block to the user.
[0012] Furthermore, the memory pool release process is as follows: based on the memory address to be released by the user, obtain the memory block size, find the memory pool, the last unit of the free list points to the memory block to be released, the last empty memory block pointer points to the memory block to be released, and the block pointer to be released points to null.
[0013] Furthermore, if multiple threads release the same memory pool simultaneously, the thread acquires a second mutex after finding the memory pool, and releases the second mutex after the pointer of the block to be released points to null.
[0014] Furthermore, the memory block size is a multiple of 4 or a square of 2.
[0015] The beneficial effects of this invention are as follows: Building upon traditional memory pools that reduce memory fragmentation, this invention allows for expansion when memory pool space is insufficient, making it suitable for scenarios where memory pool space is uncertain. Compared to dynamic memory pools, it completely avoids memory fragmentation. Furthermore, this invention separates the allocation (and expansion) path and the deallocation path of memory blocks in a linked list structure, managed by head and tail pointers respectively. This design ensures that memory block allocation operations (including expansion) and deallocation operations naturally operate at different ends of the linked list, eliminating shared competitive resources. Therefore, when one thread allocates memory, another thread can release it without waiting for each other or locking, maximizing concurrent execution and significantly improving throughput in multi-threaded environments. Compared to existing technologies, this invention has a simple structure and is applicable to most embedded scenarios. This invention eliminates the need for thread-local storage, bucket migration, or bitmap management, resulting in a simple structure with low overhead, significantly reducing lock contention and memory fragmentation, and improving memory allocation efficiency and system throughput in multi-threaded environments. Attached Figure Description
[0016] Figure 1 This is a schematic diagram of the memory pool structure; Figure 2 Flowchart for memory pool allocation and expansion; Figure 3 Create an instance graph for the memory pool; Figure 4 Diagram of an instance allocated for memory pool without expansion; Figure 5 Release instance diagram for memory pool; Figure 6 An example diagram of expanding the memory pool. Detailed Implementation
[0017] The present invention will be further described below with reference to the accompanying drawings and specific embodiments.
[0018] Example 1 This embodiment discloses a concurrent management method for a multi-threaded memory pool. This method maintains multiple memory pools, each corresponding to a preset memory block size, and each memory pool internally maintains a free list composed of concatenated empty memory blocks. Each free list has a head pointer and a tail pointer, used for memory block allocation and deallocation respectively, and the head and tail pointers initially point to the same empty memory block. Each memory pool also has memory pages for dynamic expansion, each containing several empty memory blocks. During expansion, the empty memory blocks in the new memory page are inserted at the head of the free list. When only one empty memory block remains in the free list, an expansion mechanism is triggered. The system allocates a new memory page and inserts its empty memory block at the head of the free list. This expansion and deallocation operation does not require mutual exclusion locking. Memory block allocation is only taken from the head of the free list, and deallocation is only inserted at the tail of the free list. Allocation and deallocation operations can be performed concurrently without locks.
[0019] Separate first and second mutexes are set for each memory pool to protect concurrent modifications to the head and tail pointers, respectively, thereby limiting lock contention to a smaller range.
[0020] During system initialization, multiple memory pools are created, each with a different memory block size. For example, when creating pools based on multiples of 4, the memory block sizes are 8, 12, 16, 20, 24, 28...; when creating pools based on squares of 2, the memory block sizes are 8, 16, 32, 64... In this embodiment, the user's empty memory block size is a square of 2. Therefore, the first memory pool has a size of 8, the second memory pool has a size of 16, the third memory pool has a size of 32, and the fourth memory pool has a size of 64.
[0021] like Figure 1 As shown, a memory pool consists of a memory pool head and multiple memory pages (mempage); a memory page consists of a memory page head and multiple memory blocks (memblock). A memory block is the basic unit for storing user data. The memory pool head structure includes: a pointer to the first empty memory block (bh), a pointer to the last empty memory block (bt), a pointer to the first memory page (ph), a pointer to the last memory page (pt), the memory card size, a first mutex, and a second mutex.
[0022] The empty memory blocks within each memory pool form a linked list, called the free list.
[0023] The memory page header structure includes a pointer to the next memory page.
[0024] Figure 1 In this context, 'e' represents an empty memory block, 'f' represents an occupied block, and the number represents the block number.
[0025] The memory pool creation process is as follows: A1. Create multiple memory pools according to user needs.
[0026] A2. Initialize the first memory pool and set the memory block size; A3. Create memory pages.
[0027] A4. Configure the relevant pointers for the memory pool head.
[0028] A5. All memory pools are initialized using steps 2.2-2.4.
[0029] like Figure 3 The memory pool creation example described in this embodiment is as follows: 1. Initialize the memory pool head; 2. Initialize memory page 1 and create 4 empty memory blocks; 3. Associate the memory pool with all memory pages; 4. If space is insufficient, continue to expand memory pages 2, 3, ..., N.
[0030] like Figure 2 As shown, the memory pool allocation and expansion process is as follows: Based on the user's requested space size, obtain the most suitable memory pool address. Acquire the first mutex lock. If bh and bt are not equal, it means the number of remaining empty memory blocks is greater than 1; proceed to step B1 to allocate memory pool space. If they are equal, proceed to step B5 to expand the memory pool.
[0031] B1, temporarily store bh; The pointers B2 and bh are changed to point to the second empty block; B3. Return the temporarily stored bh to the user; B4. Exit.
[0032] B5. Create a new memory page following the steps in B2; B6. The last memory block in the newly created memory page is equal to bh; B7 and bh point to the first memory block of the newly created memory page; B8. Modify the first 4 bytes of the first empty block to the size of the memory block, and then return the address after adding 4 bytes to the user. B9. Release the first mutex.
[0033] In this embodiment, the memory pool release process is as follows: the released memory block is added to the tail of the free list. The specific process is as follows: C1. Based on the memory address that the user wants to release, obtain the size of the memory block and locate the memory pool.
[0034] C2. Acquire the second mutex lock.
[0035] C3. The last unit of the free list points to the freed memory block; C4 and bt point to the freed memory block.
[0036] C5. The block pointer to be released points to null.
[0037] C6. Release the second mutex.
[0038] like Figure 4 As shown, the memory pool allocation and no expansion instance are as follows: After the memory pool is created, the user requests one memory block. The detailed steps are as follows: D1 and bh point to memory block 1; D2, temporarily store bh.
[0039] D3. Modify the pointer of bh from pointing to memory block 1 to pointing to memory block 2.
[0040] D4. The address of memory block 1 is returned to the user.
[0041] exist Figure 4 Based on this, the memory pool release instance is as follows: E1. Modify the pointer of bt, changing it from block 4 to memory block 1.
[0042] E2. Modify the back pointer of memory block 4 from null to bt (memory block 1).
[0043] E3. Note that bh does not point to the same value.
[0044] Example of memory pool release Figure 5 As shown.
[0045] like Figure 6 As shown, the memory pool expansion example is as follows: When only block number 2 remains after a user has requested memory, the memory pool needs to be expanded. The detailed process is as follows: F1. Following the initialization process, expand a new memory page. The new memory page contains four empty memory blocks: 5, 6, 7, and 8, which form a linked list. The pointers of F2 and pt have been changed from memory page 1 to memory page 2; F3. Change the back pointer of memory block 8 from null to bh (block 2). The pointers of F4 and bh have been changed from memory block 2 to memory block 5; F5, and the new memory page is placed at the head of the free list.
[0046] F6 If memory needs to be requested, simply return the first empty memory slot to the user.
[0047] In this embodiment, the multi-threaded lock-free design includes: The memory pool defined in this invention requires resource contention only in very rare cases. Detailed description: 1. After initialization, multiple memory pools with different memory block sizes will be created. Users can freely design their granularity to reduce the probability of threads accessing the same memory pool and fundamentally reduce contention.
[0048] II. Concurrency safety of the memory pool is ensured through a first mutex and a second mutex: First mutex lock: Protects the head pointer (bh). When multiple threads need to concurrently perform memory allocation or trigger resizing operations (both of which require modifying bh), they must compete for this lock.
[0049] The second mutex lock protects the tail pointer (bt). When multiple threads need to concurrently perform memory release operations (which require modifying bt and the next pointer of the predecessor node), they need to compete for this lock.
[0050] Third, since the allocation / expansion operation only interacts with the head pointer, and the release operation only interacts with the tail pointer, a thread performing an allocation (or expansion) and another thread performing a release can execute concurrently without any mutual exclusion. This is the key to the improved multithreading performance of this invention.
[0051] Fourth, when a single thread requests and expands the resource, it needs to modify pointers such as bh and pt, and this process is protected by the first mutex lock. At the same time, the release operation of another thread is protected by the second mutex lock. The two locks protect different resources, so requesting and releasing the resource can be performed simultaneously.
[0052] In this embodiment, when requesting (without expansion), the pointer to block 1 is obtained, and the pointer to bh is modified; when releasing, the pointer to bt is obtained, and the pointer to bt is modified. There is no resource conflict between the two operations.
[0053] When requesting and expanding, modify the pointer of pt; obtain the pointer of bh; modify the pointer of block 8; modify the pointer of bh again; when releasing, obtain the pointer of bt and modify the pointer of bt. These two operations do not conflict with resources.
[0054] In summary, this invention, while reducing memory fragmentation compared to traditional memory pools, allows for expansion when memory pool space is insufficient, making it suitable for scenarios where memory pool space is uncertain. Compared to dynamic memory pools, it completely avoids memory fragmentation. Furthermore, it exhibits low mutex lock usage in multi-threaded environments, significantly improving operational efficiency.
[0055] The above description is merely the basic principle and preferred embodiment of the present invention. Improvements and substitutions made by those skilled in the art based on the present invention are within the scope of protection of the present invention.
Claims
1. A method for concurrent management of a multi-threaded memory pool, characterized in that: This method includes: Maintain multiple independent memory pools, each corresponding to a preset memory block size. A memory block is the basic unit for storing user data. Each memory pool maintains a singly linked list consisting of free memory blocks, denoted as the free list. Each free list has a head pointer for memory block allocation and expansion, and a tail pointer for memory block reclamation. Memory block allocation is performed only by operating the head pointer from the head of the free list, and memory block reclamation is performed only by inserting into the tail of the free list, allowing memory block allocation and reclamation operations to be performed concurrently under lock-free conditions. When there is only one empty memory block left in the free list, an expansion operation is triggered, a new memory page is allocated, and its empty memory block is inserted into the head of the free list. Set separate first and second mutexes for each memory pool. The first and second mutexes protect concurrent modifications to the head pointer and tail pointer, respectively.
2. The concurrency management method for a multi-threaded memory pool according to claim 1, characterized in that: When a thread requests memory, a free memory block is taken from the head of the free list and allocated; when a thread releases memory, the released memory block is inserted into the tail of the free list; the expansion operation involves allocating new memory pages, linking all free memory blocks in the new memory pages together, and then inserting them into the head of the free list.
3. The concurrency management method for a multi-threaded memory pool according to claim 1, characterized in that: There is one first mutex and one second mutex. When multiple threads concurrently perform memory allocation or expansion operations, they compete for the first mutex. When multiple threads concurrently perform memory release operations, they compete for the second mutex.
4. The concurrency management method for a multi-threaded memory pool according to claim 1, characterized in that: During system initialization, multiple memory pools are created. Each memory pool includes a memory pool header and multiple memory pages. A memory page includes a memory page header and multiple memory blocks. A memory block is the basic unit for storing user data. The size of the memory blocks in each memory pool is different. All empty memory blocks in the memory pool form a free linked list; the head of the memory pool has pointers to the first empty memory block, the last empty memory block, the first memory page, the last memory page, the memory block size, the first mutex, and the second mutex.
5. The concurrency management method for a multi-threaded memory pool according to claim 4, characterized in that: The process of creating a memory pool is as follows: create multiple memory pools according to user needs, initialize the memory pool, set the memory block size, create memory pages, and configure the pointer of the memory pool head; The process of creating a memory page is as follows: allocate memory page space, form a free linked list of all memory blocks of the memory page, and store the linked list address in the starting space of the free memory block.
6. The concurrent management method for a multi-threaded memory pool according to claim 4, characterized in that: The memory pool allocation and expansion process is as follows: Based on the space requested by the user, obtain the most suitable memory pool address. Compare the pointers to the first and last empty memory blocks. If the pointers to the first and last empty memory blocks are not equal, it means that the number of remaining empty memory blocks is greater than 1. Temporarily store the pointer to the first empty memory block, change the pointer to the second empty memory block, and then return the temporarily stored pointer to the user. If the pointers to the first and last empty memory blocks are equal, it means that the number of remaining empty memory blocks is equal to 1. Expand the memory pool by first creating a new memory page, with the last memory page pointer pointing to the new memory page, the backward pointer of the last memory block in the new memory page pointing to the memory block where the pointer to the first empty memory block is located, and the pointer to the first memory block of the new memory page pointing to the first memory block of the new memory page. Return the first empty memory block to the user.
7. The concurrent management method for a multi-threaded memory pool according to claim 6, characterized in that: If multiple threads simultaneously request the same memory pool, the first mutex is acquired after obtaining the most suitable memory pool address, and the first mutex is released after returning the first empty memory block to the user.
8. The concurrent management method for a multi-threaded memory pool according to claim 4, characterized in that: The memory pool release process is as follows: based on the memory address to be released by the user, obtain the size of the memory block, find the memory pool, the last unit of the free list points to the memory block to be released, the last empty memory block pointer points to the memory block to be released, and the block pointer to be released points to null.
9. The concurrent management method for a multi-threaded memory pool according to claim 8, characterized in that: If multiple threads release the same memory pool at the same time, after finding the memory pool, acquire the second mutex lock, and release the second mutex lock after the block pointer to be released points to null.
10. The concurrency management method for a multi-threaded memory pool according to claim 1, characterized in that: The memory block size is a multiple of 4 or a square of 2.