Memory Allocation Method, Device, Computer Equipment, and Computer Readable Storage Medium
By determining the memory level according to the length of the data to be stored, establishing a target linked list, and performing memory allocation and release, the problem of increasing memory fragmentation is solved and memory management efficiency is improved.
Patent Information
- Application Number
- CN202210082263.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-01-24
- Publication Date
- 2025-06-13
- Estimated Expiration
- 2042-01-24
AI Technical Summary
Existing memory management methods can easily lead to increased system memory fragmentation when allocating memory, thereby reducing the efficiency of memory management.
By determining the target level of the memory to be allocated according to the length of the data to be stored, a corresponding target linked list is established, and the target memory block is determined based on the linked list header, memory allocation and release are performed, and the generation of memory fragmentation is reduced.
It effectively reduces the generation of memory fragmentation, improves the efficiency of memory allocation, and improves the system's memory management performance.
Smart Images

Figure CN114490060B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the technical field of memory management, and more particularly, to a memory allocation method, apparatus, computer device, and computer-readable storage medium. Background Art
[0002] With the development of technology, various applications have entered people's lives, bringing great convenience to people. When these applications need to use memory, memory management is generally achieved by calling the system's memory allocation or memory release functions.
[0003] In the related art, the Malloc function is often used when allocating memory. For example, when allocating memory, a heap is first taken out from the heap list, and a memory block A is taken out from this heap. When the size of memory block A is greater than or equal to the size of the memory to be allocated, memory block A is split into memory block B and memory block C, where the size of memory block B is the same as the size of the memory to be allocated, and the size of memory block C is the difference obtained by subtracting the size of memory block B from the size of memory block A. Then, memory block B is used as the allocated memory block and the address of memory block B is returned, thus completing the memory allocation.
[0004] However, this method has the problem of increasing system memory fragmentation, which in turn leads to very low efficiency in managing memory. Summary of the Invention
[0005] The purpose of this application is to provide a memory allocation method, apparatus, device, and computer-readable storage medium, which can reduce the generation of memory fragmentation, and thus can achieve the effect of improving the efficiency of memory allocation.
[0006] The embodiments of this application are implemented as follows:
[0007] In the first aspect of the embodiments of this application, a memory allocation method is provided, including:
[0008] Determine the target level of the memory to be allocated according to the length of the data to be stored;
[0009] According to the target level of the memory to be allocated, determine the target linked list corresponding to the target level. The target linked list includes at least one pointer and a linked list head. Each pointer points to a memory block respectively, and each pointer is used to indicate the address of each memory block in the target linked list. The linked list head is the first pointer among the pointers, and the linked list head is used to point to the first empty memory block among the memory blocks in the target linked list;
[0010] Determine the target memory block according to the linked list head;
[0011] Store the data to be stored in the target memory block according to the memory allocation instruction, and point the linked list head to the first empty memory block after the target memory block.
[0012] Optionally, storing the data to be stored in the target memory block pointed to by the linked list head according to the memory allocation instruction includes:
[0013] Store the length of the data to be stored in a memory unit of a preset length starting from the starting position of the target memory block according to the memory allocation instruction;
[0014] Store the data to be stored in the memory unit after the memory unit of the preset length.
[0015] Optionally, before determining the target linked list corresponding to the target level according to the target level of the memory to be allocated, it further includes:
[0016] Determine the target memory block capacity corresponding to the target level according to the target level of the memory to be allocated;
[0017] Determine at least one empty memory block matching the target memory block capacity from the memory according to the target memory block capacity;
[0018] Establish the target linked list corresponding to the target level, respectively point the pointers of the target linked list to the empty memory blocks matching the target memory block capacity, and point the linked list head of the target linked list to the first empty memory block among the empty memory blocks matching the target memory block capacity.
[0019] Optionally, determining the target level of the memory to be allocated according to the length of the data to be stored includes:
[0020] Sum the value of the length and a preset length value to obtain a value to be allocated;
[0021] Determine the target level of the memory to be allocated according to the value to be allocated and the step value of the memory block linked list, where the step value is the difference between the maximum storage thresholds of the memory block linked lists of two adjacent levels.
[0022] Optionally, determining the target level of the memory to be allocated according to the value to be allocated and the step value of the memory block linked list includes:
[0023] Determine the target ratio of the value to be allocated to the step value;
[0024] Take the memory level corresponding to the target ratio as the target level of the memory to be allocated.
[0025] Optionally, determining the target ratio of the value to be allocated to the step value includes:
[0026] If the initial ratio of the value to be allocated to the ladder value is a non-integer, round the initial ratio to obtain the target ratio.
[0027] Optionally, after storing the data to be stored in the target memory block according to the memory allocation instruction, the method further includes:
[0028] Delete the data stored in the target memory block according to the memory release instruction for the target memory block;
[0029] Update the target linked list so that the target memory block is inserted into the target linked list as an empty memory block.
[0030] Optionally, updating the target linked list includes:
[0031] Point the head of the target linked list to the target memory block.
[0032] Optionally, the method further includes:
[0033] Determine the number of empty memory blocks in the target linked list;
[0034] If the number of empty memory blocks in the target linked list is greater than or equal to a preset threshold, release at least one empty memory block arranged in the target linked list.
[0035] Optionally, the method further includes:
[0036] If the target linked list is empty, allocate multiple memory blocks, and use one of the multiple memory blocks as the target memory block;
[0037] Insert the other memory blocks of the multiple memory blocks into the target linked list.
[0038] Optionally, determining the target memory block according to the head of the linked list includes:
[0039] Determine the head node of the target linked list, and determine the head of the target linked list according to the head node of the target linked list;
[0040] Use the memory block pointed to by the head of the linked list as the target memory block.
[0041] In a second aspect of the embodiments of the present application, a memory allocation device is provided, and the memory allocation device includes:
[0042] A first determination module, configured to determine a target level of the memory to be allocated according to the length of the data to be stored;
[0043] A second determination module, configured to determine a target linked list corresponding to the target level according to the target level of the memory to be allocated;
[0044] A third determination module, configured to determine a target memory block according to the linked list head;
[0045] A storage processing module, configured to store the data to be stored into the target memory block according to a memory allocation instruction, and point the linked list head to the first empty memory block after the target memory block.
[0046] Optionally, the second determination module is further configured to store the length of the data to be stored into a memory unit with a preset length starting from the starting position of the target memory block according to the memory allocation instruction;
[0047] Store the data to be stored into the memory unit after the memory unit with the preset length.
[0048] Optionally, the second determination module is further configured to determine a target memory block capacity corresponding to the target level according to the target level of the memory to be allocated;
[0049] Determine at least one empty memory block matching the target memory block capacity from the memory according to the target memory block capacity;
[0050] Establish a target linked list corresponding to the target level, respectively point each pointer of the target linked list to each empty memory block matching the target memory block capacity, and point the linked list head of the target linked list to the first empty memory block among the empty memory blocks matching the target memory block capacity.
[0051] Optionally, the first determination module is further configured to sum the value of the length and a preset length value to obtain a value to be allocated;
[0052] Determine the target level of the memory to be allocated according to the value to be allocated and the step value of the memory block linked list.
[0053] Optionally, the first determination module is further configured to determine a target ratio of the value to be allocated to the step value;
[0054] Use the memory level corresponding to the target ratio as the target level of the memory to be allocated.
[0055] Optionally, the first determination module is further configured to round the initial ratio to obtain the target ratio.
[0056] Optionally, the device further includes a deletion module and an update module.
[0057] The deletion module is configured to delete the data stored in the target memory block according to a memory release instruction for the target memory block.
[0058] The update module is configured to update the target linked list so that the target memory block is inserted into the target linked list as an empty memory block.
[0059] In a third aspect of the embodiments of the present application, a computer device is provided. The computer device includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the computer program is executed by the processor, the memory allocation method described in the first aspect above is implemented.
[0060] In a fourth aspect of the embodiments of the present application, a computer-readable storage medium is provided. The computer-readable storage medium stores a computer program. When the computer program is executed by a processor, the memory allocation method described in the first aspect above is implemented.
[0061] The beneficial effects of the embodiments of the present application include:
[0062] A memory allocation method provided by the embodiments of the present application determines the target level of the memory to be allocated according to the length of the data to be stored, then determines the target linked list corresponding to the target level according to the target level of the memory to be allocated, then determines the target memory block according to the head of the target linked list, and finally stores the data to be stored in the target memory block according to a memory allocation instruction, and points the head of the linked list to the first empty memory block after the target memory block. Among them, by determining the target linked list corresponding to the target level according to the target level of the memory to be allocated, the capacity of the memory block required to store the data to be stored can be accurately determined, and the situation where the memory allocated to store the data to be stored is too large can be avoided. By storing the data to be stored in the target memory block according to a memory allocation instruction, the data to be stored can be stored in the memory block in the target linked list corresponding to the target level, so that the generation of memory fragments can be reduced. In addition, by pointing the head of the linked list to the first empty memory block after the target memory block, when the data to be stored needs to be stored in the memory block in the target linked list next time, it can be directly stored in the first empty memory block of the target linked list. In this way, the generation of memory fragments can be reduced, and the efficiency of memory allocation can be improved. Description of the Drawings
[0063] To more clearly illustrate the technical solutions of the embodiments of the present application, the following will briefly introduce the accompanying drawings required in the embodiments. It should be understood that the following drawings only show some embodiments of the present application, and thus should not be regarded as limiting the scope. For those of ordinary skill in the art, without creative efforts, other related drawings can also be obtained based on these drawings.
[0064] Figure 1 Flowchart of the first memory allocation method provided by the embodiments of the present application;
[0065] Figure 2 Structural schematic diagram of a target linked list provided by the embodiments of the present application;
[0066] Figure 3 Structural schematic diagram of a target memory block provided by the embodiments of the present application;
[0067] Figure 4 Flowchart of the second memory allocation method provided by the embodiments of the present application;
[0068] Figure 5 Flowchart of the third memory allocation method provided by the embodiments of the present application;
[0069] Figure 6 Flowchart of the fourth memory allocation method provided by the embodiments of the present application;
[0070] Figure 7 Flowchart of the fifth memory allocation method provided by the embodiments of the present application;
[0071] Figure 8 Flowchart of the sixth memory allocation method provided by the embodiments of the present application;
[0072] Figure 9 Flowchart of the seventh memory allocation method provided by the embodiments of the present application;
[0073] Figure 10 Structural schematic diagram of a memory allocation device provided by the embodiments of the present application;
[0074] Figure 11 Structural schematic diagram of a computer device provided by the embodiments of the present application. Detailed implementation manners
[0075] To make the objectives, technical solutions, and advantages of the embodiments of the present application clearer, the following will clearly and completely describe the technical solutions in the embodiments of the present application with reference to the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are some, but not all, of the embodiments of the present application. Usually, the components of the embodiments of the present application described and shown in the accompanying drawings here can be arranged and designed in various different configurations.
[0076] Accordingly, the following detailed description of the embodiments of the present application provided in the accompanying drawings is not intended to limit the scope of the claimed present application, but merely represents selected embodiments of the present application. All other embodiments obtained by those of ordinary skill in the art based on the embodiments in the present application without creative efforts fall within the scope of protection of the present application.
[0077] It should be noted that like reference numerals and letters denote like items in the following drawings. Therefore, once an item is defined in one drawing, it does not need to be further defined and explained in subsequent drawings.
[0078] When an application needs to use memory, memory management is generally achieved by calling the system's memory allocation or memory release functions. Currently, the Malloc function is often used for memory allocation. For example, when allocating memory, a heap is first taken out from the heap list, and a memory block A is taken out from this heap. When the size of memory block A is greater than or equal to the size of the memory to be allocated, memory block A is split into memory block B and memory block C, where the size of memory block B is the same as the size of the memory to be allocated, and the size of memory block C is the difference obtained by subtracting the size of memory block B from the size of memory block A. Then, memory block B is used as the allocated memory block and the address of memory block B is returned, thus completing the memory allocation. However, in the case of long-term system operation, this solution will generate a large amount of memory fragmentation, which will lead to too low memory allocation efficiency. In addition, the Free function is often used for memory release. For example, when releasing memory, according to the address of the memory block D to be released, the corresponding heap is found. According to the address and size of memory block D, the adjacent memory blocks E and F to memory block D are determined. It is determined whether memory block D can be merged with memory block E and / or memory block F. If so, the merge operation is performed to obtain memory block G, and the above steps are repeatedly executed for memory block G until there are no adjacent memory blocks that can be merged with memory block G, thus completing the memory release.
[0079] For this reason, the embodiments of the present application provide a memory allocation method. By determining the target level of the memory to be allocated, determining the target linked list corresponding to the target level according to the target level, determining the target memory block according to the head of the target linked list, storing the data to be stored in the target memory block pointed to by the head of the linked list, and pointing the head of the linked list to the first empty memory block after the target memory block, the generation of memory fragmentation can be reduced, and thus the effect of improving the memory allocation efficiency can be achieved.
[0080] The embodiments of the present application are described by taking the memory allocation method as an example. However, it does not mean that the embodiments of the present application can only be used for memory allocation.
[0081] The memory allocation method provided by the embodiments of the present application will be explained in detail below.
[0082] Figure 1 It is a flowchart of a memory allocation method provided by the present application. This method can be applied to a computer device, which can be a terminal device or a server. Refer to Figure 1 An embodiment of the present application provides a memory allocation method, including:
[0083] Step 1001: Determine the target level of the memory to be allocated according to the length of the data to be stored.
[0084] Optionally, the length of the data to be stored can represent the size of the data to be stored. For example, if the stored data is 8-byte data, then the length of the data to be stored is 8.
[0085] Optionally, the memory to be allocated can be the size of the memory used to store the data to be stored.
[0086] Optionally, the target level can be a level set according to certain rules. The higher the target level, the larger the memory to be allocated can be represented, and it can also represent the larger the length of the data to be stored.
[0087] For example, the memory to be allocated can be divided into 7 levels: level 0, level 1,..., level 6. The level 0 of the memory to be allocated can represent that the memory to be allocated is smaller, that is, the length of the data to be stored is larger. The level 6 of the memory to be allocated can represent that the memory to be allocated is larger, that is, the length of the data to be stored is larger.
[0088] It should be noted that by determining the target level of the memory to be allocated according to the length of the data to be stored, the level of the memory that needs to be allocated for the data to be stored can be better determined, which is convenient for performing subsequent steps and can achieve the effect of improving the memory allocation efficiency.
[0089] Step 1002: Determine the target linked list corresponding to the target level according to the target level of the memory to be allocated.
[0090] Optionally, the target linked list can be a memory linked list corresponding to the target level of the memory to be allocated. The memory linked list can also be divided into multiple levels, and the sizes of the memory blocks in the memory linked lists of each level are not equal. The embodiments of the present application do not make any limitations in this regard.
[0091] For example, the memory to be allocated can be divided into 7 levels: level 0, level 1,..., level 6. Then the memory linked list can also be divided into 7 levels: level 0, level 1,..., level 6. If the level of the memory to be allocated is level 1, then the target linked list corresponding to the level of the memory to be allocated can be the memory linked list of level 2. The embodiments of the present application do not make any limitations in this regard.
[0092] Optionally, the target linked list may be a linked list of memory blocks composed of multiple nodes, and the linked list of memory blocks may also be a singly linked list. The speed of accessing the first node of the singly linked list is the fastest and the efficiency is the highest. The singly linked list has a head node that points to the starting address of the singly linked list in memory. No matter which node is accessed in the singly linked list, it is necessary to start from the head node of the singly linked list and search backward in sequence. Moreover, since the tail node of the singly linked list has no subsequent node, its pointer field is empty.
[0093] Exemplarily, each node in the target linked list has two parts: a data field and a pointer field. The data field is used to store data, and the pointer field is used to point to the address of the next node. And the pointer field of the last node in the target linked list is empty.
[0094] In addition, accessing each node in the target linked list needs to start from the head of the target linked list, that is, it needs to start from the first node in the target linked list. The addresses of the other nodes in the target linked list except the first node can be given by the previous node of each node respectively, and the address of the first node can be given by the head node of the target linked list.
[0095] That is to say, the memory blocks in the target linked list can be arranged in a certain order, and the pointers in the target linked list and the list head can be used to indicate the arrangement order of the memory blocks in the target linked list. The embodiments of the present application do not limit this.
[0096] Optionally, the target linked list includes at least one pointer and a list head. Each pointer points to a memory block respectively, and the memory blocks pointed to by each pointer have the same capacity.
[0097] Optionally, the pointer can be used to indicate the address of the next memory block in the target linked list.
[0098] Optionally, the list head can be used to point to the first empty memory block among the memory blocks pointed to by each pointer. The list head can also be used to point to the first empty memory block among the memory blocks in the target linked list.
[0099] Optionally, the list head can be the first pointer among the pointers.
[0100] It is worth noting that by determining the target linked list corresponding to the target level according to the target level of the memory to be allocated, since the memory blocks pointed to by the pointers in the target linked list have the same capacity, the capacity of the memory block required to store the data to be stored can be accurately determined, and thus the situation that the allocated memory for storing the data to be stored is too large can be avoided. In this way, the generation of memory fragments can be reduced, and further the effect of improving the efficiency of memory allocation can be achieved.
[0101] Step 1003: Determine the target memory block pointed to by the linked list head.
[0102] Optionally, the linked list head can be determined according to the first node among the nodes in the target linked list, and the first node can be used to indicate the number of empty memory blocks in the target linked list and the address of the first empty memory block in the target linked list.
[0103] Optionally, the target memory block can be the first memory block pointed to by the pointers in the target linked list.
[0104] Step 1004: Store the data to be stored into the target memory block according to the memory allocation instruction, and make the linked list head point to the first empty memory block after the target memory block.
[0105] Optionally, the memory allocation instruction can be an instruction set in advance and triggered by certain conditions, or an instruction generated or / input in real time during actual application. The embodiments of the present application do not make any limitations in this regard.
[0106] Optionally, the first empty memory block after the target memory block can be the second empty memory block among the memory blocks pointed to by the pointers in the target linked list before storing the data to be stored into the target memory block pointed to by the linked list head.
[0107] In a possible way, if the target linked list is empty, multiple memory blocks can be allocated, and one of the multiple memory blocks is used as the target memory block.
[0108] Optionally, the target linked list being empty may mean that there are no empty memory blocks in the target linked list, or that there is only one empty memory block in the target linked list, or that the number of empty memory blocks in the target linked list is less than a preset number, and the preset number can be any positive integer. The embodiments of the present application do not make any limitations in this regard.
[0109] Insert the other memory blocks among the multiple memory blocks into the target linked list.
[0110] Optionally, the other memory blocks among the multiple memory blocks refer to all the other memory blocks except the one used as the target memory block among the multiple memory blocks.
[0111] Exemplarily, if the target linked list is empty, call the Malloc function to allocate N memory blocks, where N is a positive integer greater than or equal to 1.
[0112] Insert N - 1 of the N memory blocks into the target linked list;
[0113] Use the one memory block among the N memory blocks that has not been inserted into the target linked list as the target memory block.
[0114] Further, the operation of inserting N - 1 memory blocks among the N memory blocks into the target linked list can specifically be as follows:
[0115] Insert N - 1 memory blocks among the N memory blocks into the head of the target linked list.
[0116] For example, referring to Figure 2 , Figure 2 Figure (a) therein shows a schematic structural diagram of an unallocated memory linked list. The memory linked list includes pointer 1, pointer 2, pointer 3, and a linked list head. Pointer 1 points from memory block 1 to memory block 2, pointer 2 points from memory block 2 to memory block 3, pointer 3 points from memory block 3 to memory block 4, and the linked list head points to memory block 1. At this time, memory blocks 1, 2, 3, and 4 are all empty memory blocks and are not allocated to any data to be stored.
[0117] If there is data to be stored that needs to be stored in the memory block pointed to by this memory linked list, or in other words, if there is a memory block pointed to by this memory linked list that needs to be allocated to the data to be stored, then the memory block 1 pointed to by the current linked list head will be taken out, and memory block 1 will be used as the target memory block, and the data to be stored will be stored in memory block 1.
[0118] Figure 2 Figure (b) therein shows a schematic structural diagram of the memory linked list after taking out the first memory block. The memory linked list includes pointer 2, pointer 3, and a linked list head. Since memory block 1 has been allocated for storing the data to be stored, the linked list head of this memory linked list no longer points to memory block 1, and pointer 1 no longer points from memory block 1 to memory block 2. As shown in Figure 2 Figure (b) therein, pointer 2 points from memory block 2 to memory block 3, pointer 3 points from memory block 3 to memory block 4, and the linked list head points to memory block 2. If there is another data to be stored that needs to be stored in the memory block pointed to by this memory linked list, then the memory block 2 pointed to by the current linked list head will be taken out, and memory block 2 will be used as the target memory block, and the data to be stored will be stored in memory block 2.
[0119] It should be noted that the memory blocks pointed to by each pointer in the target linked list are all empty memory blocks with the same capacity. By storing the data to be stored in the target memory block pointed to by the linked list head, the data to be stored can be stored in the memory block in the target linked list corresponding to the target level, which can reduce the generation of memory fragmentation. And by pointing the linked list head to the first empty memory block after the target memory block, in this way, the next time when the data to be stored needs to be stored in the memory block in the target linked list, it can be directly stored in the first empty memory block of the target linked list. In this way, the efficiency of memory allocation can be improved.
[0120] In the embodiment of the present application, by determining the target level of the memory to be allocated according to the length of the data to be stored, then determining the target linked list corresponding to the target level according to the target level of the memory to be allocated, then determining the target memory block according to the head of the target linked list, and finally storing the data to be stored in the target memory block pointed to by the head of the linked list according to the memory allocation instruction, and pointing the head of the linked list to the first empty memory block after the target memory block. Among them, by determining the target linked list corresponding to the target level according to the target level of the memory to be allocated, the capacity of the memory block required to store the data to be stored can be accurately determined, and the situation that the allocated memory for storing the data to be stored is too large can be avoided. By storing the data to be stored in the target memory block according to the memory allocation instruction, the data to be stored can be stored in the memory block in the target linked list corresponding to the target level, so that the generation of memory fragmentation can be reduced. In addition, by pointing the head of the linked list to the first empty memory block after the target memory block, when the data to be stored needs to be stored in the memory block in the target linked list next time, it can be directly stored in the first empty memory block of the target linked list. In this way, the generation of memory fragmentation can be reduced, and the efficiency of memory allocation can be improved accordingly.
[0121] In a possible implementation, storing the data to be stored in the target memory block pointed to by the head of the linked list according to the memory allocation instruction includes:
[0122] Storing the length of the data to be stored in a memory unit with a preset length starting from the starting position of the target memory block according to the memory allocation instruction.
[0123] Optionally, when the memory allocation instruction is received, the length of the data to be stored can be immediately stored in the memory unit, or after a preset time, the length of the data to be stored can be stored in the memory unit. Generally, the preset time can be set in advance, and the preset time can be set to be relatively small. For example, the preset time can be set to 2 milliseconds, or of course, it can be set to other times. The embodiment of the present application does not limit this. In this way, the computer device can have a certain processing time, and the processing pressure on the computer device can be reduced accordingly.
[0124] Optionally, the starting position of the target memory block can be the first position in the target memory block where data can be stored, or a position set according to certain rules. The embodiment of the present application does not limit this.
[0125] Further, after storing the length of the data to be stored in the memory unit with a preset length starting from the starting position of the target memory block, the method further includes:
[0126] Adjust the starting position of the target memory block to after the memory cells of the preset length.
[0127] Optionally, the memory cells of the preset length can be the part of the target memory block that is used to store the length of the data to be stored.
[0128] Optionally, the preset length can be a length value set in advance. For example, if the preset length is set to 4, then the capacity of the memory cells of the preset length is 4 bytes.
[0129] Store the data to be stored into the memory cells after the memory cells of the preset length.
[0130] Optionally, the memory cells after the memory cells of the preset length can be the part of the target memory block that is used to store the data to be stored, and the capacity of this part is the difference between the capacity of the target memory block and the size of the memory cells of the preset length.
[0131] Exemplarily, refer to Figure 3 , Figure 3 (a) in shows a schematic diagram of a memory block 5. As can be seen from Figure 3 (a) in, at this time, no data has been stored in the memory block 5, that is to say, the memory block 5 is still an empty memory block, and the starting position of the memory block 5 is the first position where data can be stored in the memory block 5.
[0132] Continue to refer to Figure 3 , Figure 3 (b) in shows a schematic diagram of the memory block 5 after storing the data to be stored. As can be seen from Figure 3 (a) in, if the memory block 5 is used as the target memory block, then the length of the data to be stored can be stored in the memory cells of the preset length L starting from the starting position of the memory block 5, and the starting position of the memory block 5 can be adjusted to the memory cells of the preset length L, and the data to be stored can also be stored in the memory cells after the memory cells of the preset length.
[0133] It should be noted that by storing the length of the data stored in the target memory block into the memory cells of the preset length located at the starting position of the target memory block, and then storing the data into the memory cells after the memory cells of the preset length, in this way, only by reading the length stored in the memory cells of the preset length in the target memory block can the size of the data stored in the target memory block or the capacity of the target memory block be determined. In this way, the efficiency of memory allocation can be improved.
[0134] In a possible implementation, refer to Figure 4, before determining the target linked list corresponding to the target level according to the target level of the memory to be allocated, the method further includes:
[0135] Step 1005: Determine the target memory block capacity corresponding to the target level according to the target level of the memory to be allocated.
[0136] Optionally, the target memory block capacity corresponding to the target level may be greater than the memory to be allocated.
[0137] For example, the memory to be allocated can be divided into 7 levels, namely level 0, level 1, …, level 6. The target memory block capacity corresponding to level 0 is Q bytes, the target memory block capacity corresponding to level 1 is 2Q bytes, …, and the target memory block capacity corresponding to level 6 is 7Q bytes. If the target level of the memory to be allocated is level 1, then the target memory block capacity corresponding to the target level of the memory to be allocated is 2Q bytes. Wherein, Q can be any real number greater than 0.
[0138] In this way, the size of the data to be stored can be determined according to the level of the memory to be allocated, so as to accurately determine the capacity of the target memory block required, and the situation that the memory allocated for storing the data to be stored is too large can be avoided, and the generation of memory fragments can be reduced.
[0139] Step 1006: Determine at least one empty memory block that matches the target memory block capacity from the memory.
[0140] Optionally, at least one empty memory block that matches the target memory block capacity can be found by traversing the memory, or at least one empty memory block that matches the target memory block capacity can be allocated by calling the Malloc function. The embodiments of the present application do not limit this.
[0141] In this way, the capacity of the empty memory block can be accurately determined, and in this way, the situation that the determined empty memory block is too large can be avoided, and the generation of memory fragments can be reduced.
[0142] Step 1007: Establish the target linked list corresponding to the target level, respectively point the pointers of the target linked list to the empty memory blocks that match the target memory block capacity, and point the head of the target linked list to the first empty memory block among the empty memory blocks that match the target memory block capacity.
[0143] It should be noted that by determining the target memory block capacity corresponding to the target level of the memory to be allocated, then determining at least one empty memory block in the memory that matches the target memory block capacity, and finally establishing a target linked list corresponding to the target level with the at least one empty memory block. In this way, the accuracy of the capacity of the memory blocks in the target linked lists established corresponding to each target level can be improved. Thus, the generation of memory fragmentation can be reduced, and the efficiency of memory allocation can be improved accordingly.
[0144] In a possible implementation, refer to Figure 5 , determining the target level of the memory to be allocated according to the length of the data to be stored, including:
[0145] Step 1008: Sum the value of the length and a preset length value to obtain a value to be allocated.
[0146] Optionally, the value of the length can be the length of the data to be stored.
[0147] Optionally, the preset length value can be the length value of the memory unit with the preset length, or a length value set by other rules. The embodiments of the present application do not make any limitations in this regard.
[0148] Since the length of the data to be stored is stored in the memory unit with the preset length starting from the starting position of the target memory block, the data actually stored in the target memory block is the data to be stored and the length of the data to be stored. Then, summing the value of the length and the preset length value can avoid the situation where the capacity of the allocated target memory block is insufficient to store the data to be stored and the length of the data to be stored. Thus, the error rate of memory allocation can be reduced, and the efficiency of memory allocation can be improved.
[0149] Step 1009: Determine the target level of the memory to be allocated according to the value to be allocated and the step value of the memory block linked list.
[0150] Optionally, the step value is the difference between the maximum storage thresholds of the memory block linked lists of two adjacent levels. That is, the difference between the maximum storage threshold of the memory blocks in any memory block linked list and the maximum storage threshold of the memory blocks in the memory block linked list of the adjacent level is the step value.
[0151] For example, the memory block linked list can be divided into 7 levels, namely level 0, level 1,..., level 6. The maximum storage threshold of the memory block linked list corresponding to level 0 is P bytes, the maximum storage threshold of the memory block linked list corresponding to level 1 is 2P bytes,..., and the maximum storage threshold of the memory block linked list corresponding to level 6 is 7P bytes. Then, the step value is P. Wherein, P can be any real number greater than 0.
[0152] In this way, it is possible to avoid the situation where the capacity of the allocated target memory block is insufficient to store the data to be stored and the length of the data to be stored. Thus, the error rate of memory allocation can be reduced and the efficiency of memory allocation can be improved.
[0153] In a possible implementation, refer to Figure 6 , and determine the target level of the memory to be allocated according to the value to be allocated and the step value of the memory block linked list, including:
[0154] Step 1010: Determine the target ratio of the value to be allocated to the step value.
[0155] Step 1011: Use the memory level corresponding to the target ratio as the target level of the memory to be allocated.
[0156] Furthermore, determining the target ratio of the value to be allocated to the step value includes:
[0157] If the initial ratio of the value to be allocated to the step value is not an integer, round the initial ratio to obtain the target ratio.
[0158] For example, if the step value is set to 8, the value of the length of the data to be stored is 10, and the preset length value is 4, then the value to be allocated is 14. Then, if the initial ratio is determined to be 1.8, rounding the initial ratio gives the target ratio of 1, and the target level of the memory to be allocated can be determined to be 1.
[0159] It should be noted that by determining the target ratio of the value to be allocated to the step value and using the memory level corresponding to the target ratio as the target level of the memory to be allocated, it is possible to ensure that the capacity of the target memory block corresponding to the target level is greater than the data to be stored and the length of the data to be stored, thus avoiding the situation where the capacity of the allocated target memory block is insufficient to store the data to be stored and the length of the data to be stored. Thus, the error rate of memory allocation can be reduced and the efficiency of memory allocation can be improved.
[0160] In a possible implementation, refer to Figure 7 , after storing the data to be stored into the target memory block according to the memory allocation instruction, the method further includes:
[0161] Step 1012: Delete the data stored in the target memory block according to the memory release instruction for the target memory block.
[0162] Optionally, the memory release instruction can be an instruction set in advance and triggered by specific conditions, or an instruction input in real time according to actual needs. The embodiments of the present application do not make any limitations in this regard.
[0163] Optionally, deleting the data stored in the target memory block may be deleting the value of the length of the data stored in the target memory block, or deleting the data stored in the target memory block, or deleting the data stored in the target memory block and the value of the length of the data.
[0164] Step 1013: Update the target linked list so that the target memory block is inserted into the target linked list as an empty memory block.
[0165] Optionally, the target memory block may be inserted into the head of the target linked list as an empty memory block, or inserted into other positions of the target linked list. The embodiments of the present application do not limit this.
[0166] Further, updating the target linked list includes:
[0167] Point the head of the target linked list to the target memory block.
[0168] Optionally, the operation of updating the target linked list may specifically be: inserting the target memory block into the head of the target linked list, and pointing a pointer from the target memory block to the next memory block.
[0169] It should be noted that according to the memory release instruction for the target memory block, delete the data stored in the target memory block, and then insert the memory block whose stored data has been deleted into the head of the target linked list to complete the memory release. In this way, the efficiency of memory release can be effectively improved.
[0170] In a possible implementation, see Figure 8 , the method further includes:
[0171] Step 1014: Determine the number of empty memory blocks in the target linked list.
[0172] Optionally, the number of empty memory blocks in the target linked list may be determined once every certain period of time, or may be determined once when the number of empty memory blocks in the target linked list changes. The embodiments of the present application do not limit this.
[0173] Step 1015: If the number of empty memory blocks in the target linked list is greater than or equal to a preset threshold, release at least one empty memory block arranged in the target linked list in sequence.
[0174] Optionally, the preset threshold can be set according to actual needs. For example, the preset threshold can be set to 10.
[0175] Optionally, releasing is a memory release operation that releases the data resident in the memory block from the memory block. Generally, memory release means clearing all the data stored in the memory block, and after the memory release, the pointer in the memory block needs to be set to zero.
[0176] Optionally, the release can start from the first empty memory block arranged in sequence in the target linked list, and a preset number of empty memory blocks can be continuously released, or the empty memory blocks can be continuously released until the number of empty memory blocks in the target linked list is less than the preset threshold.
[0177] It should be noted that when the number of empty memory blocks in the target linked list is greater than or equal to the preset threshold, at least one empty memory block at the head of the target linked list is released, so as to avoid the situation that there are too many free empty memory blocks in the memory block linked list corresponding to a target level, which is convenient for the memory block linked lists corresponding to other target levels to allocate new memory blocks from the memory stack. And because the target linked list is a singly linked list, the speed of accessing the first memory block of the target linked list is very fast, so releasing the memory blocks arranged in sequence in the target linked list can improve the efficiency of releasing memory blocks. In this way, the efficiency of memory release and memory allocation can be effectively improved.
[0178] In a possible implementation, determining the target memory block according to the linked list head includes:
[0179] Determine the head node of the target linked list, and determine the linked list head of the target linked list according to the head node of the target linked list.
[0180] Optionally, the head node of the target linked list can be a node attached before the first node of the target linked list.
[0181] Exemplarily, the pointer field of the head node can store a pointer pointing to the first node of the target linked list, that is to say, the head node can be used to indicate the position of the first memory block pointed to by each pointer, that is, it can be used to indicate the position of the memory block pointed to by the linked list head. In addition, the data field of the head node may not store any information, and the data field of the head node may also store additional information such as the length of the target linked list and the number of empty memory blocks existing in the target linked list.
[0182] Furthermore, the operation of determining the head node of the target linked list and determining the linked list head of the target linked list according to the head node of the target linked list can be: traverse all the head nodes of the linked lists until the head node of the target linked list is found.
[0183] Read the information stored in the pointer field of the head node of the target linked list to determine the linked list head of the target linked list. In this way, the first node of the target linked list can be determined, and the address of the first empty memory block of the target linked list can also be determined.
[0184] Use the memory block pointed to by the head of the linked list as the target memory block.
[0185] In this way, the target memory block can be accurately and quickly determined, thereby improving the efficiency and accuracy of memory allocation.
[0186] Next, through Figure 9 to give an example of the memory allocation method provided by the embodiments of the present application for detailed explanation.
[0187] Exemplarily, Figure 9 A flowchart of a memory allocation method is provided. Refer to Figure 9 The method includes:
[0188] Step 2001: Sum the value of the length and the preset length value to obtain the value to be allocated.
[0189] Optionally, the value of the length may be the length of the data to be stored.
[0190] Optionally, the preset length value may be the length value of the memory unit with the preset length, or may be a length value set by other rules. The embodiments of the present application do not limit this.
[0191] In this way, it can be avoided that the capacity of the allocated target memory block is insufficient to store the data to be stored and the length of the data to be stored. Thus, the error rate of memory allocation can be reduced and the efficiency of memory allocation can be improved.
[0192] Step 2002: Determine the target ratio of the value to be allocated to the step value.
[0193] Step 2003: Use the memory level corresponding to the target ratio as the target level of the memory to be allocated.
[0194] Further, the determining the target ratio of the value to be allocated to the step value includes:
[0195] If the initial ratio of the value to be allocated to the step value is a non-integer, round the initial ratio to obtain the target ratio.
[0196] In this way, it can be ensured that the capacity of the target memory block corresponding to the target level is greater than the data to be stored and the length of the data to be stored, and it can be avoided that the capacity of the allocated target memory block is insufficient to store the data to be stored and the length of the data to be stored.
[0197] Step 2004: Determine the capacity of the target memory block corresponding to the target level according to the target level of the memory to be allocated.
[0198] Optionally, the capacity of the target memory block corresponding to the target level may be greater than the memory to be allocated.
[0199] In this way, according to the level of the memory to be allocated, the size of the data to be stored can be determined to accurately determine the capacity of the target memory block required.
[0200] Step 2005: Determine at least one empty memory block that matches the capacity of the target memory block from the memory.
[0201] Optionally, at least one empty memory block that matches the capacity of the target memory block can be found by traversing the memory, or at least one empty memory block that matches the capacity of the target memory block can be allocated by calling the Malloc function. The embodiments of the present application do not limit this.
[0202] In this way, the capacity of the empty memory block can be accurately determined, so that the situation where the determined empty memory block is too large can be avoided, and the generation of memory fragmentation can be reduced.
[0203] Step 2006: Establish a target linked list corresponding to the target level, respectively point each pointer of the target linked list to each empty memory block that matches the capacity of the target memory block, and point the head of the target linked list to the first empty memory block among the empty memory blocks that match the capacity of the target memory block.
[0204] In this way, the accuracy of the capacity of the memory blocks in the target linked list established corresponding to each target level can be improved.
[0205] Step 2007: Determine the target linked list corresponding to the target level according to the target level of the memory to be allocated.
[0206] For example, the memory to be allocated can be divided into 7 levels, namely level 0, level 1, …, level 6. Then the memory linked list can also be divided into 7 levels, namely level 0, level 1, …, level 6. If the level of the memory to be allocated is level 1, then the target linked list corresponding to the level of the memory to be allocated can be the memory linked list of level 2. The embodiments of the present application do not limit this.
[0207] Optionally, the target linked list includes at least one pointer and a head of the linked list. Each pointer points to a memory block respectively, and the capacities of the memory blocks pointed to by each pointer are the same.
[0208] Optionally, the pointer is used to indicate the address of the next memory block in the target linked list.
[0209] Optionally, the head of the linked list points to the first empty memory block among the memory blocks pointed to by each pointer.
[0210] Optionally, the head of the linked list can be the first pointer among each pointer.
[0211] In this way, the capacity of the memory block required to store the data to be stored can be accurately determined, and the situation where the allocated memory for storing the data to be stored is too large can be avoided. Thus, the generation of memory fragmentation can be reduced, and the efficiency of memory allocation can be improved accordingly.
[0212] Step 2008: Determine the target memory block pointed to by the linked list head.
[0213] Optionally, the target memory block can be the first memory block pointed to by each pointer in the target linked list.
[0214] Step 2009: Store the data to be stored into the target memory block pointed to by the linked list head according to the memory allocation instruction, and make the linked list head point to the first empty memory block after the target memory block.
[0215] Optionally, the first empty memory block after the target memory block can be the second empty memory block among the memory blocks pointed to by each pointer in the target linked list before storing the data to be stored into the target memory block pointed to by the linked list head.
[0216] By storing the data to be stored into the target memory block pointed to by the linked list head, the data to be stored can be stored into the memory block in the target linked list corresponding to the target level, thus reducing the generation of memory fragmentation. And by making the linked list head point to the first empty memory block after the target memory block, when the next data to be stored needs to be stored into the memory block in the target linked list, it can be directly stored into the first empty memory block of the target linked list.
[0217] Step 2010: Delete the data stored in the target memory block according to the memory release instruction for the target memory block.
[0218] Optionally, the memory release instruction can be an instruction set in advance and triggered by specific conditions, or an instruction input in real time according to actual needs. The embodiments of the present application do not limit this.
[0219] Step 2011: Update the target linked list so that the target memory block is inserted into the target linked list as an empty memory block.
[0220] Optionally, the target memory block can be inserted into the head of the target linked list as an empty memory block, or inserted into other positions of the target linked list. The embodiments of the present application do not limit this.
[0221] Further, updating the target linked list includes:
[0222] Make the linked list head of the target linked list point to the target memory block.
[0223] Optionally, the operation of updating the target linked list may specifically be: inserting the target memory block into the head of the target linked list, and pointing a pointer from the target memory block to the next memory block.
[0224] It should be noted that according to the memory release instruction for the target memory block, the data stored in the target memory block is deleted, and then the memory block after deleting the stored data is inserted into the head of the target linked list to complete the memory release. In this way, the efficiency of memory release can be effectively improved.
[0225] Step 2012: Determine the number of empty memory blocks in the target linked list.
[0226] Optionally, the number of empty memory blocks in the target linked list can be determined once every certain period of time, or can be determined once when the number of empty memory blocks in the target linked list changes.
[0227] Step 2013: If the number of empty memory blocks in the target linked list is greater than or equal to the preset threshold, release at least one empty memory block arranged in the target linked list in sequence.
[0228] Optionally, the release can start from the first empty memory block arranged in the target linked list in sequence, and a preset number of empty memory blocks can be continuously released, or the empty memory blocks can be continuously released until the number of empty memory blocks in the target linked list is less than the preset threshold.
[0229] The following describes the device, equipment, computer-readable storage medium, etc. for implementing the memory allocation method provided in this application. For the specific implementation process and technical effects, please refer to the above, and will not be elaborated below.
[0230] Figure 10 It is a schematic structural diagram of a memory allocation device provided by an embodiment of this application. Refer to Figure 10 , the device includes:
[0231] The first determination module 301 is used to determine the target level of the memory to be allocated according to the length of the data to be stored.
[0232] The second determination module 302 is used to determine the target linked list corresponding to the target level according to the target level of the memory to be allocated.
[0233] The third determination module 303 is used to determine the target memory block according to the linked list head;
[0234] The storage processing module 304 is used to store the data to be stored in the target memory block according to the memory allocation instruction, and point the linked list head to the first empty memory block after the target memory block.
[0235] Optionally, the second determination module 302 is further configured to store the length of the data to be stored in a memory unit with a preset length starting from the starting position of the target memory block;
[0236] Store the data to be stored in a memory unit after the memory unit with the preset length.
[0237] Optionally, the second determination module 302 is further configured to determine a target memory block capacity corresponding to the target level according to the target level of the memory to be allocated.
[0238] Determine at least one empty memory block matching the target memory block capacity from the memory according to the target memory block capacity.
[0239] Establish a target linked list corresponding to the target level, respectively point the pointers of the target linked list to each empty memory block matching the target memory block capacity, and point the head of the target linked list to the first empty memory block among the empty memory blocks matching the target memory block capacity.
[0240] Optionally, the first determination module 301 is further configured to sum the value of the length and a preset length value to obtain a value to be allocated.
[0241] Determine the target level of the memory to be allocated according to the value to be allocated and the step value of the memory block linked list.
[0242] Optionally, the first determination module 301 is further configured to determine a target ratio of the value to be allocated to the step value.
[0243] Use the memory level corresponding to the target ratio as the target level of the memory to be allocated.
[0244] Optionally, the first determination module 301 is further configured to round the initial ratio to obtain the target ratio.
[0245] Optionally, the apparatus further includes a deletion module and an update module.
[0246] The deletion module is configured to delete the data stored in the target memory block according to a memory release instruction for the target memory block.
[0247] The update module is configured to update the target linked list so that the target memory block is inserted into the target linked list as an empty memory block.
[0248] The above apparatus is used to execute the method provided in the foregoing embodiment, and its implementation principle and technical effects are similar, and will not be described in detail herein.
[0249] The above-mentioned modules may be one or more integrated circuits configured to implement the above methods. For example: one or more Application Specific Integrated Circuits (ASICs), or, one or more microprocessors, or, one or more Field Programmable Gate Arrays (FPGAs), etc. For another example, when a certain module above is implemented in the form of a processing element scheduling program code, the processing element may be a general-purpose processor, such as a Central Processing Unit (CPU) or other processors that can call program code. For still another example, these modules may be integrated together and implemented in the form of a system-on-a-chip (SOC).
[0250] Figure 11 is a schematic structural diagram of a computer device provided by an embodiment of the present application. Refer to Figure 11 , the computer device 400 includes: a memory 401 and a processor 402. A computer program that can run on the processor 402 is stored in the memory 401. When the processor 402 executes the computer program, the steps in any of the above method embodiments are implemented.
[0251] An embodiment of the present application also provides a computer-readable storage medium. The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps in any of the above method embodiments can be implemented.
[0252] Optionally, the present application also provides a program product, such as a computer-readable storage medium, including a program that is used to execute any of the above interface image rendering method embodiments when executed by a processor.
[0253] In several embodiments provided by the present invention, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are merely illustrative. For example, the division of units is only a logical function division. In actual implementation, there may be other division methods. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the displayed or discussed coupling or direct coupling or communication connection between each other may be through some interfaces. The indirect coupling or communication connection of devices or units may be in an electrical, mechanical or other form.
[0254] The unit described as a separation component may or may not be physically separated. The component shown as a unit may or may not be a physical unit, that is, it may be located in one place or distributed over multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0255] In addition, each functional unit in various embodiments of the present invention can be integrated in a processing unit, or each unit can exist physically alone, or two or more units can be integrated in one unit. The above-mentioned integrated units can be implemented in the form of hardware, or in the form of a combination of hardware and software functional units.
[0256] The above-mentioned integrated unit implemented in the form of a software functional unit can be stored in a computer-readable storage medium. The above-mentioned software functional unit stored in a storage medium includes several instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor to execute some steps of the methods in various embodiments of the present invention. The aforementioned storage medium includes: various media such as USB flash drives, mobile hard disks, read-only memories (abbreviated as ROM), random access memories (abbreviated as RAM), magnetic disks, or optical discs that can store program codes.
[0257] The above is only the specific implementation manner of this application, but the protection scope of this application is not limited thereto. Any person skilled in the art can easily think of changes or substitutions within the technical scope disclosed in this application, and all of them should be covered by the protection scope of this application. Therefore, the protection scope of this application should be subject to the protection scope of the claims.
[0258] The above are only the preferred embodiments of this application and are not used to limit this application. For those skilled in the art, this application can have various changes and modifications. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of this application shall be included in the protection scope of this application.
Claims
1. A memory allocation method, characterized in that, it includes: Determining a target level of the memory to be allocated according to the length of the data to be stored; According to the target level of the memory to be allocated, determining a target linked list corresponding to the target level, the target linked list includes at least one pointer and a linked list head, each pointer points to a memory block respectively, each pointer is used to indicate the addresses of the memory blocks in the target linked list, the linked list head is the first pointer among the pointers, and the linked list head is used to point to the first empty memory block among the memory blocks in the target linked list; Determining a target memory block according to the linked list head; Storing the data to be stored into the target memory block according to a memory allocation instruction, and pointing the linked list head to the first empty memory block after the target memory block; The determining the target level of the memory to be allocated according to the length of the data to be stored includes: Adding the value of the length and a preset length value to obtain an allocation value; Determining the target level of the memory to be allocated according to the allocation value and a step value of a memory block linked list, wherein the step value is the difference between the maximum storage thresholds of the memory block linked lists of two adjacent levels.
2. The memory allocation method according to claim 1, characterized in that, The storing the data to be stored into the target memory block pointed to by the linked list head according to the memory allocation instruction includes: Storing the length of the data to be stored into a memory unit with a preset length starting from the starting position of the target memory block according to the memory allocation instruction; Storing the data to be stored into the memory unit after the memory unit with the preset length.
3. The memory allocation method according to claim 1, characterized in that, Before determining the target linked list corresponding to the target level according to the target level of the memory to be allocated, it further includes: Determining a target memory block capacity corresponding to the target level according to the target level of the memory to be allocated; Determining at least one empty memory block matching the target memory block capacity from the memory according to the target memory block capacity; Establishing the target linked list corresponding to the target level, respectively pointing each pointer of the target linked list to each empty memory block matching the target memory block capacity, and pointing the linked list head of the target linked list to the first empty memory block among the empty memory blocks matching the target memory block capacity.
4. The memory allocation method according to claim 1, characterized in that, The determining the target level of the memory to be allocated according to the allocation value and the step value of the memory block linked list includes: Determining a target ratio of the allocation value to the step value; Taking the memory level corresponding to the target ratio as the target level of the memory to be allocated.
5. The memory allocation method according to claim 4, characterized in that, The determining the target ratio of the allocation value to the step value includes: If the initial ratio of the allocation value to the step value is a non-integer, rounding the initial ratio to obtain the target ratio.
6. The memory allocation method according to any one of claims 1-5, characterized in that, After storing the data to be stored in the target memory block according to the memory allocation instruction, the method further includes: Deleting the data stored in the target memory block according to a memory release instruction for the target memory block; Updating the target linked list so that the target memory block is inserted into the target linked list as an empty memory block.
7. The memory allocation method according to claim 6, wherein, The updating the target linked list includes: Pointing the head of the target linked list to the target memory block.
8. The memory allocation method according to any one of claims 1-5, wherein, The method further includes: Determining the number of empty memory blocks in the target linked list; If the number of empty memory blocks in the target linked list is greater than or equal to a preset threshold, releasing at least one empty memory block arranged in the target linked list in sequence.
9. The memory allocation method according to any one of claims 1-5, wherein, The method further includes: If the target linked list is empty, allocating a plurality of memory blocks and designating one of the plurality of memory blocks as the target memory block; Inserting the other memory blocks of the plurality of memory blocks into the target linked list.
10. The memory allocation method according to any one of claims 1-5, wherein, The determining the target memory block according to the head of the linked list includes: Determining the head node of the target linked list and determining the head of the target linked list according to the head node of the target linked list; Designating the memory block pointed to by the head of the linked list as the target memory block.
11. A memory allocation device, wherein, The device includes: A first determination module for determining the target level of the memory to be allocated according to the length of the data to be stored; A second determination module for determining the target linked list corresponding to the target level according to the target level of the memory to be allocated, the target linked list including at least one pointer and a head of the linked list, each pointer pointing to a memory block respectively, each pointer being used to indicate the address of each memory block in the target linked list, and the head of the linked list being the first pointer among the pointers, and the head of the linked list being used to point to the first empty memory block among the memory blocks in the target linked list; A third determination module for determining the target memory block according to the head of the linked list; A storage processing module for storing the data to be stored in the target memory block according to a memory allocation instruction and pointing the head of the linked list to the first empty memory block after the target memory block; The first determination module is specifically configured to sum the value of the length and a preset length value to obtain a value to be allocated; Determining the target level of the memory to be allocated according to the value to be allocated and the step value of the memory block linked list, wherein the step value is the difference between the maximum storage thresholds of the memory block linked lists of two adjacent levels.
12. A computer device, wherein, including: A memory and a processor, wherein the memory stores a computer program that can run on the processor, and when the processor executes the computer program, the steps of the method according to any one of claims 1 to 10 are implemented.
13. A computer-readable storage medium, wherein, A computer program is stored on the computer-readable storage medium. When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 10 are implemented.
Citation Information
Patent Citations
Quick-speed application EMS memory method
CN101140531A