Memory allocation method and electronic device

By establishing a shared memory space and flexibly allocating memory resources according to the type and value of memory requirements, the problem of insufficient and wasted memory resources caused by static planning is solved, and memory resource acquisition under high load and resource optimization under low load are realized.

CN121542054BActive Publication Date: 2026-04-07INSPUR SUZHOU INTELLIGENT TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2026-01-19
Publication Date
2026-04-07

AI Technical Summary

Technical Problem

In existing technologies, memory resource allocation uses a static planning approach, which leads to insufficient resources under high business load and wasted resources under low load, reducing the flexibility of memory resource allocation.

Method used

A shared memory space is established based on the shared memory information of the target device. Memory request requests are parsed to determine the type and value of memory requirement. Contiguous memory is allocated from high address to low address, and fragmented memory is allocated from low address to high address.

Benefits of technology

It ensures memory resource allocation under high load and reduces resource waste under low load, thereby improving memory resource utilization and allocation flexibility.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121542054B_ABST
    Figure CN121542054B_ABST
Patent Text Reader

Abstract

This invention relates to the field of computer technology and discloses a memory allocation method and electronic device. By establishing a shared memory space based on the shared memory information of the target device, acquiring and parsing memory request requests from any service, and determining its memory requirement type and service requirement value, the method allocates memory resources to the service from high to low addresses in the shared memory space when the memory requirement type is contiguous memory. When the memory requirement is fragmented memory, memory resources are allocated from low to high addresses to prevent contiguous memory allocation failure due to severe fragmentation. By establishing a shared memory space and allocating memory resources to services based on their memory requirement type and value, services can still obtain memory resources under high load and reduce memory resource waste under low load, thereby improving memory resource utilization and enhancing the flexibility of memory resource allocation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer technology, and more specifically to a memory allocation method and an electronic device. Background Technology

[0002] As the demands on servers and other equipment continue to increase, it is necessary to allocate corresponding memory resources to ensure the continuity of critical business operations, thereby guaranteeing the stability and continuity of business operations. However, due to the limited memory resources of the equipment, and with the continuous increase in the types and number of business operations, how to allocate memory has become a key research topic.

[0003] In related technologies, memory allocation typically employs static planning, pre-dividing the memory space into several fixed-size regions before system startup or application loading. Each region is used exclusively by a corresponding task. However, as the workload changes during task execution, its memory requirements will also change. Therefore, under high workloads, insufficient memory allocation may occur, while under low workloads, memory resources will be wasted, reducing the flexibility of memory allocation. Summary of the Invention

[0004] This application provides a memory allocation method and an electronic device to at least solve the problem of low flexibility in memory resource allocation caused by static memory planning in related technologies.

[0005] This application provides a memory allocation method, including:

[0006] Obtain the shared memory space information of the target device; wherein, the shared memory space information includes at least the peak memory requirements of each service in the target device;

[0007] Establish a shared memory space based on the shared memory space information;

[0008] Obtain the memory allocation request for any service;

[0009] Parse memory request requests to determine the type and value of memory requirements for the business; among them, memory requirement types are divided into two types: fragmented memory and contiguous memory.

[0010] When the memory requirement type is contiguous memory, memory resources are allocated to the business according to the business requirement value, from the high address to the low address direction of the shared memory space;

[0011] When the memory requirement type is fragmented memory, memory resources are allocated to the business according to the business requirement value, from the low address to the high address direction of the shared memory space.

[0012] This application also provides a memory allocation device, including:

[0013] The first acquisition module is used to acquire shared memory space information of the target device; wherein, the shared memory space information includes at least the peak memory demand of each service in the target device;

[0014] A module is created to establish a shared memory space based on the shared memory space information.

[0015] The second acquisition module is used to acquire memory allocation requests for any business.

[0016] The parsing module is used to parse memory request requests to determine the type and value of the business's memory requirement; among them, the memory requirement type is divided into two types: fragmented memory and contiguous memory.

[0017] The first allocation module is used to allocate memory resources to services from high address to low address in the shared memory space according to the business requirement value when the memory requirement type is contiguous memory.

[0018] The second allocation module is used to allocate memory resources to services from low to high addresses in the shared memory space when the memory requirement type is fragmented memory, according to the business requirement value.

[0019] This application also provides an electronic device, including: a memory for storing a computer program; and a processor for implementing the steps of any of the above memory allocation methods when executing the computer program.

[0020] This application also provides a computer-readable storage medium storing a computer program, wherein the computer program, when executed by a processor, implements the steps of any of the above-described memory allocation methods.

[0021] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the steps of any of the above-described memory allocation methods.

[0022] This application establishes a shared memory space based on the shared memory information of the target device, acquires and parses memory request requests from any service, determines its memory requirement type and service requirement value, and allocates memory resources from high address to low address in the shared memory space when the memory requirement type is contiguous memory, and from low address to high address when the memory requirement is fragmented memory, thus preventing contiguous memory allocation failure due to severe fragmentation. By establishing a shared memory space and allocating memory resources to services based on their memory requirement type and value, services can still obtain memory resources under high load and reduce memory resource waste under low load, thereby improving memory resource utilization and enhancing the flexibility of memory resource allocation. Attached Figure Description

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

[0024] Figure 1 This is a schematic diagram of the memory allocation system on which the embodiments of this application are based;

[0025] Figure 2 A flowchart illustrating the memory allocation method provided in an embodiment of this application;

[0026] Figure 3 A schematic diagram illustrating an exemplary memory allocation structure provided for embodiments of this application;

[0027] Figure 4 A schematic diagram of an exemplary 16G shared memory space provided for embodiments of this application;

[0028] Figure 5 A schematic diagram of an exemplary 32G shared memory space provided for embodiments of this application;

[0029] Figure 6 This is a schematic diagram of the structure of the memory allocation device provided in the embodiments of this application;

[0030] Figure 7 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation

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

[0032] It should be noted that, in the description of this application, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. The terms "first," "second," etc., in this application are used to distinguish similar objects and are not used to describe a specific order or sequence.

[0033] For memory resource allocation, to ensure that critical business operations are not interrupted due to resource allocation failures, memory allocation is often required to be 100% successful, guaranteeing stability and real-time performance. That is, provided the total amount does not exceed the limit, no legitimate request should fail to allocate memory. Traditional solutions typically use a statically planned resource pool approach, where allocations from the pool are also released into the same pool. If a large amount of memory is needed, a linked list is used to assemble multiple smaller memory blocks.

[0034] In related technologies, memory allocation typically employs static planning, pre-dividing the memory space into several fixed-size regions before system startup or application loading. Each region is used exclusively by a corresponding task. However, as the workload changes during task execution, its memory requirements will also change. Therefore, under high workloads, insufficient memory allocation may occur, while under low workloads, memory resources will be wasted, reducing the flexibility of memory allocation.

[0035] To address the aforementioned technical problems, this application provides a memory allocation method and an electronic device. The method includes: acquiring shared memory space information of a target device; wherein the shared memory space information includes at least the peak memory demand of each service in the target device; establishing a shared memory space based on the shared memory space information; acquiring a memory request from any service; parsing the memory request to determine the service's memory demand type and service demand value; wherein the memory demand type is divided into two types: fragmented memory and contiguous memory; when the memory demand type is contiguous memory, allocating memory resources to the service from high address to low address in the shared memory space according to the service demand value; and when the memory demand type is fragmented memory, allocating memory resources to the service from low address to high address in the shared memory space according to the service demand value. The method described above establishes a shared memory space based on the shared memory information of the target device, acquires and parses memory request requests from any service, determines its memory requirement type and service requirement value, and allocates memory resources from high address to low address in the shared memory space when the memory requirement type is contiguous memory, and from low address to high address when the memory requirement is fragmented memory, thus preventing contiguous memory allocation failure due to severe fragmentation. By establishing a shared memory space and allocating memory resources to services based on their memory requirement type and value, services can still obtain memory resources under high load and reduce memory resource waste under low load, thereby improving memory resource utilization and enhancing the flexibility of memory resource allocation.

[0036] To enable those skilled in the art to better understand the present application, the present application will be further described in detail below with reference to the accompanying drawings and specific embodiments.

[0037] The specific application environment architecture or specific hardware architecture on which the execution of the memory allocation method depends is described here.

[0038] First, the structure of the memory allocation system on which this application is based will be described:

[0039] The memory allocation method, apparatus, electronic device, and storage medium provided in this application are applicable to allocating memory resources according to business needs, such as... Figure 1 The diagram shows the structure of the memory allocation system based on the embodiments of this application, mainly including memory resources to be allocated, a data acquisition device, and a memory allocation device. The data acquisition device is used to acquire shared memory information of the target device and memory request requests from services. The memory allocation device is used to allocate memory resources to services according to the memory request requests from services based on the memory allocation method provided in the embodiments of this application.

[0040] This application provides a memory allocation method for allocating memory resources to services based on their memory request. The execution subject of this application is an electronic device, such as a server, desktop computer, laptop computer, tablet computer, or other electronic devices that can be used for memory allocation.

[0041] like Figure 2 The diagram shown is a flowchart illustrating a memory allocation method provided in an embodiment of this application. The method includes:

[0042] Step 201: Obtain the shared memory space information of the target device.

[0043] Among them, the shared memory space information includes at least the peak memory requirements of each service in the target device.

[0044] Specifically, the shared memory space information is the sum of the peak memory requirements of each service, which includes both scattered and continuous peak memory requirements of each service.

[0045] Step 202: Establish a shared memory space based on the shared memory space information.

[0046] Specifically, the size of the shared memory space is determined and created during system initialization based on the sum of the peak memory requirements of each service. Each service then obtains memory resources from the shared memory space according to its memory needs.

[0047] Accordingly, by using the peak memory demand of each service, the maximum memory space requirement of each service under the highest load of the target device was determined, ensuring that each service can obtain the corresponding memory resources during the memory allocation process.

[0048] Step 203: Obtain the memory request for any service.

[0049] Step 204: parse the memory request to determine the memory requirement type and value of the business; among which, the memory requirement type is divided into two types: fragmented memory and contiguous memory.

[0050] Specifically, the memory request includes the memory requirement type and the required value for the service, where the required value is the amount of memory requested by the service. If the required value equals the memory unit value, the memory requirement type is determined to be fragmented memory; if the required value is greater than the memory unit value, the memory requirement type is determined to be contiguous memory. The memory unit value is the minimum allocation value during memory allocation, typically 4KB.

[0051] Accordingly, by parsing memory request requests, the type of memory requirement and the value of business requirements are determined, providing a reference for subsequent allocation of memory resources.

[0052] Step 205: When the memory requirement type is contiguous memory, allocate memory resources to the business according to the business requirement value, from the high address to the low address direction of the shared memory space.

[0053] Specifically, memory allocation follows the principle of being an integer multiple of the smallest memory unit value. If the business requirement value is not an integer multiple of the memory unit value, the system will round it up to an integer multiple of the smallest unit value and allocate the memory resources to the business. For example, with a memory unit value of 4K and a business requirement value of 6K, the system will allocate 8K memory resources to the business.

[0054] Step 206: When the memory requirement type is fragmented memory, allocate memory resources to the business according to the business requirement value, from the low address to the high address direction of the shared memory space.

[0055] Specifically, if the business requirement value is no greater than the memory unit value, and the memory requirement is determined to be fragmented memory, then memory resources of the corresponding size are allocated to the business from the low address to the high address direction of the shared memory space.

[0056] Accordingly, when memory requirements are contiguous, memory resources are allocated starting from the high address of the shared memory space; when memory requirements are fragmented, memory resources are allocated starting from the low address of the shared memory space, with the shared memory space in between. This separates the business requirements for fragmented memory from those for contiguous memory, avoiding memory fragmentation. The shared memory space ensures that any business requirement for contiguous memory can be met, and as a common memory resource, it facilitates easy memory resource configuration.

[0057] Based on the above embodiments, as one implementable method, in one embodiment, obtaining the shared memory space information of the target device includes:

[0058] Step 2011: Obtain the historical memory request records of each service in the target device;

[0059] Step 2012: For any given service, determine the memory usage of the service under the highest load condition based on the service's historical memory request records.

[0060] Step 2013: Take the memory usage of the service under the highest load as the peak memory requirement of the service.

[0061] Step 2014: Determine the shared memory space information of the target device based on the peak memory demand of each service.

[0062] Specifically, the peak memory demand is the amount of memory used under the highest system load. It is not a long-term occupation. At any given moment, the shared memory space information is greater than the size of each allocated memory resource and the total amount of memory resources to be allocated.

[0063] Accordingly, since the size of the shared memory space is no less than the sum of the peak memory requirements of each service, it ensures that any service request can obtain the corresponding memory resources.

[0064] Based on the above embodiments, as an implementable approach, in one embodiment, the method further includes:

[0065] Step 301: When any service has memory resources to be released, the memory resources to be released are treated as free memory blocks;

[0066] Step 302: If the free memory block is contiguous memory, assign a memory tag to the free memory block according to its size;

[0067] Step 303: Release the free memory blocks to the corresponding contiguous memory pool according to the memory tags.

[0068] Specifically, when the free memory block is contiguous memory, the corresponding memory tag is determined according to the size of the free memory block. Since the free memory blocks are all integer multiples of the memory unit value, the memory tags are also integer multiples of the memory unit value. For example, when the size of the free memory block is 16K, the corresponding memory tag is 16K.

[0069] Specifically, for any free memory block belonging to contiguous memory, it is determined whether a corresponding contiguous memory pool exists. If no corresponding contiguous memory pool exists, a contiguous memory pool corresponding to that memory tag is created, and the free memory block is stored in that contiguous memory pool. Specifically, for any contiguous memory pool corresponding to a memory tag, free memory blocks of the size represented by that memory tag are stored. For example, when the memory tag is 16K, a contiguous memory pool with the memory tag of 16K is created, and the free memory blocks stored in this contiguous memory pool are all 16K in size. If a corresponding contiguous memory pool exists, the free memory blocks are stored in the corresponding contiguous memory pool according to the memory tag. Multiple contiguous memory pools can be determined based on the memory tag.

[0070] Accordingly, the released memory resources are treated as free memory blocks and stored in the corresponding contiguous memory pool according to memory tags. This allows services to directly obtain memory resources of the corresponding size from the corresponding contiguous memory pool when requesting them, thereby improving memory allocation efficiency and avoiding memory fragmentation.

[0071] Specifically, in one embodiment, when the free memory block belongs to the fragmented memory pool, the free memory block is released to the fragmented memory pool.

[0072] Specifically, for any free memory block belonging to scattered memory, it is determined whether a scattered memory pool exists. If no scattered memory pool exists, a scattered memory pool is created, and the free memory block is stored in the scattered memory pool. The scattered memory pool stores free memory blocks belonging to scattered memory.

[0073] Correspondingly, by using fragmented memory pools, services can directly obtain memory resources from the corresponding fragmented memory pools, thus avoiding the generation of memory fragmentation.

[0074] Based on the above embodiments, as one implementable approach, in one embodiment, releasing free memory blocks to the corresponding contiguous memory pool includes:

[0075] Step 3031: Locate the insertion position in the memory linked list of the contiguous memory pool according to the starting address of the free memory block;

[0076] Step 3032: Insert the free memory block into the memory linked list of the contiguous memory pool.

[0077] Specifically, the contiguous memory pool has a pre-defined memory linked list, and free memory blocks in the contiguous memory pool are connected through the linked list. For any newly released free memory block, the insertion position in the memory linked list is determined according to its corresponding starting address, and the free memory block is inserted into the memory linked list.

[0078] Correspondingly, by linking free memory blocks through a linked list, suitable free memory blocks can be quickly identified during memory allocation, and free memory blocks can be stored in a contiguous memory pool more flexibly and orderly during memory reclamation.

[0079] Based on the above embodiments, as an implementable approach, in one embodiment, when the memory requirement type is contiguous memory, memory resources are allocated to the service according to the service requirement value, from the high address to the low address direction of the shared memory space, including:

[0080] Step 2051: If the memory requirement type is contiguous memory, determine the target memory label based on the business requirement value;

[0081] Step 2052: Determine whether there is a free memory block in the target contiguous memory pool corresponding to the target memory tag;

[0082] Step 2053: If there are no free memory blocks in the target contiguous memory pool, allocate memory resources to the service from the high address to the low address direction of the shared memory space.

[0083] Specifically, when the business requirement value is 16K, the corresponding target memory tag is 16K, and the corresponding target contiguous memory pool also contains 16K free memory blocks. When the business requirement type is contiguous memory, the corresponding target memory tag is determined based on the size of the business requirement value. It is then checked whether there are free memory blocks in the target contiguous memory pool corresponding to the target memory tag. If there are no free memory blocks in the target contiguous memory pool, memory resources of the corresponding size are obtained from the shared memory space, in descending order of address, and allocated to the business.

[0084] For example, when the business involves an I / O path, it is necessary to allocate large, contiguous blocks of memory. Based on the contiguous nature of the memory, it can be directly accessed by the DMA engine.

[0085] For example, under a specific hardware configuration, such as a total memory of 16GB, the maximum peak memory required for the function of using contiguous memory is determined. For example, in a 16GB total memory environment, it can be determined that there are 65536 contiguous 16K memory units (1GB), 4096 contiguous 512K memory units (2GB), and 1GB of scattered memory, etc.

[0086] Accordingly, the size requests for multiple contiguous memory blocks are independent of each other, and contiguous memory blocks of any size can be requested and released at will.

[0087] Specifically, in one embodiment, if a free memory block exists in the target contiguous memory pool, a free memory block is obtained from the target contiguous memory pool and allocated to the service. Specifically, for any memory request, it is first determined whether a free memory block exists in the target contiguous memory pool; if so, the free memory block is allocated to the service.

[0088] Accordingly, by prioritizing the acquisition of free memory blocks from the target contiguous memory pool, the direct acquisition of memory resources from the shared memory space is avoided, thus preventing memory fragmentation. At the same time, since the free memory blocks in the target contiguous memory pool meet the business requirements in the memory request, the flexibility and efficiency of memory allocation are improved.

[0089] Based on the above embodiments, as an implementable approach, in one embodiment, when the memory requirement type is fragmented memory, memory resources are allocated to services according to the service requirement value, from the low address to the high address direction of the shared memory space, including:

[0090] Step 2061: If the memory requirement type is fragmented memory, determine whether there are any free memory blocks in the fragmented memory pool.

[0091] Step 2062: If there are no free memory blocks in the fragmented memory pool, allocate memory resources to the service from the low address to the high address direction of the shared memory space.

[0092] Specifically, in one embodiment, if there are free memory blocks in the fragmented memory pool, free memory blocks are obtained from the fragmented memory pool and allocated to services.

[0093] Specifically, for any memory request, if the memory requirement type corresponding to the request is fragmented memory, the first step is to determine whether there are free memory blocks in the fragmented memory pool. If there are, the free memory blocks in the fragmented memory pool are allocated to the service. If there are no free memory blocks, memory resources are obtained from the shared memory space in the direction from low address to high address and allocated to the corresponding service.

[0094] For example, if the business involves hash tables, metadata, etc., the memory requirement is fragmented. The corresponding memory resources can be obtained from the fragmented memory pool and allocated to the business.

[0095] Accordingly, by allocating memory resources from the fragmented memory pool for memory request types of memory demand, memory fragmentation caused by fragmented memory in the shared memory space is avoided, and memory resources are obtained directly from the fragmented memory pool, thus improving memory allocation efficiency.

[0096] For example, such as Figure 3The diagram illustrates an exemplary memory allocation structure provided in this application embodiment. Based on the sum of peak memory requirements for each service, shared memory information is determined, and a shared memory space of 8GB is created. Memory request requests from services are obtained. If the memory requirement type is fragmented memory, memory resources are allocated to the service starting from the low address of the shared memory space. After the service releases memory resources, these resources are stored as free memory blocks in the fragmented memory pool. If a new service memory request indicates a fragmented memory requirement type, free memory blocks are obtained from the fragmented memory pool and allocated to the service. As shown in the diagram, the current size of the fragmented memory pool is 3GB, of which the total currently used memory resources are 1GB, and the total memory resources corresponding to the free memory blocks in the fragmented memory pool are 2GB. When the memory requirement type is contiguous memory, memory resources are allocated to the service starting from the high address of the shared memory space. For example, if the initial memory request value is 512K, 512K of memory resources are obtained from the high address. After the service completes, the 512K free memory block is released, a contiguous memory pool with the memory tag 512K is created, and stored in this contiguous memory pool. When a subsequent memory request is 512K, it is first checked whether there is a free memory block in the 512K contiguous memory pool. If there is, the free memory block is allocated to the service; otherwise, memory resources are allocated to the service from the high address of the shared memory space. When a subsequent memory request is initially 16K, memory resources are allocated to the service starting from the unallocated high address of the shared memory space. After the service completes, the 16K free memory block is released, a contiguous memory pool with the memory tag 16K is created, and stored in this contiguous memory pool. When a subsequent memory request is 16K, it is first checked whether there is a free memory block in the 16K contiguous memory pool. If a 512K memory request occurs after a 16K memory request, and there are no free memory blocks in the current 512K contiguous memory pool, then memory resources will continue to be allocated to the application from the unallocated high address in the collinear memory space. After long-term operation, there will be requests and releases for each type and size. Within each pool, there will be free unallocated memory. For example, 16K contiguous memory may occupy two "memory segments" in the entire memory region. This is related to the request timing of the application modules. Even if there are free blocks in each memory pool, they are not released to free shared memory, but are occupied and reserved for later allocation. At the same time, the peak value of 16K is 1G, which means that the highest historical request was 1G, which was then released. Currently, 512M is used, while the theoretical maximum request for 16K by the application module is 2G. If a request of more than 1G is made, it will be further divided from the total memory pool because it has been calculated in advance to ensure that memory can be allocated. In addition to the scattered memory pools and multiple contiguous memory pools, the remaining shared memory space is 2G.

[0097] For example, such as Figure 4The diagram shown is an exemplary structural diagram of a 16G shared memory space provided in an embodiment of this application. The system has three services, A, B, and C, all of which require both fragmented and contiguous memory. However, each module has its own upper limit. For example, service A has a maximum fragmented memory of 2G and a maximum contiguous memory of 1.5G. Although it frequently requests and releases memory during operation, it will not exceed its own peak value. Under this premise, services A, B, and C can use a maximum of 9G of fragmented memory and 4.5G of contiguous memory, ensuring that the sum of fragmented and contiguous memory is less than the total allocatable memory, which means that allocation will definitely be successful.

[0098] For example, such as Figure 5 The diagram shown illustrates an exemplary 32GB shared memory space structure provided in this application embodiment. The system contains three services: A, B, and C, all requiring both fragmented and contiguous memory. However, each module has its own upper limit; for example, service A has a maximum fragmented memory size of 4GB and a maximum contiguous memory size of 3GB. The total size of the fragmented memory and the total size of the contiguous memory pool are automatically adjusted as needed and allocation is always successful.

[0099] Specifically, when business requirements change, the system can dynamically "adapt" without needing to manually preset each memory boundary in advance; the system automatically adjusts according to the load. During initialization, it is guaranteed that the sum of the memory sizes in each module is less than the total pool capacity, eliminating the need for runtime verification and preventing failures. It avoids complex operations such as block merging, defragmentation, and size conversion, achieving the desired effect with minimal overhead. It can more flexibly identify scattered or contiguous memory, facilitating debugging or DMA usage.

[0100] Specifically, in one embodiment, when the shared memory space is entirely allocated to fragmented memory pools and contiguous memory pools, and the memory usage of each contiguous memory pool is monitored, if the actual utilization rate of free memory blocks in the contiguous memory pools is lower than a preset utilization rate threshold over a long period, some free memory blocks are released to the shared memory space for use by other urgently needed service requests. This avoids a situation where some contiguous memory pools have remaining free memory blocks while others have no remaining free memory blocks.

[0101] The memory allocation method provided in this application includes: obtaining shared memory space information of a target device; wherein the shared memory space information includes at least the peak memory demand of each service in the target device; establishing a shared memory space based on the shared memory space information; obtaining a memory request for any service; parsing the memory request to determine the memory demand type and service demand value of the service; wherein the memory demand type is divided into two types: fragmented memory and contiguous memory; when the memory demand type is contiguous memory, allocating memory resources for the service from high address to low address in the shared memory space according to the service demand value; when the memory demand type is fragmented memory, allocating memory resources for the service from low address to high address in the shared memory space according to the service demand value. The method described above establishes a shared memory space based on the shared memory information of the target device, acquires and parses memory request requests from any service, determines its memory requirement type and service requirement value, and allocates memory resources from high address to low address in the shared memory space when the memory requirement type is contiguous memory, and from low address to high address when the memory requirement is fragmented memory, thus preventing contiguous memory allocation failure due to severe fragmentation. By establishing a shared memory space and allocating memory resources to services based on their memory requirement type and value, services can still obtain memory resources under high load and reduce memory resource waste under low load, thereby improving memory resource utilization and enhancing the flexibility of memory resource allocation.

[0102] Furthermore, by analyzing the peak memory requirements of each service, the maximum memory space requirement of each service under peak load was determined, ensuring that each service could obtain corresponding memory resources during memory allocation. Parsing memory request requests identified the memory requirement type and service requirement value, providing a reference for subsequent memory resource allocation. When memory requirements are contiguous, memory resources are allocated starting from the high address of the shared memory space; when memory requirements are fragmented, they are allocated starting from the low address of the shared memory space, with the shared memory space in between. This separates fragmented memory requirements from contiguous memory requirements, avoiding memory fragmentation. The shared memory space ensures that any contiguous memory requirement can be met, and as a common memory resource, it facilitates easy configuration. Since the size of the shared memory space is no less than the sum of the peak memory requirements of all services, it ensures that any service request can obtain the corresponding memory resources. Released memory resources are stored as free memory blocks according to memory tags in the corresponding contiguous memory pool, allowing services to directly obtain memory resources of the corresponding size from the corresponding contiguous memory pool when requesting them, improving memory allocation efficiency while avoiding memory fragmentation. By using a fragmented memory pool, applications can directly obtain memory resources from the corresponding fragmented memory pool, avoiding memory fragmentation. Linking free memory blocks using a linked list allows for rapid identification of suitable free memory blocks during memory allocation, and more flexible and orderly storage of free memory blocks into the contiguous memory pool during memory reclamation. Multiple contiguous memory requests are independent of each other, and contiguous memory of any size can be freely requested and released. By prioritizing the acquisition of free memory blocks from the target contiguous memory pool, direct acquisition of memory resources from the shared memory space is avoided, thus preventing memory fragmentation. Furthermore, since the free memory blocks in the target contiguous memory pool meet the application requirements in the memory request, the flexibility and efficiency of memory allocation are improved. By allocating memory resources from the fragmented memory pool for memory requests with fragmented memory requirements, memory fragmentation caused by fragmented memory in the shared memory space is avoided, and direct acquisition of memory resources from the fragmented memory pool improves memory allocation efficiency.

[0103] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods according to the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method.

[0104] Embodiments of this application also provide a memory allocation apparatus for executing the memory allocation method provided in the above embodiments.

[0105] like Figure 6The diagram shown is a structural schematic of a memory allocation device provided in an embodiment of this application. The memory allocation device 60 includes: a first acquisition module 601, an establishment module 602, a second acquisition module 603, a parsing module 604, a first allocation module 605, and a second allocation module 606.

[0106] The system comprises the following modules: a first acquisition module for acquiring shared memory space information of the target device, including at least the peak memory requirements of each service on the target device; an establishment module for establishing a shared memory space based on the shared memory space information; a second acquisition module for acquiring memory request requests from any service; a parsing module for parsing memory request requests to determine the memory requirement type and value of the service, where the memory requirement type is divided into two types: fragmented memory and contiguous memory; a first allocation module for allocating memory resources to the service from high to low addresses in the shared memory space according to the service requirement value when the memory requirement type is contiguous memory; and a second allocation module for allocating memory resources to the service from low to high addresses in the shared memory space when the memory requirement type is fragmented memory.

[0107] For a description of the features in the embodiment corresponding to the memory allocation device, please refer to the relevant description in the embodiment corresponding to the memory allocation method, which will not be repeated here.

[0108] Embodiments of this application also provide an electronic device, such as... Figure 7 The diagram shown is a schematic diagram of the structure of an electronic device provided in an embodiment of this application, including a processor 10 and a memory 20. The memory 20 stores a computer program, and the processor 10 is configured to run the computer program to perform the steps in any of the above-described memory allocation method embodiments.

[0109] Embodiments of this application also provide a computer-readable storage medium storing a computer program, wherein the computer program is configured to execute the steps in any of the above-described memory allocation method embodiments at runtime.

[0110] In one exemplary embodiment, the aforementioned computer-readable storage medium may include, but is not limited to, various media capable of storing computer programs, such as a USB flash drive, read-only memory (ROM), random access memory (RAM), portable hard disk, magnetic disk, or optical disk.

[0111] Embodiments of this application also provide a computer program product, which includes a computer program that, when executed by a processor, implements the steps in any of the above-described memory allocation method embodiments.

[0112] Embodiments of this application also provide another computer program product, including a non-volatile computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps in any of the above-described memory allocation method embodiments.

[0113] Those skilled in the art will further recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.

[0114] The foregoing has provided a detailed description of a memory allocation method and electronic device provided in this application. Specific examples have been used to illustrate the principles and implementation methods of this application. The descriptions of the embodiments above are only intended to help understand the method and core ideas of this application. It should be noted that those skilled in the art can make various improvements and modifications to this application without departing from its principles, and these improvements and modifications also fall within the protection scope of the claims of this application.

Claims

1. A memory allocation method, characterized in that, The method includes: Obtain shared memory space information of the target device; wherein, the shared memory space information includes at least the peak memory requirements of each service in the target device; A shared memory space is established based on the shared memory space information; Obtain the memory allocation request for any of the aforementioned services; The memory request is parsed to determine the memory requirement type and service requirement value of the service; wherein, the memory requirement type is divided into two types: fragmented memory and contiguous memory; When the memory requirement type is contiguous memory, memory resources are allocated to the service from high address to low address in the shared memory space according to the service requirement value. When the memory requirement type is fragmented memory, memory resources are allocated to the service from the low address to the high address direction of the shared memory space according to the service requirement value; When any of the aforementioned services has memory resources to be released, the memory resources to be released are treated as free memory blocks; If the free memory block is contiguous memory, a memory tag is assigned to the free memory block according to its size; According to the memory tag, the free memory block is released to the corresponding contiguous memory pool; If the free memory block belongs to the fragmented memory pool, the free memory block will be released to the fragmented memory pool. When the memory requirement type is contiguous memory, allocating memory resources to the service from high address to low address in the shared memory space according to the service requirement value includes: When the memory requirement type is contiguous memory, the target memory label is determined based on the business requirement value; Determine whether there are any free memory blocks in the target contiguous memory pool corresponding to the target memory tag; If there are no free memory blocks in the target contiguous memory pool, memory resources are allocated to the service from the high address to the low address direction of the shared memory space; When the memory requirement type is fragmented memory, the method of allocating memory resources to the service from the low address to the high address direction of the shared memory space according to the service requirement value includes: If the memory requirement type is fragmented memory, determine whether there are any free memory blocks in the fragmented memory pool; If there are no free memory blocks in the fragmented memory pool, memory resources are allocated to the service from low address to high address in the shared memory space.

2. The memory allocation method according to claim 1, characterized in that, The method further includes: If there are free memory blocks in the target contiguous memory pool, obtain free memory blocks from the target contiguous memory pool and allocate the free memory blocks to the service.

3. The memory allocation method according to claim 1, characterized in that, The method further includes: If there are free memory blocks in the scattered memory pool, obtain free memory blocks from the scattered memory pool and allocate the free memory blocks to the service.

4. The memory allocation method according to claim 1, characterized in that, The step of releasing the free memory block to the corresponding contiguous memory pool includes: Based on the starting address of the free memory block, locate the insertion position in the memory linked list of the contiguous memory pool; Insert the free memory block into the memory linked list of the contiguous memory pool; The continuous memory pool is pre-configured with the memory linked list.

5. The memory allocation method according to claim 1, characterized in that, The step of obtaining the shared memory space information of the target device includes: Obtain the historical memory request records of each service in the target device; For any of the aforementioned services, the memory usage of the service under the highest load state is determined based on the service's historical memory request records; The memory usage of the service under the highest load condition is taken as the peak memory requirement of the service. Based on the peak memory requirements of each of the aforementioned services, the shared memory space information of the target device is determined.

6. An electronic device, characterized in that, include: Memory, used to store computer programs; A processor for implementing the steps of the memory allocation method as described in any one of claims 1 to 5 when executing the computer program.

Citation Information

Patent Citations

  • A mobile terminal-based cache sharing method and system

    CN106021127A

  • Shared-memory-based data read-write method and system, and readable storage medium

    WO2020207319A1