Memory management method and device, equipment and medium

By using multiple memory boxes of different sizes in memory management to cluster memory blocks, the fragmentation problem during memory application in the prior art is solved, and the memory processing efficiency is improved.

CN119597450APending Publication Date: 2025-03-11BEIJING QIYI CENTURY SCI & TECH CO LTD
View PDF 0 Cites 2 Cited by

Patent Information

Application Number
CN202411517853.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-10-29
Publication Date
2025-03-11

AI Technical Summary

Technical Problem

In the prior art, the memory requester applies for memory to the operating system through a system call method, which has fragmentation problems and memory processing efficiency needs to be improved.

Method used

The memory blocks of each memory size are clustered through multiple memory boxes of different size ranges to form memory blocks of fixed memory sizes, and then respond to memory requests in units of memory blocks.

Benefits of technology

Reduces fragmentation problems, improves memory processing efficiency, and reduces the system overhead of applying for memory.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119597450A_ABST
    Figure CN119597450A_ABST
Patent Text Reader

Abstract

The invention relates to a memory management method and device, equipment and a medium, and the method comprises the steps: obtaining a memory application request sent by memory requester equipment for a target resource; analyzing the memory application request to obtain a to-be-applied first memory size; a target memory box of which the size range comprises a first memory size is determined from a plurality of memory boxes of different size ranges, the memory boxes are obtained by clustering memory blocks of each memory size specification, and each memory box comprises a preset number of memory blocks of which the memory sizes are within the size range corresponding to the memory box; a target memory block matched with the first memory size is determined in all memory blocks of the target memory box, and the memory size of the target memory block is larger than or equal to the first memory size; and in response to the memory application request, allocating the target memory block to a target resource of the memory requester device. According to the technical scheme, the fragmentation problem can be reduced, and the memory processing efficiency is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the field of computer technology, and in particular to a memory management method, device, equipment and medium. Background Art

[0002] When an application is running, it needs to apply for memory to meet the requirements of starting and running the application.

[0003] In the related art, a memory requester applies for memory from an operating system through a system call method. This method has a fragmentation problem and the memory processing efficiency needs to be improved. Summary of the invention

[0004] In order to solve the above technical problems, the present disclosure provides a memory management method, apparatus, device and medium.

[0005] In a first aspect, an embodiment of the present disclosure provides a memory management method, including:

[0006] Obtaining a memory application request sent by a memory requesting device for a target resource;

[0007] Parsing the memory application request to obtain a first memory size to be applied for; the first memory size is a memory size declared to be applied for the target resource;

[0008] Determine, from a plurality of memory boxes of different size ranges, a target memory box whose size range includes the first memory size; the memory box is obtained by clustering memory blocks of each memory size specification, and each of the memory boxes includes a preset number of memory blocks whose memory sizes are within the size range corresponding to the memory box;

[0009] Determine a target memory block matching the first memory size among all memory blocks of the target memory box; wherein the memory size of the target memory block is greater than or equal to the first memory size;

[0010] The memory application request is responded to according to the target memory block to allocate the target memory block to the target resource of the memory requester device.

[0011] In a second aspect, an embodiment of the present disclosure provides a memory management device, including:

[0012] An acquisition module, used to acquire a memory application request sent by a memory requesting device for a target resource;

[0013] A parsing module, configured to parse the memory application request to obtain a first memory size to be applied for; the first memory size is a memory size declared to be applied for the target resource;

[0014] a determination module, configured to determine, from a plurality of memory boxes of different size ranges, a target memory box whose size range includes the first memory size; the memory box is obtained by clustering memory blocks of each memory size specification, and each of the memory boxes includes a preset number of memory blocks whose memory sizes are within the size range corresponding to the memory box;

[0015] A matching module, configured to determine a target memory block matching the first memory size among all memory blocks of the target memory box; wherein the memory size of the target memory block is greater than or equal to the first memory size;

[0016] A response module is used to respond to the memory application request according to the target memory block to allocate the target memory block to the target resource of the memory requester device.

[0017] In a third aspect, an embodiment of the present disclosure provides an electronic device, comprising: a processor; a memory for storing executable instructions of the processor; the processor is used to read the executable instructions from the memory and execute the instructions to implement the memory management method described in the first aspect above.

[0018] In a fourth aspect, an embodiment of the present disclosure provides a computer-readable storage medium, wherein the storage medium stores a computer program, and when the computer program is executed by a processor, the memory management method described in the first aspect is implemented.

[0019] Compared with the prior art, the technical solution provided by the disclosed embodiment has the following advantages: the memory management device stores memory blocks of corresponding memory sizes through multiple memory boxes of different size ranges. When applying for memory, the memory requester sends a memory application request for the target resource. The memory management device parses the memory application request to obtain the first memory size to be applied for, obtains the target memory block matching the first memory size from the memory box, and allocates the target memory block to the target resource. Thus, memory blocks of each memory size specification are clustered through multiple memory boxes of different size ranges to form memory blocks of fixed memory size, and then respond to memory application requests in units of memory blocks, thereby reducing fragmentation problems and improving efficiency during memory processing. BRIEF DESCRIPTION OF THE DRAWINGS

[0020] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present disclosure and, together with the description, serve to explain the principles of the present disclosure.

[0021] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.

[0022] Figure 1 A flowchart of a memory management method provided by an embodiment of the present disclosure;

[0023] Figure 2 A flowchart of another memory management method provided by an embodiment of the present disclosure;

[0024] Figure 3 A schematic diagram of a memory application process provided by an embodiment of the present disclosure;

[0025] Figure 4 A schematic diagram of a memory recovery process provided by an embodiment of the present disclosure;

[0026] Figure 5 A schematic diagram of the structure of a memory management device provided by an embodiment of the present disclosure. DETAILED DESCRIPTION

[0027] In order to more clearly understand the above-mentioned objectives, features and advantages of the present disclosure, the scheme of the present disclosure will be further described below. It should be noted that the embodiments of the present disclosure and the features in the embodiments can be combined with each other without conflict.

[0028] In the following description, many specific details are set forth to facilitate a full understanding of the present disclosure, but the present disclosure may also be implemented in other ways different from those described herein; it is obvious that the embodiments in the specification are only part of the embodiments of the present disclosure, rather than all of the embodiments.

[0029] The following first defines some technical terms involved in this disclosure:

[0030] Memory box: A container for storing memory blocks. The container is composed of code and data in the program.

[0031] Memory block: A continuous section of memory. The memory management device manages the memory in units of memory blocks. The minimum memory size of the memory blocks managed by the memory management device is the unit memory block size.

[0032] Memory page: The operating system manages memory in units of memory pages. The memory size of a memory page is the unit memory page size.

[0033] Memory page box: used to record memory pages requested from the operating system through system call methods. The recorded contents include memory size and memory address. System call methods include, for example, mmap (a memory mapping method).

[0034] Figure 1 This is a flow chart of a memory management method provided in an embodiment of the present disclosure. The method provided in an embodiment of the present disclosure can be executed by a memory management device, which can be implemented in software and / or hardware and can be integrated on any electronic device with computing capabilities.

[0035] like Figure 1 As shown, the memory management method provided by the embodiment of the present disclosure may include:

[0036] Step 101: Obtain a memory application request sent by a memory requesting device for a target resource.

[0037] In this embodiment, a memory management device is set up. The memory management device applies for memory from the operating system through a system call method and caches the applied memory. When applying for memory, the memory applicant can send a memory application request for a target resource to the memory management device. The memory management device responds to the memory application request and allocates memory to the target resource.

[0038] The target resource includes a resource that forms a read-write interaction with the memory, and the target resource is, for example, an application.

[0039] Step 102: parse the memory application request to obtain a first memory size to be applied for.

[0040] In this embodiment, when the memory management device receives a memory application request, the memory management device parses the memory application request to obtain a first memory size to be applied for, wherein the first memory size is a memory size declared to be applied for the target resource.

[0041] In one embodiment of the present disclosure, a memory application request is parsed to obtain a memory size carried in the memory application request, and the memory size carried in the memory application request is used as the first memory size. The memory size carried in the memory application request indicates the memory size actually occupied by the target resource.

[0042] In one embodiment of the present disclosure, a memory application request is parsed to obtain a second memory size carried in the memory application request, and a first memory size is determined based on the second memory size and the unit memory block size. The second memory size represents the memory size actually occupied by the target resource. For ease of management, the first memory size is an integer multiple of the unit memory block size, the first memory size is greater than or equal to the second memory size, and the difference between the first memory size and the second memory size is the smallest. The above-mentioned implementation method for determining the first memory size is expressed by the following formula: A = (floor ((R-1) ÷ BS0) + 1) × BS0, wherein R is the second memory size, A is the first memory size, the floor function is used to round down, and BS0 is the unit memory block size.

[0043] As an example, the unit memory block size is 16 bytes, the second memory size is 31 bytes, and the first memory size is 32 bytes.

[0044] Step 103 , determining a target memory box whose size range includes the first memory size from among a plurality of memory boxes of different size ranges.

[0045] In this embodiment, the memory management device includes a plurality of memory boxes of different size ranges, and the memory boxes are obtained by clustering memory blocks of each memory size specification. When the memory management device applies for memory from the operating system, the memory blocks of each memory size specification are clustered to form memory blocks of a fixed memory size, and each memory box includes a preset number of memory blocks whose memory sizes are within the size range corresponding to the memory box. Optionally, the size ranges of the memory boxes do not have an intersection.

[0046] The size range of the memory box may be a fixed value or an interval. For example, if the size range of the memory box is 16 bytes, the memory box is used to store memory blocks with a memory size of 16 bytes. For another example, if the size range of the memory box is (2048, 2176], the memory box is used to store memory blocks with a memory size greater than 2048 bytes and less than or equal to 2176 bytes.

[0047] After obtaining the first memory size, multiple size ranges of multiple memory boxes are queried, a target size range including the first memory size is determined from the multiple size ranges, and a memory box corresponding to the target size range is determined as a target memory box.

[0048] As an example, the first memory size is 64 bytes, and the size ranges of the multiple memory boxes are 16 bytes, 32 bytes, 48 ​​bytes, and 64 bytes, respectively. Then, the memory box with a size range of 64 bytes is determined as the target memory box.

[0049] Step 104: Determine a target memory block matching the first memory size among all memory blocks of the target memory box.

[0050] The memory size of the target memory block is greater than or equal to the first memory size.

[0051] In this embodiment, among all the memory blocks of the target memory box, a memory block having a memory size greater than or equal to the first memory size is determined as the target memory block.

[0052] As an example, the size range of the target memory box is a fixed value, and any memory block is obtained from all memory blocks stored in the target memory box as the target memory block.

[0053] As another example, the size range of the target memory box is an interval, and any memory block having a memory size greater than or equal to a first memory size is determined from all memory blocks stored in the target memory box as the target memory block.

[0054] Step 105 , responding to the memory application request according to the target memory block to allocate the target memory block to the target resource of the memory requesting device.

[0055] In this embodiment, after determining the target memory block matching the first memory size, the target memory block is returned to the memory requester so that the target resource can perform read and write interaction with the memory.

[0056] According to the technical solution of the embodiment of the present disclosure, the memory management device stores memory blocks of corresponding memory sizes through multiple memory boxes of different size ranges. When applying for memory, the memory requester sends a memory application request for the target resource. The memory management device parses the memory application request to obtain the first memory size to be applied for, obtains the target memory block matching the first memory size from the memory box, and allocates the target memory block to the target resource. Thus, memory blocks of each memory size specification are clustered through multiple memory boxes of different size ranges to form memory blocks of fixed memory size, and then respond to memory application requests in units of memory blocks, thereby reducing fragmentation problems and improving the efficiency of memory application, memory recovery and other processing. In addition, requesting memory from the memory management device can be implemented based on user-state functions. Compared with the method of requesting memory from the operating system, each time a system call is executed, a switch from user state -> kernel state -> user state will be performed. The system overhead of the above switching process is greater than the user-state function call. The present solution only needs to call the user-state function to implement memory request, thereby reducing the interaction between the program and the operating system, reducing the system overhead of memory request, and improving the processing efficiency of memory request. Furthermore, the memory boxes are first matched according to the size range, and then the target memory blocks are matched from the memory boxes, thereby reducing the number of matches and further improving the processing efficiency of memory request.

[0057] Based on the above embodiments, Figure 2 A flowchart of another memory management method provided by an embodiment of the present disclosure is shown in FIG. Figure 2 As shown, determining a target memory block matching a first memory size among all memory blocks of a target memory box comprises the following steps:

[0058] Step 201: If the target memory box is the first memory box, any memory block is obtained from all memory blocks of the target memory box as a target memory block.

[0059] In this embodiment, the memory size of the memory blocks included in each memory box is an integer multiple of the unit memory block size, and there is no intersection between the size ranges of different memory boxes. The multiple memory boxes with different size ranges include at least one first memory box and at least one second memory box. The first memory box stores memory blocks with consistent memory size specifications, and the second memory box stores memory blocks with multiple memory size specifications. For example, the size range of the first memory box is a fixed value, and the size range of the second memory box is an interval. Optionally, the size range (fixed value) of any first memory box is less than the lower limit value of the size range (interval) of any second memory box.

[0060] As an example, the plurality of memory boxes of different size ranges include a plurality of first memory boxes, and the size ranges of the plurality of first memory boxes are all different, wherein the memory size of the memory block stored in the i-th first memory box is BS i =BS0×i, BS0 is the unit memory block size, and the maximum value of the size range (fixed value) of the plurality of first memory boxes is recorded as BS_max. The plurality of memory boxes of different size ranges also include a plurality of second memory boxes, wherein the size range upper limit value of the i-th second memory box is LBS i _max = LBS i-1 _max+k1×BS0, the lower limit is LBS i-1 _max, k1 is a preset value, optionally, LBS0_max=BS_max.

[0061] Step 202: If the target memory box is the second memory box, obtain at least one first candidate memory block from all memory blocks of the target memory box.

[0062] The memory size of the first candidate memory block is greater than or equal to the first memory size.

[0063] Step 203: determine a target memory block from the first candidate memory blocks.

[0064] In this embodiment, a first difference between the memory size of each first candidate memory block and the first memory size is calculated, and a first candidate memory block with the smallest first difference is determined as the target memory block.

[0065] Optionally, the size ranges of the plurality of second memory boxes are all different, and the difference between the upper limit value and the lower limit value of the size range may be the same or different.

[0066] As an example, the plurality of memory boxes of different size ranges include a plurality of second memory boxes, and the plurality of second memory boxes are divided into two categories, namely nl first-category second memory boxes and nh second-category second memory boxes. For nl first-category second memory boxes, the size range upper limit value of the i-th first-category second memory box is LBS i _max = LBS i-1 _max+k1×BS0, the lower limit is LBS i-1 _max, k1 is a preset value, LBS0_max = BS_max, BS0 is the unit memory block size, BS_max is the maximum size range of the first memory box. nh second-class second memory boxes are divided into multiple groups, where the upper limit of the size range of the i-th second-class second memory box is HBS i _max = HBS i-1 _max+m×k2×BS0, m means it belongs to the mth group, and the lower limit is HBS i-1 _max, k2 is a preset value, optionally, the upper limit value of the size range of the nh-th second-type second memory box is a preset value, HBS0_max=LBS nl _max.

[0067] Step 204: If the target memory block is not obtained in the first memory box and the second memory box, determine the target memory block from the third memory box.

[0068] In this embodiment, the plurality of memory boxes of different size ranges further include a third memory box, and the third memory box is used to store memory blocks that do not conform to the size range of the first memory box and the size range of the second memory box. Optionally, the second memory box is provided with a capacity upper limit, and when the number of memory blocks stored in the second memory box reaches the capacity upper limit, the third memory box is further used to store memory blocks whose memory size is within the size range of any second memory box that reaches the capacity upper limit.

[0069] In one embodiment of the present disclosure, if the target memory box is the third memory box, a preset number of queries are performed in the third memory box to obtain at least one second candidate memory block, wherein the memory size of the second candidate memory block is greater than or equal to the first memory size, and then the target memory block is determined from the second candidate memory blocks, for example, the second difference between the memory size of each second candidate memory block and the first memory size is calculated, and the second candidate memory block with the smallest second difference is determined as the target memory block. The preset number of times can be set as needed, and each query obtains a memory block in the third memory box, and the query is stopped after the preset number of queries.

[0070] The following is an example to illustrate the memory application process.

[0071] For example, the unit memory block size is 16 bytes, the maximum memory size of the memory block is 524288 bytes, and the memory management device includes 128 first memory boxes, which are used to store memory blocks with memory sizes of 16 bytes, 32 bytes, 48 ​​bytes, 64 bytes, ..., 1024 bytes, and 2048 bytes, respectively. The memory management device includes 64 first-class second memory boxes and 32 second-class second memory boxes. The size ranges of the first-class second memory boxes are (2048, 2176], (2176, 2304], (2304, 2432], ..., (10112, 10240]; the second-class second memory boxes are divided into four groups, among which the size ranges of the first group are (10240, 14336], (14336, 18432], ..., (38912, 43008], and the size ranges of the second group are (43008, 51200], (51200, 59392], ..., (100352, 108544], and the third and fourth groups can refer to the above steps and will not be repeated here.

[0072] Parse the memory application request to obtain the first memory size to be applied for. If the first memory size falls within the size range of a first memory box, determine whether there is a memory block in the first memory box. If there is a memory block in the first memory box, return any memory block as the target memory block to respond to the memory application request; if the first memory size falls within the size range of a first-type second memory box, determine whether there is a candidate memory block with a memory size greater than or equal to the first memory size in the first-type second memory box. If there is a candidate memory block, determine the candidate memory block with the smallest difference between the memory size and the first memory size from the candidate memory blocks as the target memory block to respond to the memory application request; if the first memory size falls within the size range of a second-type second memory box, determine whether there is a candidate memory block with a memory size greater than or equal to the first memory size in the first-type second memory box. If there is a candidate memory block, determine the candidate memory block with the smallest difference between the memory size and the first memory size from the candidate memory blocks as the target memory block to respond to the memory application request; Within the size range of the memory box, determine whether there is a candidate memory block with a memory size greater than or equal to the first memory size in the second memory box of the second category; if there is a candidate memory block, determine the candidate memory block with the smallest difference between its memory size and the first memory size from the candidate memory blocks as the target memory block to respond to the memory application request; if the target memory block is not obtained in the aforementioned step, query the third memory box n times, where n is a positive integer; if the candidate memory blocks with a memory size greater than or equal to the first memory size are obtained through n queries, determine the candidate memory block with the smallest difference between its memory size and the first memory size from the candidate memory blocks as the target memory block to respond to the memory application request.

[0073] In the disclosed embodiment, the processing efficiency of memory application can be improved, and the memory size of the memory block is an integer multiple of the unit memory block size, which is conducive to memory recovery and memory redistribution.

[0074] Based on the above embodiments, Figure 3A schematic diagram of a memory application process provided by an embodiment of the present disclosure, such as Figure 3 As shown, after determining, from among a plurality of memory boxes of different size ranges, a target memory box whose size range includes the first memory size, the following steps are also included:

[0075] Step 301: if there is no memory block with a memory size greater than or equal to a first memory size in the target memory box, determine other memory boxes from memory boxes with different size ranges.

[0076] During the memory application process, there may not be a memory block with a memory size greater than or equal to the first memory size in the target memory box. For example, when the size range of the target memory box is a fixed value, there is no memory block in the target memory box. For another example, when the size range of the target memory box is an interval, the memory sizes of the memory blocks in the target memory box are all smaller than the first memory size. In this case, there is no target memory block matching the first memory size in the target memory box. In this embodiment, other memory boxes are determined from memory boxes with multiple different size ranges, wherein any value within the size range of other memory boxes is greater than any value within the size range of the target memory box, and then, among all the memory blocks in other memory boxes, a target memory block matching the first memory size is determined.

[0077] As an example, the multiple memory boxes include memory boxes with a size range of 512 bytes, memory boxes with a size range of 1024 bytes, memory boxes with a size range of 2048 bytes, and memory boxes with a size range of (2048, 2176]; the first memory size is 1024 bytes, and the size range of the target memory box is 1024 bytes. Then, it is determined that the other memory boxes include memory boxes with a size range of 2048 bytes and memory boxes with a size range of (2048, 2176], and further, a target memory block with a memory size greater than or equal to 1024 bytes is determined in the above other memory boxes.

[0078] Optionally, multiple memory boxes are sorted in ascending order of size range. If there is no target memory block matching the first memory size in the target memory box, the next memory box is obtained in sequence according to the sorting, and the target memory block matching the first memory size is determined from the next memory box.

[0079] As an example, the first memory size is 32 bytes, the size ranges of the multiple memory boxes are 16 bytes, 32 bytes, 48 ​​bytes, and 64 bytes respectively, the size range of the target memory box is 32 bytes, and when the target memory block does not exist in the target memory box, the target memory block is determined from the memory box with a size range of 48 bytes. Furthermore, when the target memory block does not exist in the memory box with a size range of 48 bytes, the target memory block is determined from the memory box with a size range of 64 bytes.

[0080] Step 302: If there is no memory block with a memory size greater than or equal to the first memory size in other memory boxes, apply for memory from the operating system to obtain a target memory block.

[0081] After determining other memory boxes from a plurality of memory boxes of different size ranges, there may not be any memory blocks whose memory size is greater than or equal to the first memory size in other memory boxes. For example, in the initial state, all memory boxes of the memory management device may be empty. At this time, there is no target memory block matching the first memory size in all memory boxes. In this embodiment, the memory management device applies for memory from the operating system through a system call method, wherein the memory size of the applied memory is greater than or equal to the first memory size.

[0082] Optionally, the memory management device determines the third memory size according to the first memory size and the unit memory page size, and then, through the system call method, applies to the operating system for a memory block of the third memory size, so as to determine the memory block of the third memory size as a target memory block matching the first memory size. The third memory size is greater than or equal to the first memory size. For ease of management, the third memory size is an integer multiple of the unit memory page size, and the difference between the third memory size and the first memory size is the smallest. The above-mentioned implementation method for determining the third memory size is expressed by the following formula: M = (floor ((A–1) ÷ PS0) + 1) × PS0, wherein M is the third memory size, A is the first memory size, the floor function is used to round down, and PS0 is the unit memory page size.

[0083] As an example, the unit memory page size is 4kB, if 1kB is 1024 bytes, the first memory size is 1024 bytes, and the third memory size is 4096 bytes.

[0084] Optionally, the memory management device may apply for memory from the operating system upon receiving a memory application request, or may apply for memory from the operating system in advance and divide the applied memory into multiple memory blocks, which are stored in memory boxes according to the memory sizes.

[0085] In the disclosed embodiment, if the target memory block is not obtained in the memory box, mmap is called to apply for memory from the operating system. When applying for memory from the operating system, the third memory size is calculated. The calculation process refers to the aforementioned embodiment and will not be repeated here. When the memory block of the third memory size is applied for, the memory size and the memory address are recorded in the memory page box, and the memory block of the third memory size is used as the target memory block to respond to the memory application request.

[0086] Furthermore, after the target memory block is determined in the aforementioned step, if the memory size of the target memory block is larger than the first memory size, the target memory block may be segmented and recycled to improve resource utilization. Figure 4 A schematic diagram of a memory recovery process provided by an embodiment of the present disclosure, such as Figure 4 As shown, the following steps are included:

[0087] Step 401: if the difference between the memory size of the target memory block and the first memory size is greater than or equal to a preset threshold, the target memory block is divided into a first sub-memory block and a second sub-memory block.

[0088] The memory size of the first sub-memory block is the first memory size, and the memory size of the second sub-memory block is the difference.

[0089] In this embodiment, when the difference between the memory size of the target memory block and the first memory size is greater than or equal to a preset threshold, it is determined to perform a split and recycle operation, and the target memory block is split into a first sub-memory block and a second sub-memory block, the first sub-memory block is used to respond to a memory application request, and the second sub-memory block is used for recycling. The preset threshold can be set as needed, for example, the preset threshold is 8×BS0, and BS0 is the unit memory block size. Optionally, the memory size of the first sub-memory block and the memory size of the second sub-memory block are both integer multiples of the unit memory block size.

[0090] In which, in response to the memory application request, the first sub-memory block is allocated to the target resource.

[0091] In one embodiment of the present disclosure, after responding to a memory application request according to a first sub-memory block, when a memory recovery request for the first memory block is detected, a recovery memory box whose size range includes the memory size of the first memory block is determined from memory boxes of multiple different size ranges, and the first sub-memory block is stored in the memory box. Optionally, when the memory size of the first sub-memory block is equal to the size range (fixed value) of a first memory box, the first sub-memory block is stored in the first memory box, or, when the memory size of the first sub-memory block falls within the size range (interval) of a second memory box, and the number of memory blocks in the second memory box does not reach the upper capacity limit, the first sub-memory block is stored in the second memory box, otherwise, the first sub-memory block is stored in the third memory box. It should be noted that the above explanation of the recovery of the first sub-memory block is also applicable to the recovery of the target memory block when responding to the memory application request according to the target memory block.

[0092] Step 402, reclaiming the second sub-memory block through a plurality of memory boxes of different size ranges.

[0093] In this embodiment, a recycled memory box having a size range that includes the difference is determined from memory boxes of multiple different size ranges, and then the second sub-memory block is stored in the recycled memory box.

[0094] Optionally, the memory size of the second sub-memory block is matched with the size range of the first memory box and the size range of the second memory box to determine the first memory box or the second memory box whose size range includes the memory size of the second sub-memory block. When the memory size of the second sub-memory block is equal to the size range (fixed value) of a certain first memory box, the second sub-memory block is stored in the first memory box, or, when the memory size of the second sub-memory block falls within the size range (interval) of a certain second memory box and the number of memory blocks in the second memory box does not reach the upper capacity limit, the second sub-memory block is stored in the second memory box, otherwise, the second sub-memory block is stored in a third memory box.

[0095] Therefore, when applying for memory from the memory management device, some memory blocks can be recovered and stored in the memory box for subsequent memory applications, further reducing the number of memory applications from the operating system, reducing the system overhead of memory applications, and improving the processing efficiency of memory applications.

[0096] Step 403: merge the memory blocks in the memory box to generate a merged memory block.

[0097] In this embodiment, after the second sub-memory block is recovered through multiple memory boxes of different size ranges, the memory blocks in the memory box may be merged. Optionally, a memory block to be merged is determined from the memory box, and the memory block to be merged is merged to generate a merged memory block. The memory block to be merged is any memory block in the memory box.

[0098] In one embodiment of the present disclosure, for each memory block to be merged, adjacent memory blocks of the memory block to be merged are determined from a plurality of memory boxes of different size ranges, and the adjacent memory blocks are adjacent to the memory addresses of the memory block to be merged, and then the memory block to be merged and the adjacent memory blocks are merged to generate a merged memory block. For any memory block, if the adjacent memory blocks of the memory block exist in all memory boxes, the memory block is merged with the adjacent memory blocks to generate a new memory block, and the merged adjacent memory blocks are deleted from the memory box, and the above steps are repeated for the new memory block until there are no adjacent memory blocks, and a merged memory block is obtained.

[0099] As an example, taking the third memory box as an example, the memory blocks in the third memory box are used as memory blocks to be merged, and for each memory block to be merged, the adjacent memory blocks of the memory block to be merged are determined from all memory boxes, wherein the adjacent memory blocks are adjacent to the memory addresses of the memory blocks to be merged, and then, if there are adjacent memory blocks of the memory block to be merged, the memory block to be merged and the adjacent memory blocks are merged to generate a new memory block to be merged, and the above steps are repeated until there are no adjacent memory blocks, and a merged memory block is obtained, and the merged memory block can be stored in the third memory box.

[0100] In one embodiment of the present disclosure, for multiple memory blocks to be merged, the sum of the memory sizes of the multiple memory blocks to be merged is obtained. If the sum of the memory sizes is within the size range of the memory box, the multiple memory blocks to be merged are merged to generate a merged memory block. Then, the merged memory block can be stored in a memory box of a corresponding size range. Among them, multiple memory blocks to be merged can be obtained from the same memory box, or multiple memory blocks to be merged can be obtained from different memory boxes. For the sum of the memory sizes of multiple memory blocks to be merged, the multiple memory blocks to be merged can be merged when the sum of the memory sizes is within the size range of the current memory box, or when the sum of the memory sizes is within the size range of any memory box, and there is no limitation here.

[0101] As an example, taking the second memory box as an example, multiple memory blocks to be merged are obtained from the second memory box. If the sum of the memory sizes of the multiple memory blocks to be merged is within the size range of the second memory box, the multiple memory blocks to be merged are merged to generate a merged memory block, and the merged memory block is stored in the second memory box.

[0102] Among them, a memory block merging condition can be set, and the memory block merging process is performed when the memory block merging condition is met. Optionally, the memory block merging condition includes: counting the number of times the memory block is recycled to the memory box, and when the count reaches a preset number of times, the memory block merging condition is met, for example, each time the memory block is stored in the third memory box, the count is increased by one, and when the count reaches a preset number of times, the memory block merging condition is met, the memory block merging operation is started, and the counting is restarted.

[0103] Step 404: reclaim the merged memory block to the operating system.

[0104] In this embodiment, after the merged memory block is generated, it is determined whether the merged memory block and the pre-recorded memory page meet the consistency condition. If the merged memory block and the pre-recorded memory page meet the consistency condition, the merged memory block is recycled to the operating system.

[0105] Optionally, based on the memory size and memory address of the merged memory block, it is determined whether the merged memory block and the pre-recorded memory page meet a consistency condition. For example, the consistency condition includes: the memory size and memory address of the merged memory block are consistent with the memory size and memory address of a certain piece of data recorded in the memory page box, or the memory size and memory address of the merged memory block include the memory size and memory address of a certain piece of data recorded in the memory page box.

[0106] As an example, taking the third memory box as an example, when the memory block merging condition is met, each memory block in the third memory box is checked in turn, and the aforementioned memory block merging process is performed on each memory block to obtain a merged memory block. Furthermore, after the merging process, for each memory block in the third memory box, it is determined whether the memory block meets the consistency condition. If the consistency condition is met, the memory block is recycled to the operating system, and the memory block is deleted from the third memory box, and the corresponding record that meets the consistency condition in the memory page box is deleted. In this way, the memory can be recycled to the operating system, improving resource utilization.

[0107] Currently, there is an implementation method for managing memory read and write permissions on the operating system side, but there is no implementation method for controlling read and write permissions by a memory management device. In order to improve the security of memory management, in one embodiment of the present disclosure, in response to receiving a permission update request for a target memory page, the memory address of the target memory page is determined, and then, based on the mapping relationship between the memory block and the memory address, at least one memory block corresponding to the memory address of the target memory page is determined, and based on the permission update request, the read and write permissions of the at least one memory block are updated.

[0108] In this embodiment, when changing the memory read and write permissions, mprotect is called to set the read and write permissions for the specified memory page in the memory page box, and the memory block corresponding to the memory address is determined according to the memory address of the specified memory page recorded in the memory page box. The memory block can be a memory block stored in the memory box or a memory block that has been applied for. By determining the memory block corresponding to the memory address, the read and write permissions of the memory block can be changed, thereby improving the security of memory management.

[0109] Figure 5 A schematic diagram of the structure of a memory management device provided by an embodiment of the present disclosure is shown in FIG. Figure 5 As shown, the memory management device includes: an acquisition module 51, a parsing module 52, a determination module 53, a matching module 54, and a response module 55.

[0110] An acquisition module 51 is used to acquire a memory application request sent by a memory requesting device for a target resource;

[0111] The parsing module 52 is used to parse the memory application request to obtain a first memory size to be applied for; the first memory size is the memory size declared to be applied for the target resource;

[0112] a determination module 53, configured to determine, from a plurality of memory boxes of different size ranges, a target memory box whose size range includes the first memory size; a memory box is obtained by clustering memory blocks of each memory size specification, and each memory box includes a preset number of memory blocks whose memory sizes are within the size range corresponding to the memory box;

[0113] A matching module 54, configured to determine a target memory block matching the first memory size among all memory blocks of the target memory box; wherein the memory size of the target memory block is greater than or equal to the first memory size;

[0114] The response module 55 is used to respond to the memory application request according to the target memory block to allocate the target memory block to the target resource of the memory requesting device.

[0115] In one embodiment of the present disclosure, the parsing module 52 is specifically used to:

[0116] Parse the memory application request to obtain a second memory size carried in the memory application request; the second memory size indicates the memory size actually occupied by the target resource;

[0117] The first memory size is determined according to the second memory size and the unit memory block size; wherein the first memory size is an integer multiple of the unit memory block size, the first memory size is greater than or equal to the second memory size, and the difference between the first memory size and the second memory size is the smallest.

[0118] In one embodiment of the present disclosure, the memory size of the memory blocks included in each memory box is an integer multiple of the unit memory block size, and there is no intersection between the size ranges of different memory boxes. The multiple memory boxes with different size ranges include at least one first memory box and at least one second memory box. The first memory box stores memory blocks with the same memory size specification, and the second memory box stores memory blocks with multiple memory size specifications. The matching module 54 is specifically used to:

[0119] If the target memory box is the first memory box, obtain any memory block from all the memory blocks of the target memory box as the target memory block; if the target memory box is the second memory box, obtain at least one first candidate memory block from all the memory blocks of the target memory box; the memory size of the first candidate memory block is greater than or equal to the first memory size; determine the target memory block from the first candidate memory blocks.

[0120] In one embodiment of the present disclosure, a target memory block is determined from the first candidate memory blocks, including: calculating a first difference between the memory size of each first candidate memory block and the first memory size, and determining the first candidate memory block with the smallest first difference as the target memory block.

[0121] In one embodiment of the present disclosure, the plurality of memory boxes of different size ranges further include a third memory box, the third memory box being used to store memory blocks that do not conform to the size range of the first memory box and the size range of the second memory box, and the matching module 54 is specifically used to:

[0122] If the target memory box is the third memory box, query the third memory box a preset number of times to obtain at least one second candidate memory block; the memory size of the second candidate memory block is greater than or equal to the first memory size; and determine the target memory block from the second candidate memory blocks.

[0123] In one embodiment of the present disclosure, a target memory block is determined from the second candidate memory blocks, including: calculating a second difference between the memory size of each second candidate memory block and the first memory size, and determining the second candidate memory block with the smallest second difference as the target memory block.

[0124] In one embodiment of the present disclosure, the device further includes:

[0125] A calling module is used to determine other memory boxes from memory boxes of multiple different size ranges if there is no target memory block matching the first memory size among all memory blocks of the target memory box; any value within the size range of the other memory boxes is greater than any value within the size range of the target memory box; and determine the target memory block matching the first memory size among all memory blocks of the other memory boxes.

[0126] In one embodiment of the present disclosure, the device further includes:

[0127] The first recycling module is used to divide the target memory block into a first sub-memory block and a second sub-memory block if the difference between the memory size of the target memory block and the first memory size is greater than or equal to a preset threshold; the memory size of the first sub-memory block is the first memory size, and the memory size of the second sub-memory block is the difference; the first sub-memory block is allocated to the target resource; and the second sub-memory block is recycled through multiple memory boxes of different size ranges.

[0128] In one embodiment of the present disclosure, the first recycling module is specifically used to: determine a recycling memory box whose size range includes the difference from a plurality of memory boxes of different size ranges; and store the second sub-memory block in the recycling memory box.

[0129] In one embodiment of the present disclosure, the memory size of the first sub-memory block and the memory size of the second sub-memory block are both integer multiples of the unit memory block size.

[0130] In one embodiment of the present disclosure, the device also includes: a first merging module, used to determine, for each memory block to be merged, adjacent memory blocks of the memory block to be merged from multiple memory boxes of different size ranges; wherein the memory block to be merged is any memory block in the memory box, and the adjacent memory blocks are adjacent to the memory address of the memory block to be merged; the memory block to be merged is merged with the adjacent memory block to generate a merged memory block.

[0131] In one embodiment of the present disclosure, the device also includes: a second merging module, used to obtain the sum of the memory sizes of multiple memory blocks to be merged; wherein the memory block to be merged is any memory block in the memory box; if the sum of the memory sizes is within the size range of the memory box, the multiple memory blocks to be merged are merged to generate a merged memory block.

[0132] In one embodiment of the present disclosure, the device also includes: a second recycling module, used to determine whether the merged memory block and the pre-recorded memory page meet the consistency condition; if the merged memory block and the pre-recorded memory page meet the consistency condition, the merged memory block is recycled to the operating system.

[0133] In one embodiment of the present disclosure, the device also includes: a permission control module, which is used to determine the memory address of the target memory page in response to receiving a permission update request for the target memory page; determine at least one memory block corresponding to the memory address of the target memory page based on a mapping relationship between memory blocks and memory addresses; and update the read and write permissions of at least one memory block based on the permission update request.

[0134] The memory management device provided in the embodiment of the present disclosure can execute any memory management method provided in the embodiment of the present disclosure, and has the corresponding functional modules and beneficial effects of the execution method. The contents not described in detail in the embodiment of the device of the present disclosure can refer to the description in any method embodiment of the present disclosure.

[0135] The present disclosure also provides an electronic device, which includes one or more processors and a memory. The processor may be a central processing unit (CPU) or other forms of processing units with data processing capabilities and / or instruction execution capabilities, and may control other components in the electronic device to perform desired functions. The memory may include one or more computer program products, and the computer program product may include various forms of computer-readable storage media, such as volatile memory and / or non-volatile memory. Volatile memory may include, for example, random access memory (RAM) and / or cache memory (cache), etc. Non-volatile memory may include, for example, read-only memory (ROM), hard disk, flash memory, etc. One or more computer program instructions may be stored on a computer-readable storage medium, and the processor may run the program instructions to implement the method of the above embodiment of the present disclosure and / or other desired functions. Various contents such as input signals, signal components, noise components, etc. may also be stored in the computer-readable storage medium.

[0136] In one example, the electronic device may further include: an input device and an output device, and these components are interconnected through a bus system and / or other forms of connection mechanisms. In addition, the input device may also include, for example, a keyboard, a mouse, etc. The output device may output various information to the outside, including determined distance information, direction information, etc. The output device may include, for example, a display, a speaker, a printer, and a communication network and a remote output device connected thereto, etc. In addition, according to the specific application, the electronic device may also include any other appropriate components such as a bus, an input / output interface, etc.

[0137] In addition to the above methods and devices, the embodiments of the present disclosure may also be a computer program product, which includes computer program instructions. When the computer program instructions are executed by a processor, the processor executes any method provided by the embodiments of the present disclosure.

[0138] The computer program product may be written in any combination of one or more programming languages ​​to write program code for performing the operations of the disclosed embodiments, including object-oriented programming languages ​​such as Java, C++, etc., and conventional procedural programming languages ​​such as "C" or similar programming languages. The program code may be executed entirely on the user computing device, partially on the user device, as a separate software package, partially on the user computing device and partially on a remote computing device, or entirely on a remote computing device or server.

[0139] In addition, the embodiments of the present disclosure may also be a computer-readable storage medium on which computer program instructions are stored. When the computer program instructions are executed by a processor, the processor executes any method provided by the embodiments of the present disclosure.

[0140] Computer readable storage media can adopt any combination of one or more readable media. The readable medium can be a readable signal medium or a readable storage medium. The readable storage medium can include, for example, but is not limited to, a system, device or device of electricity, magnetism, light, electromagnetic, infrared, or semiconductor, or any combination of the above. More specific examples (non-exhaustive list) of readable storage media include: an electrical connection with one or more wires, a portable disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above.

[0141] It should be noted that, in this article, relational terms such as "first" and "second" are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.

[0142] The above description is only a specific embodiment of the present disclosure, so that those skilled in the art can understand or implement the present disclosure. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present disclosure. Therefore, the present disclosure will not be limited to the embodiments described herein, but will conform to the widest scope consistent with the principles and novel features disclosed herein.

Claims

1. A memory management method, characterized in that: include: Obtaining a memory application request sent by a memory requesting device for a target resource; Parsing the memory application request to obtain a first memory size to be applied for; The first memory size is a memory size declared to be applied for the target resource; Determine, from a plurality of memory boxes of different size ranges, a target memory box whose size range includes the first memory size; the memory box is obtained by clustering memory blocks of each memory size specification, and each of the memory boxes includes a preset number of memory blocks whose memory sizes are within the size range corresponding to the memory box; Determine a target memory block matching the first memory size among all memory blocks of the target memory box; wherein the memory size of the target memory block is greater than or equal to the first memory size; The memory application request is responded to according to the target memory block to allocate the target memory block to the target resource of the memory requester device.

2. The method according to claim 1, characterized in that The parsing of the memory application request to obtain the first memory size to be applied for includes: Parsing the memory application request to obtain a second memory size carried in the memory application request; the second memory size indicates a memory size actually occupied by the target resource; The first memory size is determined according to the second memory size and the unit memory block size; wherein the first memory size is an integer multiple of the unit memory block size, the first memory size is greater than or equal to the second memory size, and the difference between the first memory size and the second memory size is the smallest.

3. The method according to claim 1, characterized in that The memory size of the memory blocks included in each of the memory boxes is an integer multiple of the unit memory block size, and there is no intersection between the size ranges of different memory boxes. The multiple memory boxes with different size ranges include at least one first memory box and at least one second memory box. The first memory box stores memory blocks with consistent memory size specifications, and the second memory box stores memory blocks with multiple memory size specifications. Determining a target memory block matching the first memory size among all the memory blocks in the target memory box includes: If the target memory box is the first memory box, obtaining any memory block from all memory blocks of the target memory box as the target memory block; If the target memory box is the second memory box, obtaining at least one first candidate memory block from all memory blocks of the target memory box; the memory size of the first candidate memory block is greater than or equal to the first memory size; Determine the target memory block from the first candidate memory blocks.

4. The method according to claim 3, characterized in that The step of determining the target memory block from the first candidate memory blocks comprises: A first difference between the memory size of each of the first candidate memory blocks and the first memory size is calculated, and a first candidate memory block with the smallest first difference is determined as the target memory block.

5. The method according to claim 3, characterized in that The plurality of memory boxes of different size ranges further include a third memory box, the third memory box being used to store memory blocks that do not conform to the size range of the first memory box and the size range of the second memory box, and determining a target memory block that matches the first memory size among all the memory blocks in the target memory box, comprising: If the target memory box is the third memory box, query the third memory box for a preset number of times to obtain at least one second candidate memory block; the memory size of the second candidate memory block is greater than or equal to the first memory size; Determine the target memory block from the second candidate memory blocks.

6. The method according to claim 5, characterized in that The step of determining the target memory block from the second candidate memory blocks comprises: A second difference between the memory size of each of the second candidate memory blocks and the first memory size is calculated, and a second candidate memory block with the smallest second difference is determined as the target memory block.

7. The method according to claim 1, characterized in that After determining that the size range includes a target memory box of the first memory size, the method further includes: If there is no target memory block matching the first memory size among all the memory blocks of the target memory box, then determine other memory boxes from the memory boxes of the multiple different size ranges; any value within the size range of the other memory boxes is greater than any value within the size range of the target memory box; Among all the memory blocks of the other memory boxes, determine the target memory block that matches the first memory size.

8. The method according to claim 1, characterized in that Before responding to the memory application request according to the target memory block, the method further includes: If the difference between the memory size of the target memory block and the first memory size is greater than or equal to a preset threshold, the target memory block is divided into a first sub-memory block and a second sub-memory block; the memory size of the first sub-memory block is the first memory size, and the memory size of the second sub-memory block is the difference; Allocating the first sub-memory block to the target resource; The second sub-memory block is recycled through the multiple memory boxes of different size ranges.

9. The method according to claim 8, characterized in that The step of recycling the second sub-memory block through the plurality of memory boxes of different size ranges includes: Determine, from the memory bins of the plurality of different size ranges, a recycled memory bin whose size range includes the difference; The second sub-memory block is stored in the recycled memory box.

10. The method according to claim 8, characterized in that The memory size of the first sub-memory block and the memory size of the second sub-memory block are both integer multiples of the unit memory block size.

11. The method according to claim 8, characterized in that After recycling the second sub-memory block through the plurality of memory boxes of different size ranges, the method further includes: For each memory block to be merged, determine an adjacent memory block of the memory block to be merged from the multiple memory boxes of different size ranges; wherein the memory block to be merged is any memory block in the memory box, and the adjacent memory block is adjacent to the memory address of the memory block to be merged; The memory block to be merged is merged with the adjacent memory block to generate a merged memory block.

12. The method according to claim 8, characterized in that After recycling the second sub-memory block through the plurality of memory boxes of different size ranges, the method further includes: For a plurality of memory blocks to be merged, a sum of memory sizes of the plurality of memory blocks to be merged is obtained; wherein the memory block to be merged is any memory block in the memory box; If the sum of the memory sizes is within the size range of the memory box, the multiple memory blocks to be merged are merged to generate a merged memory block.

13. The method according to claim 11 or 12, characterized in that: After generating the merged memory block, the method further includes: Determining whether the merged memory block and the pre-recorded memory page meet a consistency condition; If the merged memory block and the pre-recorded memory page meet the consistency condition, the merged memory block is recycled to the operating system.

14. The method according to claim 1, wherein: The method further comprises: In response to receiving a permission update request for a target memory page, determining a memory address of the target memory page; Determine at least one memory block corresponding to the memory address of the target memory page according to a mapping relationship between memory blocks and memory addresses; According to the permission update request, the read and write permissions of the at least one memory block are updated.

15. A memory management device, characterized in that: include: An acquisition module, used to acquire a memory application request sent by a memory requesting device for a target resource; A parsing module, used for parsing the memory application request to obtain a first memory size to be applied for; The first memory size is a memory size declared to be applied for the target resource; a determination module, configured to determine, from a plurality of memory boxes of different size ranges, a target memory box whose size range includes the first memory size; the memory box is obtained by clustering memory blocks of each memory size specification, and each of the memory boxes includes a preset number of memory blocks whose memory sizes are within the size range corresponding to the memory box; A matching module, configured to determine a target memory block matching the first memory size among all memory blocks of the target memory box; wherein the memory size of the target memory block is greater than or equal to the first memory size; A response module is used to respond to the memory application request according to the target memory block to allocate the target memory block to the target resource of the memory requester device.

16. An electronic device, characterized in that: include: processor; a memory for storing instructions executable by the processor; The processor is used to read the executable instructions from the memory and execute the instructions to implement the memory management method described in any one of claims 1-14 above.

17. A computer-readable storage medium, characterized in that: The storage medium stores a computer program, and when the computer program is executed by a processor, the memory management method described in any one of claims 1 to 14 is implemented.

Citation Information

Cited By

  • Memory control method, computing device, storage medium and program product

    CN121501518A

  • Memory control method, computing device, storage medium, and program product

    CN121501518B