Memory allocation method and apparatus
By dynamically adjusting the local cache capacity and system memory granularity, the problem of low memory allocation efficiency in high-concurrency scenarios with unbalanced loads is solved, thereby improving memory allocation efficiency.
Patent Information
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Filing Date
- 2025-03-24
- Publication Date
- 2026-04-02
AI Technical Summary
In high-concurrency scenarios with uneven loads, existing technologies that allocate memory using fixed-capacity local caches suffer from low memory allocation efficiency.
By adjusting the local cache capacity, the capacity threshold is dynamically set based on the number of times business threads request and release memory from the system. The system memory is also divided and numbered with granularity, and memory is allocated from the local cache first, thereby improving memory allocation efficiency.
It effectively solves the problem of low memory allocation efficiency in scenarios with unbalanced load, improves memory allocation efficiency, adapts to current load changes, and maximizes the benefits of caching.
Smart Images

Figure CN2025084456_02042026_PF_FP_ABST
Abstract
Description
Memory allocation method and device
[0001] Cross-reference to related applications
[0002] The present application claims priority from the Chinese patent application No. 202411384755.6 filed on September 30, 2024, and entitled "Memory allocation method and device", the content of which is incorporated herein by reference in its entirety. TECHNICAL FIELD
[0003] Embodiments of the present application relate to the field of computers, and in particular, to a memory allocation method and device. BACKGROUND
[0004] In the protocol stack process of a storage system, the service threads processing IO (Input / Output) have high requirements for latency. The number of these service threads will be many according to the current business scenario, and there will be several tens or hundreds of them. If multiple service threads simultaneously apply for memory from the system, there will be a lot of competition in the memory allocation process. Moreover, with the increase in the number of service threads, this competition will become greater and greater, which will inevitably lead to a long memory allocation time for some threads, affecting the latency of IO.
[0005] In related technologies, in order to increase the memory allocation efficiency in a high concurrency scenario, the general strategy is to reserve some local cache to reduce the competition in the allocation process. However, this simple cache reservation method is difficult to produce obvious effects in the face of a high concurrency business scenario with uneven load. SUMMARY
[0006] Embodiments of the present application provide a memory allocation method and device to at least solve the problem that in related technologies, service threads implement memory allocation through memory in a fixed-capacity local cache, resulting in a relatively low memory allocation efficiency in a business scenario with uneven load.
[0007] According to one embodiment of the present application, a memory allocation method is provided, comprising: upon receiving a memory allocation request of a target service thread, determining a memory request amount corresponding to the memory allocation request; if the memory request amount is greater than a first memory amount that can be allocated by a local cache corresponding to the target service thread, allocating a number corresponding to the first memory amount to the target service thread; applying a first number of memories to the local cache according to a first capacity threshold corresponding to the local cache, wherein the first capacity threshold is calculated based on a number of times that the target service thread applies for memories from a target system, a number of times that the target service thread releases memories to the target system, and a first preset capacity threshold; and allocating a second number of memories to the target service thread according to the number of the first number of memories in the local cache, so that the target service thread performs service processing based on the allocated memories, wherein the second number is determined based on a difference between the first memory amount and the memory request amount.
[0008] In one example embodiment, after determining the memory request amount corresponding to the memory allocation request, the method further comprises: determining whether the memory request amount is less than or equal to the first memory amount in the local cache; and if the memory request amount is less than or equal to the first memory amount in the local cache, allocating the memory amount corresponding to the memory allocation request to the target service thread according to the number corresponding to the memory in the local cache.
[0009] In one example embodiment, after applying the first number of memories to the local cache according to the first capacity threshold, the method further comprises: updating a first target number corresponding to the target service thread, wherein the first target number is a number of times that the target service thread applies for memories from the target system.
[0010] In one example embodiment, after allocating the second number of memories to the target service thread according to the number of the first number of memories in the local cache, so that the target service thread performs service processing based on the allocated memories, the method further comprises: upon receiving a memory release request of the target service thread, determining a memory release amount corresponding to the memory release request; and releasing the memory corresponding to the memory release amount according to the memory release amount and a second memory amount that can be allocated in the local cache.
[0011] In one example embodiment, releasing the memory corresponding to the memory release amount according to the memory release amount and the second memory amount that can be allocated in the local cache comprises: obtaining a second capacity threshold of the local cache, wherein the second capacity threshold is calculated based on a number of times that the target service thread applies for memories, a number of times that the target service thread releases memories, and the first capacity threshold; and releasing the memory corresponding to the memory release amount according to the second capacity threshold, the memory release amount, and the second memory amount.
[0012] In one example embodiment, the releasing the memory corresponding to the memory release amount according to the second capacity threshold, the memory release amount and the second memory amount comprises: calculating a difference between the second capacity threshold and the second memory amount to obtain a first difference; determining whether the memory release amount is less than or equal to the first difference; and if the memory release amount is less than or equal to the first difference, releasing the memory corresponding to the memory release amount into the local cache.
[0013] In one example embodiment, after determining whether the memory release amount is less than or equal to the first difference, the method further comprises: if the memory release amount is greater than the first difference, releasing the memory corresponding to the first difference of the memory release request into the local cache; releasing a third amount of memory in the local cache to the target system and releasing a fourth amount of memory corresponding to the memory release request to the local cache, wherein the third amount is calculated based on the second capacity threshold, and the fourth amount is calculated based on the first difference and the memory release amount.
[0014] In one example embodiment, after releasing the third amount of memory in the local cache to the target system, the method further comprises: updating a second target amount corresponding to the target business thread, wherein the second target amount is a number of times that the target business thread releases memory to the target system.
[0015] In one example embodiment, before determining the memory request amount corresponding to the memory allocation request, the method further comprises: dividing the memory corresponding to the target system in granularity to obtain a plurality of divided memories according to a data reading / writing scenario corresponding to the target system; and numbering the plurality of divided memories and establishing a mapping relationship between the number and a memory address of the number corresponding memory, so as to realize memory allocation based on the number.
[0016] In one example embodiment, before determining the memory request amount corresponding to the memory allocation request, the method further comprises: at a preset time point, obtaining a first target amount in a first time period and a second target amount in the first time period of the target business thread, wherein the first target amount is a number of times that the target business thread applies for memory to the target system in the first time period, and the second target amount is a number of times that the target business thread releases memory to the target system in the first time period; obtaining a first target amount in a second time period and a second target amount in the second time period of the target business thread; and calculating a first capacity threshold according to the first target amount in the first time period, the second target amount in the first time period, the first target amount in the second time period, the second target amount in the second time period and a first preset capacity threshold.
[0017] In an example embodiment, the first capacity threshold is calculated according to the first target quantity in the first time period, the second target quantity in the first time period, the first target quantity in the second time period, the second target quantity in the second time period, and the first preset capacity threshold, including: calculating a first value according to the first target quantity in the first time period and the second target quantity in the first time period; calculating a second value according to the first target quantity in the second time period and the second target quantity in the second time period; and calculating the first capacity threshold according to the first value, the second value, and the first preset capacity threshold.
[0018] In an example embodiment, the first capacity threshold is calculated according to the first value, the second value, and the first preset capacity threshold, including: if the second value is greater than the first value, calculating the first capacity threshold according to the first value, the second value, and the first preset capacity threshold by using a first calculation method; if the second value is less than the first value, calculating the first capacity threshold according to the first value, the second value, and the first preset capacity threshold by using a second calculation method; and if the second value is equal to the first value, setting the first preset capacity threshold as the first capacity threshold.
[0019] In an example embodiment, the first capacity threshold is calculated according to the first value, the second value, and the first preset capacity threshold by using the first calculation method, including: calculating a second difference value between the second value and the first value, and calculating a third value by summing the second difference value and a preset target value; calculating a first ratio value between the third value and the first value, and calculating a first product between the first ratio value and the first preset capacity threshold; and determining the first capacity threshold based on a value relationship between the first product and the first preset capacity threshold and a second preset capacity threshold.
[0020] In an example embodiment, the first capacity threshold is calculated according to the first value, the second value, and the first preset capacity threshold by using the second calculation method, including: calculating a second difference value between the second value and the first value, and calculating a fourth value by subtracting the second difference value from a preset target value; calculating a second ratio value between the fourth value and the first value, and calculating a second product between the second ratio value and the first preset capacity threshold; and determining the first capacity threshold based on a value relationship between the second product and the first preset capacity threshold and the second preset capacity threshold.
[0021] In one example embodiment, the determining the first capacity threshold based on the numerical relationship between the second product and the first preset capacity threshold, the second preset capacity threshold comprises: if the second product is greater than or equal to the first preset capacity threshold and less than or equal to the second preset capacity threshold, determining the second product as the first capacity threshold; if the second product is less than the first preset capacity threshold, determining the first preset capacity threshold as the first capacity threshold; if the second product is greater than the second preset capacity threshold, determining the second preset capacity threshold as the first capacity threshold.
[0022] In one example embodiment, after the first capacity threshold is obtained by calculating according to the first target number in the first time period, the second target number in the first time period, the first target number in the second time period, the second target number in the second time period and the first preset capacity threshold, the method further comprises: if the first capacity threshold is not equal to the first preset capacity threshold, performing a zero processing on the first target number in the first time period, the second target number in the first time period, the first target number in the second time period, the second target number in the second time period and the first preset capacity threshold.
[0023] In one example embodiment, after the first capacity threshold is obtained by calculating according to the first target number in the first time period, the second target number in the first time period, the first target number in the second time period, the second target number in the second time period and the first preset capacity threshold, the method further comprises: if the first capacity threshold is equal to the first preset capacity threshold, setting the first target number in the first time period to be the same as the first target number in the second time period, and zeroing the first target number in the second time period; setting the second target number in the first time period to be the same as the second target number in the second time period, and zeroing the second target number in the second time period.
[0024] According to another embodiment of the present application, a memory allocation apparatus is provided, comprising: a first determining unit configured to determine a memory request amount corresponding to a memory allocation request of a target service thread when the memory allocation request is received; a first allocating unit configured to allocate a number of memory corresponding to a first memory amount to the target service thread if the memory request amount is greater than an allocable first memory amount of a local cache corresponding to the target service thread; a requesting unit configured to apply a first number of memory to the local cache according to a first capacity threshold corresponding to the local cache, wherein the first capacity threshold is calculated based on a number of times of applying memory to a target system by the target service thread, a number of times of releasing memory to the target system, and a preset first preset capacity threshold; and a second allocating unit configured to allocate a second number of memory to the target service thread according to the number of the first number of memory in the local cache, so that the target service thread performs service processing based on the allocated memory, wherein the second number is determined based on a difference between the first memory amount and the memory request amount.
[0025] In one example embodiment, the judging unit is configured to judge whether the memory request amount is less than or equal to the first memory amount in the local cache after determining the memory request amount corresponding to the memory allocation request; and a third allocating unit is configured to allocate the memory amount corresponding to the memory allocation request to the target service thread according to the number of memory in the local cache if the memory request amount is less than or equal to the first memory amount in the local cache.
[0026] In one example embodiment, the apparatus further comprises a first updating unit configured to update a first target number corresponding to the target service thread after applying the first number of memory to the local cache according to the first capacity threshold, wherein the first target number is a number of times of applying memory to the target system by the target service thread.
[0027] In one example embodiment, the apparatus further comprises a second determining unit configured to determine a memory release amount corresponding to a memory release request of the target service thread if the memory release request is received after allocating the second number of memory to the target service thread according to the number of the first number of memory in the local cache, so that the target service thread performs service processing based on the allocated memory; and a first releasing unit configured to release memory corresponding to the memory release amount according to the memory release amount and an allocable second memory amount in the local cache.
[0028] In one example embodiment, the first releasing unit comprises: an obtaining subunit configured to obtain a current second capacity threshold of the local cache, wherein the second capacity threshold is calculated based on a number of times of memory application by the target service thread, a number of times of memory release, and the first capacity threshold; and a releasing subunit configured to release memory corresponding to the memory release amount according to the second capacity threshold, the memory release amount, and the second memory amount.
[0029] In one example embodiment, the releasing subunit comprises: a calculating module configured to calculate a difference between the second capacity threshold and the second memory amount to obtain a first difference; a judging module configured to judge whether the memory release amount is less than or equal to the first difference; and a releasing module configured to release memory corresponding to the memory release amount into the local cache if the memory release amount is less than or equal to the first difference.
[0030] In one example embodiment, the apparatus further comprises: a second releasing unit configured to release memory corresponding to the first difference into the local cache if the memory release amount is greater than the first difference after judging whether the memory release amount is less than or equal to the first difference; and a third releasing unit configured to release a third amount of memory in the local cache to the target system and release a fourth amount of memory corresponding to the memory release request to the local cache, wherein the third amount is calculated based on the second capacity threshold, and the fourth amount is calculated based on the first difference and the memory release amount.
[0031] In one example embodiment, the apparatus further comprises: a second updating unit configured to update a second target amount corresponding to the target service thread after releasing the third amount of memory in the local cache to the target system, wherein the second target amount is a number of times of memory release by the target service thread to the target system.
[0032] In one example embodiment, the apparatus further comprises: a dividing unit configured to divide memory corresponding to the target system according to a data reading / writing scenario of the target system to obtain a plurality of divided memories before determining a memory request amount corresponding to the memory allocation request; and a numbering unit configured to number the plurality of divided memories and establish a mapping relationship between a number and a memory address of the number corresponding memory, so as to realize memory allocation based on the number.
[0033] In an example embodiment, the apparatus further comprises: a first obtaining unit configured to obtain, at a preset time point, a first target number of the target service thread in a first time period and a second target number of the target service thread in the first time period before determining the memory request amount corresponding to the memory allocation request, wherein the first target number is a number of times of memory application to the target system by the target service thread in the first time period, and the second target number is a number of times of memory release to the target system by the target service thread in the first time period; a second obtaining unit configured to obtain a first target number of the target service thread in a second time period and a second target number of the target service thread in the second time period; and a calculating unit configured to calculate, according to the first target number in the first time period, the second target number in the first time period, the first target number in the second time period, the second target number in the second time period, and a first preset capacity threshold, to obtain the first capacity threshold.
[0034] In an example embodiment, the calculating unit comprises: a first calculating sub-unit configured to calculate, according to the first target number in the first time period and the second target number in the first time period, to obtain a first value; a second calculating sub-unit configured to calculate, according to the first target number in the second time period and the second target number in the second time period, to obtain a second value; and a third calculating sub-unit configured to calculate, according to the first value, the second value, and the first preset capacity threshold, to obtain the first capacity threshold.
[0035] In an example embodiment, the third calculating sub-unit comprises: a first calculating module configured to, if the second value is greater than the first value, calculate, by using a first calculating device, the first value, the second value, and the first preset capacity threshold, to obtain the first capacity threshold; a second calculating module configured to, if the second value is less than the first value, calculate, by using a second calculating device, the first value, the second value, and the first preset capacity threshold, to obtain the first capacity threshold; and a setting module configured to, if the second value is equal to the first value, set the first preset capacity threshold as the first capacity threshold.
[0036] In an example embodiment, the first calculating module comprises: a first calculating sub-module configured to calculate a difference between the second value and the first value to obtain a second difference, and calculate a sum of the second difference and a preset target value to obtain a third value; a second calculating sub-module configured to calculate a ratio between the third value and the first value to obtain a first ratio, and calculate a product between the first ratio and the first preset capacity threshold to obtain a first product; and a first determining sub-module configured to determine the first capacity threshold based on a numerical relationship between the first product and the first preset capacity threshold and a second preset capacity threshold.
[0037] In one example embodiment, the second calculating module comprises: a third calculating submodule configured to calculate a difference between the second value and the first value to obtain a second difference, and calculate a difference between the second difference and a preset target value to obtain a fourth value; a fourth calculating submodule configured to calculate a ratio between the fourth value and the first value to obtain a second ratio, and calculate a product between the second ratio and a first preset capacity threshold to obtain a second product; and a second determining submodule configured to determine the first capacity threshold based on a value relationship between the second product and the first preset capacity threshold and a second preset capacity threshold.
[0038] In one example embodiment, the second determining submodule comprises: a first determining sub-submodule configured to determine the second product as the first capacity threshold if the second product is greater than or equal to the first preset capacity threshold and less than or equal to the second preset capacity threshold; a second determining sub-submodule configured to determine the first preset capacity threshold as the first capacity threshold if the second product is less than the first preset capacity threshold; and a third determining sub-submodule configured to determine the second preset capacity threshold as the first capacity threshold if the second product is greater than the second preset capacity threshold.
[0039] In one example embodiment, the device further comprises: a first processing unit configured to, after obtaining the first capacity threshold based on the first target quantity in the first time period, the second target quantity in the first time period, the first target quantity in the second time period, the second target quantity in the second time period and the first preset capacity threshold, clear the first target quantity in the first time period, the second target quantity in the first time period, the first target quantity in the second time period, the second target quantity in the second time period and the first preset capacity threshold if the first capacity threshold is not equal to the first preset capacity threshold.
[0040] In one example embodiment, the device further comprises: a second processing unit configured to, after obtaining the first capacity threshold based on the first target quantity in the first time period, the second target quantity in the first time period, the first target quantity in the second time period, the second target quantity in the second time period and the first preset capacity threshold, set the first target quantity in the first time period to be the same as the first target quantity in the second time period and clear the first target quantity in the second time period if the first capacity threshold is equal to the first preset capacity threshold; and a third processing unit configured to set the second target quantity in the first time period to be the same as the second target quantity in the second time period and clear the second target quantity in the second time period.
[0041] According to a further embodiment of the present application, an electronic device is also provided, comprising a memory and a processor, the memory storing a computer program, and the processor being configured to execute the computer program to perform the steps of any of the above method embodiments.
[0042] According to a further embodiment of the present application, a computer program product is also provided, comprising a computer program, which, when executed by a processor, implements the steps of any of the above method embodiments.
[0043] According to the present application, the capacity setting of the local cache is adjusted according to the number of times of memory application to the target system, the number of times of memory release to the target system and the first preset capacity threshold of the target business thread during operation, so that the cache capacity of each business thread adapts to the current load to maximize the benefits of cache. Therefore, the problem that the memory allocation efficiency is relatively low in the uneven load business scenario caused by the memory allocation of the business thread through the memory in the local cache with fixed capacity in the related art can be solved, and the effect of improving the memory allocation efficiency is achieved. BRIEF DESCRIPTION OF DRAWINGS
[0044] FIG. 1 is a hardware structure block diagram of a server device of a memory allocation method according to an embodiment of the present application;
[0045] FIG. 2 is a flowchart of a memory allocation method according to an embodiment of the present application;
[0046] FIG. 3 is a schematic diagram one of memory allocation and release according to an embodiment of the present application;
[0047] FIG. 4 is a schematic diagram two of memory allocation and release according to an embodiment of the present application;
[0048] FIG. 5 is a schematic diagram three of memory allocation and release according to an embodiment of the present application;
[0049] FIG. 6 is a schematic diagram four of memory allocation and release according to an embodiment of the present application;
[0050] FIG. 7 is a structure block diagram of a memory allocation device according to an embodiment of the present application. DETAILED DESCRIPTION
[0051] Hereinafter, the embodiments of the present application will be described in detail with reference to the accompanying drawings and in conjunction with embodiments.
[0052] It should be noted that the terms "first", "second" and the like in the specification and claims of the present application and the above-described drawings are used to distinguish similar objects, and do not necessarily have to describe a specific order or a chronological sequence.
[0053] The method embodiments provided in the embodiments of the present application can be executed in a server device or similar computing device. Taking the case of running on a server device, Fig. 1 is a hardware structure block diagram of a server device of a memory allocation method according to an embodiment of the present application. As shown in Fig. 1, the server device can include one or more (only one is shown in Fig. 1) processors 102 (the processor 102 can include but is not limited to a processing device such as a microprocessor MCU or a programmable logic device FPGA) and a memory 104 configured to store data, wherein the above-mentioned server device can further include a transmission device 106 configured to have a communication function and an input and output device 108. Those skilled in the art can understand that the structure shown in Fig. 1 is only schematic, which does not limit the structure of the above-mentioned server device. For example, the server device can further include more or less components than those shown in Fig. 1, or have a different configuration from that shown in Fig. 1.
[0054] The memory 104 can be configured to store computer programs, for example, software programs of application software and modules, such as a computer program corresponding to the memory allocation method in the embodiments of the present application. The processor 102 executes various functional applications and data processing by running the computer programs stored in the memory 104, that is, implements the above-mentioned method. The memory 104 can include a high-speed random access memory, and can further include a non-volatile memory, such as one or more magnetic storage devices, a flash memory, or other non-volatile solid-state memories. In some examples, the memory 104 can further include a memory remotely arranged with respect to the processor 102, which can be connected to the server device through a network. Examples of the above-mentioned network include but are not limited to the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0055] The transmission device 106 is configured to receive or send data via a network. Specific examples of the above-mentioned network can include a wireless network provided by a communication service provider of the server device. In one example, the transmission device 106 includes a network adapter (Network Interface Controller, NIC for short), which can be connected to other network devices through a base station so as to communicate with the Internet. In one example, the transmission device 106 can be a radio frequency (Radio Frequency, RF for short) module, which is configured to communicate with the Internet in a wireless manner.
[0056] In the embodiments of the present application, a memory allocation method is provided. Fig. 2 is a flow chart of the memory allocation method according to an embodiment of the present application. As shown in Fig. 2, the flow includes the following steps:
[0057] In step S202, when receiving a memory allocation request of a target service thread, the memory request amount corresponding to the memory allocation request is determined.
[0058] Step S204, if the memory request amount is greater than the first memory amount of the local cache corresponding to the target service thread, the number of the memory corresponding to the first memory amount is allocated to the target service thread;
[0059] Step S206, according to the first capacity threshold of the local cache, the target system is applied for the first amount of memory to the local cache, wherein the first capacity threshold is calculated based on the number of times of applying for memory from the target system, the number of times of releasing memory to the target system and the first preset capacity threshold;
[0060] Step S208, according to the number of the first amount of memory in the local cache, the second amount of memory is allocated to the target service thread, so that the target service thread performs service processing based on the allocated memory, wherein the second amount is determined based on the difference between the first memory amount and the memory request amount.
[0061] In an optional embodiment, a local cache is set for each service thread in the protocol stack system (i.e. the aforementioned target system), and the service thread preferentially allocates from its local cache when applying for memory. Since the memory of the protocol stack system is limited, the size of the cache cannot be set too large. The load of different service threads in different scenarios is different, and in the entire IO process, the application and release of the same block of memory are not necessarily on the same service thread, i.e. the memory allocated on the service thread-A may be released on the service thread-B after the IO processing is completed.
[0062] In the face of such uneven load high concurrency scenarios, in the memory allocation method provided in the embodiment of the application, an initial value (i.e. the first preset capacity threshold) is set for the cache capacity of each service thread, for example, the initial value is 8, and each service thread preferentially allocates from its local cache when applying for memory. If the cache of the service thread is empty, the service thread will apply for memory of 1 / 2 size of the cache capacity from the system to the local cache of the service thread, and then allocate from the local cache of the service thread. Each service thread will record the number of times of applying for memory from the system (denoted as m) and the number of times of releasing memory to the system (denoted as n), and then adjust the capacity setting of the local cache according to the changes of m and n, so that the cache capacity of each service thread adapts to the current load.
[0063] In an optional embodiment, in order to improve the memory allocation efficiency, the memory of the system can also be divided into granules and numbered globally, and a mapping relationship between the number and the memory address is established, so as to improve the memory allocation efficiency in the form of caching the number instead of the memory content. For example, the memory is divided into sectors or page granules and numbered, and then the memory is allocated in the form of page granule.
[0064] In an optional embodiment, the schematic structure of the service cache can be as follows:
[0065] struct Local_cache{
[0066] long size;
[0067] long index;
[0068] long pages[];
[0069] long refill;
[0070] long giveback;
[0071] long t1,m1,n1;
[0072] long t2,m2,n2;
[0073] }
[0074] In the above structure, struct Local_cache represents the local cache structure, size represents the capacity threshold of the local cache, i.e. the element number of the member pages array; pages is a dynamic array, and each element of the array is the number of the memory divided into page granules, i.e. the page number. The element number of the pages array is controlled by size; index is the cursor of the pages array, representing the current start position of the allocation. long pages[] is used to record the page number, refill represents the number of times of applying the memory from the system to the local cache, wherein the number of pages applied each time is half of the capacity of the cache, i.e. size / 2. The member giveback represents the number of times of releasing the memory from the local cache to the system. m1 is the increase of the refill number in the t1 time period, n1 is the increase of the giveback number in the t1 time period, and t1 represents the length of the last statistical time period. m2 is the increase of the refill number in the t2 time period, n2 is the increase of the giveback number in the t2 time period, and t2 represents the length of the current time period.
[0075] In an optional embodiment, the capacity threshold of the service thread can be dynamically adjusted according to the refill times and giveback times in the t1 time period and the refill times and giveback times in the t2 time period.
[0076] For the target service thread, the initial capacity of the local cache of the target service thread is a preset first preset capacity threshold. During the running of the target service thread, the number of times of applying for memory from the system and the number of times of releasing memory to the system are recorded, and then the first preset capacity threshold is adjusted to the first capacity threshold according to the changes of the number of times of applying for memory and the number of times of releasing memory to the system.
[0077] After the first preset capacity threshold is adjusted to the first capacity threshold, when the memory allocation request of the target service thread is received, the memory request amount corresponding to the memory allocation request needs to be determined in order to accurately determine whether the remaining memory in the local cache corresponding to the target service thread is sufficient to allocate the memory allocation request.
[0078] In an optional embodiment, after the memory request amount corresponding to the memory allocation request is obtained, it is determined whether the memory request amount is greater than the first allocable memory amount of the local cache corresponding to the target service thread. If the memory request amount is greater than the first allocable memory amount of the local cache corresponding to the target service thread, the number of the memory corresponding to the first memory amount is allocated to the target service thread, and a first number of memories are applied to the local cache from the target system according to the first capacity threshold of the local cache. It should be noted that the first number can be half of the first capacity threshold.
[0079] After the first number of memories are applied to the local cache, a second number of memories are allocated to the target service thread from the first number of memories in the local cache, so that the target service thread performs service processing based on the allocated memories.
[0080] It should be noted that if the memory in the local cache cannot meet the memory required by the memory allocation request after the target service thread applies the first number of memories to the local cache from the target system, the target service thread will again apply the first number of memories to the local cache from the target system until the memory in the local cache meets the memory required by the memory allocation request.
[0081] It should be noted that, since the memory of the system as a whole is divided into granules and numbered, when allocating memory, the page page code can be directly allocated according to the page code, for example, the local cache capacity of the business thread thread-A is 8, that is, a maximum of 8 page pages, and there are only 5 page pages in the current cache, that is, the page page available for allocation in the pages array of the Local_cache is only 5, and the numbers of the 5 page pages available for allocation are N1, Na, N3, N4, and Nx, respectively, then N1, Na, N3, N4, and Nx can be directly allocated to the corresponding business thread thread-A.
[0082] Through the above steps, since the target business thread adjusts the capacity setting of the local cache according to the number of times of applying for memory from the target system, the number of times of releasing memory from the target system, and the first preset capacity threshold during the operation of the target business thread, the cache capacity of each business thread adapts to the current load, so that the efficiency of the cache is maximized, thereby solving the problem that in the related art, the business thread implements memory allocation through the memory in the local cache with a fixed capacity, resulting in low memory allocation efficiency in an unbalanced load business scenario, and achieving the effect of memory allocation efficiency.
[0083] The execution subject of the above steps can be a server, a terminal, etc., but is not limited thereto.
[0084] In an optional embodiment, in the memory allocation method provided in the embodiments of the present application, after determining the memory request amount corresponding to the memory allocation request, the method further includes: judging whether the memory request amount is less than or equal to the first memory amount in the local cache; if the memory request amount is less than or equal to the first memory amount in the local cache, then allocating the memory amount corresponding to the memory allocation request to the target business thread according to the number corresponding to the memory in the local cache.
[0085] In an optional embodiment, after determining the memory request amount corresponding to the memory allocation request, it is judged whether the allocable memory in the local cache can meet the memory required by the memory allocation request, if the allocable memory in the local cache can meet the memory required by the memory allocation request, that is, the memory request amount is less than or equal to the first memory amount in the local cache, then the memory amount corresponding to the memory allocation request is directly allocated to the target business thread according to the number of the allocable memory in the local cache. By allocating memory in the form of number rather than memory content, the memory allocation efficiency can be effectively improved.
[0086] In an optional embodiment, in the memory allocation method provided in the embodiments of the present application, after the first quantity of memory is applied for to the target system into the local cache according to the first capacity threshold, the method further comprises: updating a first target quantity corresponding to the target business thread, wherein the first target quantity is the number of times of applying for memory to the target system by the target business thread.
[0087] In an optional embodiment, in order to facilitate subsequent adjustment of the capacity threshold of the local cache, after the target business thread applies for memory to the target system, the number of times of applying for memory to the target system corresponding to the target business thread (i.e., the first target quantity) is updated, that is, the refill number in the local cache is incremented by 1. By recording the number of times of applying for memory to the target system corresponding to the business thread, the load condition of the business thread can be accurately evaluated.
[0088] In an optional embodiment, in the memory allocation method provided in the embodiments of the present application, after the second quantity of memory is allocated to the target business thread according to the number of the first quantity of memory in the local cache, so that the target business thread performs business processing based on the allocated memory, the method further comprises: determining a memory release amount corresponding to the memory release request of the target business thread when the memory release request is received; and releasing the memory corresponding to the memory release amount according to the memory release amount and the second memory amount that can be allocated in the local cache.
[0089] In an optional embodiment, in order to avoid the situation that the same block of memory is applied for and released on the same business thread, in the memory allocation method provided in the embodiments of the present application, when the memory release request of the target business thread is received, the memory corresponding to the memory release amount is released according to the memory release amount corresponding to the memory release request and the second memory amount that can be allocated in the local cache, that is, whether the local cache can accept the memory release amount corresponding to the memory release request is considered first.
[0090] In an optional embodiment, releasing the memory corresponding to the memory release amount according to the memory release amount and the second memory amount that can be allocated in the local cache comprises: obtaining a second capacity threshold of the local cache at present, wherein the second capacity threshold is calculated based on the number of times of applying for memory by the target business thread, the number of times of releasing memory, and the first capacity threshold; and releasing the memory corresponding to the memory release amount according to the second capacity threshold, the memory release amount, and the second memory amount.
[0091] In an optional embodiment, the current capacity threshold of the local cache corresponding to the target business thread is determined first, that is, the second capacity threshold is obtained. It should be noted that when the memory release request is received, the capacity threshold is not adjusted again, and therefore, the second capacity threshold can also be the first capacity threshold.
[0092] It should be noted that, when the memory release request is received, if the capacity threshold is adjusted again, then the corresponding second capacity threshold is calculated according to the number of times of memory application by the target business thread, the number of times of memory release, and the first capacity threshold.
[0093] After the second capacity threshold is obtained, the memory corresponding to the memory release amount is released according to the second capacity threshold, the memory release amount, and the second memory amount.
[0094] In an optional embodiment, the memory corresponding to the memory release amount is released according to the second capacity threshold, the memory release amount, and the second memory amount, including the following steps: calculating a difference between the second capacity threshold and the second memory amount to obtain a first difference; judging whether the memory release amount is less than or equal to the first difference; if the memory release amount is less than or equal to the first difference, releasing the memory corresponding to the memory release amount to the local cache.
[0095] If the memory release amount is greater than the first difference, releasing the memory corresponding to the first difference of the memory release request to the local cache; releasing the third amount of memory in the local cache to the target system, and releasing the fourth amount of memory corresponding to the memory release request to the local cache, wherein the third amount is calculated based on the second capacity threshold, and the fourth amount is calculated based on the first difference and the memory release amount.
[0096] In an optional embodiment, after the second capacity threshold is obtained, the current amount of memory that can be accommodated by the local cache is determined according to the second capacity threshold and the second memory amount, that is, the first difference described above, and then it is judged whether the memory release amount is less than or equal to the first difference, that is, whether the local cache can accommodate the to-be-released memory.
[0097] If the memory release amount is less than or equal to the first difference, the memory corresponding to the memory release amount is released to the local cache. If the memory release amount is greater than the first difference, the memory corresponding to the first difference of the memory release request is first released to the local cache, and then the third amount of memory in the local cache is released to the target system, and finally the fourth amount of memory corresponding to the memory release request is released to the local cache. In short, when the business thread has memory release, it is preferentially put into its own local cache, and if the current cache is full, half of the memory in the cache is released to the system at one time, and then the to-be-released memory is put into the local cache. It should be noted that the third amount can be half of the second capacity threshold, and the fourth amount is the difference between the first difference and the memory release amount.
[0098] In an optional embodiment, the local cache capacity of the business thread thread-A is 8, i.e. the maximum number of page pages is 8, and the current cache has only 5 page pages, i.e. the number of page pages available for allocation in the pages array of the Local_cache is 5. As shown in FIG. 3, the 5 available page pages are coded as N1, Na, N3, N4, and Nx, respectively, which do not have to be in order. The last three blank boxes represent that the positions have no available page pages. The business thread thread-A has multiple memory allocations, and 3 page pages are allocated, and finally the state of the cache is as shown in FIG. 4, and the available memory in the local cache is N1 and Na. The business thread thread-A has multiple memory releases in succession, and 6 page pages are released, and finally the state of the local cache is as shown in FIG. 5, and the available memory is N1, Na, Nx, Nv, Nz, Ny, N3, and Nw. The business thread thread-A has to release 1 page page, which is coded as N5, and at this time, the cache is full and cannot be directly put in. First, 4 page pages in the back of the cache are released to the system at one time, and the value of the cache refilling is increased by 1, and then the N5 page page is released to the local cache, as shown in FIG. 6.
[0099] In an optional embodiment, if the page pages are released to the local cache in the order of the number, the memory with a smaller number in the local cache can be released to the target memory first when the memory in the local cache is released to the target system.
[0100] Through the above memory release process, the problem that the application and release of the same memory are not necessarily in the same business thread can be effectively avoided, thereby effectively avoiding the problem of memory competition.
[0101] In an optional embodiment, in the memory allocation method provided in the embodiments of the present application, after the third number of memories in the local cache are released to the target system, the method further comprises: updating the second target number corresponding to the target business thread, wherein the second target number is the number of times of releasing the memory to the target system by the target business thread.
[0102] In an optional embodiment, in order to facilitate subsequent adjustment of the capacity threshold of the local cache, after the target business thread applies for memory from the target system, the number of times of releasing the memory to the target system corresponding to the target business thread (i.e. the second target number) is updated, i.e. the number of times of giveback in the local cache is increased by 1. By recording the number of times of releasing the memory to the target system corresponding to the business thread, the load condition of the business thread can be accurately evaluated.
[0103] In an optional embodiment, in the memory allocation method provided in the embodiments of the present application, before determining the memory request amount corresponding to the memory allocation request, the method further comprises: performing granularity division on the memory corresponding to the target system according to the data read / write scenario corresponding to the target system, to obtain a plurality of divided memories; performing numbering processing on the plurality of divided memories, and establishing a mapping relationship between the number and the memory address of the memory corresponding to the number, so as to realize memory allocation based on the number.
[0104] In an optional embodiment, in order to improve the memory allocation efficiency, the memory corresponding to the target system is divided in granularity according to the data read / write scenario (i.e. IO scenario) corresponding to the target system, for example, the division is performed in sector or page granularity. Then, the plurality of divided memories are numbered, and finally the mapping relationship between the number and the memory address of the memory corresponding to the number is established, so that the number and the memory address have a linear mapping relationship, and the number can be directly converted into the address. The memory allocation is also performed in sector or page granularity, and the number is also used in the allocation process. In this way, the processing granularity of the IO scenario model is met, and complex address operations are avoided. The business module can use the memory very conveniently according to the linear mapping relationship between the number and the memory address.
[0105] By performing granularity division and numbering on the memory and establishing the mapping relationship between the number and the memory address, the memory is allocated in the form of caching the number instead of the memory content, which can improve the memory allocation efficiency.
[0106] In an optional embodiment, in the memory allocation method provided in the embodiments of the present application, before determining the memory request amount corresponding to the memory allocation request, the method further comprises: at a preset time point, obtaining a first target number of the target business thread in a first time period and a second target number in the first time period, wherein the first target number is the number of times of applying for memory of the target system by the target business thread in the first time period, and the second target number is the number of times of releasing memory of the target system by the target business thread in the first time period; obtaining a first target number of the target business thread in a second time period and a second target number in the second time period; and calculating the first capacity threshold value according to the first target number in the first time period, the second target number in the first time period, the first target number in the second time period, the second target number in the second time period, and the first preset capacity threshold value.
[0107] In an alternative embodiment, in order to dynamically control the local cache capacity, the following steps are used to calculate the first capacity threshold in the memory allocation method provided by the embodiments of the present application: at a preset time point, the first target number m1 and the second target number n1 of the target service thread in a first time period (for example, t1) are obtained, and the first target number m2 and the second target number n1 of the target service thread in a second time period (for example, t2) are obtained. Finally, the first capacity threshold is calculated according to the first target number in the first time period, the second target number in the first time period, the first target number in the second time period, the second target number in the second time period, and the first preset capacity threshold.
[0108] In an alternative embodiment, the service thread performs statistics on the refill and giveback of the Local_cache every certain period of time, and saves the related data in the t1, m1, n1, t2, m2 and n2 members of the Local_cache. Wherein t1 represents the last statistical duration, m1 represents the increase of the refill times in the t1 period, n1 represents the increase of the giveback times in the t1 period; t2 represents the statistical duration of the current period, m2 represents the increase of the refill times in the t2 period, and n2 represents the increase of the giveback times in the t2 period. According to the changes of m and n in the two periods, the capacity value of the local cache (i.e. the size member of the Local_cache) is adjusted.
[0109] In an alternative embodiment, the adjustment mode of the capacity value of the local cache can be determined according to the size relationship between m and n, for example, the ratio between m and n is calculated to obtain the first capacity threshold according to the first preset capacity threshold.
[0110] Through the above steps, the local cache capacity of the service thread can be quickly iterated to the optimal value suitable for the current load, greatly reducing the competition caused by constantly applying and releasing memory to the system.
[0111] In an alternative embodiment, in the memory allocation method provided by the embodiments of the present application, the first capacity threshold is calculated according to the first target number in the first time period, the second target number in the first time period, the first target number in the second time period, the second target number in the second time period, and the first preset capacity threshold, including: calculating a first value according to the first target number in the first time period and the second target number in the first time period; calculating a second value according to the first target number in the second time period and the second target number in the second time period; and calculating the first capacity threshold according to the first value, the second value, and the first preset capacity threshold.
[0112] In an optional embodiment, the first capacity threshold can be calculated by the following steps: calculating a first value according to the first target number in the first time period and the second target number in the first time period, for example, calculating the sum of the first target number in the first time period and the second target number in the first time period, and then calculating the ratio of the sum to the first time period to obtain the first value, for example, the first value is (m1+n1) / t1.
[0113] Then, a second value is calculated according to the first target number in the second time period and the second target number in the second time period, for example, calculating the sum of the first target number in the second time period and the second target number in the second time period, and then calculating the ratio of the sum to the second time period to obtain the second value, for example, the second value is (m2+n2) / t2.
[0114] Finally, the first capacity threshold is calculated according to the first value, the second value and the first preset capacity threshold. For example, the first capacity threshold is calculated according to the ratio between the first value and the second value and the first preset capacity threshold.
[0115] Through the above steps, the local cache capacity of the business thread can be quickly iterated to the optimal value suitable for the current load, greatly reducing the competition caused by constantly applying and releasing memory to the system.
[0116] In an optional embodiment, in the memory allocation method provided in the embodiments of the present application, the first capacity threshold is calculated according to the first value, the second value and the first preset capacity threshold, including: if the second value is greater than the first value, the first calculation method is used to calculate the first value, the second value and the first preset capacity threshold to obtain the first capacity threshold; if the second value is less than the first value, the second calculation method is used to calculate the first value, the second value and the first preset capacity threshold to obtain the first capacity threshold; if the second value is equal to the first value, the first preset capacity threshold is set as the first capacity threshold.
[0117] In an optional embodiment, the first value, the second value and the first preset capacity threshold value can be calculated to obtain the first capacity threshold value by the following steps: judging the size relationship between the first value and the second value, and determining the method of calculating the capacity threshold value according to the size relationship between the first value and the second value: if the second value is greater than the first value, the first calculation method is used to calculate the first value, the second value and the first preset capacity threshold value to obtain the first capacity threshold value. It should be noted that the first value and the second value represent the change amount of memory application and memory release in the first time period and the second time period respectively, and when the second value is greater than the first value, it represents that the load in the second time period is greater than the load in the first time period, so the capacity threshold value of the current local cache needs to be adjusted, that is, the first calculation method is used to calculate the first value, the second value and the first preset capacity threshold value to obtain the first capacity threshold value.
[0118] If the second value is less than the first value, the second calculation method is used to calculate the first value, the second value and the first preset capacity threshold value to obtain the first capacity threshold value. It should be noted that when the second value is less than the first value, it represents that the load in the second time period is less than the load in the first time period, so the capacity threshold value of the current local cache needs to be adjusted, that is, the second calculation method is used to calculate the first value, the second value and the first preset capacity threshold value to obtain the first capacity threshold value.
[0119] If the second value is equal to the first value, it means that the current capacity threshold value can adapt to the current load, so the first preset capacity threshold value can still be set as the first capacity threshold value.
[0120] The change amount of memory application and memory release in the two time periods can accurately evaluate the load change of the target business thread, and then accurately adjust the capacity threshold value.
[0121] In an optional embodiment, in the memory allocation method provided in the embodiments of the present application, the first calculation method is used to calculate the first value, the second value and the first preset capacity threshold value to obtain the first capacity threshold value, including: calculating the difference between the second value and the first value to obtain a second difference, and summing the second difference and a preset target value to obtain a third value; calculating the ratio between the third value and the first value to obtain a first ratio, and calculating the product between the first ratio and the first preset capacity threshold value to obtain a first product; determining the first capacity threshold value based on the value relationship between the first product and the first preset capacity threshold value and a second preset capacity threshold value.
[0122] In an optional embodiment, the first calculation method comprises the following steps: first, calculating the difference between the second value and the first value to obtain a second difference; then, summing the second difference and the preset target value to obtain a third value; then, calculating the ratio between the third value and the first value; and finally, calculating the product between the first ratio and the first preset capacity threshold to obtain a first product, and then determining the first capacity threshold according to the numerical relationship between the first product and the first preset capacity threshold and the second preset capacity threshold. It should be noted that the first preset capacity threshold is a minimum value min_size, and because the memory of the system is limited, the capacity of the local cache also has an upper limit, which is max_size, the second preset capacity threshold described above. The minimum value and the maximum value are set in combination with the characteristics and specifications of the system itself.
[0123] In an optional embodiment, if (m2+n2) / t2 is greater than (m1+n1) / t1, the first capacity threshold is calculated using the following formula: size_new=(1+((m2+n2) / t2-(m1+n1) / t1) / ((m1+n1) / t1))*size, where size_new is the first capacity threshold described above, and size is the first preset capacity threshold described above.
[0124] In an optional embodiment, determining the first capacity threshold according to the numerical relationship between the first product and the first preset capacity threshold and the second preset capacity threshold comprises: if size_new is greater than or equal to max_size, setting the capacity of the local cache (i.e., the size member of Local_cache) to max_size; if size_new is less than or equal to min_size, setting the capacity of the local cache (i.e., the size member of Local_cache) to min_size; and if size_new is greater than min_size and less than max_size, setting the capacity of the local cache (i.e., the size member of Local_cache) to size_new.
[0125] By dynamically controlling the capacity of the local cache to adapt to memory allocation under different load pressures, the competition caused by continuous application and release of memory in the system under high concurrency scenarios is greatly reduced, and the memory allocation efficiency is improved.
[0126] In an optional embodiment, in the memory allocation method provided in the embodiments of the present application, the first value, the second value and the first preset capacity threshold are calculated by using the second calculation method to obtain the first capacity threshold, including: calculating the difference between the second value and the first value to obtain a second difference, and calculating the difference between the second difference and the preset target value to obtain a fourth value; calculating the ratio between the fourth value and the first value to obtain a second ratio, and calculating the product between the second ratio and the first preset capacity threshold to obtain a second product; determining the first capacity threshold based on the numerical relationship between the second product and the first preset capacity threshold and the second preset capacity threshold.
[0127] In an optional embodiment, the first value, the second value and the first preset capacity threshold are calculated by using the second calculation method to obtain the first capacity threshold, including: first, calculating the difference between the second value and the first value, then calculating the difference between the second value and the first value and the preset target value, then calculating the ratio between the fourth value and the first value to obtain a second ratio, and finally calculating the product between the second ratio and the first preset capacity threshold to obtain a second product, and then determining the first capacity threshold based on the numerical relationship between the second product and the first preset capacity threshold and the second preset capacity threshold.
[0128] In an optional embodiment, if (m2+n2) / t2 is less than (m1+n1) / t1, the first capacity threshold is calculated by using the following formula: size_new=(1-((m1+n1) / t1-(m2+n2) / t2) / ((m1+n1) / t1))*size.
[0129] In an optional embodiment, in the memory allocation method provided in the embodiments of the present application, the first capacity threshold is determined based on the numerical relationship between the second product and the first preset capacity threshold and the second preset capacity threshold, including: if the second product is greater than or equal to the first preset capacity threshold and less than or equal to the second preset capacity threshold, the second product is determined as the first capacity threshold; if the second product is less than the first preset capacity threshold, the first preset capacity threshold is determined as the first capacity threshold; and if the second product is greater than the second preset capacity threshold, the second preset capacity threshold is determined as the first capacity threshold.
[0130] In an optional embodiment, determining the first capacity threshold according to the numerical relationship between the second product and the first preset capacity threshold and the second preset capacity threshold comprises: judging the size relationship between the second product and the first preset capacity threshold and the second preset capacity threshold, if size_new is greater than or equal to max_size, setting the capacity of the local cache (i.e. the size member of Local_cache) as max_size; if size_new is less than or equal to min_size, setting the capacity of the local cache (i.e. the size member of Local_cache) as min_size; if size_new is greater than min_size and less than max_size, setting the capacity of the local cache (i.e. the size member of Local_cache) as size_new.
[0131] Through the above steps, the local cache capacity of the service thread can be quickly iterated to the optimal value suitable for the current load, greatly reducing the competition caused by constantly applying and releasing memory to the system, maximizing the benefits of the cache, and improving the efficiency of memory allocation.
[0132] In an optional embodiment, in the memory allocation method provided in the embodiments of the present application, after the first capacity threshold is obtained by calculating according to the first target number in the first time period, the second target number in the first time period, the first target number in the second time period, the second target number in the second time period and the first preset capacity threshold, the method further comprises: if the first capacity threshold is not equal to the first preset capacity threshold, performing zero processing on the first target number in the first time period, the second target number in the first time period, the first target number in the second time period, the second target number in the second time period and the first preset capacity threshold.
[0133] If the first capacity threshold is equal to the first preset capacity threshold, the first target number in the first time period is set to be the same as the first target number in the second time period, and the first target number in the second time period is zeroed; the second target number in the first time period is set to be the same as the second target number in the second time period, and the second target number in the second time period is zeroed.
[0134] In an optional embodiment, after the capacity threshold of the local cache is adjusted, the memory allocation method provided in the embodiments of the present application further includes the following steps: if the size of the Local_cache is adjusted, that is, the first capacity threshold is not equal to the first preset capacity threshold, t1, m1, n1, t2, m2 and n2 are cleared, and after the first capacity threshold is set, t1, m1, n1, t2, m2 and n2 are counted again, and then the threshold can be adjusted again at the preset time point.
[0135] If the size of the Local_cache is not changed, that is, the first capacity threshold is equal to the first preset capacity threshold, t1=t2, m1=m2 and n1=n2, and then t2, m2 and n2 are cleared. That is, the first target number in the first time period is set to be the same as the first target number in the second time period, and the first target number in the second time period is cleared; the second target number in the first time period is set to be the same as the second target number in the second time period, and the second target number in the second time period is cleared, and when there is new statistical data, the current new statistical data is assigned to t2, m2 and n2, and then the threshold can be adjusted again at the preset time point. Finally, the local cache capacity of the business thread can be quickly iterated to the optimal value suitable for the current load.
[0136] In summary, in a high-concurrency storage system, the protocol stack process divides the system global memory into granules and numbers them, and establishes a mapping relationship between the numbers and the memory addresses. In the form of caching numbers rather than memory content, the memory allocation efficiency can be improved. In addition, each business thread dynamically controls the local cache capacity to adapt to memory allocation under different load pressures, and the local cache capacity of the business thread can be quickly iterated to the optimal value suitable for the current load, greatly reducing the competition caused by constantly applying and releasing memory to the system.
[0137] Those skilled in the art can clearly understand that the method according to the above-mentioned embodiments can be realized by means of software on a general hardware platform, and of course, can also be realized by hardware, but in many cases, the former is a better implementation. Based on such understanding, the technical solutions of the present application or the parts that make contributions to the related art can be embodied in the form of a software product, which is stored in a storage medium (such as a ROM (Read-Only Memory), a RAM (Random Access Memory), a magnetic disk, or an optical disk) and includes a plurality of instructions for causing an end device (which can be a mobile phone, a computer, a server, or a network device) to execute the method of each embodiment of the present application.
[0138] In the embodiment, a memory allocation apparatus is also provided, which is configured to implement the above-mentioned embodiments and optional implementation manners, and the description of which has been made above. As used below, the term "module" can be a combination of software and / or hardware that implements a predetermined function. Although the apparatus described in the following embodiments is preferably implemented in software, implementation in hardware or a combination of software and hardware is also possible and contemplated.
[0139] FIG. 7 is a structural block diagram of a memory allocation apparatus according to an embodiment of the present application. As shown in FIG. 7, the apparatus includes a first determination unit 701, a first allocation unit 702, a request unit 703, and a second allocation unit 704.
[0140] The first determination unit 701 is configured to determine a memory request amount corresponding to the memory allocation request of the target service thread when the memory allocation request of the target service thread is received.
[0141] The first allocation unit 702 is configured to allocate the number of the memory corresponding to the first memory amount to the target service thread if the memory request amount is greater than the allocable first memory amount of the local cache corresponding to the target service thread.
[0142] The request unit 703 is configured to apply the first number of memories to the local cache according to a first capacity threshold value corresponding to the local cache, wherein the first capacity threshold value is calculated based on the number of times of applying memories to the target system by the target service thread, the number of times of releasing memories from the target system, and a preset first preset capacity threshold value.
[0143] The second allocation unit 704 is configured to allocate the second number of memories to the target service thread according to the number of the first number of memories in the local cache, so that the target service thread performs service processing based on the allocated memories, wherein the second number is determined based on the difference between the first memory amount and the memory request amount.
[0144] In the memory allocation apparatus provided by the embodiment of the present application, the capacity setting of the local cache is adjusted according to the number of times of memory application and the number of times of memory release of the target service thread to the target system and the first preset capacity threshold during the operation of the target service thread, so that the cache capacity of each service thread is adapted to the current load to maximize the benefit of the cache. Therefore, the problem that the memory allocation efficiency is relatively low in the uneven load service scenario due to the fact that the memory allocation is realized by the memory in the local cache with a fixed capacity in the related art can be solved, and the effect of memory allocation efficiency is achieved.
[0145] In an optional embodiment, in the memory allocation apparatus provided by the embodiment of the present application, the apparatus further comprises a judgment unit configured to judge whether the memory request amount is less than or equal to the first memory amount in the local cache after determining the memory request amount corresponding to the memory allocation request; and a third allocation unit configured to allocate the memory amount corresponding to the memory allocation request to the target service thread according to the number of the memory in the local cache if the memory request amount is less than or equal to the first memory amount in the local cache.
[0146] In an optional embodiment, in the memory allocation apparatus provided by the embodiment of the present application, the apparatus further comprises a first updating unit configured to update the first target number corresponding to the target service thread after applying the first number of memories to the local cache from the target system according to the first capacity threshold, wherein the first target number is the number of times of memory application of the target service thread to the target system.
[0147] In an optional embodiment, in the memory allocation apparatus provided by the embodiment of the present application, the apparatus further comprises a second determining unit configured to determine the memory release amount corresponding to the memory release request of the target service thread after the target service thread performs the service processing based on the allocated memory, if the memory release request of the target service thread is received; and a first release unit configured to release the memory corresponding to the memory release amount according to the memory release amount and the second memory amount that can be allocated in the local cache.
[0148] In an optional embodiment, in the memory allocation apparatus provided by the embodiment of the present application, the first release unit comprises an obtaining subunit configured to obtain the current second capacity threshold of the local cache, wherein the second capacity threshold is calculated based on the number of times of memory application, the number of times of memory release and the first capacity threshold of the target service thread; and a release subunit configured to release the memory corresponding to the memory release amount according to the second capacity threshold, the memory release amount and the second memory amount.
[0149] In an optional embodiment, in the memory allocation apparatus provided by the embodiments of the present application, the releasing subunit comprises: a calculation module configured to calculate a difference between the second capacity threshold and the second memory amount, to obtain a first difference; a judgment module configured to judge whether the memory releasing amount is less than or equal to the first difference; and a releasing module configured to release the memory corresponding to the memory releasing amount to the local cache if the memory releasing amount is less than or equal to the first difference.
[0150] In an optional embodiment, in the memory allocation apparatus provided by the embodiments of the present application, the apparatus further comprises: a second releasing unit configured to, after judging whether the memory releasing amount is less than or equal to the first difference, release the memory corresponding to the first difference of the memory releasing request to the local cache if the memory releasing amount is greater than the first difference; and a third releasing unit configured to release the third amount of memory in the local cache to the target system, and release the fourth amount of memory corresponding to the memory releasing request to the local cache, wherein the third amount is calculated based on the second capacity threshold, and the fourth amount is calculated based on the first difference and the memory releasing amount.
[0151] In an optional embodiment, in the memory allocation apparatus provided by the embodiments of the present application, the apparatus further comprises: a second updating unit configured to, after releasing the third amount of memory in the local cache to the target system, update the second target amount corresponding to the target business thread, wherein the second target amount is the number of times of releasing memory by the target business thread to the target system.
[0152] In an optional embodiment, in the memory allocation apparatus provided by the embodiments of the present application, the apparatus further comprises: a dividing unit configured to, before determining the memory request amount corresponding to the memory allocation request, perform granularity division on the memory corresponding to the target system according to the data reading / writing scene corresponding to the target system, to obtain a plurality of divided memories; and a numbering unit configured to perform numbering processing on the plurality of divided memories, and establish a mapping relationship between the number and the memory address of the memory corresponding to the number, so as to realize memory allocation based on the number.
[0153] In an optional embodiment, in the memory allocation apparatus provided by the embodiments of the present application, the apparatus further comprises: a first obtaining unit configured to, before determining the memory request amount corresponding to the memory allocation request, obtain a first target number of the target service thread in a first time period and a second target number of the target service thread in the first time period at a preset time point, wherein the first target number is the number of times of memory application to the target system by the target service thread in the first time period, and the second target number is the number of times of memory release to the target system by the target service thread in the first time period; a second obtaining unit configured to obtain a first target number of the target service thread in a second time period and a second target number of the target service thread in the second time period; and a calculating unit configured to calculate the first capacity threshold according to the first target number in the first time period, the second target number in the first time period, the first target number in the second time period, the second target number in the second time period, and the first preset capacity threshold.
[0154] In an optional embodiment, in the memory allocation apparatus provided by the embodiments of the present application, the calculating unit comprises: a first calculating sub-unit configured to calculate a first value according to the first target number in the first time period and the second target number in the first time period; a second calculating sub-unit configured to calculate a second value according to the first target number in the second time period and the second target number in the second time period; and a third calculating sub-unit configured to calculate the first capacity threshold according to the first value, the second value, and the first preset capacity threshold.
[0155] In an optional embodiment, in the memory allocation apparatus provided by the embodiments of the present application, the third calculating sub-unit comprises: a first calculating module configured to, if the second value is greater than the first value, calculate the first capacity threshold by using a first calculating device according to the first value, the second value, and the first preset capacity threshold; a second calculating module configured to, if the second value is less than the first value, calculate the first capacity threshold by using a second calculating device according to the first value, the second value, and the first preset capacity threshold; and a setting module configured to, if the second value is equal to the first value, set the first preset capacity threshold as the first capacity threshold.
[0156] In an optional embodiment, in the memory allocation apparatus provided by the embodiments of the present application, the first calculation module comprises: a first calculation submodule configured to calculate a difference between the second value and the first value to obtain a second difference, and perform summation calculation on the second difference and the preset target value to obtain a third value; a second calculation submodule configured to calculate a ratio between the third value and the first value to obtain a first ratio, and calculate a product between the first ratio and the first preset capacity threshold to obtain a first product; and a first determination submodule configured to determine the first capacity threshold based on a numerical relationship between the first product, the first preset capacity threshold and the second preset capacity threshold.
[0157] In an optional embodiment, in the memory allocation apparatus provided by the embodiments of the present application, the second calculation module comprises: a third calculation submodule configured to calculate a difference between the second value and the first value to obtain a second difference, and perform difference calculation on the second difference and the preset target value to obtain a fourth value; a fourth calculation submodule configured to calculate a ratio between the fourth value and the first value to obtain a second ratio, and calculate a product between the second ratio and the first preset capacity threshold to obtain a second product; and a second determination submodule configured to determine the first capacity threshold based on a numerical relationship between the second product, the first preset capacity threshold and the second preset capacity threshold.
[0158] In an optional embodiment, in the memory allocation apparatus provided by the embodiments of the present application, the second determination submodule comprises: a first determination sub-submodule configured to determine the second product as the first capacity threshold if the second product is greater than or equal to the first preset capacity threshold and less than or equal to the second preset capacity threshold; a second determination sub-submodule configured to determine the first preset capacity threshold as the first capacity threshold if the second product is less than the first preset capacity threshold; and a third determination sub-submodule configured to determine the second preset capacity threshold as the first capacity threshold if the second product is greater than the second preset capacity threshold.
[0159] In an optional embodiment, in the memory allocation apparatus provided by the embodiments of the present application, the apparatus further comprises: a first processing unit configured to, after obtaining the first capacity threshold by performing calculation on the first target quantity in the first time period, the second target quantity in the first time period, the first target quantity in the second time period, the second target quantity in the second time period and the first preset capacity threshold, perform zero processing on the first target quantity in the first time period, the second target quantity in the first time period, the first target quantity in the second time period, the second target quantity in the second time period and the first preset capacity threshold if the first capacity threshold is not equal to the first preset capacity threshold.
[0160] In an optional embodiment, in the memory allocation apparatus provided by the embodiments of the present application, the apparatus further comprises: a second processing unit configured to, after obtaining the first capacity threshold value according to the first target quantity in the first time period, the second target quantity in the first time period, the first target quantity in the second time period, the second target quantity in the second time period and the first preset capacity threshold value, if the first capacity threshold value is equal to the first preset capacity threshold value, set the first target quantity in the first time period to be the same as the first target quantity in the second time period, and clear the first target quantity in the second time period; and a third processing unit configured to set the second target quantity in the first time period to be the same as the second target quantity in the second time period, and clear the second target quantity in the second time period.
[0161] It should be noted that the above modules can be implemented by software or hardware, and for the latter, the following implementation manners can be used, but are not limited thereto: the above modules are located in the same processor; or the above modules are located in different processors in any combination.
[0162] The embodiments of the present application further provide a computer nonvolatile readable storage medium, which stores a computer program, and the computer program is configured to execute the steps in any of the above method embodiments when running.
[0163] In an example embodiment, the above computer readable storage medium can include, but is not limited to, a U disk, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk or an optical disk, and various media that can store computer programs.
[0164] The embodiments of the present application further provide an electronic device, which comprises a memory storing a computer program and a processor configured to execute the computer program to perform the steps in any of the above method embodiments.
[0165] In an example embodiment, the above electronic device can further comprise a transmission device connected with the processor and an input and output device connected with the processor.
[0166] The embodiments of the present application further provide a computer program product, which comprises a computer program, and the computer program is executed by a processor to implement the steps in any of the above method embodiments.
[0167] The specific examples in the present embodiment can refer to the examples described in the above embodiments and exemplary embodiments, which will not be repeated here.
[0168] Obviously, those skilled in the art should understand that the modules or steps of the present application described above can be realized by general computing devices, which can be centralized on a single computing device or distributed on a network composed of multiple computing devices, which can be realized by program codes executable by the computing devices, so that they can be stored in storage devices and executed by the computing devices, and in some cases, the steps shown or described can be executed in different order, or they can be respectively manufactured into individual integrated circuit modules, or multiple modules or steps among them can be manufactured into a single integrated circuit module to realize. Thus, the present application is not limited to any specific combination of hardware and software.
[0169] The above only describes optional embodiments of the present application and is not used to limit the present application. For those skilled in the art, the present application can have various modifications and changes. Any modification, equivalent replacement, improvement, etc. within the principles of the present application shall be included in the protection scope of the present application.
Claims
1. A memory allocation method, comprising: determining a memory request amount corresponding to a memory allocation request of a target service thread upon receiving the memory allocation request of the target service thread; allocating a first memory amount corresponding to a number of memories in a local cache to the target service thread if the memory request amount is greater than the first memory amount in the local cache corresponding to the target service thread; applying a first number of memories to the local cache according to a first capacity threshold corresponding to the local cache, wherein the first capacity threshold is calculated based on a number of times of applying memories to a target system by the target service thread, a number of times of releasing memories to the target system, and a first preset capacity threshold; and allocating a second number of memories to the target service thread according to the number of the first number of memories in the local cache, so that the target service thread processes services based on the allocated memories, wherein the second number is determined based on a difference between the first memory amount and the memory request amount. 2.The method of claim 1, wherein after the determining the memory request amount corresponding to the memory allocation request, the method further comprises: determining whether the memory request amount is less than or equal to the first memory amount in the local cache; and allocating the memory request amount corresponding to the memories in the local cache to the target service thread if the memory request amount is less than or equal to the first memory amount in the local cache. 3.The method of claim 1, wherein after the applying the first number of memories to the local cache according to the first capacity threshold, the method further comprises: updating a first target number corresponding to the target service thread, wherein the first target number is a number of times of applying memories to the target system by the target service thread. 4.The method of claim 1, wherein after the allocating the second number of memories to the target service thread according to the number of the first number of memories in the local cache, so that the target service thread processes services based on the allocated memories, the method further comprises: determining a memory release amount corresponding to a memory release request of the target service thread upon receiving the memory release request of the target service thread; and releasing the memory release amount corresponding to the memories according to the memory release amount and a second memory amount in the local cache. 5.The method of claim 4, wherein the releasing the memory release amount corresponding to the memories according to the memory release amount and the second memory amount in the local cache, comprises: obtaining a second capacity threshold of the local cache, wherein the second capacity threshold is calculated based on the number of times of applying memories by the target service thread, the number of times of releasing memories, and the first capacity threshold; and releasing the memory release amount corresponding to the memories according to the second capacity threshold, the memory release amount, and the second memory amount. 6.The method of claim 5, wherein The releasing, according to the second capacity threshold, the memory release amount and the second memory amount, the memory corresponding to the memory release amount comprises: calculating a difference between the second capacity threshold and the second memory amount to obtain a first difference; determining whether the memory release amount is less than or equal to the first difference; if the memory release amount is less than or equal to the first difference, releasing the memory corresponding to the memory release amount into the local cache.
7. The method of claim 6, wherein, after the determination of whether the memory release amount is less than or equal to the first difference, the method further comprises: if the memory release amount is greater than the first difference, releasing the memory corresponding to the first difference of the memory release request into the local cache; releasing a third amount of memory in the local cache to the target system and releasing a fourth amount of memory corresponding to the memory release request to the local cache, wherein the third amount is calculated based on the second capacity threshold, and the fourth amount is calculated based on the first difference and the memory release amount.
8. The method of claim 7, wherein, after the releasing of the third amount of memory in the local cache to the target system, the method further comprises: updating a second target amount corresponding to the target business thread, wherein the second target amount is a number of times of releasing memory by the target business thread to the target system.
9. The method of claim 1, wherein, before the determination of the memory request amount corresponding to the memory allocation request, the method further comprises: performing granularity division on the memory corresponding to the target system according to a data reading / writing scenario corresponding to the target system to obtain a plurality of divided memories; numbering the plurality of divided memories and establishing a mapping relationship between a number and a memory address of the number corresponding memory, so as to realize memory allocation based on the number.
10. The method of claim 1, wherein, before the determination of the memory request amount corresponding to the memory allocation request, the method further comprises: at a preset time point, obtaining a first target number of the target business thread in a first time period and a second target number of the target business thread in the first time period, wherein the first target number is a number of times of applying for memory by the target business thread to the target system in the first time period, and the second target number is a number of times of releasing memory by the target business thread to the target system in the first time period; obtaining a first target number of the target business thread in a second time period and a second target number of the target business thread in the second time period; performing calculation according to the first target number in the first time period, the second target number in the first time period, the first target number in the second time period, the second target number in the second time period and the first preset capacity threshold to obtain the first capacity threshold.
11. The method of claim 10, wherein, The first capacity threshold is calculated according to the first target quantity in the first time period, the second target quantity in the first time period, the first target quantity in the second time period, the second target quantity in the second time period, and the first preset capacity threshold. A first value is calculated according to the first target quantity in the first time period and the second target quantity in the first time period. A second value is calculated according to the first target quantity in the second time period and the second target quantity in the second time period. The first capacity threshold is calculated according to the first value, the second value, and the first preset capacity threshold.
12. The method of claim 11, wherein the first capacity threshold is calculated according to the first value, the second value, and the first preset capacity threshold by: if the second value is greater than the first value, using a first calculation method to calculate the first capacity threshold according to the first value, the second value, and the first preset capacity threshold; if the second value is less than the first value, using a second calculation method to calculate the first capacity threshold according to the first value, the second value, and the first preset capacity threshold; and if the second value is equal to the first value, setting the first preset capacity threshold as the first capacity threshold.
13. The method of claim 12, wherein the first capacity threshold is calculated according to the first value, the second value, and the first preset capacity threshold by the first calculation method by: calculating a second difference value between the second value and the first value, and calculating a third value by summing the second difference value and a preset target value; calculating a first ratio value between the third value and the first value, and calculating a first product between the first ratio value and the first preset capacity threshold; and determining the first capacity threshold based on a numerical relationship between the first product, the first preset capacity threshold, and a second preset capacity threshold.
14. The method of claim 12, wherein the first capacity threshold is calculated according to the first value, the second value, and the first preset capacity threshold by the second calculation method by: calculating a second difference value between the second value and the first value, and calculating a fourth value by subtracting the second difference value from a preset target value; calculating a second ratio value between the fourth value and the first value, and calculating a second product between the second ratio value and the first preset capacity threshold; and determining the first capacity threshold based on a numerical relationship between the second product, the first preset capacity threshold, and a second preset capacity threshold.
15. The method of claim 14, wherein the first capacity threshold is calculated according to the first value, the second value, and the first preset capacity threshold by the second calculation method by: The determining the first capacity threshold based on the numerical relationship between the second product and the first preset capacity threshold and the second preset capacity threshold comprises: if the second product is greater than or equal to the first preset capacity threshold and less than or equal to the second preset capacity threshold, the second product is determined as the first capacity threshold; if the second product is less than the first preset capacity threshold, the first preset capacity threshold is determined as the first capacity threshold; if the second product is greater than the second preset capacity threshold, the second preset capacity threshold is determined as the first capacity threshold.
16. The method of claim 13, wherein, after the calculating the first capacity threshold according to the first target quantity in the first time period, the second target quantity in the first time period, the first target quantity in the second time period, the second target quantity in the second time period and the first preset capacity threshold, the method further comprises: if the first capacity threshold is not equal to the first preset capacity threshold, the first target quantity in the first time period, the second target quantity in the first time period, the first target quantity in the second time period, the second target quantity in the second time period and the first preset capacity threshold are cleared.
17. The method of claim 13, wherein, after the calculating the first capacity threshold according to the first target quantity in the first time period, the second target quantity in the first time period, the first target quantity in the second time period, the second target quantity in the second time period and the first preset capacity threshold, the method further comprises: if the first capacity threshold is equal to the first preset capacity threshold, the first target quantity in the first time period is set to be the same as the first target quantity in the second time period, and the first target quantity in the second time period is cleared; and the second target quantity in the first time period is set to be the same as the second target quantity in the second time period, and the second target quantity in the second time period is cleared.
18. A memory allocation apparatus, comprising: a first determining unit configured to determine a memory request amount corresponding to a memory allocation request of a target service thread when the memory allocation request is received; a first allocating unit configured to allocate a number of memory corresponding to a first memory amount of a local cache corresponding to the target service thread to the target service thread if the memory request amount is greater than the first memory amount; a requesting unit configured to apply a first quantity of memory to the local cache according to a first capacity threshold corresponding to the local cache, wherein the first capacity threshold is calculated based on a number of times of applying memory to a target system by the target service thread, a number of times of releasing memory to the target system and a first preset capacity threshold. A second allocation unit is configured to allocate a second amount of memory to the target service thread according to a number of the first amount of memory in the local cache, so that the target service thread performs service processing based on the allocated memory, wherein the second amount is determined based on a difference between the first amount of memory and the memory request amount. 19.An electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein, the processor implements the steps of the method in any one of claims 1 to 17 when executing the computer program. 20.A computer program product, comprising a computer program, wherein, the computer program implements the steps of the method in any one of claims 1 to 17 when executed by a processor.
Citation Information
Patent Citations
Data caching method and device, computer equipment and storage medium
CN114721844A
Memory management method and device, medium and electronic equipment
CN115599707A
Memory management method and device for computing chip and computing chip
CN117311997A
Memory allocation method and device
CN118897737A