Memory allocation method, device, electronic device and storage medium
Through a large page-aware memory allocator and dynamic threshold adjustment, combined with incompletely allocated memory pages and operating system free memory pages, the inefficiency of traditional memory allocators is solved, and a fast response to different types of memory requests is achieved, memory fragmentation is reduced, and the overall performance of memory management is improved.
Patent Information
- Application Number
- CN202510866089.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-26
- Publication Date
- 2025-09-05
- Estimated Expiration
- 2045-06-26
AI Technical Summary
Traditional user-mode memory allocators are inefficient when processing large-scale memory requests, especially in environments with severe memory fragmentation. Sequentially searching for free blocks of appropriate sizes takes a long time, affecting system performance.
It adopts a large page-aware memory allocator, which uses dynamic threshold adjustment and efficient memory release strategy, combined with incompletely allocated memory pages, preset memory page sets and operating system free memory pages, to flexibly respond to different types of memory requests, reduce memory fragmentation, and improve memory utilization and search efficiency.
It improves the speed and accuracy of memory allocation, solves the time-consuming and inefficient problem of finding suitable free blocks in traditional memory allocators, and improves the overall performance of memory management.
Smart Images

Figure CN120371724B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a memory allocation method, device, electronic device, and storage medium. Background Art
[0002] In current computer systems, memory management is critical to ensuring efficient operation. However, traditional user-mode memory allocators exhibit significant issues when handling large-scale memory requests. In particular, these allocators exhibit inefficiencies in finding suitable free blocks to respond to memory requests. Due to their fixed allocation strategy, they are particularly inflexible when handling large memory blocks. Sequentially searching for free blocks of the appropriate size takes a long time, significantly reducing memory allocation efficiency, especially in environments with severe memory fragmentation. As the scale and complexity of applications continue to rise, the demand for memory is exploding, and this inefficient memory allocation method is increasingly becoming a significant factor limiting system performance.
[0003] Therefore, how to improve memory allocation efficiency is a technical problem that those skilled in the art need to solve. Summary of the Invention
[0004] The present invention provides a memory allocation method, device, electronic device and storage medium, which improve memory allocation efficiency.
[0005] The present invention provides a memory allocation method, comprising: when receiving a memory request from a requester, determining the memory size requested by the memory request; if the memory size is less than or equal to a first threshold, responding to the memory request based on incompletely allocated memory pages; wherein the size of the memory page is greater than or equal to a preset value, and the first threshold is less than the size of the memory page; if the memory size is greater than the first threshold and less than or equal to a second threshold, responding to the memory request based on free memory pages in a preset memory page set; wherein the second threshold is greater than the size of the memory page; if the memory size is greater than the second threshold, responding to the memory request based on cached unreleased free memory pages and / or free memory pages in an operating system.
[0006] The present invention also provides a memory allocation device, comprising: a determination module, used to determine the memory size requested by the memory application when receiving a memory application from a requester; a first response module, used to respond to the memory application based on incompletely allocated memory pages when the memory size is less than or equal to a first threshold; wherein the size of the memory page is greater than or equal to a preset value, and the first threshold is less than the size of the memory page; a second response module, used to respond to the memory application based on free memory pages in a preset memory page set when the memory size is greater than the first threshold and less than or equal to a second threshold; wherein the second threshold is greater than the size of the memory page; a third response module, used to respond to the memory application based on cached unreleased free memory pages and / or free memory pages in the operating system when the memory size is greater than the second threshold.
[0007] The present invention also provides an electronic device, comprising: a memory for storing a computer program; and a processor for implementing the steps of any one of the above memory allocation methods when executing the computer program.
[0008] The present invention also provides a computer-readable storage medium, in which a computer program is stored. When the computer program is executed by a processor, the steps of any one of the above-mentioned memory allocation methods are implemented.
[0009] The present invention also provides a computer program product, comprising a computer program, which implements the steps of any of the above memory allocation methods when executed by a processor.
[0010] The beneficial effects of the present invention are as follows: the memory allocation method provided by the present invention realizes flexible response to different types of memory requests. For smaller memory requests, responses are made based on incompletely allocated large page memory, which reduces the generation of memory fragmentation, improves the efficiency of memory block search, and improves internal memory utilization. For memory requests between the first and second thresholds, the demand is met by the free memory pages in the preset memory page set. This method helps to quickly respond to medium-sized memory demands while maintaining the stability of the system. When faced with large-scale memory requests greater than the second threshold, it relies on the free memory pages provided by the cache or the operating system to respond, ensuring that continuous large memory requests can be responded to in a timely manner. In this way, not only the problem of time-consuming and inefficient search for suitable free blocks in traditional memory allocators is solved, but also the speed and accuracy of memory allocation are greatly improved. The present invention also discloses a memory allocation device and an electronic device, a computer-readable storage medium and a computer program product, which can also achieve the above-mentioned technical effects.
[0011] It should be understood that the foregoing general description and the following detailed description are exemplary only and are not restrictive of the invention. BRIEF DESCRIPTION OF THE DRAWINGS
[0012] In order to more clearly illustrate the embodiments of the present invention, the following is a brief introduction to the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
[0013] Figure 1 The figure is a flowchart of a memory allocation method according to an exemplary embodiment.
[0014] Figure 2 The figure is a flowchart of a method for adjusting a first threshold according to an exemplary embodiment.
[0015] Figure 3 This is a schematic diagram of the memory allocation process in the application embodiment provided by the present invention.
[0016] Figure 4 This is a schematic diagram of the dynamic threshold adjustment process in the application embodiment provided by the present invention.
[0017] Figure 5 This is a schematic diagram of the memory release process in the application embodiment provided by the present invention.
[0018] Figure 6 This is a schematic diagram of the memory return process based on a sliding window in an application embodiment provided by the present invention.
[0019] Figure 7 The figure is a structural diagram of a memory allocation device according to an exemplary embodiment.
[0020] Figure 8 The figure is a structural diagram of an electronic device according to an exemplary embodiment. DETAILED DESCRIPTION
[0021] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of them. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making any creative efforts shall fall within the scope of protection of the present invention.
[0022] It should be noted that, in the description of the present invention, the terms "comprises," "includes," or any other variations thereof are intended to encompass non-exclusive inclusion, such that a process, method, article, or apparatus comprising a series of elements includes not only those elements but also other elements not explicitly listed, or elements inherent to such process, method, article, or apparatus. The terms "first," "second," etc., in the present invention are used to distinguish similar objects, and are not used to describe a particular order or precedence.
[0023] In the related art, memory allocators have numerous drawbacks in memory utilization, with excessive memory fragmentation being a major problem. As memory allocation and release operations continue, a large number of small, scattered free memory blocks appear. These fragments cannot be effectively utilized, resulting in wasted memory space and reduced overall memory utilization. Furthermore, memory access delays caused by TLB misses (Translation Lookaside Buffer misses) also seriously impact system performance. Due to the large number of page table entries in traditional memory management models, TLB misses frequently occur during memory accesses, forcing the processor to spend extra time traversing the page table to obtain physical addresses, increasing memory access latency. Therefore, the present invention reduces memory fragmentation by constructing a large-page-aware memory allocator and utilizing large-page memory to allocate space. Furthermore, the reduced number of page table entries due to large-page memory reduces the probability of TLB misses, effectively improving memory utilization efficiency and addressing the memory access delays caused by excessive memory fragmentation and TLB misses.
[0024] In addition, in the related art, most memory allocators adopt a fixed allocation strategy, which lacks adaptability when facing memory allocation requests of different sizes. It cannot make targeted responses to memory requirements under different application scenarios. Moreover, many existing memory allocators generally adopt static threshold settings when processing large page memory. Once the threshold for using large page memory is set, it cannot be flexibly adjusted according to the actual memory usage during system operation. For example, in a scenario where the memory demand is small in the initial stage of application operation and the demand increases greatly in the later stage, the static threshold may cause the large page memory to be enabled too early or too late, affecting the overall performance of the system. Therefore, the present invention is based on an efficient memory allocation strategy with dynamic threshold adjustment, which can execute different strategies according to allocation requests of different memory sizes, and adjust the threshold in real time by dynamically monitoring the memory allocation mode, so that the system can adaptively select the optimal memory allocation path, effectively solving the problem of lack of flexibility of the memory allocation strategy to meet diverse memory requirements.
[0025] Furthermore, in the related art, large page memory release faces a dilemma. If idle large pages are released too early, it is necessary to re-apply for memory through system calls when subsequently allocating them again, and trigger a page fault interrupt, which undoubtedly increases the delay and affects the system response speed. In particular, for applications that are sensitive to delays, it may cause their performance to drop significantly. Releasing idle large pages too late will cause a waste of memory resources. In a system with tight memory, this waste may cause a sharp drop in system performance. When processing large page memory recovery, the current memory allocator is unable to promptly and effectively reincorporate the released large page memory into the allocable resource pool, or does not fully consider the continuity and locality of large page memory when reallocating, resulting in a low large page memory utilization rate. Therefore, the present invention proposes an efficient memory release strategy, which effectively solves the problem of unreasonable large page memory release, accurately grasps the timing of large page memory release, avoids the delay problem caused by premature release, and prevents the memory waste caused by late release, thereby improving the efficiency of large page memory use.
[0026] In order to enable those skilled in the art to better understand the solutions of the present invention, the present invention is further described in detail below with reference to the accompanying drawings and specific implementation methods.
[0027] The embodiment of the present invention provides a memory allocation method, and the method is described in detail in conjunction with the execution process of the memory allocation method. Figure 1 , a flowchart of a memory allocation method according to an exemplary embodiment is shown.
[0028] S101: When a memory application from a requester is received, a memory size requested by the memory application is determined.
[0029] Memory request refers to an application's request to the memory allocator for a certain amount of memory space. Memory size refers to the specific capacity of the requested memory, typically measured in bytes.
[0030] In this step, when the memory allocator receives a memory request from a requester (such as an application), it first needs to determine the requested memory size. This step is accomplished by reading the parameters in the memory request. For example, when the application calls a memory allocation function, it passes in a parameter representing the requested memory size. The memory allocator parses this parameter to obtain the requested memory size.
[0031] S102: If the memory size is less than or equal to a first threshold, respond to the memory request based on an incompletely allocated memory page; wherein the size of the memory page is greater than or equal to a preset value, and the first threshold is less than the size of the memory page.
[0032] The first threshold is a preset memory size threshold used to distinguish between small memory requests and larger memory requests. An incompletely allocated memory page refers to a memory page that has been allocated to certain requesters but is not yet fully occupied. The size of a memory page refers to the basic unit size of a memory page in the system. In this embodiment, the memory page is an inner large page, i.e., its capacity is greater than or equal to a preset value, such as 2MB or 1GB.
[0033] In a specific implementation, if the requested memory size is less than or equal to the first threshold, it means that this is a small memory request. At this time, the first memory allocator is used to allocate a memory block for the requester in the memory page managed by the first memory allocator; wherein, the first memory allocator is used to manage incompletely allocated memory pages, and specifically, the first memory allocator is used to record the remaining space of the incompletely allocated memory pages and the allocated memory block information. Specifically, the first memory allocator will search the existing incompletely allocated memory pages to see if there is enough space to satisfy this request. If so, a memory block of the required size is allocated from the memory page; if not, it may be necessary to obtain a new memory page from the cache or the operating system, and add it to the incompletely allocated memory page before allocating it.
[0034] For example, assume the memory page size is 2MB and the first threshold is set to 1MB. When a request for 512KB of memory is received, the first memory allocator checks existing partially allocated memory pages. If an partially allocated memory page has 1MB of free space, it allocates 512KB of memory from that page to satisfy the request.
[0035] This approach allows full utilization of existing partially allocated memory pages, reduces memory fragmentation, and improves memory utilization. It also avoids frequent requests for new memory pages from the operating system, reducing system call overhead and improving memory allocation efficiency.
[0036] As a feasible implementation method, it also includes: when the memory size is less than or equal to a first threshold and the memory page managed by the first memory allocator cannot meet the memory request, determining whether there is a free memory page in the cache managed by the page host; if so, selecting a first target memory page from the free memory pages in the cache, allocating a memory block for the requester in the first target memory page, and handing over the first target memory page to the first memory allocator for management; if not, selecting a second target memory page from the free memory pages in the operating system, allocating a memory block for the requester in the second target memory page, and handing over the second target memory page to the first memory allocator for management.
[0037] In a specific implementation, when the size of a memory request is less than or equal to a first threshold, and there is insufficient space in the memory pages managed by the first memory allocator to satisfy the request, the system will further check whether there are free memory pages in the cache managed by the page host. If there are free memory pages in the cache, the system will select a suitable memory page (called the first target memory page), allocate the required memory block from it to the requester, and hand over the memory page to the first memory allocator for management so that subsequent memory allocation operations can continue to use the memory page. If there are no free memory pages in the cache, a new free memory page (called the second target memory page) is obtained from the operating system, a memory block is allocated from it to the requester, and the memory page is handed over to the first memory allocator for management. This strategy reduces frequent requests to the operating system by making full use of the free memory pages in the cache, while improving the flexibility and efficiency of memory allocation. In particular, when processing small memory allocation requests, it can effectively reduce memory fragmentation and improve the performance of overall memory management.
[0038] S103: If the memory size is greater than the first threshold and less than or equal to a second threshold, respond to the memory request based on the free memory pages in a preset memory page set; wherein the second threshold is greater than the size of the memory page.
[0039] The second threshold is a preset memory size threshold used to distinguish between medium memory requests and large memory requests. The preset memory page set is a pre-allocated memory page set used to respond to medium-sized memory allocation requests.
[0040] In a specific implementation, if the requested memory size is greater than the first threshold and less than or equal to the second threshold, it means that this is a medium memory request. At this time, the second memory allocator is used to allocate memory blocks in one or more memory pages to the requester in the memory pages managed by the second memory allocator; wherein, the second memory allocator is used to manage the free memory pages in the preset memory page set. Specifically, the second memory allocator will search the preset memory page set to see if there are enough free memory pages to meet this request. If so, a memory block of the required size is allocated from these free memory pages; if not, it may be necessary to obtain new memory pages from the cache or operating system, add them to the preset memory page set, and then allocate them.
[0041] For example, assume the memory page size is 2MB, the first threshold is 1MB, and the second threshold is 16MB. When a request for 8MB of memory is received, the memory allocator checks the pre-defined memory page set. If there are sufficient free memory pages in the set (for example, four free 2MB pages), 8MB of memory is allocated from it to satisfy the request.
[0042] As can be seen, managing medium-sized memory requests through a dedicated set of pre-defined memory pages can improve the efficiency and flexibility of memory allocation. At the same time, this approach can better utilize the continuity of memory pages, reduce memory fragmentation, and further improve memory utilization.
[0043] As a feasible implementation method, after using the second memory allocator to allocate memory blocks in one or more memory pages to the requester in the memory pages managed by the second memory allocator, it also includes: if there are unallocated memory blocks in the memory pages allocated to the requester, the memory pages with unallocated memory blocks will be handed over to the first memory allocator for management.
[0044] In a specific implementation, after the second memory allocator (e.g., a large-granularity allocator) allocates a memory block to the requester, if there are still unallocated memory blocks in the memory pages allocated to the requester, these memory pages with remaining free space are handed over to the first memory allocator (e.g., a small-granularity allocator) for management. This mechanism fully utilizes the remaining free space in memory pages, avoiding memory waste, and enables the small-granularity allocator to better manage this fragmented memory space, thereby improving memory utilization and allocation efficiency.
[0045] As a feasible implementation method, it also includes: when the memory size is greater than the first threshold and less than or equal to the second threshold, if the memory page managed by the second memory allocator cannot meet the memory request, then determine whether there is a free memory page that meets the memory request in the cache managed by the page host; if so, select one or more third target memory pages from the free memory pages in the cache, and allocate memory blocks to the requester in one or more of the third target memory pages; if not, select one or more fourth target memory pages from the free memory pages in the operating system, and allocate memory blocks to the requester in one or more of the fourth target memory pages.
[0046] In a specific implementation, when the size of a memory request is greater than a first threshold and less than or equal to a second threshold, if the memory pages managed by the second memory allocator cannot satisfy the memory request, the system will further check whether there are free memory pages in the cache managed by the page host that meet the request. If there are suitable free memory pages in the cache, one or more third target memory pages are selected from them for allocation; if not, one or more fourth target memory pages are selected from the operating system for allocation. This mechanism reduces frequent calls to the operating system by prioritizing the use of free memory pages in the page host cache, while being able to quickly respond to medium-sized memory requests, thereby improving the efficiency and flexibility of memory allocation.
[0047] As a feasible implementation method, after allocating memory blocks to the requester in one or more of the third target memory pages, it also includes: if there are unallocated memory blocks in the third target memory pages allocated to the requester, the third target memory pages with unallocated memory blocks are handed over to the first memory allocator for management; correspondingly, after allocating memory blocks to the requester in one or more of the fourth target memory pages, it also includes: if there are unallocated memory blocks in the fourth target memory pages allocated to the requester, the fourth target memory pages with unallocated memory blocks are handed over to the first memory allocator for management.
[0048] In a specific implementation, after allocating memory blocks from the third or fourth target memory pages to the requester, if there are still unallocated memory blocks in these target memory pages, these memory pages with remaining space are handed over to the first memory allocator for management. This mechanism ensures that the remaining space in the memory pages can be fully utilized, avoiding the problem of memory fragmentation. By handing over these memory pages to the small-granularity allocator for management, subsequent small memory requests can be better met, further optimizing the collaborative work of the memory allocators and improving the performance and efficiency of overall memory management.
[0049] S104: If the memory size is greater than the second threshold, respond to the memory request based on unreleased free memory pages in the cache and / or free memory pages in the operating system.
[0050] Among them, cached unreleased free memory pages refer to memory pages that were previously allocated and then released. These memory pages are cached for subsequent reuse. Free memory pages in the operating system refer to memory pages that the operating system currently does not allocate to any process.
[0051] In a specific implementation, if the requested memory size is greater than the second threshold, it means that this is a large memory request. At this time, the page host is used to allocate memory blocks from multiple memory pages to the requester from the unreleased free memory pages in the cache, and / or, the resource allocator is used to allocate memory blocks from multiple memory pages to the requester from the free memory pages in the operating system. Specifically, the page host will first check whether there are enough free memory pages in the cache to meet this request. If so, the required memory pages are allocated directly from the cache; if there are not enough free memory pages, new memory pages are requested from the operating system through the resource allocator, and added to the cache before allocation.
[0052] For example, assume the memory page size is 2MB and the second threshold is 16MB. When a request for 32MB of memory is received, the memory allocator checks the cache. If there are 16 free 2MB pages in the cache, these pages can be allocated directly from the cache to satisfy the request. If there are only 8 free 2MB pages in the cache, a system call is required to request 8 more 2MB pages from the operating system, which are then allocated to the requester.
[0053] This approach allows full utilization of free memory pages in the cache, reducing frequent calls to the operating system and thus lowering system call overhead. At the same time, for large memory requests, contiguous memory pages can be quickly allocated, improving memory allocation efficiency and performance.
[0054] As a feasible implementation method, a page host is used to allocate memory blocks from multiple memory pages to the requester from the unreleased free memory pages in the cache, and / or a resource allocator is used to allocate memory blocks from multiple memory pages to the requester from the free memory pages in the operating system, including: determining whether the number of free memory pages in the cache managed by the page host meets the memory request; if so, selecting multiple fifth target memory pages from the free memory pages in the cache, and allocating memory blocks to the requester from the multiple fifth target memory pages; if not, selecting multiple sixth target memory pages from the free memory pages in the operating system, and allocating memory blocks to the requester from the multiple sixth target memory pages.
[0055] In a specific implementation, when allocating memory blocks from multiple memory pages to the requester, the page host is first used to check whether there are enough free memory pages in the cache it manages to meet the memory request. If the number of free memory pages in the cache is sufficient, multiple fifth target memory pages are selected from these cached free memory pages, and the required memory blocks are allocated to the requester in these memory pages. If the number of free memory pages in the cache is insufficient, multiple sixth target memory pages are selected from the operating system through the resource allocator, and memory blocks are allocated to the requester in these memory pages. This mechanism reduces frequent calls to the operating system by giving priority to the use of free memory pages in the page host cache, while being able to quickly respond to large memory requests, thereby improving the efficiency and flexibility of memory allocation.
[0056] As a feasible implementation method, after allocating memory blocks to the requester from multiple fifth target memory pages, it also includes: if there are unallocated memory blocks in the fifth target memory pages allocated to the requester, the fifth target memory page with the unallocated memory blocks will be handed over to the first memory allocator for management; correspondingly, after allocating memory blocks to the requester from multiple sixth target memory pages, it also includes: if there are unallocated memory blocks in the sixth target memory pages allocated to the requester, the sixth target memory page with the unallocated memory blocks will be handed over to the first memory allocator for management.
[0057] In a specific implementation, after allocating memory blocks for the requester in multiple fifth or sixth target memory pages, if there are still unallocated memory blocks in these target memory pages, these memory pages with remaining space are handed over to the first memory allocator (e.g., a small-granularity allocator) for management. This mechanism ensures that the remaining space in the memory pages can be fully utilized, avoiding the problem of memory fragmentation. By handing over these memory pages to the first memory allocator for management, subsequent small memory requests can be better met, further optimizing the collaborative work of the memory allocators and improving the performance and efficiency of overall memory management.
[0058] The memory allocation method provided by the embodiment of the present invention realizes flexible response to different types of memory requests. For smaller memory requests, responses are made based on incompletely allocated large page memory, which reduces the generation of memory fragmentation, improves the efficiency of searching memory blocks, and improves internal memory utilization. For memory requests between the first and second thresholds, the demand is met by the free memory pages in the preset memory page set. This method helps to quickly respond to medium-sized memory demands while maintaining the stability of the system. When faced with large-scale memory requests greater than the second threshold, the free memory pages provided by the cache or the operating system are relied upon to respond, ensuring that continuous large memory requests can be responded to in a timely manner. In this way, not only the problem of time-consuming and inefficient search for suitable free blocks in traditional memory allocators is solved, but also the speed and accuracy of memory allocation are greatly improved.
[0059] Based on the above embodiment, as a preferred implementation, the system further includes: continuously collecting application memory usage data within a preset time window, including the frequency, size, and duration of memory allocation requests, as well as memory release patterns. Furthermore, the system records the application's memory demand characteristics at different runtime stages, such as memory usage during the initialization phase, peak periods, and trough periods. Based on the collected memory usage data, an intelligent prediction model is constructed. This model can employ machine learning algorithms, such as time series analysis, neural networks, or other advanced prediction algorithms, to analyze historical patterns of memory demand and predict future memory demand trends. The prediction model inputs include historical memory allocation and release data, the application's runtime status (such as the current runtime phase), and system load. Based on the output of the intelligent prediction model, memory is pre-allocated for the application. If the prediction model determines that the application's memory demand will increase significantly in the near future, additional memory pages are pre-allocated from the operating system based on the current memory allocation and added to the page manager's cache. The number of pre-allocated memory pages can be dynamically adjusted based on the predicted demand growth to ensure that the application's needs are met without excessively consuming system resources. After pre-allocating memory, the application's actual memory usage is continuously monitored. If the actual memory demand is lower than the predicted value, the excess pre-allocated memory pages are promptly released to the operating system to avoid wasting resources. If the actual demand is higher than the predicted value, more memory pages are requested from the operating system to meet the application's needs.
[0060] It can be seen that by introducing a dynamic memory pre-allocation mechanism based on intelligent prediction, this embodiment can significantly reduce the allocation delay of applications during peak memory demand periods. Traditional memory allocation methods usually allocate memory only when the application requests it, which may cause delays under high load conditions. However, this embodiment predicts and pre-allocates memory in advance, allowing applications to quickly obtain memory resources when needed, thereby improving the system's response speed and overall performance. In addition, the mechanism for dynamically adjusting the amount of pre-allocated memory can effectively avoid resource waste and ensure efficient use of system resources. This memory management strategy that combines intelligent prediction with dynamic adjustment is particularly suitable for application scenarios that are sensitive to latency, such as real-time data processing, high-frequency trading systems, and high-performance computing.
[0061] This embodiment introduces a method for adjusting the first threshold. For details, see Figure 2 , Figure 2 The figure is a flowchart of a method for adjusting a first threshold according to an exemplary embodiment.
[0062] S201: Setting the first threshold to a preset ratio of the size of the memory page.
[0063] In a specific implementation, during the system initialization or configuration phase, the first threshold is set based on the memory page size. Specifically, the memory page size is multiplied by a preset ratio to obtain an initial value for the first threshold. For example, if the memory page size is 2MB and the preset ratio is 0.5, the initial value of the first threshold is 1MB.
[0064] S202: Counting the target proportion of the number of directory-type memory applications to the total number of memory applications within a preset time window; wherein the directory-type memory application is a memory application whose requested memory size is less than or equal to the first threshold.
[0065] The preset time window is a fixed time range used to collect data related to memory requests. Directory-type memory requests refer to requests whose requested memory size is less than or equal to the first threshold, typically corresponding to small memory requests. The target ratio refers to the ratio of directory-type memory requests to the total number of all memory requests and is used to assess the frequency of small memory requests.
[0066] In this step, the system counts all memory requests and directory-type (small memory) requests within a preset time window. The target ratio is then calculated as the ratio of directory-type requests to the total number of requests. For example, if the system receives 100 memory requests within a 10-second window, and 80 of them are directory-type requests (i.e., the requested memory size is less than or equal to the first threshold), the target ratio is 80%.
[0067] By counting target ratios, the system can understand the frequency and proportion of small memory requests in real time. This data provides an important basis for dynamically adjusting the first threshold, allowing the memory allocation strategy to be optimized based on actual operating conditions, thereby better adapting to different application scenarios and load changes.
[0068] S203: If the target ratio is greater than a first ratio threshold, increase the first threshold; if the target ratio is less than a second ratio threshold, decrease the first threshold; wherein the second ratio threshold is less than the first ratio threshold.
[0069] The first ratio threshold is a preset upper ratio value used to determine whether the first threshold needs to be increased. The second ratio threshold is a preset lower ratio value used to determine whether the first threshold needs to be lowered. The second ratio threshold is smaller than the first ratio threshold, forming a ratio range.
[0070] In a specific implementation, the first threshold is dynamically adjusted based on a comparison between the target ratio and a preset first ratio threshold and a second ratio threshold. If the target ratio is greater than the first ratio threshold, it indicates that the proportion of small memory applications is too high. In this case, the first threshold is raised so that more memory applications can be processed according to the small memory allocation strategy, thereby reducing memory fragmentation. If the target ratio is less than the second ratio threshold, it indicates that the proportion of small memory applications is too low. In this case, the first threshold is lowered so that more memory applications can be processed according to the larger memory allocation strategy, thereby improving memory allocation efficiency.
[0071] For example, assume the first ratio threshold is 80% and the second ratio threshold is 20%. If the target ratio is 85%, which is greater than the first ratio threshold of 80%, the system increases the first threshold from 1MB to 1.5MB. If the target ratio is 15%, which is less than the second ratio threshold of 20%, the system decreases the first threshold from 1MB to 0.5MB.
[0072] This demonstrates that by dynamically adjusting the first threshold, the system can flexibly optimize memory allocation strategies based on actual operating conditions. When small memory requests are frequent, raising the first threshold can reduce memory fragmentation and improve memory utilization. When small memory requests are rare, lowering the first threshold can avoid overpacking and improve memory allocation efficiency. This dynamic adjustment mechanism significantly enhances the flexibility and performance of memory management.
[0073] This embodiment introduces a memory release method.
[0074] S301: When a first memory block smaller than or equal to the first threshold is released, a first memory page to which the first memory block belongs is handed over to the first memory allocator for management; when the first memory allocator detects that the first memory page is completely idle, the first memory page is handed over to the page host for management.
[0075] In a specific implementation, when a memory block (a first memory block) is freed and its size is less than or equal to a first threshold, the memory page (the first memory page) to which the memory block belongs is handed over to the first memory allocator for management. The first memory allocator then checks whether the memory page is completely free (i.e., all memory blocks in the memory page have been freed). If so, the memory page is handed over to the page manager for management, allowing subsequent memory allocations to reuse these free memory pages.
[0076] For example, assume the first threshold is 1MB and a 512KB memory block is freed. This memory block belongs to a 2MB memory page. After freeing, the 2MB memory page is handed over to the first memory allocator for management. If the first memory allocator detects that other memory blocks in the memory page have also been freed, making the entire memory page completely free, the memory page is handed over to the page manager for management.
[0077] By assigning the management of memory pages containing small memory blocks to the first memory allocator, the remaining space on these memory pages can be better utilized, reducing memory fragmentation. At the same time, assigning completely idle memory pages to the page manager can improve memory page reuse efficiency, reduce frequent requests to the operating system, and improve overall memory management performance.
[0078] S302: When a second memory block greater than the first threshold and less than or equal to the second threshold is released, the second memory page to which the second memory block belongs is handed over to the second memory allocator for management; when the second memory allocator detects that the second memory page is completely idle, the second memory page is handed over to the page host for management.
[0079] In a specific implementation, when a memory block (second memory block) is freed and its size is greater than the first threshold and less than or equal to the second threshold, the memory block may span multiple 2MB memory pages. At this point, these memory pages (second memory pages) are handed over to the second memory allocator for management. The second memory allocator checks whether these memory pages are completely free (that is, all memory blocks within these memory pages have been freed). If a memory page is completely free, it is handed over to the page manager for management so that subsequent memory allocations can reuse these free memory pages.
[0080] For example, assume the first threshold is 1MB and the second threshold is 16MB. An 8MB block of memory is freed. Since memory pages are fixed at 2MB, this 8MB block of memory may span four 2MB pages. After being freed, these four pages are handed over to the second memory allocator for management. If the second memory allocator detects that other memory blocks within a particular page have also been freed, making the entire page completely free, the page is handed over to the page manager for management.
[0081] By assigning the management of multiple memory pages belonging to medium-sized memory blocks to the secondary memory allocator, the remaining space on these memory pages can be better utilized, reducing memory fragmentation. At the same time, assigning completely free memory pages to the page manager can improve memory page reuse efficiency, reduce frequent requests to the operating system, and improve overall memory management performance.
[0082] S303: When a third memory block larger than the second threshold is released, the third memory page to which the third memory block belongs is handed over to the page host for management.
[0083] In specific implementations, when a memory block (the third memory block) is freed and its size exceeds the second threshold, it may span multiple 2MB memory pages. In this case, these memory pages (the third memory pages) are directly managed by the page manager. The page manager checks whether these memory pages are completely free (that is, all memory blocks within these pages have been freed). If a memory page is completely free, it is cached by the page manager so that subsequent memory allocations can reuse these free memory pages.
[0084] For example, assume the second threshold is 16MB, and a 32MB memory block is freed. Since memory pages are fixed at 2MB, this 32MB block may span 16 2MB pages. After freeing, these 16 pages are directly handed over to the page manager for management. The page manager checks whether these pages are completely free. If a page is completely free, it caches it so that it can be reused for subsequent memory allocations.
[0085] By assigning the multiple memory pages belonging to large memory blocks directly to the page manager, these pages can be quickly released, improving memory page reuse efficiency. This approach reduces frequent requests to the operating system and improves overall memory management performance, particularly when processing large memory requests, significantly increasing system responsiveness. Furthermore, the page manager can better manage these free memory pages, ensuring efficient subsequent memory allocations.
[0086] Based on the above embodiment, as a preferred implementation method, it also includes: counting the minimum required number of memory pages and the maximum required number of memory pages for memory applications within a preset time window, and calculating the difference in the number of memory pages between the maximum required number of memory pages and the minimum required number of memory pages; when the number of free memory pages managed by the page host is greater than the difference in the number of memory pages, releasing the free memory pages in the cache managed by the page host to the operating system.
[0087] In a specific implementation, the number of free memory pages managed by the page host is dynamically adjusted by counting the minimum and maximum demand memory pages for memory requests within a preset time window and calculating the difference between the two. Specifically, the system determines the rationality of the free memory pages in the page host based on the fluctuation range of memory demand (i.e., the difference between the maximum and minimum demand). If the number of free memory pages managed by the page host exceeds this difference, it means that too many free memory pages in the current cache are idle and not fully utilized. At this point, the system will release the excess free memory pages to the operating system so that this part of the memory resources can be reallocated to other processes or applications in need, thereby improving the overall memory utilization of the system. This method can effectively avoid the waste of memory resources, while reducing the memory usage problem caused by too many free memory pages, and further optimize the efficiency and performance of memory management.
[0088] The following describes an application embodiment provided by the present invention. The large page-aware memory allocator in this embodiment includes a small-granularity allocator, a large-granularity allocator, a page manager, and a resource allocator. These components work together to achieve efficient large page memory management.
[0089] The small-granularity allocator component is used to manage partially used large pages (i.e., large pages that are not fully allocated); the large-granularity allocator component is used to manage allocations across multiple large pages; the page host component's main function is to cache unused large pages to reduce system calls; the resource allocator component interacts directly with the operating system and is responsible for allocating and releasing large pages from the operating system. The system uses a large page size of 2MB, and the allocator is managed at a 4KB granularity. When a memory allocation request is received, the appropriate allocation path is selected based on the allocation request threshold (0~2MB). The specific allocation process is as follows. Figure 3 shown.
[0090] When a memory request is made, the requested memory size is first counted, and the threshold is then dynamically adjusted based on the requested memory size. Next, the allocation process compares the requested memory size with the threshold and selects the appropriate allocation path.
[0091] When the memory size of an allocation request is less than this threshold, it indicates that the memory size of the allocation request is smaller than the huge page. The small-grained allocator will try to bundle these requests into the same huge page to improve memory utilization. The small-grained allocator maintains a data structure internally that records the remaining space of each incompletely allocated huge page and information about allocated memory blocks, such as the starting address and size, to facilitate quick search for suitable huge pages for allocation. When the small-grained allocator's linked list does not have suitable memory to allocate, it will traverse the page host to see if there are free huge pages. If there are, a huge page will be allocated from it, and the remaining memory of the huge page will be handed over to the small-grained allocator's linked list for management. If there are no free huge pages in the page host, the resource allocator will obtain a huge page from the system using mmap (Memory Map), and the remaining portion will be handed over to the small-grained allocator's linked list for management.
[0092] When the memory size of the allocation request is greater than this threshold and less than 1GB, memory allocation is performed through the large-grained allocator, which internally manages large page collections and allocation area information through data such as tree structures to support fast search and allocation. When there is an allocation request that meets the management scope of this component, the large-grained allocator will search for a suitable large page or large page combination from the large page collection it manages for allocation. If the large-grained allocator cannot meet the demand, it will also query the page host first. If there is a large page in the cache, it will be allocated. Otherwise, the resource allocator will obtain the large page from the operating system using mmap.
[0093] When the memory size requested is greater than 1GB, multiple contiguous huge pages are obtained from the page manager or resource allocator. If the page manager (which manages the huge page cache) has sufficient contiguous huge pages, they are allocated from the cache; if not, the resource allocator obtains contiguous huge pages from the operating system for allocation. If the requested memory size is an integer multiple of a huge page, multiple huge pages are directly obtained through the page manager or resource allocator for allocation. Otherwise, huge pages are allocated based on the size of the allocation request, and the remaining portion is donated to the small-grained allocator component for management. For example, if a request is made to allocate 1.1GB (1126.4MB) of memory, 1128MB (564 huge pages) will be obtained from the system through the page manager or resource allocator, and the remaining 1.6MB of memory will be managed by the small-grained allocator to accommodate memory requests below the threshold.
[0094] It should be noted that the threshold parameters of the above allocation process are not fixed, but are optimized in real time through a dynamic threshold adjustment mechanism. The flowchart of dynamic threshold adjustment is as follows: Figure 4 As shown, the specific implementation is as follows:
[0095] First, a dedicated counter is set up in the memory allocation module. Whenever the system receives an allocation request, it determines whether the request is allocated through the small-granularity allocator, large-granularity allocator, or resource allocator. If the allocation is made through the small-granularity allocator, the small memory allocation request count is incremented by 1. A total request counter is also set, which increments by 1 for each request received (regardless of size). Next, a time window is defined within which the proportion of small, medium, and large memory allocation requests is calculated. When the proportion of small memory allocation requests exceeds 80%, the small memory allocation threshold is adjusted from 1 / 2 huge page size to 3 / 4 huge page size, allowing more small requests to be allocated in batches through the small-granularity allocator, thereby reducing memory fragmentation. When the proportion of small memory allocation requests falls below 20%, the small memory allocation threshold is adjusted to 1 / 4 huge page size. This dynamic threshold adjustment effectively improves memory allocation efficiency and reduces memory fragmentation.
[0096] For memory release, similar to memory allocation, different memory sizes also have different release strategies. The flowchart of memory release is as follows Figure 5 As shown in the figure, the process is as follows: First, when memory less than the threshold is released, it is returned to the small-grained allocator. The small-grained allocator checks whether the huge page is completely free. If so, it attempts to release it to the page manager. When medium-allocated memory is returned, it is returned to the large-grained allocator. The large-grained allocator checks whether the relevant huge page is completely free. If so, it returns it to the huge page cache area. When large-allocated memory is released, it is released directly to the page manager. After the free huge pages are returned to the page manager, the number of huge pages required by the current system and the number of huge pages owned by the page manager are counted, and the excess free huge pages are released to the operating system based on system requirements.
[0097] Furthermore, by returning free huge pages to the operating system, we can find a balance between reducing memory usage and reducing system call overhead, and predict future demand through short-term historical demand, thus avoiding the limitations of static strategies. Figure 6 As shown in the figure, the process is as follows: First, a sliding window statistical mechanism is used with a fixed time window. Within this time window, the system continuously records data related to memory demand. Specifically, a dedicated monitoring module monitors memory usage in real time, accurately recording every memory allocation and release operation. Then, based on this recorded data, the maximum (max) and minimum (min) memory demand values requested within this two-second period are calculated. This statistical method can quickly reflect short-term fluctuations in memory demand and provide a reliable basis for subsequent memory release decisions.
[0098] When the number of free huge pages in the page manager exceeds the maximum minus minimum range, the system returns the excess huge pages to the operating system (OS). If the number of free huge pages in the page manager exceeds this fluctuation range, it indicates that too many huge pages are idle in the cache and are not being fully utilized. The resource processor unmaps these huge pages and returns them to the OS, allowing these memory resources to be returned to the system for use by other processes or applications, thereby improving overall system memory utilization.
[0099] It can be seen that, in this embodiment, through the collaborative work between different components, the system can intelligently select the optimal allocation path according to memory requests of different sizes. The small-granularity allocator manages small memory requests in packages to improve the internal utilization of large pages; the large-granularity allocator optimizes the allocation efficiency of medium-sized memory; and the page host and resource allocator ensure the rapid allocation of large blocks of continuous memory, thereby improving the overall memory management efficiency. In addition, this embodiment dynamically adjusts the small memory allocation threshold by real-time statistics of memory request distribution, so that the system reduces fragmentation when there are high-frequency small requests, and avoids excessive packaging when there are more large requests, thereby flexibly adapting to the needs of different application scenarios. Furthermore, this embodiment adopts a demand prediction strategy based on a sliding window, and the system can dynamically adjust the release timing of idle large pages to achieve a balance between reducing memory usage and reducing system call overhead. Compared with the fixed-rate release strategy, this mechanism can more accurately predict future memory needs and avoid overhead or waste caused by premature or late release.
[0100] The following is an introduction to a memory allocation device provided by an embodiment of the present invention. The memory allocation device described below and the memory allocation method described above can be used as a reference. Figure 7 , a structural diagram of a memory allocation device according to an exemplary embodiment.
[0101] The determination module 100 is configured to determine the memory size requested by the memory application when receiving a memory application from a requester.
[0102] The first response module 200 is used to respond to the memory request based on the incompletely allocated memory page when the memory size is less than or equal to a first threshold; wherein the size of the memory page is greater than or equal to a preset value, and the first threshold is less than the size of the memory page.
[0103] The second response module 300 is used to respond to the memory request based on the free memory pages in the preset memory page set when the memory size is greater than the first threshold and less than or equal to a second threshold; wherein the second threshold is greater than the size of the memory page.
[0104] The third response module 400 is configured to respond to the memory request based on unreleased free memory pages in the cache and / or free memory pages in the operating system when the memory size is greater than the second threshold.
[0105] The memory allocation device provided by the embodiment of the present invention realizes flexible response to different types of memory requests. For smaller memory requests, it responds based on incompletely allocated large page memory, reduces the generation of memory fragmentation, improves the efficiency of searching memory blocks, and improves internal memory utilization. For memory requests between the first and second thresholds, the demand is met by the free memory pages in the preset memory page set. This method helps to quickly respond to medium-sized memory demands while maintaining the stability of the system. When faced with large-scale memory requests greater than the second threshold, it relies on the free memory pages provided by the cache or the operating system to respond, ensuring that continuous large memory requests can be responded to in a timely manner. In this way, not only the problem of time-consuming and inefficient search for suitable free blocks in traditional memory allocators is solved, but also the speed and accuracy of memory allocation are greatly improved.
[0106] Based on the above embodiments, as a preferred implementation mode, the first response module 200 is specifically used to: use the first memory allocator to allocate memory blocks for the requester in the memory pages managed by the first memory allocator; wherein, the first memory allocator is used to manage incompletely allocated memory pages; accordingly, the second response module 300 is specifically used to: use the second memory allocator to allocate memory blocks in one or more memory pages for the requester in the memory pages managed by the second memory allocator; wherein, the second memory allocator is used to manage free memory pages in a preset memory page set; accordingly, the third response module 400 is specifically used to: use the page host to allocate memory blocks in multiple memory pages for the requester from the cached unreleased free memory pages, and / or use the resource allocator to allocate memory blocks in multiple memory pages for the requester from the free memory pages in the operating system.
[0107] Based on the above embodiment, as a preferred implementation manner, the first memory allocator is used to record the remaining space of the incompletely allocated memory pages and the allocated memory block information.
[0108] On the basis of the above embodiment, as a preferred implementation manner, it also includes: a first allocation module, which is used to determine whether there are free memory pages in the cache managed by the page host when the memory size is less than or equal to a first threshold and the memory pages managed by the first memory allocator cannot meet the memory request; if so, select a first target memory page from the free memory pages in the cache, allocate a memory block for the requester in the first target memory page, and hand over the first target memory page to the first memory allocator for management; if not, select a second target memory page from the free memory pages in the operating system, allocate a memory block for the requester in the second target memory page, and hand over the second target memory page to the first memory allocator for management.
[0109] On the basis of the above embodiments, as a preferred implementation manner, it also includes: a first management module, which is used to allocate memory blocks in one or more memory pages to the requester in the memory pages managed by the second memory allocator using the second memory allocator. If there are unallocated memory blocks in the memory pages allocated to the requester, the memory pages with unallocated memory blocks will be handed over to the first memory allocator for management.
[0110] On the basis of the above embodiment, as a preferred implementation manner, it also includes: a second allocation module, which is used to determine whether there are free memory pages that meet the memory application in the cache managed by the page host when the memory size is greater than the first threshold and less than or equal to the second threshold, if the memory page managed by the second memory allocator cannot meet the memory application; if so, select one or more third target memory pages from the free memory pages in the cache, and allocate memory blocks to the requester in one or more of the third target memory pages; if not, select one or more fourth target memory pages from the free memory pages in the operating system, and allocate memory blocks to the requester in one or more of the fourth target memory pages.
[0111] On the basis of the above embodiments, as a preferred implementation manner, it also includes: a second management module, which is used to allocate memory blocks to the requester in one or more of the third target memory pages. If there are unallocated memory blocks in the third target memory pages allocated to the requester, the third target memory pages with unallocated memory blocks will be handed over to the first memory allocator for management; a third management module, which is used to allocate memory blocks to the requester in one or more of the fourth target memory pages. If there are unallocated memory blocks in the fourth target memory pages allocated to the requester, the fourth target memory pages with unallocated memory blocks will be handed over to the first memory allocator for management.
[0112] Based on the above embodiment, as a preferred implementation mode, the third response module 400 is specifically used to: determine whether the number of free memory pages in the cache managed by the page host meets the memory request; if so, select multiple fifth target memory pages from the free memory pages in the cache, and allocate memory blocks to the requester from the multiple fifth target memory pages; if not, select multiple sixth target memory pages from the free memory pages in the operating system, and allocate memory blocks to the requester from the multiple sixth target memory pages.
[0113] On the basis of the above embodiment, as a preferred implementation manner, it also includes: a fourth management module, which is used to allocate memory blocks to the requester among multiple fifth target memory pages, and if there are unallocated memory blocks in the fifth target memory pages allocated to the requester, the fifth target memory page with the unallocated memory blocks will be handed over to the first memory allocator for management; a fifth management module, which is used to allocate memory blocks to the requester among multiple sixth target memory pages, and if there are unallocated memory blocks in the sixth target memory pages allocated to the requester, the sixth target memory page with the unallocated memory blocks will be handed over to the first memory allocator for management.
[0114] On the basis of the above embodiment, as a preferred implementation manner, it also includes: an adjustment module, used to set the first threshold to a preset proportion of the size of the memory page; counting the target proportion of the number of directory-type memory applications to the total number of memory applications within a preset time window; wherein, the directory-type memory application is a memory application whose requested memory size is less than or equal to the first threshold; if the target proportion is greater than the first proportion threshold, then the first threshold is increased; if the target proportion is less than the second proportion threshold, then the first threshold is lowered; wherein, the second proportion threshold is less than the first proportion threshold.
[0115] On the basis of the above embodiment, as a preferred implementation manner, it also includes: a first memory release module, which is used to hand over the first memory page belonging to the first memory block to the first memory allocator for management when the first memory block less than or equal to the first threshold is released, and when the first memory allocator detects that the first memory page is completely idle, hand over the first memory page to the page trustee for management; a second memory release module, which is used to hand over the second memory page belonging to the second memory block to the second memory allocator for management when the second memory block greater than the first threshold and less than or equal to the second threshold is released, and when the second memory allocator detects that the second memory page is completely idle, hand over the second memory page to the page trustee for management; a third memory release module, which is used to hand over the third memory page belonging to the third memory block to the page trustee for management when the third memory block greater than the second threshold is released.
[0116] On the basis of the above embodiment, as a preferred implementation mode, it also includes: a fourth memory release module, which is used to count the minimum required number of memory pages and the maximum required number of memory pages of memory applications within a preset time window, and calculate the difference in the number of memory pages between the maximum required number of memory pages and the minimum required number of memory pages; when the number of free memory pages managed by the page host is greater than the difference in the number of memory pages, the free memory pages in the cache managed by the page host are released to the operating system.
[0117] Regarding the apparatus in the above embodiment, the specific manner in which each module performs operations has been described in detail in the embodiment of the method, and will not be elaborated here.
[0118] An embodiment of the present invention further provides an electronic device, Figure 8 FIG. 1 is a structural diagram of an electronic device according to an exemplary embodiment. Figure 8 As shown, the electronic device includes: a communication interface 1 capable of exchanging information with other devices, such as network devices; a processor 2 connected to the communication interface 1 to implement information exchange with other devices, and configured to execute the memory allocation method provided by one or more of the above technical solutions when running a computer program. The computer program is stored in a memory 3.
[0119] Of course, in actual application, the various components in the electronic device are coupled together through the bus system 4. It can be understood that the bus system 4 is used to realize the connection and communication between these components. In addition to the data bus, the bus system 4 also includes a power bus, a control bus, and a status signal bus. However, for the sake of clarity, Figure 8 Various buses are labeled as bus system 4.
[0120] The memory 3 in the embodiment of the present invention is used to store various types of data to support the operation of the electronic device. Examples of such data include: any computer program used to operate on the electronic device.
[0121] It is understood that the memory 3 can be a volatile memory or a non-volatile memory, or can include both volatile and non-volatile memories. Among them, the non-volatile memory can be a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), a magnetic random access memory (FRAM), a flash memory, a magnetic surface memory, an optical disc, or a compact disc read-only memory (CD-ROM); the magnetic surface memory can be a magnetic disk or a magnetic tape. The volatile memory can be a random access memory (RAM), which is used as an external cache. By way of example and not limitation, many forms of RAM are available, such as static random access memory (SRAM), synchronous static random access memory (SSRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDRSDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous link dynamic random access memory (SLDRAM), and direct rambus random access memory (DRRAM).The memory 3 described in the embodiments of the present invention is intended to include but is not limited to these and any other suitable types of memories.
[0122] The method disclosed in the above embodiment of the present invention can be applied to processor 2 or implemented by processor 2. Processor 2 may be an integrated circuit chip with signal processing capabilities. During implementation, each step of the above method can be completed by the hardware integrated logic circuit in processor 2 or by instructions in software form. The above processor 2 can be a general-purpose processor, a DSP, or other programmable logic device, discrete gate or transistor logic device, discrete hardware component, etc. Processor 2 can implement or execute the various methods, steps and logic block diagrams disclosed in the embodiment of the present invention. A general-purpose processor can be a microprocessor or any conventional processor, etc. The steps of the method disclosed in the embodiment of the present invention can be directly embodied as being executed by a hardware decoding processor, or can be executed by a combination of hardware and software modules in the decoding processor. The software module can be located in a storage medium, which is located in memory 3. Processor 2 reads the program in memory 3 and completes the steps of the above method in combination with its hardware.
[0123] When the processor 2 executes the program, the corresponding processes in the various methods of the embodiments of the present invention are implemented, which will not be described here for the sake of brevity.
[0124] An embodiment of the present invention further provides a computer-readable storage medium, in which a computer program is stored. The computer program is configured to execute the steps of any one of the above-mentioned memory allocation method embodiments when running.
[0125] In an exemplary embodiment, the computer-readable storage medium may include, but is not limited to, various media that can store computer programs, such as a USB flash drive, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk, or an optical disk.
[0126] An embodiment of the present invention further provides a computer program product, which includes a computer program. When the computer program is executed by the processor 2, the steps in any one of the above-mentioned memory allocation method embodiments are implemented.
[0127] An embodiment of the present invention further provides another computer program product, including a non-volatile computer-readable storage medium, wherein the non-volatile computer-readable storage medium stores a computer program, and when the computer program is executed by the processor 2, the steps of any of the above-mentioned memory allocation method embodiments are implemented.
[0128] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the above description has generally described the components and steps of each example according to their functions. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of the present invention.
[0129] The above is a detailed introduction to the memory allocation system, method, device and equipment, medium and product provided by the present invention. Specific examples are used herein to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method and core ideas of the present invention. It should be pointed out that for ordinary technicians in this technical field, without departing from the principles of the present invention, the present invention can also be improved and modified in a number of ways, and these improvements and modifications also fall within the scope of protection of the present invention.
Claims
1. A memory allocation method, characterized in that: include: When receiving a memory application from a requester, determining a memory size requested by the memory application; If the memory size is less than or equal to a first threshold, responding to the memory request based on an incompletely allocated memory page; wherein the size of the memory page is greater than or equal to a preset value, and the first threshold is less than the size of the memory page; If the memory size is greater than the first threshold and less than or equal to a second threshold, responding to the memory request based on a free memory page in a preset memory page set; wherein the second threshold is greater than the size of the memory page; If the memory size is greater than the second threshold, responding to the memory request based on unreleased free memory pages in the cache and / or free memory pages in the operating system; The step of responding to the memory request based on the unreleased free memory pages in the cache and / or the free memory pages in the operating system includes: Determine whether the number of free memory pages in the cache managed by the page host meets the memory request; if so, respond to the memory request based on the unreleased free memory pages in the cache; if not, respond to the memory request based on the free memory pages in the operating system.
2. The memory allocation method according to claim 1, wherein: Responding to the memory request based on the incompletely allocated memory page includes: Allocating a memory block for the requester in a memory page managed by the first memory allocator using a first memory allocator; wherein the first memory allocator is used to manage incompletely allocated memory pages; Accordingly, responding to the memory request based on the free memory pages in the preset memory page set includes: Allocating memory blocks in one or more memory pages for the requester in the memory pages managed by the second memory allocator using a second memory allocator; wherein the second memory allocator is used to manage free memory pages in a preset memory page set; Accordingly, responding to the memory request based on the unreleased free memory pages in the cache and / or the free memory pages in the operating system includes: Utilize a page host to allocate memory blocks in multiple memory pages to the requester from unreleased free memory pages in the cache, and / or utilize a resource allocator to allocate memory blocks in multiple memory pages to the requester from free memory pages in the operating system.
3. The memory allocation method according to claim 2, wherein: The first memory allocator is used to record the remaining space of the incompletely allocated memory pages and the allocated memory block information.
4. The memory allocation method according to claim 2, wherein: Also includes: When the memory size is less than or equal to a first threshold and the memory pages managed by the first memory allocator cannot satisfy the memory request, determining whether there are free memory pages in the cache managed by the page host; If so, selecting a first target memory page from the free memory pages in the cache, allocating a memory block for the requester in the first target memory page, and handing the first target memory page over to the first memory allocator for management; If not, a second target memory page is selected from the free memory pages in the operating system, a memory block is allocated to the requester in the second target memory page, and the second target memory page is handed over to the first memory allocator for management.
5. The memory allocation method according to claim 2, wherein: After allocating memory blocks in one or more memory pages to the requester in the memory pages managed by the second memory allocator using the second memory allocator, the method further includes: If there are unallocated memory blocks in the memory pages allocated to the requester, the memory pages with the unallocated memory blocks are handed over to the first memory allocator for management.
6. The memory allocation method according to claim 2, wherein: Also includes: When the memory size is greater than the first threshold and less than or equal to the second threshold, if the memory page managed by the second memory allocator cannot satisfy the memory request, determining whether there is a free memory page that satisfies the memory request in the cache managed by the page host; If so, selecting one or more third target memory pages from the free memory pages in the cache, and allocating a memory block to the requester in the one or more third target memory pages; If not, one or more fourth target memory pages are selected from the free memory pages in the operating system, and a memory block is allocated to the requester in the one or more fourth target memory pages.
7. The memory allocation method according to claim 6, wherein: After allocating a memory block for the requester in one or more third target memory pages, the method further includes: If there are unallocated memory blocks in the third target memory page allocated to the requester, the third target memory page with the unallocated memory blocks is handed over to the first memory allocator for management; Accordingly, after allocating a memory block for the requester in one or more of the fourth target memory pages, the method further includes: If there are unallocated memory blocks in the fourth target memory page allocated to the requester, the fourth target memory page with the unallocated memory blocks is handed over to the first memory allocator for management.
8. The memory allocation method according to claim 2, wherein: Allocating a memory block in a plurality of memory pages to the requester from unreleased free memory pages in the cache by using a page host, and / or allocating a memory block in a plurality of memory pages to the requester from free memory pages in an operating system by using a resource allocator, including: Determining whether the number of free memory pages in the cache managed by the page host satisfies the memory request; If so, selecting a plurality of fifth target memory pages from the free memory pages in the cache, and allocating memory blocks to the requester from the plurality of fifth target memory pages; If not, a plurality of sixth target memory pages are selected from the free memory pages in the operating system, and memory blocks are allocated to the requester in the plurality of sixth target memory pages.
9. The memory allocation method according to claim 8, wherein: After allocating a memory block for the requester in a plurality of the fifth target memory pages, the method further includes: If there is an unallocated memory block in the fifth target memory page allocated to the requester, the fifth target memory page with the unallocated memory block is handed over to the first memory allocator for management; Correspondingly, after allocating memory blocks for the requester in the plurality of sixth target memory pages, the method further includes: If there are unallocated memory blocks in the sixth target memory page allocated to the requester, the sixth target memory page with the unallocated memory blocks is handed over to the first memory allocator for management.
10. The memory allocation method according to claim 1, wherein: Also includes: Setting the first threshold to a preset ratio of the size of the memory page; Counting the target ratio of the number of directory-type memory applications to the total number of memory applications within a preset time window; wherein the directory-type memory application is a memory application whose requested memory size is less than or equal to the first threshold; If the target ratio is greater than a first ratio threshold, increasing the first threshold; If the target ratio is less than a second ratio threshold, the first threshold is lowered; wherein the second ratio threshold is less than the first ratio threshold.
11. The memory allocation method according to claim 2, wherein: Also includes: When a first memory block smaller than or equal to the first threshold is released, a first memory page to which the first memory block belongs is handed over to the first memory allocator for management; when the first memory allocator detects that the first memory page is completely free, the first memory page is handed over to the page trustee for management; When a second memory block whose value is greater than the first threshold and less than or equal to the second threshold is released, the second memory page to which the second memory block belongs is handed over to the second memory allocator for management; when the second memory allocator detects that the second memory page is completely free, the second memory page is handed over to the page trustee for management; When the third memory block whose size is larger than the second threshold is released, the third memory page to which the third memory block belongs is handed over to the page host for management.
12. The memory allocation method according to claim 2, wherein: Also includes: Counting the minimum required number of memory pages and the maximum required number of memory pages of memory applications within a preset time window, and calculating the difference in the number of memory pages between the maximum required number of memory pages and the minimum required number of memory pages; When the number of free memory pages managed by the page host is greater than the memory page number difference, the free memory pages in the cache managed by the page host are released to the operating system.
13. A memory allocation device, characterized in that: include: a determination module, configured to, upon receiving a memory application from a requester, determine a memory size requested by the memory application; a first response module, configured to respond to the memory request based on the incompletely allocated memory page when the memory size is less than or equal to a first threshold; wherein the size of the memory page is greater than or equal to a preset value, and the first threshold is less than the size of the memory page; a second response module, configured to respond to the memory request based on free memory pages in a preset memory page set when the memory size is greater than the first threshold and less than or equal to a second threshold; wherein the second threshold is greater than the size of the memory page; a third response module, configured to respond to the memory request based on unreleased free memory pages in the cache and / or free memory pages in the operating system when the memory size is greater than the second threshold; Among them, the third response module is specifically used to: when the memory size is greater than the second threshold, determine whether the number of free memory pages in the cache managed by the page host meets the memory request; if so, respond to the memory request based on the unreleased free memory pages in the cache; if not, respond to the memory request based on the free memory pages in the operating system.
14. An electronic device, characterized in that: include: memory for storing computer programs; A processor, configured to implement the steps of the memory allocation method according to any one of claims 1 to 12 when executing the computer program.
15. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, which, when executed, implements the steps of the memory allocation method according to any one of claims 1 to 12.
Citation Information
Patent Citations
Memory anti-fragmentation method and system and storage medium
CN111338981A
Memory management method and apparatus, electronic device, and storage medium
WO2025044869A1