Method, device, equipment, medium and program product for managing a shared memory pool

By not building a Freelist during shared memory pool initialization and dynamically allocating memory blocks during memory request, the problem of memory waste is solved, memory utilization efficiency is improved, and a large amount of physical memory is saved, especially in scenarios with small memory blocks.

CN122633362APending Publication Date: 2026-08-25TENCENT TECH SHANGHAI
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510208263.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-02-24
Publication Date
2026-08-25

AI Technical Summary

Technical Problem

In existing technologies, shared memory pools build a Freelist for each unit pool during initialization, which causes unused memory blocks to occupy physical resources for a long time, resulting in memory waste.

Method used

Instead of building a Freelist during shared memory pool initialization, the free list of the unit pool is checked during memory allocation. If it is empty, a memory block is allocated from unused memory and added to the free list during release.

Benefits of technology

It effectively avoids wasting physical memory and improves memory utilization efficiency, especially saving a lot of physical memory in scenarios with many memory blocks smaller than 4KB.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122633362A_ABST
    Figure CN122633362A_ABST
Patent Text Reader

Abstract

Embodiments of the present application provide a shared memory pool management method, device, equipment, medium and program product, the method comprising: when initializing the shared memory pool, establishing an empty free list for each unit pool; when receiving a memory application request of a service module, determining a target memory pool block from the shared memory pool; if the free list of the target unit pool is not empty, selecting a memory block from the free list of the target unit pool and allocating the memory block to the service module, otherwise allocating a memory block from the unused memory of the target unit pool; when the service module releases a used memory block, adding the released memory block to the corresponding free list. The method establishes an empty free list during initialization, avoids allocating physical memory for the memory block during initialization, and allocates physical memory only when the memory block is called, and preferentially allocates the used free memory block in the free list, thereby improving the use efficiency of the physical memory.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of memory management technology, and in particular to a method, apparatus, device, medium and program product for managing a shared memory pool. Background Technology

[0002] A shared memory pool is a contiguous area of ​​memory pre-allocated in the system. Multiple processes or threads can access and use this memory simultaneously, thereby enabling data sharing and exchange.

[0003] During system startup or program initialization, a contiguous memory region is allocated as a shared memory pool. Simultaneously, data structures are created to manage this shared memory pool, such as a freelist, to record memory block usage. A unitpool is the basic collection of storage units within the shared memory pool, used to store actual data objects or data blocks. Multiple unitpools together constitute the main storage structure of the shared memory pool. Each unitpool contains multiple memory blocks of equal size; these memory blocks are where the actual data is stored. In existing technologies, a freelist is constructed during shared memory initialization. Constructing the freelist requires accessing the first N bytes of each memory block; assuming N equals 8, this requires allocating 4KB of physical memory for that memory block.

[0004] These memory blocks allocated during initialization may never be used, but they occupy physical resources, preventing other programs from using this physical memory and thus causing a waste of physical memory in the shared memory pool. Summary of the Invention

[0005] This application provides a method, apparatus, device, medium, and program product for managing a shared memory pool, which improves the utilization of physical memory.

[0006] In a first aspect, embodiments of this application provide a method for managing a shared memory pool. The method includes: initializing a shared memory pool, the shared memory pool including at least one unit pool, wherein the free list of each unit pool is empty upon initialization, and the free list is used to maintain free memory blocks within the unit pool; when a memory request from a service module is received, determining a target unit pool from the shared memory pool; if the free list of the target unit pool is not empty, selecting at least one memory block from the free list of the target unit pool and allocating it to the service module; if the free list of the target unit pool is empty, allocating at least one memory block from the unused memory of the target unit pool to the service module; and when the service module releases a used memory block, adding the released memory block to the free list of the corresponding unit pool.

[0007] In some exemplary embodiments, initializing the shared memory pool includes: creating a contiguous block of memory as the shared memory pool through a shared memory interface; initializing the metadata of the shared memory pool; updating a first memory offset to the latest unused memory location, wherein the first memory offset is the offset of the used memory in the shared memory pool relative to the starting address of the shared memory pool; creating the at least one unit pool for the shared memory pool according to the first memory offset; initializing the management information of each unit pool; and updating the first memory offset to the latest unused memory location.

[0008] In some exemplary embodiments, the management information of the unit pool is stored in the header of the unit pool, and the management information of the unit pool includes the free list of the unit pool.

[0009] In some exemplary embodiments, the management information of the cell pool further includes at least one of the following: the size of the memory block, the number of memory blocks, the start address of the cell pool, the end address of the cell pool, and a second memory offset, wherein the second memory offset is the offset of the used memory in the cell pool relative to the start address of the cell pool.

[0010] In some exemplary embodiments, the step of allocating at least one memory block from the unused memory of the target unit pool to the service module if the free list of the target unit pool is empty includes:

[0011] Starting from the second memory offset of the target unit pool, allocate at least one memory block to the service module according to the size of the memory block, and update the second memory offset of the target unit pool to the latest unused memory location in the target unit pool.

[0012] In some exemplary embodiments, the shared memory pool further includes a header of the memory pool, wherein the metadata of the shared memory pool is stored in the header, and the metadata of the shared memory pool includes the start address, the end address and the first memory offset of the shared memory pool.

[0013] In some exemplary embodiments, the step of creating at least one unit pool for the shared memory pool based on the first memory offset, initializing the management information of each unit pool, and updating the first memory offset to the latest unused memory location includes: performing the following creation steps: taking a contiguous block of memory starting from the first memory offset as the newly created unit pool, initializing the management information of the newly created unit pool, and updating the first memory offset to the latest unused memory location; repeating the creation steps until a preset number of unit pools are created.

[0014] In some exemplary embodiments, determining the target memory pool block from the shared memory pool includes: selecting the target cell pool from at least one cell pool included in the shared memory pool, based on the type of memory requested by the memory request.

[0015] In some exemplary embodiments, when the service module releases a used memory block, adding the released memory block to the free list of the corresponding unit pool includes: when the service module releases a used memory block, if the free list of the unit pool containing the released memory block is empty, then pointing the pointer of the free list of the unit pool containing the released memory block to the starting address of the released memory block; if the free list of the unit pool containing the released memory block is not empty, then adding the starting address of the released memory block to the first N bytes of the previous free memory block in the free list of the unit pool containing the released memory block.

[0016] In some exemplary embodiments, N takes the value of 4 or 8.

[0017] Secondly, embodiments of this application provide a shared memory pool management device, the device comprising: an initialization module and a management module; the initialization module is configured to initialize the shared memory pool, the shared memory pool comprising at least one unit pool, wherein the free list of each unit pool is empty upon initialization, and the free list is used to maintain free memory blocks within the unit pool; the management module is configured to, upon receiving a memory request from a service module, determine a target unit pool from the shared memory pool; if the free list of the target unit pool is not empty, select at least one memory block from the free list of the target unit pool and allocate it to the service module; if the free list of the target unit pool is empty, allocate at least one memory block from the unused memory of the target unit pool to the service module; the management module is further configured to, when the service module releases a used memory block, add the released memory block to the free list of the corresponding unit pool.

[0018] Thirdly, embodiments of this application provide an electronic device, the electronic device comprising: a processor and a memory, the memory being used to store a computer program, and the processor being used to call and run the computer program stored in the memory to perform the method as described in the first aspect above.

[0019] Fourthly, embodiments of this application provide a computer-readable storage medium for storing a computer program that causes a computer to perform the method described in the first aspect above.

[0020] Fifthly, embodiments of this application provide a computer program product, including a computer program that, when executed by a processor, implements the method described in the first aspect above.

[0021] The technical solution provided in this application, during the initialization of the shared memory pool, establishes an empty free list for the unit pools included in the shared memory pool. When a memory request from a business module is received, a target memory block is determined from the shared memory pool. If the free list of the target unit pool is not empty, at least one memory block is selected from the free list of the target unit pool and allocated to the business module. If the free list of the target unit pool is empty, at least one memory block is allocated from the unused memory of the target unit pool to the business module. When the business module releases a used memory block, the released memory block is added to the free list. This method, by establishing an empty free list during initialization, avoids allocating physical memory for memory blocks during initialization. Instead, physical memory is allocated only when the memory block is called, and priority is given to allocating used free memory blocks in the free list. This effectively avoids wasting physical memory and improves the efficiency of physical memory utilization. Attached Figure Description

[0022] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0023] Figure 1 This is a schematic diagram of the structure after the initialization of a traditional shared memory pool.

[0024] Figure 2 A flowchart illustrating the shared memory pool management method provided in Embodiment 1 of this application;

[0025] Figure 3 This is a schematic diagram of the structure of a shared memory pool managed using the method of this application embodiment;

[0026] Figure 4 A schematic diagram illustrating the structural changes of the cell pool during memory block allocation;

[0027] Figure 5 A schematic diagram of the structure of the shared memory pool management device provided in Embodiment 2 of this application;

[0028] Figure 6 This is a schematic diagram of the structure of an electronic device provided in Embodiment 3 of this application. Detailed Implementation

[0029] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0030] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this invention are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of the invention described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or server that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or devices.

[0031] To facilitate understanding of the embodiments of this application, before describing the various embodiments of this application, some concepts involved in all embodiments of this application will be appropriately explained.

[0032] A shared memory pool is a contiguous area of ​​memory pre-allocated in the system. Multiple processes or threads can access and use this memory simultaneously, thereby enabling data sharing and exchange. It's important to note that the same process or thread can use the shared memory pool at different times. For example, if a process uses the shared memory pool and then kills it, the shared memory pool still exists and is not released. When the process is subsequently created, it can continue to use the shared memory pool, thus achieving data sharing.

[0033] During system startup or program initialization, a contiguous memory region is allocated as a shared memory pool. Simultaneously, data structures are created to manage this memory pool, such as a freelist, to record the usage of memory blocks within the shared memory pool.

[0034] Typically, a shared memory pool consists of a Mempool_Header and multiple unitpools. The Mempool_Header is the header structure of the shared memory pool, storing metadata about the entire pool and playing a crucial guiding role in its management and operation. A unitpool is a collection of basic storage units within the shared memory pool, used to store actual data objects or blocks. Multiple unitpools together constitute the main storage structure of the shared memory pool, providing concrete space for data storage and sharing.

[0035] For example, Mempool_Header contains the following: memory pool size, number of units in the pool, starting address of the shared memory pool (Top_ptr), ending address of the shared memory pool (Bottom_ptr), and first memory offset (Top_offset). Optionally, it may also contain some flags related to memory pool management, version number, and other information to ensure the correct use and maintenance of the memory pool.

[0036] Top_offset acts like a pointer, indicating the starting address of currently unused memory in the shared memory pool. During the initialization of the shared memory pool and subsequent memory allocation, Top_offset is continuously updated to ensure that memory allocation always starts from the correct location, avoiding memory reuse and conflicts.

[0037] At the beginning of the shared memory pool initialization phase, a contiguous memory region is first requested from the operating system as the shared memory pool. After the shared memory pool is successfully requested, Top_offset is initialized to 0, indicating that the starting position of the shared memory pool is unused, and the next memory allocation will start from this position.

[0038] The unit pool consists of a unit pool header (Unitpool_Header) and multiple memory blocks (UnitBlock, or simply Block). The Unitpool_Header contains management information for the memory blocks. For example, the Unitpool_Header includes basic data such as the size of the memory blocks, the number of memory blocks, the starting address of the unit pool (Unitpool_top_ptr), the ending address of the unit pool (Unitpool_bottom_ptr), the second memory offset (Unitpool_top_offset), and a Freelist pointer.

[0039] Freelist is used to connect all free memory blocks within the Unitpool, forming a linked list structure. The first N bytes (e.g., the first 8 bytes) of each memory block can be used to store the address of the next free memory block, thus linking the lists. Freelist allows for the rapid discovery and allocation of free memory blocks.

[0040] When a process or thread needs to use memory, it sends a memory allocation request to the shared memory pool. The shared memory pool management module searches for a suitable free memory block from the free list based on the current memory usage, marks it as used, and then returns the address of the memory block to the requester.

[0041] When a process or thread finishes using memory, it returns the memory block to the shared memory pool. The shared memory pool management module marks the memory block as free and inserts it into the free list so that subsequent memory allocation requests can reuse it.

[0042] When a process needs to allocate memory in the shared memory pool, the shared memory pool management module first accesses the Mempool_Header to check the list of free unit pools and finds a free unit pool. Then, within that unit pool, it searches for a free memory block using the Freelist and allocates it to the requesting process.

[0043] When memory is no longer needed and needs to be released, the memory block is first released back to its original cell pool, and the cell pool's Freelist and related management information are updated. If all memory blocks in the cell pool become free, the cell pool is marked as free, and the list of free cell pools in Mempool_Header is updated.

[0044] Traditional shared memory pool schemes construct a Freelist for each unit pool during initialization. Constructing the Freelist requires accessing the first 8 bytes of each memory block within the unit pool, allocating 4KB of physical memory for each block. It's understood that physical memory is paged, with a page size of 4KB. Even accessing a single byte within a page requires allocating an entire page of physical memory; a page is the smallest unit of physical memory allocation. While some systems may use page sizes of 8KB or 16KB, this embodiment uses 4KB for illustration. If these pre-allocated memory blocks during shared memory pool initialization are never used by upper-layer business modules, the 4KB of physical memory occupied by each block is wasted.

[0045] Figure 1 This is a schematic diagram of the structure after the initialization of a traditional shared memory pool, such as... Figure 1As shown, during the initialization of the shared memory pool, a separate Freelist is constructed for each cell pool. The pointer to the Freelist is stored in the header of the cell pool, and the pointer to the Freelist points to the starting address of the first memory block. Taking cell pool 2 as an example, cell pool 2 has N memory blocks. In the Freelist of cell pool 2, the first N bytes of each memory block (where N is 8 in a 64-bit system and 4 in a 32-bit system) store the address of the next free memory block. For example, the first 8 bytes of memory block 1 store the starting address of memory block 2, the first 8 bytes of memory block 2 store the starting address of memory block 3, and so on. The pointer to the Freelist points to the starting address of memory block 1.

[0046] Traditional shared memory pool solutions create a Freelist for each cell pool during initialization. The first N bytes of each memory block within a cell pool store the address of the next memory block. Therefore, the shared memory pool management model accesses all memory blocks within the cell pool and allocates physical memory to each. Taking a system page size of 4KB as an example, the size of a memory block within the shared memory pool can be less than, equal to, or greater than 4KB. If a memory block is less than or equal to 4KB, all memory blocks in the cell pool will be allocated 4KB of physical memory; if a memory block is greater than 4KB, only the first 4KB of that block will be allocated 4KB of physical memory, and the remaining memory will not be allocated. These memory blocks may never be used by upper-layer business modules, but they continuously occupy physical memory, preventing other programs from using this memory and resulting in a waste of the device's physical memory.

[0047] To address the aforementioned issues, this application provides a shared memory pool management method. During shared memory pool initialization, instead of building a Freelist for the unit pool, when a business module requests memory, it first checks if the unit pool's Freelist is empty. If the Freelist is not empty, a memory block is selected from the unit pool's Freelist and allocated to the business module. If the Freelist is empty, an unallocated memory block is allocated from the unit pool to the business module. When a business module releases a memory block, the released memory block is added to the Freelist. This method avoids pre-allocating multiple memory blocks; instead, it allocates memory blocks to the business module only when it needs to use memory, prioritizing the allocation of already used free memory blocks from the Freelist. This avoids the problem of wasting physical memory due to excessive pre-allocation of memory blocks.

[0048] The method described in this application embodiment can be executed by an electronic device, which can be a terminal device or a server. The terminal device can be a mobile phone, tablet computer, laptop computer, desktop computer, smart TV, smart wearable device, smart voice interaction device, smart home appliance, vehicle terminal, aircraft, etc.

[0049] A server can be a standalone physical server, a server cluster or distributed system consisting of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, content delivery networks (CDN), and big data and artificial intelligence platforms, but it is not limited to these.

[0050] For example, the electronic device is a game server, which uses the shared memory pool solution provided in this application to reduce the physical memory occupied by the game process. For instance, the scene process, which hosts the scene, used 20GB of memory before optimization and now uses 18GB, saving 2GB of physical memory; the homesvr process used 3.6GB of memory before optimization and now uses 0.4GB, saving 3.2GB of physical memory.

[0051] Figure 2 A flowchart of the shared memory pool management method provided in Embodiment 1 of this application is shown below. Figure 2 As shown, the method provided in this embodiment includes the following steps.

[0052] S101. Initialize the shared memory pool, which includes at least one unit pool. When the initialization is complete, the free list of each unit pool is empty. The free list is used to maintain the free memory blocks in the unit pool.

[0053] During the initialization of the shared memory pool, an empty Freelist is created for each cell pool. The Freelist is used to maintain free memory blocks within the cell pool. Unlike existing technologies, in this embodiment, the Freelist of the cell pool is empty after initialization. Therefore, no physical memory is allocated for the memory blocks within the cell pool, thus avoiding waste of physical memory.

[0054] Optionally, the Freelist of the cell pool is included in the management information of the cell pool, which is used to manage the cell pool.

[0055] For example, the initialization process mainly includes two steps: Step 1, creating a contiguous block of memory as a shared memory pool through the shared memory interface, initializing the metadata of the shared memory pool, and updating the first memory offset to the latest unused memory location. Step 2, creating at least one unit pool for the shared memory pool based on the first memory offset, initializing the management information of each unit pool, and updating the first memory offset to the latest unused memory location.

[0056] In step one, the shared memory pool management module can first create a contiguous shared memory as a shared memory pool through a memory interface (such as the shmget function), and initialize the shared memory pool's metadata (also known as basic information or management information). The shared memory pool's metadata is stored in the shared memory pool's header (i.e., Mempool_Header). Therefore, initializing the shared memory pool's metadata can also be understood as initializing Mempool_Header.

[0057] For example, the metadata (or Mempool_Header) of a shared memory pool includes the following: the size of the cell pool, the number of cell pools, the starting address of the shared memory pool, the ending address of the shared memory pool, the first memory offset, etc.

[0058] The first memory offset is the offset of the used memory in the shared memory pool relative to the starting address (i.e., Top_ptr) of the shared memory pool. After initializing the metadata of the shared memory pool, the first memory offset is updated to (or moved to) the latest unused memory location, which is the address after the end address of Mempool_Header.

[0059] Step 2 is used to create or register a unit pool. The shared memory pool management module can create a unit pool through the following steps: take a contiguous block of memory starting from the first memory offset as the newly created unit pool, initialize the management information of the newly created unit pool, and update the first memory offset to the latest unused memory location; repeat this creation step until the preset number of unit pools are created.

[0060] The unit pool consists of Unitpool_Header (the header of the unit pool) and multiple memory blocks. The management information of the unit pool is stored in Unitpool_Header. Therefore, initializing the management information of the unit pool can also be understood as initializing Unitpool_Header.

[0061] For example, the management information of the unit pool (or Unitpool_Header) includes basic data such as the size of the memory block, the number of memory blocks, the starting address of the unit pool, the ending address of the unit pool, the second memory offset (Unitpool_top_offset), and a Freelist pointer.

[0062] The second memory offset is the offset of the used memory in the cell pool relative to the starting address of that cell pool. It can be understood that the first memory offset applies to the shared memory pool, while the second memory offset applies to the cell pool.

[0063] When multiple unit pools are created for a shared memory pool, these unit pools may have different types and sizes. The type of a unit pool is determined by the size of the memory blocks within it. For example, the shared memory pool management module creates two unit pools: Unit Pool 1 and Unit Pool 2. The memory block size in Unit Pool 1 is 4KB, and the memory block size in Unit Pool 2 is 8KB. Therefore, Unit Pool 1 and Unit Pool 2 have different types. Assuming that the number of memory blocks in both Unit Pool 1 and Unit Pool 2 is 1000, the sizes of the two unit pools will also be different, with Unit Pool 2 having more memory than Unit Pool 1.

[0064] Figure 3 This is a schematic diagram of the structure of a shared memory pool managed using the method of this application embodiment, as shown below. Figure 3 As shown, the shared memory pool management module creates two unit pools: Unit Pool 1 and Unit Pool 2. The start and end addresses of the entire shared memory pool are shown in the figure. Starting from the start address, the header of the shared memory pool is initialized first, then Unit Pool 1 and Unit Pool 2 are created, and the headers of Unit Pool 1 and Unit Pool 2 are initialized. Taking Unit Pool 2 as an example, the start and end addresses of Unit Pool 2 are shown below. Figure 3 As shown, starting from the beginning address of cell pool 2, the sequence is the header of cell pool 2 followed by three memory blocks, with the second memory offset located at the end of memory block 3. It's understandable that only three memory blocks are shown in the diagram; cell pool 2 can contain more or fewer memory blocks.

[0065] pass Figure 3As can be seen, after the shared memory pool is initialized, there are no pointer connections between memory blocks, meaning no Freelist is created. Alternatively, it can be understood that although a Freelist is created after the shared memory pool is initialized, it is empty. In this case, no physical memory is allocated to the memory blocks in the unit pool. The method in this embodiment allocates physical memory to the memory blocks in the unit pool only when the business module needs to use memory, thereby avoiding the waste of physical memory.

[0066] S102. When a memory request from a business module is received, the target unit pool is determined from the shared memory pool. If the free list of the target unit pool is not empty, at least one memory block is selected from the free list of the target unit pool and allocated to the business module. If the free list of the target unit pool is empty, at least one memory block is allocated from the unused memory of the target unit pool to the business module.

[0067] This business module is usually located in the upper layer of the application, and is therefore also called the upper layer business module. In different application scenarios, the business module performs different business. For example, in a game scenario, this business module can be a game process or a thread.

[0068] When a business module has data to process or transmit, it requests memory from the shared memory pool. The business module can send a memory request to the management module of the shared memory pool. Optionally, the memory request includes the size and number of memory blocks to be requested.

[0069] The shared memory pool management module is used to quickly and efficiently allocate memory blocks from the shared memory pool based on memory request needs from processes or threads. For example, in a multi-tasking server system, when a new memory request arrives and needs to be processed, the shared memory pool management module can quickly allocate the necessary memory space to the process handling the request, reducing waiting time and improving system response speed. It can employ different allocation strategies, such as first-fit and best-fit, to improve memory allocation efficiency.

[0070] The shared memory pool management module is also responsible for promptly reclaiming memory blocks after a process or thread has finished using them, restoring them to a free state so that subsequent memory requests can use them. During the reclamation process, the shared memory pool management module also needs to update relevant data structures, such as Freelist, to accurately reflect memory usage.

[0071] In this embodiment, when the shared memory pool management module receives a memory request, it first determines a target cell pool from the shared memory pool based on the memory request. The shared memory pool management module can select one cell pool as the target cell pool from multiple cell pools according to a pre-configured allocation strategy.

[0072] In one implementation, the shared memory pool management module selects a target unit pool from at least one unit pool included in the shared memory pool based on the type of memory requested in the memory allocation request. The type of memory requested in the memory allocation request refers to the size of the requested memory block. If the business module requests the allocation of a 4KB memory block, a unit pool with a memory block size of 4KB is selected from the shared memory pool as the target unit pool. If the business module requests the allocation of an 8KB memory block, a unit pool with a memory block size of 8KB is selected from the shared memory pool as the target unit pool.

[0073] Optionally, the shared memory pool management module can also select the pool with the most free memory blocks from the shared memory pool as the target pool, or select a pool as the target pool according to a predefined access order.

[0074] After determining the target unit pool, the shared memory pool management module checks if the target unit pool's Freelist is empty. The target unit pool's Freelist is empty after the shared memory pool initialization; an empty Freelist can be understood as the Freelist pointer being null. Subsequently, when allocated memory blocks in the target unit pool are released, the released memory blocks are added to the target unit pool's Freelist. The allocated memory blocks in the target unit pool refer to those allocated to business modules, and these allocated memory blocks have been allocated physical memory.

[0075] The Freelist of the target unit pool is empty after the shared memory pool is initialized. Assuming that a memory block allocated in the target unit pool is released for the first time after a period of time, the pointer of the target unit pool's Freelist is updated to point to the starting address of the first released memory block. That is, the value of the Freelist pointer stored in the header of the target unit pool is updated from Null to the starting address of the first released memory block. Subsequently, when a memory block allocated in the target unit pool is released, the starting address of the released memory block is added to the first N bytes of the previous free memory block in the target unit pool's Freelist, completing the update of the target unit pool's Freelist.

[0076] When the Freelist of the target unit pool is not empty, it means that there are free memory blocks available in the Freelist. At this time, the shared memory pool management module can select at least one free memory block from the Freelist of the target unit pool and allocate it to the business module. The free memory blocks in the Freelist are memory blocks already allocated in the target unit pool. Therefore, when allocating memory blocks to business modules, the general principle of the shared memory pool management module is to prioritize memory blocks already allocated in the unit pool, which can also be understood as prioritizing the most recently used memory blocks in the unit pool, further avoiding waste of physical memory.

[0077] When the Freelist of the target unit pool is empty, it means that there are no free memory blocks available in the Freelist of the target unit pool. At this time, the management module of the shared memory pool allocates at least one memory block from the unused memory of the target unit pool to the business module.

[0078] In one implementation, the management module of the shared memory pool reads the second memory offset of the target unit pool from the management information of the target unit pool, allocates at least one memory block to the business module according to the size of the memory block starting from the second memory offset of the target unit pool, and updates the second memory offset of the target unit pool to the unused memory in the target unit pool.

[0079] Understandably, when the shared memory pool management module allocates multiple memory blocks to a business module, and these multiple memory blocks are of the same size, the shared memory pool management module starts from the second memory offset of the target unit pool, first allocates the first memory block to the business module according to the size of the memory block, and updates the second memory offset of the target unit pool to the unused memory in the target unit pool. Then, starting from the updated second memory offset, it allocates the second memory block to the business module according to the size of the memory block, and updates the second memory offset of the target unit pool to the unused memory in the target unit pool, and so on, until the allocation is completed.

[0080] S103. When a business module releases a used memory block, the released memory block is added to the corresponding free list.

[0081] After a business module finishes using the internally allocated memory block, it releases the memory block. The shared memory pool management module then adds the released memory block to the corresponding Freelist. Specifically, when a business module releases a used memory block, if the Freelist of the cell pool containing the released memory block is empty, the pointer of the Freelist of the cell pool containing the released memory block is set to the starting address of the released memory block; if the Freelist of the cell pool containing the released memory block is not empty, the starting address of the released memory block is added to the first N bytes of the previous free memory block in the Freelist of the cell pool containing the released memory block.

[0082] In this embodiment of the application, the memory blocks allocated by the shared memory pool management module to the business module all occupy physical memory. The memory blocks allocated by the shared memory pool management module to the business module include memory blocks in the Freelist and memory blocks currently being used by the business module. The memory blocks in the Freelist are memory blocks released after being used by the business module, i.e., memory blocks used by the business module.

[0083] Figure 4 This is a diagram illustrating the structural changes of the cell pool during memory block allocation, such as... Figure 4 As shown, when the shared memory pool management module receives a memory request from a business module, it first allocates the first memory block (i.e., memory block 1) to the business module from the unit pool. When it receives another memory request from the business module, it allocates the second memory block (i.e., memory block 2) to the business module. Figure 4 It can be seen that after allocating the first two memory blocks, a Freelist was not created or the Freelist was empty. The business module uses the allocated memory blocks 1 and 2. When the business module releases memory block 1, the shared memory pool management module creates a Freelist and sets the Freelist pointer to the starting address of memory block 1. Subsequently, when the business module releases memory block 2, the shared memory pool management module adds the starting address of memory block 2 to the first N bytes of memory block 1, thus adding memory block 2 to the Freelist. At this point, the Freelist contains two free memory blocks: memory block 1 and memory block 2. When the shared memory pool management module receives another memory request from the business module, it allocates a third memory block for the business module. This time, since the Freelist is not empty, it selects memory block 1 from the Freelist and allocates it to the business module, setting the Freelist pointer to the starting address of memory block 2.

[0084] The shared memory pool solution provided in this application can save physical memory, especially in scenarios where a large number of memory blocks need to be pre-allocated but not all of them will be used. For memory blocks smaller than 4KB, the saved physical memory is the size of all unused memory blocks (for example, in a scenario where 100,000 memory blocks are pre-allocated but only 50,000 are actually used, using the solution of this application, the remaining 50,000 memory blocks will not be allocated physical memory, saving 50,000 * 4KB = 195MB of memory). For memory blocks larger than 4KB, the saved physical memory is the number of unused memory blocks * 4KB. If 100,000 memory blocks are pre-allocated, the saved physical memory is 100,000 * 4KB = 390MB. The shared memory pool solution proposed in this application is more effective in scenarios with a large number of memory blocks smaller than 4KB.

[0085] In this embodiment, during the initialization of the shared memory pool, an empty free list is established for the unit pools included in the shared memory pool. When a memory request from a business module is received, a target memory block is determined from the shared memory pool. If the free list of the target unit pool is not empty, at least one memory block is selected from the free list of the target unit pool and allocated to the business module. If the free list of the target unit pool is empty, at least one memory block is allocated from the unused memory of the target unit pool to the business module. When a business module releases a used memory block, the released memory block is added to the free list. This method, by establishing an empty free list during initialization, avoids allocating physical memory for memory blocks during initialization. Instead, physical memory is allocated only when a memory block is requested, and priority is given to allocating used free memory blocks in the free list. This effectively avoids wasting physical memory and improves the efficiency of physical memory utilization.

[0086] To facilitate better implementation of the shared memory pool management method of this application embodiment, this application embodiment also provides a shared memory pool management device. Figure 5 This is a schematic diagram of the structure of the shared memory pool management device provided in Embodiment 2 of this application, as shown below. Figure 5 As shown, the management device 100 of the shared memory pool may include: an initialization module 11 and a management module 12.

[0087] The initialization module 11 is used to initialize the shared memory pool, which includes at least one unit pool. When the initialization is completed, the free list of each unit pool is empty. The free list is used to maintain the free memory blocks in the unit pool.

[0088] The management module 12 is used to determine the target unit pool from the shared memory pool when it receives a memory request from the service module. If the free list of the target unit pool is not empty, it selects at least one memory block from the free list of the target unit pool and allocates it to the service module. If the free list of the target unit pool is empty, it allocates at least one memory block from the unused memory of the target unit pool to the service module.

[0089] The management module 12 is also used to add the released memory block to the free list of the corresponding unit pool when the business module releases the used memory block.

[0090] In some implementations, the initialization module 11 is specifically used for:

[0091] A contiguous block of memory is created as the shared memory pool through the shared memory interface, the metadata of the shared memory pool is initialized, and the first memory offset is updated to the latest unused memory location, wherein the first memory offset is the offset of the used memory in the shared memory pool relative to the starting address of the shared memory pool;

[0092] Based on the first memory offset, at least one unit pool is created for the shared memory pool, the management information of each unit pool is initialized, and the first memory offset is updated to the latest unused memory location.

[0093] In some implementations, the management information of the unit pool is stored in the header of the unit pool, and the management information of the unit pool includes the free list of the unit pool.

[0094] In some implementations, the management information of the cell pool also includes at least one of the following: the size of the memory block, the number of memory blocks, the starting address of the cell pool, the ending address of the cell pool, and a second memory offset, wherein the second memory offset is the offset of the used memory in the cell pool relative to the starting address of the cell pool.

[0095] In some implementations, the management module 12 is specifically used for:

[0096] Starting from the second memory offset of the target unit pool, allocate at least one memory block to the service module according to the size of the memory block, and update the second memory offset of the target unit pool to the latest unused memory location in the target unit pool.

[0097] In some implementations, the shared memory pool further includes a header, in which metadata about the shared memory pool is stored. This metadata includes the start address, end address, and first memory offset of the shared memory pool.

[0098] In some implementations, the initialization module 11 is specifically used for:

[0099] Perform the following creation steps: take a contiguous block of memory starting from the first memory offset as the newly created unit pool, initialize the management information of the newly created unit pool, and update the first memory offset to the latest unused memory location;

[0100] Repeat the creation steps until the preset number of cell pools have been created.

[0101] In some implementations, the management module 12 is specifically used to: select the target unit pool from at least one unit pool included in the shared memory pool according to the type of memory requested in the memory request.

[0102] In some implementations, the management module 12 is specifically used for:

[0103] When the business module releases a used memory block, if the free list of the unit pool where the released memory block is located is empty, then the pointer of the free list of the unit pool where the released memory block is located is set to the starting address of the released memory block.

[0104] If the free list of the cell pool containing the released memory block is not empty, then the starting address of the released memory block is added to the first N bytes of the previous free memory block in the free list of the cell pool containing the released memory block.

[0105] In some implementations, N takes the value of 4 or 8.

[0106] It should be understood that the device embodiments and method embodiments can correspond to each other, and similar descriptions can be referred to the method embodiments. To avoid repetition, further details will not be provided here.

[0107] The apparatus 100 of this application embodiment has been described above from the perspective of functional modules in conjunction with the accompanying drawings. It should be understood that this functional module can be implemented in hardware, in software instructions, or in a combination of hardware and software modules. Specifically, the steps of the method embodiments in this application can be completed by integrated logic circuits in the processor's hardware and / or by software instructions. The steps of the method disclosed in this application embodiment can be directly manifested as execution by a hardware decoding processor, or by a combination of hardware and software modules in the decoding processor. Optionally, the software module can reside in a mature storage medium in the art, such as random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, registers, etc. This storage medium is located in memory, and the processor reads information from the memory and, in conjunction with its hardware, completes the steps in the above method embodiments.

[0108] In this application embodiment, the terms "module" or "unit" refer to a computer program or part of a computer program that has a predetermined function and works with other related parts to achieve a predetermined goal, and can be implemented wholly or partially using software, hardware (such as processing circuitry or memory), or a combination thereof. Similarly, a processor (or multiple processors or memory) can be used to implement one or more modules or units. Furthermore, each module or unit can be part of an overall module or unit that includes the functionality of that module or unit.

[0109] This application also provides an electronic device. Figure 6 This is a schematic diagram of the structure of an electronic device provided in Embodiment 3 of this application, such as... Figure 6 As shown, the electronic device 200 may include: a processor 21 with one or more processing cores, a memory 22 with one or more computer-readable storage media, and a computer program stored on the memory 22 and executable on the processor. The processor 21 and the memory 22 are electrically connected. Those skilled in the art will understand that the computer device structure shown in the figures does not constitute a limitation on the computer device, and may include more or fewer components than shown, or combine certain components, or have different component arrangements.

[0110] The processor 21 is the control center of the electronic device 200. It connects various parts of the electronic device 200 through various interfaces and lines. By running or loading software programs and / or modules stored in the memory 22, and calling data stored in the memory 22, it executes various functions of the electronic device 200 and processes data, thereby performing overall processing of the electronic device 200.

[0111] In this embodiment, the processor 21 in the electronic device 200 loads the instructions corresponding to the processes of one or more applications into the memory 22 according to the following steps, and the processor 21 runs the applications stored in the memory 22 to achieve the following functions:

[0112] Initialize a shared memory pool, which includes at least one unit pool. When initialization is complete, the free list of each unit pool is empty. The free list is used to maintain free memory blocks within the unit pool.

[0113] When a memory request is received from a business module, a target unit pool is determined from the shared memory pool. If the free list of the target unit pool is not empty, at least one memory block is selected from the free list of the target unit pool and allocated to the business module. If the free list of the target unit pool is empty, at least one memory block is allocated from the unused memory of the target unit pool to the business module.

[0114] When the business module releases a used memory block, it adds the released memory block to the free list of the corresponding unit pool.

[0115] For details on the implementation of each of the above operations, please refer to the previous examples, which will not be repeated here.

[0116] In some embodiments, the processor 21 may include, but is not limited to: a general-purpose processor, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc.

[0117] In some embodiments of this application, the memory 22 includes, but is not limited to, volatile memory and / or non-volatile memory. The non-volatile memory may be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), or flash memory. The volatile memory may be random access memory (RAM), which serves as an external cache. By way of example, but not limitation, many forms of RAM are available, such as Static RAM (SRAM), Dynamic RAM (DRAM), Synchronous DRAM (SDRAM), Double Data Rate SDRAM (DDR SDRAM), Enhanced Synchronous DRAM (ESDRAM), Synchronous Link DRAM (SLDRAM), and Direct Rambus RAM (DR RAM).

[0118] Optional, such as Figure 6 As shown, the electronic device 20 also includes a display screen 23, a radio frequency circuit 24, an audio circuit 25, an input unit 26, and a power supply 27. The processor 21 is electrically connected to the display screen 23, the radio frequency circuit 24, the audio circuit 25, the input unit 26, and the power supply 27. Those skilled in the art will understand that... Figure 6 The computer device structure shown does not constitute a limitation on the computer device and may include more or fewer components than shown, or combine certain components, or have different component arrangements.

[0119] The display screen 23 can be used to display a graphical user interface (GUI) and receive operation commands generated by the user interacting with the GUI. The display screen 23 can be a touch screen, which may include a display panel and a touch panel. The display panel can be used to display information input by the user or information provided to the user, as well as various graphical user interfaces of the computer device. These graphical user interfaces can be composed of graphics, text, icons, video, and any combination thereof. Optionally, the display panel can be configured using a liquid crystal display (LCD), organic light-emitting diode (OLED), or other similar technologies. The touch panel can be used to collect touch operations performed by the user on or near it (such as operations performed by the user using a finger, stylus, or any suitable object or accessory on or near the touch panel), generate corresponding operation commands, and execute the corresponding program according to the operation commands. Optionally, the touch panel may include a touch detection device and a touch controller. The touch detection device detects the user's touch location and the signal generated by the touch operation, transmitting the signal to the touch controller. The touch controller receives touch information from the touch detection device, converts it into touch point coordinates, and sends it to the processor 21. It can also receive and execute commands from the processor 21. The touch panel can cover the display panel. When the touch panel detects a touch operation on or near it, it transmits the information to the processor 21 to determine the type of touch event. Subsequently, the processor 21 provides corresponding visual output on the display panel based on the type of touch event. In this embodiment, the touch panel and the display panel can be integrated into a touch display screen to achieve input and output functions. However, in some embodiments, the touch panel and the touch display screen can be implemented as two independent components to achieve input and output functions. That is, the touch display screen can also be used as part of the input unit 26 to achieve input functions.

[0120] The radio frequency circuit 24 can be used to transmit and receive radio frequency signals to establish wireless communication with network devices or other computer devices, and to transmit and receive signals with network devices or other computer devices.

[0121] Audio circuitry 25 can be used to provide an audio interface between a user and a computer device via a speaker and a microphone. Audio circuitry 25 can convert received audio data into electrical signals and transmit them to the speaker, where the speaker converts them into sound signals for output. Conversely, the microphone converts collected sound signals into electrical signals, which are then received by audio circuitry 25, converted back into audio data, and then processed by processor 21 before being transmitted via radio frequency circuitry 24 to, for example, another computer device, or output to memory 22 for further processing. Audio circuitry 25 may also include an earphone jack to facilitate communication between peripheral headphones and the computer device.

[0122] The input unit 26 can be used to receive input numbers, characters, or object feature information (such as fingerprints, iris, facial information, etc.), and to generate keyboard, mouse, joystick, optical, or trackball signal inputs related to user settings and function control.

[0123] Power supply 27 is used to supply power to various components of electronic device 200. Optionally, power supply 27 can be logically connected to processor 21 through a power management system, thereby enabling functions such as charging, discharging, and power consumption management through the power management system. Power supply 27 may also include one or more DC or AC power supplies, recharging systems, power fault detection circuits, power converters or inverters, power status indicators, and other arbitrary components.

[0124] although Figure 6 As not shown in the diagram, the electronic device 200 may also include a camera, sensor, wireless fidelity module, Bluetooth module, etc., which will not be described in detail here.

[0125] It should be understood that the various components in the electronic device are connected through a bus system, which includes a data bus, a power bus, a control bus, and a status signal bus.

[0126] This application also provides a computer storage medium storing a computer program thereon, which, when executed by a computer, enables the computer to perform the methods of the above-described method embodiments. Alternatively, embodiments of this application also provide a computer program product containing instructions that, when executed by a computer, cause the computer to perform the methods of the above-described method embodiments.

[0127] This application also provides a computer program product comprising a computer program stored in a computer-readable storage medium. The processor of an electronic device reads the computer program from the computer-readable storage medium and executes the computer program, causing the electronic device to perform the corresponding processes in the above method embodiments; for brevity, these will not be elaborated further here.

[0128] In the several embodiments provided in this application, it should be understood that the disclosed systems, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of modules is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple modules or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between apparatuses or modules may be electrical, mechanical, or other forms.

[0129] The modules described as separate components may or may not be physically separate. The components shown as modules may or may not be physical modules; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. For example, the functional modules in the various embodiments of this application may be integrated into one processing module, or each module may exist physically separately, or two or more modules may be integrated into one module.

[0130] The above are merely specific embodiments of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.

Claims

1. A method for managing a shared memory pool, characterized in that, The method includes: Initialize a shared memory pool, which includes at least one unit pool. When initialization is complete, the free list of each unit pool is empty. The free list is used to maintain free memory blocks within the unit pool. When a memory request is received from a business module, a target unit pool is determined from the shared memory pool. If the free list of the target unit pool is not empty, at least one memory block is selected from the free list of the target unit pool and allocated to the business module. If the free list of the target unit pool is empty, at least one memory block is allocated from the unused memory of the target unit pool to the business module. When the business module releases a used memory block, it adds the released memory block to the free list of the corresponding unit pool.

2. The method according to claim 1, characterized in that, The initialization of the shared memory pool includes: A contiguous block of memory is created as the shared memory pool through the shared memory interface, the metadata of the shared memory pool is initialized, and the first memory offset is updated to the latest unused memory location, wherein the first memory offset is the offset of the used memory in the shared memory pool relative to the starting address of the shared memory pool; Based on the first memory offset, at least one unit pool is created for the shared memory pool, the management information of each unit pool is initialized, and the first memory offset is updated to the latest unused memory location.

3. The method according to claim 2, characterized in that, The management information of the unit pool is stored in the header of the unit pool, and the management information of the unit pool includes the free list of the unit pool.

4. The method according to claim 3, characterized in that, The management information of the cell pool also includes at least one of the following: the size of the memory block, the number of memory blocks, the starting address of the cell pool, the ending address of the cell pool, and the second memory offset, wherein the second memory offset is the offset of the used memory in the cell pool relative to the starting address of the cell pool.

5. The method according to claim 4, characterized in that, If the free list of the target unit pool is empty, then allocating at least one memory block from the unused memory of the target unit pool to the service module includes: Starting from the second memory offset of the target unit pool, allocate at least one memory block to the service module according to the size of the memory block, and update the second memory offset of the target unit pool to the latest unused memory location in the target unit pool.

6. The method according to any one of claims 2-5, characterized in that, The shared memory pool also includes a memory pool header, in which the metadata of the shared memory pool is stored. The metadata of the shared memory pool includes the start address, end address, and first memory offset of the shared memory pool.

7. The method according to any one of claims 2-5, characterized in that, The step of creating at least one unit pool for the shared memory pool based on the first memory offset, initializing the management information of each unit pool, and updating the first memory offset to the latest unused memory location includes: Perform the following creation steps: take a contiguous block of memory starting from the first memory offset as the newly created unit pool, initialize the management information of the newly created unit pool, and update the first memory offset to the latest unused memory location; Repeat the creation steps until the preset number of cell pools have been created.

8. The method according to any one of claims 1-5, characterized in that, Determining the target memory pool block from the shared memory pool includes: Based on the type of memory requested in the memory request, the target cell pool is selected from at least one cell pool included in the shared memory pool.

9. The method according to any one of claims 1-5, characterized in that, When the business module releases a used memory block, it adds the released memory block to the free list of the corresponding unit pool, including: When the business module releases a used memory block, if the free list of the unit pool where the released memory block is located is empty, then the pointer of the free list of the unit pool where the released memory block is located is set to the starting address of the released memory block. If the free list of the cell pool containing the released memory block is not empty, then the starting address of the released memory block is added to the first N bytes of the previous free memory block in the free list of the cell pool containing the released memory block.

10. The method according to claim 9, characterized in that, The value of N can be 4 or 8.

11. A management device for a shared memory pool, characterized in that, include: An initialization module is used to initialize a shared memory pool, which includes at least one unit pool. When initialization is complete, the free list of each unit pool is empty. The free list is used to maintain free memory blocks within the unit pool. The management module is used to determine the target unit pool from the shared memory pool when it receives a memory request from the business module. If the free list of the target unit pool is not empty, it selects at least one memory block from the free list of the target unit pool and allocates it to the business module. If the free list of the target unit pool is empty, it allocates at least one memory block from the unused memory of the target unit pool to the business module. The management module is also used to add the released memory block to the free list of the corresponding unit pool when the business module releases the used memory block.

12. An electronic device, characterized in that, include: A processor and a memory, the memory being used to store a computer program, the processor being used to invoke and run the computer program stored in the memory to perform the method of any one of claims 1 to 10.

13. A computer-readable storage medium, characterized in that, Used to store a computer program that causes a computer to perform the method as described in any one of claims 1 to 10.

14. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the method as described in any one of claims 1 to 10.