Memory allocation method and related device
By receiving and processing memory allocation requests, allocating memory to threads based on expected memory capacity, and recording and releasing memory attribute information, it solves the problem of insufficient memory resources in embedded systems, improves the utilization of memory resources, and ensures the stability of the system. and business continuity.
Patent Information
- Application Number
- CN202011004107.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-09-22
- Publication Date
- 2024-09-27
- Estimated Expiration
- 2040-09-22
AI Technical Summary
In embedded systems, threads cannot apply for memory resources due to insufficient system memory resources, resulting in task processing failure and affecting system business. How to improve the utilization of memory resources.
By receiving memory allocation requests, allocate target memory to threads based on expected memory capacity, and record memory attribute information. After receiving memory release requests, the memory is released based on attribute information to ensure the efficiency and accuracy of memory allocation and release.
It improves the utilization of memory resources, avoids thread task processing failures due to insufficient memory, and reduces the business impact of the system.
Smart Images

Figure CN112214313B
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer storage technology, and in particular to a memory allocation method and related equipment. Background Art
[0002] Embedded systems often use multiple threads to complete different tasks. Multiple threads can use the same system memory resources, but system memory resources are limited. When a thread cannot apply for memory resources due to insufficient system memory resources, the thread cannot process tasks, which has a huge impact on the system's business. Therefore, how to improve the utilization of memory resources is an urgent problem to be solved. Summary of the invention
[0003] The embodiments of the present application provide a memory allocation method and related devices for improving the utilization of memory resources.
[0004] In a first aspect, an embodiment of the present application provides a memory allocation method, which is applied to an electronic device, and the method includes:
[0005] receiving a memory allocation request, the memory allocation request being used to request allocation of memory for the first thread, the memory allocation request carrying an expected memory capacity;
[0006] Allocating target memory to the first thread based on the expected memory capacity, and recording attribute information of the target memory;
[0007] receiving a memory release request, wherein the memory release request is used to release the target memory;
[0008] The target memory is released based on the attribute information.
[0009] In a second aspect, an embodiment of the present application provides a memory allocation device, which is applied to an electronic device, and the device includes:
[0010] A first receiving unit, configured to receive a memory allocation request, wherein the memory allocation request is used to request allocation of memory for a first thread, and the memory allocation request carries an expected memory capacity;
[0011] an allocating unit, configured to allocate a target memory to the first thread based on the expected memory capacity;
[0012] A recording unit, used for recording attribute information of the target memory;
[0013] A second receiving unit, configured to receive a memory release request, wherein the memory release request is used to release the target memory;
[0014] A releasing unit is used to release the target memory based on the attribute information.
[0015] In a third aspect, an embodiment of the present application provides an electronic device, comprising a processor, a memory, and one or more programs, wherein the one or more programs are stored in the memory and are configured to be executed by the processor, and the one or more programs include instructions for executing the steps in the method described in the first aspect of the embodiment of the present application.
[0016] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program for electronic data exchange, wherein the computer program enables a computer to execute part or all of the steps described in the method described in the first aspect of the embodiment of the present application.
[0017] In a fifth aspect, an embodiment of the present application provides a computer program product, wherein the computer program product includes a non-transitory computer-readable storage medium storing a computer program, and the computer program is operable to cause a computer to execute some or all of the steps described in the method described in the first aspect of the embodiment of the present application. The computer program product may be a software installation package.
[0018] It can be seen that in the embodiment of the present application, a memory allocation request is first received, the memory allocation request is used to request memory allocation for the first thread, and the memory allocation request carries the expected memory capacity; then the target memory is allocated to the first thread based on the expected memory capacity, and the attribute information of the target memory is recorded; then a memory release request is received, and the memory release request is used to release the target memory; finally, the target memory is released based on the attribute information. Since the electronic device allocates the target memory to the first thread according to the expected memory capacity, rather than arbitrarily allocating memory to the first thread, it is beneficial to improve the utilization rate of memory resources. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] In order to more clearly illustrate the embodiments of the present application 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, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0020] Figure 1 It is a flowchart of a memory allocation method provided in an embodiment of the present application;
[0021] Figure 2 This is a memory allocation architecture diagram provided by an embodiment of the present application;
[0022] Figure 3 This is a schematic diagram of attribute information storage provided by an embodiment of the present application;
[0023] Figure 4 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application;
[0024] Figure 5 A schematic diagram of the structure of a memory allocation device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0025] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present application.
[0026] The following are detailed descriptions of each.
[0027] The terms "first", "second", "third" and "fourth" etc. in the specification and claims of the present application and the drawings are used to distinguish different objects, rather than to describe a specific order. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device that includes a series of steps or units is not limited to the listed steps or units, but optionally includes steps or units that are not listed, or optionally includes other steps or units inherent to these processes, methods, products or devices.
[0028] Reference to "embodiments" herein means that a particular feature, structure, or characteristic described in conjunction with the embodiments may be included in at least one embodiment of the present application. The appearance of the phrase in various locations in the specification does not necessarily refer to the same embodiment, nor is it an independent or alternative embodiment that is mutually exclusive with other embodiments. It is explicitly and implicitly understood by those skilled in the art that the embodiments described herein may be combined with other embodiments.
[0029] Electronic devices may include various handheld devices, vehicle-mounted devices, wearable devices, computing devices or other processing devices connected to a wireless modem with wireless communication functions, as well as various forms of user equipment (UE), mobile station (MS), terminal device, etc.
[0030] The following is a detailed description of the embodiments of the present application.
[0031] like Figure 1As shown, a memory allocation method provided in an embodiment of the present application is applied to the above electronic device and specifically includes the following steps:
[0032] Step 101: Receive a memory allocation request, where the memory allocation request is used to request memory allocation for a first thread, and the memory allocation request carries an expected memory capacity.
[0033] Before receiving the memory allocation request, the electronic device may have allocated memory for the first thread, or may not have allocated memory for the first thread.
[0034] Step 102: Allocate target memory to the first thread based on the expected memory capacity, and record attribute information of the target memory.
[0035] Among them, Figure 2 In the memory allocation architecture shown, the memory capacity of memory pool 2 is smaller than the memory capacity of memory pool 3, and memory pool 2, memory pool 3 and memory block 4 are all allocated from memory pool 1.
[0036] in, Figure 2 The n in the formula ( 1 ) can be 16, and m is a positive integer. In other embodiments, the value of n can be adjusted accordingly according to actual conditions, that is, it can be greater than 16 or less than 16.
[0037] The memory pool 2 may include 16 memory blocks, each memory block has a memory capacity of 512KB, each memory block includes at least one memory node, and the memory capacity of the memory node is less than or equal to 512KB. In other embodiments, the number of memory blocks included in the memory pool 2 and the memory capacity of each memory block may be adjusted accordingly according to actual conditions.
[0038] The memory pool 3 includes 16 memory blocks, each memory block has a memory capacity of 3M, each memory block includes at least one memory node, and the memory capacity of the memory node is greater than 512KB and less than or equal to 3M. In other embodiments, the number of memory blocks included in the memory pool 3 and the memory capacity of each memory block can be adjusted accordingly according to actual conditions.
[0039] The memory capacity of memory block 4 is greater than 3M. That is, the memory capacity of memory block 4 is greater than the memory capacity of memory blocks in memory pool 3, and the memory capacity of memory blocks in memory pool 3 is greater than the memory capacity of memory blocks in memory pool 2.
[0040] In other embodiments, the memory capacity of the memory block 4 may be adjusted accordingly according to actual conditions, and the relationship between the memory capacity of the memory block 4 and the memory capacity of the memory blocks in the memory pool 3 and the memory capacity of the memory blocks in the memory pool 2 may also be adjusted accordingly according to actual conditions.
[0041] Among them, if the expected memory capacity is less than or equal to 512KB, memory nodes are allocated from memory blocks in memory pool 2. If the expected memory capacity is greater than 512KB and less than or equal to 3M, memory nodes are allocated from memory blocks in memory pool 3. If the expected memory capacity is greater than 3M, memory blocks are taken from memory pool 1.
[0042] The target memory can be Figure 1 It can be memory block 4 in memory pool 2 or memory node in memory pool 3.
[0043] The attribute information includes the address of the target memory, the address of the memory block where the target memory is located, and the actual memory capacity applied for by the first thread.
[0044] The capacity of the target memory is greater than or equal to the actual memory capacity applied for by the first thread, and the first thread is allowed to use the actual memory capacity applied for in the target memory.
[0045] For example, if the capacity of the target memory is 3.5M, and the actual memory capacity applied for by the first thread is 2M, the first thread can use 2M of the target memory.
[0046] Optionally, the memory allocation request also carries an address alignment condition, the address alignment condition is used to determine an attribute record address, the attribute record address is used to store the attribute information, and the attribute record address is determined based on the address alignment condition and the starting address of the target memory.
[0047] The attribute record address is determined based on the address alignment condition and the starting address of the target memory, including:
[0048] Determine a second starting address based on the first starting address of the target memory and the address alignment condition;
[0049] The attribute record address is determined based on the second start address.
[0050] The second starting address is the address returned to the user.
[0051] The address alignment condition is that the second start address is an integer multiple of a preset value, and the preset value may be 256 bytes or other values.
[0052] Among them, Figure 3The figure shows a schematic diagram of attribute information storage. The attribute record addresses storing attribute information include address 1, address 2, address 3 and address 4. Address 1 is the third address before address 4, address 2 is the second address before address 4, and address 3 is the first address before address 4.
[0053] Among them, the address of the memory block where the target memory is located is stored in address 1, the actual memory capacity applied for is stored in address 2, the target memory address is stored in address 3, and address 4 is the second starting address.
[0054] The character type of the address is uint8_t. It can be understood that in other embodiments, the character type of the address can be other types.
[0055] Optionally, the actual memory capacity applied for is determined based on the address alignment condition, the expected memory capacity and the memory capacity required for additional information.
[0056] The additional information includes control information and / or debugging information, such as the size of the current memory segment, the starting address of the next memory segment, a memory boundary mark, and the like.
[0057] The memory capacity required for the additional information in a 32-bit operating system is 12 bytes, and the memory capacity required for the additional information in a 64-bit operating system is 24 bytes.
[0058] For example, if the expected memory capacity is 1M, the address alignment condition is that the second start address is an integer multiple of 256 bytes, and the memory capacity required for the additional information is 24 bytes, then the actual memory capacity applied for is 1M+256byte+24byte.
[0059] Step 103: Receive a memory release request, where the memory release request is used to release the target memory.
[0060] Step 104: Release the target memory based on the attribute information.
[0061] It can be seen that in the embodiment of the present application, a memory allocation request is first received, the memory allocation request is used to request memory allocation for the first thread, and the memory allocation request carries the expected memory capacity; then the target memory is allocated to the first thread based on the expected memory capacity, and the attribute information of the target memory is recorded; then a memory release request is received, and the memory release request is used to release the target memory; finally, the target memory is released based on the attribute information. Since the electronic device allocates the target memory to the first thread according to the expected memory capacity, rather than arbitrarily allocating memory to the first thread, it is beneficial to improve the utilization rate of memory resources.
[0062] In an implementation of the present application, allocating a target memory to the first thread based on the expected memory capacity includes:
[0063] If the expected memory capacity is greater than the first memory capacity, allocating the target memory from the first memory pool, the capacity of the target memory being greater than the expected memory capacity;
[0064] If the expected memory capacity is less than or equal to the first memory capacity, the target memory is allocated based on the occupancy information of the second memory pool and the expected memory capacity, and the capacity of the first memory pool is greater than the capacity of the second memory pool.
[0065] The first memory capacity may be 3M or other capacities.
[0066] Wherein, when the expected memory capacity is greater than the first memory capacity, the target memory is Figure 1 Memory block 4 in.
[0067] The second memory pool is allocated from the first memory pool. The second memory pool may be Figure 1 Memory pool 2 in , or it may be Figure 1 Memory pool 3 in.
[0068] It can be seen that allocating target memory according to expected memory capacity is conducive to improving memory utilization.
[0069] In an implementation of the present application, the second memory pool includes N first memory blocks, each of the first memory blocks includes at least one memory node, and N is a positive integer;
[0070] The allocating the target memory based on the occupancy information of the second memory pool and the expected memory capacity includes:
[0071] In a case where the first thread does not occupy the N first memory blocks and the N first memory blocks are all occupied, or the N first memory blocks are all occupied, the first thread occupies M first memory blocks, and there is no free memory node in the M first memory blocks, the first memory is allocated from the first memory pool, the target memory is determined in the first memory, and the target memory is allocated, where M is a positive integer and is less than or equal to N;
[0072] In a case where the first thread occupies the M first memory blocks and there are free memory nodes in the M first memory blocks, determining the target memory based on the M first memory blocks, and allocating the target memory;
[0073] When the first thread occupies the M first memory blocks, there are no free memory nodes in the M first memory blocks, and the N first memory blocks include a first free memory block, the target memory is determined based on the first free memory blocks, and the target memory is allocated. The first free memory block is an unoccupied first memory block among the N first memory blocks except the M first memory blocks.
[0074] The capacities of the N first memory blocks may be the same.
[0075] The capacity of each memory node may be the same.
[0076] Among them, the memory nodes in the same memory block can only be occupied by one thread, and different threads need to occupy memory nodes in different memory blocks.
[0077] If the first thread occupies M first memory blocks, the M first memory blocks form a bidirectional circular linked list, the head of the linked list may be the first memory block first occupied by the first thread, and the tail of the linked list may be the first memory block last occupied by the first thread.
[0078] The first memory may be divided into 16 memory blocks, the 16 memory blocks include the target memory, and the memory capacity of each of the 16 memory blocks may be 3M or 512KB.
[0079] The first free memory block is stored in the free pool in the form of a one-way linked list.
[0080] The target memory is determined based on the first free block, that is, the first free block as the head of the one-way linked list is determined as the target memory.
[0081] It can be seen that in the embodiment of the present application, the first thread does not occupy the memory nodes in the memory blocks occupied by other threads, which is beneficial to avoid the electronic device locking the memory nodes and reduces the computational complexity of the electronic device.
[0082] In an implementation of the present application, the header information of the first memory is stored in a system heap.
[0083] The header information of the first memory includes control information and debugging information, such as the size of the current memory segment, the starting address of the next memory segment, a memory boundary mark, and the like.
[0084] In an implementation of the present application, determining the target memory in the first memory includes:
[0085] Dividing the first memory into S second memory blocks, and placing the S second memory blocks into the second memory pool;
[0086] Determining a first target memory block among the S second memory blocks;
[0087] A first target memory node in the first target memory block is determined as the target memory.
[0088] The S second memory blocks form a one-way linked list, and the header of the one-way linked list is the first target memory block.
[0089] The memory capacities of the S second memory blocks are the same.
[0090] Here, S can be 16 or other values.
[0091] The memory nodes in the first target memory block form a one-way linked list, and the header of the one-way linked list is the first target memory node.
[0092] It can be seen that in the embodiment of the present application, the header information of the first memory is stored in the system heap so that the header information of the first memory does not occupy the storage space of the first memory, which is beneficial to increase the number of second memory blocks.
[0093] In an implementation of the present application, the free memory node includes a first free memory node and / or a second free memory node, the first free memory node is a memory node that has not been occupied, or a memory node applied for by the first thread and released by the first thread, and the second free memory node is a memory node applied for by the first thread and not released by the first thread;
[0094] The determining the target memory based on the M first memory blocks includes:
[0095] In a case where the first idle memory node exists in the M first memory blocks, determining a second target memory node in the first idle memory node as the target memory;
[0096] In a case where the first free memory node does not exist in the M first memory blocks and the second free memory node exists in the M first memory blocks, the third target memory node in the second free memory nodes is determined as the target memory.
[0097] Among them, if the first idle node is a memory node applied for and released by the first thread, then the first idle node is a private memory node of the first thread, and the first idle node constitutes a one-way linked list, and the header of the one-way linked list is the second target memory node.
[0098] After determining that the head of the one-way linked list is the second target memory node, the one-way linked list does not include the second target memory node, and the head of the one-way linked list is the downstream neighbor node of the second target memory node.
[0099] The second idle node is a common memory node of the first thread, and the second idle nodes form a one-way linked list, and the header of the one-way linked list is the third target memory node.
[0100] After determining that the head of the one-way linked list is the third target memory node, the one-way linked list does not include the third target memory node, and the head of the one-way linked list is the downstream neighbor node of the third target memory node.
[0101] The second idle node is a memory node to be released.
[0102] It can be seen that in the embodiment of the present application, since the time required to release the memory node is much longer than the time to apply for the memory node, the second idle node is used to avoid the problem of memory exhaustion after the thread runs for a long time.
[0103] In an implementation of the present application, determining the target memory based on the first free memory block includes:
[0104] Determine a second target memory block in the first free memory block;
[0105] A fourth target memory node in the second target memory block is determined as the target memory.
[0106] The first free memory block is stored in the free pool in the form of a one-way linked list, and the header of the one-way linked list is the second target memory block.
[0107] After determining that the header of the one-way linked list is the second target memory block, the one-way linked list does not include the second target memory block, and the header of the one-way linked list is the downstream neighbor node of the second target memory block.
[0108] The memory nodes in the second target memory block form a single-link list, and the header of the one-way linked list is the fourth target memory node.
[0109] After determining that the head of the one-way linked list is the fourth target memory node, the one-way linked list does not include the fourth target memory node, and the head of the one-way linked list is the downstream neighbor node of the fourth target memory node.
[0110] It can be seen that in the embodiment of the present application, determining the second target memory block in the first free memory block is conducive to improving memory utilization.
[0111] In an implementation of the present application, the attribute information includes a first target address of the target memory, a second target address of a third memory block where the target memory is located;
[0112] The releasing the target memory based on the attribute information comprises:
[0113] If the second target address is the first address, based on the first target address, using a second thread to release the target memory;
[0114] If the second target address is not the first address, based on the first target address and the second target address, a third thread is used to release the target memory.
[0115] The value corresponding to the first address may be zero or other values.
[0116] The second thread and the third thread may be the same thread or may not be the same thread.
[0117] If the second target address is the first address, it means that the target memory is allocated by the first memory pool, and the target memory is released to the first memory pool.
[0118] If the second target address is not the first address, the memory block where the target memory is located can be determined based on the second target address, and the target memory can be released to the third memory block based on the first target address.
[0119] It can be seen that in the embodiment of the present application, determining the memory where the target memory is located through the second target address is conducive to improving the efficiency of releasing the target memory.
[0120] In an implementation of the present application, after the target memory is released by the third thread, the method further includes:
[0121] In a case where the first thread and the third thread are not the same thread and the first thread has been closed, defining a public memory node included in the third memory block as a private memory node of the third memory block;
[0122] When the number of memory nodes allocated to the third memory block is the first number, the third thread is used to release the third memory block.
[0123] Therein, the first number may be zero.
[0124] Optionally, if the first thread and the third thread are the same thread, the target memory node is defined as a private node of the first thread, and the number of memory nodes that can be allocated to the third memory block is reduced by one;
[0125] When the number of memory nodes that can be allocated to the third memory block is zero, the third memory block is released.
[0126] Optionally, if the first thread and the third thread are not the same thread and the first thread has not ended, the target memory node is defined as a common node of the third memory block.
[0127] It can be seen that in an embodiment of the present application, when the first thread has ended, the target memory node is defined as a private node of the first thread, and the memory block to which the target memory node belongs is not changed, thereby avoiding the problem of memory exhaustion caused by the third thread being able to release the memory node but not being responsible for the memory node application.
[0128] In an implementation of the present application, if the expected memory capacity is greater than the second memory capacity, the second memory pool is determined to be a third memory pool, and the second memory capacity is less than the first memory capacity;
[0129] If the expected memory capacity is less than or equal to the second memory capacity, the second memory pool is determined to be the fourth memory pool, and the capacity of the memory nodes included in the third memory pool is greater than the capacity of the memory nodes included in the fourth memory pool.
[0130] The third memory pool and the fourth memory pool are both allocated from the first memory pool.
[0131] Among them, the third memory pool is Figure 1 Memory pool 2 in the fourth memory pool is Figure 1 Memory pool 3 in.
[0132] Among them, the unoccupied memory blocks in the third memory pool can form a one-way linked list.
[0133] The fourth memory pool includes at least one memory block, and each memory block includes at least one memory node.
[0134] Among them, the unoccupied memory blocks in the fourth memory pool can form a one-way linked list.
[0135] It can be seen that in the embodiment of the present application, different target memory capacities apply for different memory nodes, which is beneficial to improving memory utilization.
[0136] With the above Figure 1 For details on the embodiments shown in the drawings, please refer to Figure 4 , Figure 4 1 is a schematic diagram of the structure of an electronic device provided by an embodiment of the present application. As shown in the figure, the electronic device includes a processor, a memory, and one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the processor, and the program includes instructions for executing the following steps:
[0137] receiving a memory allocation request, the memory allocation request being used to request allocation of memory for the first thread, the memory allocation request carrying an expected memory capacity;
[0138] Allocating target memory to the first thread based on the expected memory capacity, and recording attribute information of the target memory;
[0139] receiving a memory release request, wherein the memory release request is used to release the target memory;
[0140] The target memory is released based on the attribute information.
[0141] In an implementation of the present application, in terms of allocating target memory to the first thread based on the expected memory capacity, the program includes instructions for executing the following steps:
[0142] If the expected memory capacity is greater than the first memory capacity, allocating the target memory from the first memory pool, the capacity of the target memory being greater than the expected memory capacity;
[0143] If the expected memory capacity is less than or equal to the first memory capacity, the target memory is allocated based on the occupancy information of the second memory pool and the expected memory capacity, and the capacity of the first memory pool is greater than the capacity of the second memory pool.
[0144] In an implementation of the present application, the second memory pool includes N first memory blocks, each of the first memory blocks includes at least one memory node, and N is a positive integer; in terms of allocating the target memory based on the occupancy information of the second memory pool and the expected memory capacity, the above program is specifically used to execute the following steps:
[0145] In a case where the first thread does not occupy the N first memory blocks and the N first memory blocks are all occupied, or the N first memory blocks are all occupied, the first thread occupies M first memory blocks, and there is no free memory node in the M first memory blocks, the first memory is allocated from the first memory pool, the target memory is determined in the first memory, and the target memory is allocated, where M is a positive integer and is less than or equal to N;
[0146] In a case where the first thread occupies the M first memory blocks and there are free memory nodes in the M first memory blocks, determining the target memory based on the M first memory blocks, and allocating the target memory;
[0147] When the first thread occupies the M first memory blocks, there are no free memory nodes in the M first memory blocks, and the N first memory blocks include a first free memory block, the target memory is determined based on the first free memory blocks, and the target memory is allocated. The first free memory block is an unoccupied first memory block among the N first memory blocks except the M first memory blocks.
[0148] In an implementation of the present application, the header information of the first memory is stored in a system heap.
[0149] In an implementation of the present application, in terms of determining the target memory in the first memory, the above program is specifically used to execute instructions of the following steps:
[0150] Dividing the first memory into S second memory blocks, and placing the S second memory blocks into the second memory pool;
[0151] Determining a first target memory block among the S second memory blocks;
[0152] A first target memory node in the first target memory block is determined as the target memory.
[0153] In an implementation of the present application, the free memory node includes a first free memory node and / or a second free memory node, the first free memory node is a memory node that has not been occupied, or a memory node applied for by the first thread and released by the first thread, and the second free memory node is a memory node applied for by the first thread and not released by the first thread;
[0154] In terms of determining the target memory based on the M first memory blocks, the above program is specifically used to execute instructions of the following steps:
[0155] In a case where the first idle memory node exists in the M first memory blocks, determining a second target memory node in the first idle memory node as the target memory;
[0156] In a case where the first free memory node does not exist in the M first memory blocks and the second free memory node exists in the M first memory blocks, the third target memory node in the second free memory nodes is determined as the target memory.
[0157] In an implementation of the present application, in terms of determining the target memory based on the first free memory block, the above program is specifically used to execute instructions of the following steps:
[0158] Determine a second target memory block in the first free memory block;
[0159] A fourth target memory node in the second target memory block is determined as the target memory.
[0160] In an implementation of the present application, the attribute information includes a first target address of the target memory, a second target address of a third memory block where the target memory is located;
[0161] In terms of releasing the target memory based on the attribute information, the program is specifically used to execute instructions of the following steps:
[0162] If the second target address is the first address, based on the first target address, using a second thread to release the target memory;
[0163] If the second target address is not the first address, based on the first target address and the second target address, a third thread is used to release the target memory.
[0164] In an implementation of the present application, after the target memory is released by the third thread, the program is further used to execute the following steps:
[0165] In a case where the first thread and the third thread are not the same thread and the first thread has been closed, defining a public memory node included in the third memory block as a private memory node of the third memory block;
[0166] When the number of memory nodes allocated to the third memory block is the first number, the third thread is used to release the third memory block.
[0167] In an implementation of the present application, if the expected memory capacity is greater than the second memory capacity, the second memory pool is determined to be a third memory pool, and the second memory capacity is less than the first memory capacity;
[0168] If the expected memory capacity is less than or equal to the second memory capacity, the second memory pool is determined to be the fourth memory pool, and the capacity of the memory nodes included in the third memory pool is greater than the capacity of the memory nodes included in the fourth memory pool.
[0169] It should be noted that the specific implementation process of this embodiment can refer to the specific implementation process described in the above method embodiment, which will not be described here.
[0170] See also Figure 5 , Figure 5 A memory allocation device provided in an embodiment of the present application is applied to the above electronic device, and the device includes:
[0171] A first receiving unit 501 is used to receive a memory allocation request, where the memory allocation request is used to request memory allocation for a first thread, and the memory allocation request carries an expected memory capacity;
[0172] An allocating unit 502 is configured to allocate a target memory to the first thread based on the expected memory capacity;
[0173] A recording unit 503, used to record the attribute information of the target memory;
[0174] A second receiving unit 504 is used to receive a memory release request, where the memory release request is used to release the target memory;
[0175] The releasing unit 505 is configured to release the target memory based on the attribute information.
[0176] In an implementation of the present application, in terms of allocating target memory to the first thread based on the expected memory capacity, the allocating unit 502 is used to execute instructions of the following steps:
[0177] If the expected memory capacity is greater than the first memory capacity, allocating the target memory from the first memory pool, the capacity of the target memory being greater than the expected memory capacity;
[0178] If the expected memory capacity is less than or equal to the first memory capacity, the target memory is allocated based on the occupancy information of the second memory pool and the expected memory capacity, and the capacity of the first memory pool is greater than the capacity of the second memory pool.
[0179] In an implementation of the present application, the second memory pool includes N first memory blocks, each of the first memory blocks includes at least one memory node, and N is a positive integer; in terms of allocating the target memory based on the occupancy information of the second memory pool and the expected memory capacity, the allocation unit 502 is specifically used to execute the following steps:
[0180] In a case where the first thread does not occupy the N first memory blocks and the N first memory blocks are all occupied, or the N first memory blocks are all occupied, the first thread occupies M first memory blocks, and there is no free memory node in the M first memory blocks, the first memory is allocated from the first memory pool, the target memory is determined in the first memory, and the target memory is allocated, where M is a positive integer and is less than or equal to N;
[0181] In a case where the first thread occupies the M first memory blocks and there are free memory nodes in the M first memory blocks, determining the target memory based on the M first memory blocks, and allocating the target memory;
[0182] When the first thread occupies the M first memory blocks, there are no free memory nodes in the M first memory blocks, and the N first memory blocks include a first free memory block, the target memory is determined based on the first free memory blocks, and the target memory is allocated. The first free memory block is an unoccupied first memory block among the N first memory blocks except the M first memory blocks.
[0183] In an implementation of the present application, the header information of the first memory is stored in a system heap.
[0184] In an implementation of the present application, in determining the target memory in the first memory, the allocation unit 502 is specifically used to execute instructions of the following steps:
[0185] Dividing the first memory into S second memory blocks, and placing the S second memory blocks into the second memory pool;
[0186] Determining a first target memory block among the S second memory blocks;
[0187] A first target memory node in the first target memory block is determined as the target memory.
[0188] In an implementation of the present application, the free memory node includes a first free memory node and / or a second free memory node, the first free memory node is a memory node that has not been occupied, or a memory node applied for by the first thread and released by the first thread, and the second free memory node is a memory node applied for by the first thread and not released by the first thread;
[0189] In terms of determining the target memory based on the M first memory blocks, the allocation unit 502 is specifically used to execute instructions of the following steps:
[0190] In a case where the first idle memory node exists in the M first memory blocks, determining a second target memory node in the first idle memory node as the target memory;
[0191] In a case where the first free memory node does not exist in the M first memory blocks and the second free memory node exists in the M first memory blocks, the third target memory node in the second free memory nodes is determined as the target memory.
[0192] In an implementation of the present application, in determining the target memory based on the first free memory block, the allocation unit 502 is specifically used to execute instructions of the following steps:
[0193] Determine a second target memory block in the first free memory block;
[0194] A fourth target memory node in the second target memory block is determined as the target memory.
[0195] In an implementation of the present application, the attribute information includes a first target address of the target memory, a second target address of a third memory block where the target memory is located;
[0196] In terms of releasing the target memory based on the attribute information, the release unit 305 is specifically used to execute instructions of the following steps:
[0197] If the second target address is the first address, based on the first target address, using a second thread to release the target memory;
[0198] If the second target address is not the first address, based on the first target address and the second target address, a third thread is used to release the target memory.
[0199] In an implementation of the present application, the memory allocation device further includes a definition unit 506 and a determination unit 507 .
[0200] In an implementation of the present application, after the target memory is released by the third thread, the definition unit 506 is specifically used to execute the following steps:
[0201] In a case where the first thread and the third thread are not the same thread and the first thread has been closed, defining a public memory node included in the third memory block as a private memory node of the third memory block;
[0202] The release unit 505 is also specifically used to execute instructions of the following steps:
[0203] When the number of memory nodes allocated to the third memory block is the first number, the third thread is used to release the third memory block.
[0204] In an implementation of the present application, the determining unit 507 is specifically configured to execute instructions of the following steps:
[0205] If the expected memory capacity is greater than the second memory capacity, determining that the second memory pool is a third memory pool, and the second memory capacity is less than the first memory capacity;
[0206] If the expected memory capacity is less than or equal to the second memory capacity, the second memory pool is determined to be the fourth memory pool, and the capacity of the memory nodes included in the third memory pool is greater than the capacity of the memory nodes included in the fourth memory pool.
[0207] It should be noted that the first receiving unit 501, the allocating unit 502, the recording unit 503, the second receiving unit 504, the releasing unit 505, the defining unit 506 and the determining unit 507 of the electronic device may be implemented by a processor.
[0208] An embodiment of the present application also provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program for electronic data exchange, wherein the computer program enables a computer to execute some or all of the steps described for the electronic device in the above method embodiment.
[0209] The present application also provides a computer program product, wherein the computer program product includes a non-transitory computer-readable storage medium storing a computer program, and the computer program is operable to cause a computer to execute some or all of the steps described for the electronic device in the above method. The computer program product may be a software installation package.
[0210] The steps of the method or algorithm described in the embodiment of the present application can be implemented in a hardware manner, or can be implemented by a processor executing software instructions. The software instructions can be composed of corresponding software modules, and the software modules can be stored in a random access memory (Random Access Memory, RAM), a flash memory, a read-only memory (Read Only Memory, ROM), an erasable programmable read-only memory (Erasable Programmable ROM, EPROM), an electrically erasable programmable read-only memory (Electrically EPROM, EEPROM), a register, a hard disk, a mobile hard disk, a read-only compact disk (CD-ROM) or any other form of storage medium known in the art. An exemplary storage medium is coupled to the processor so that the processor can read information from the storage medium and write information to the storage medium. Of course, the storage medium can also be a component of the processor. The processor and the storage medium can be located in an ASIC. In addition, the ASIC can be located in an access network device, a target network device, or a core network device. Of course, the processor and the storage medium can also be present in an access network device, a target network device, or a core network device as discrete components.
[0211] Those skilled in the art should be aware that in one or more of the above examples, the functions described in the embodiments of the present application can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiments of the present application is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium, for example, the computer instructions can be transmitted from a website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (Digital Subscriber Line, DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, server or data center. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrations. The available medium may be a magnetic medium (eg, a floppy disk, a hard disk, a magnetic tape), an optical medium (eg, a digital video disc (DVD)), or a semiconductor medium (eg, a solid state disk (SSD)).
[0212] The specific implementation methods described above further illustrate the purpose, technical solutions and beneficial effects of the embodiments of the present application. It should be understood that the above description is only the specific implementation method of the embodiments of the present application and is not intended to limit the protection scope of the embodiments of the present application. Any modifications, equivalent substitutions, improvements, etc. made on the basis of the technical solutions of the embodiments of the present application should be included in the protection scope of the embodiments of the present application.
Claims
1. A memory allocation method, characterized in that: Applied to electronic equipment, the method comprises: receiving a memory allocation request, the memory allocation request being used to request memory allocation for the first thread, the memory allocation request carrying an expected memory capacity and an address alignment condition; Allocate a target memory for the first thread based on the expected memory capacity, and record attribute information of the target memory; wherein the attribute information of the target memory is stored in an attribute record address, the attribute record address is determined based on the address alignment condition and the starting address of the target memory, the attribute information includes the address of the target memory, the address of the memory block where the target memory is located, and the actual memory capacity applied for by the first thread, the actual memory capacity applied for is determined based on the address alignment condition, the expected memory capacity, and the memory capacity required by additional information, the additional information includes control information and / or debugging information; receiving a memory release request, wherein the memory release request is used to release the target memory; releasing the target memory based on the attribute information; The attribute information includes a first target address of the target memory and a second target address of a third memory block where the target memory is located; The releasing the target memory based on the attribute information comprises: If the second target address is the first address, based on the first target address, the target memory is released using a second thread; wherein the first address is an address allocated by the first memory pool; If the second target address is not the first address, based on the first target address and the second target address, using a third thread to release the target memory; In a case where the first thread and the third thread are not the same thread and the first thread has been closed, defining a public memory node included in the third memory block as a private memory node of the third memory block; When the number of memory nodes allocated to the third memory block is the first number, the third thread is used to release the third memory block.
2. The method according to claim 1, characterized in that The allocating a target memory to the first thread based on the expected memory capacity includes: If the expected memory capacity is greater than the first memory capacity, allocating the target memory from the first memory pool, the capacity of the target memory being greater than the expected memory capacity; If the expected memory capacity is less than or equal to the first memory capacity, the target memory is allocated based on the occupancy information of the second memory pool and the expected memory capacity, and the capacity of the first memory pool is greater than the capacity of the second memory pool.
3. The method according to claim 2, characterized in that The second memory pool includes N first memory blocks, each of the first memory blocks includes at least one memory node, and N is a positive integer; The allocating the target memory based on the occupancy information of the second memory pool and the expected memory capacity includes: In a case where the first thread does not occupy the N first memory blocks and the N first memory blocks are all occupied, or the N first memory blocks are all occupied, the first thread occupies M first memory blocks, and there is no free memory node in the M first memory blocks, the first memory is allocated from the first memory pool, the target memory is determined in the first memory, and the target memory is allocated, where M is a positive integer and is less than or equal to N; In a case where the first thread occupies the M first memory blocks and there are free memory nodes in the M first memory blocks, determining the target memory based on the M first memory blocks, and allocating the target memory; When the first thread occupies the M first memory blocks, there are no free memory nodes in the M first memory blocks, and the N first memory blocks include a first free memory block, the target memory is determined based on the first free memory blocks, and the target memory is allocated. The first free memory block is an unoccupied first memory block among the N first memory blocks except the M first memory blocks.
4. The method according to claim 3, characterized in that: The header information of the first memory is stored in a system heap.
5. The method according to claim 4, characterized in that The determining the target memory in the first memory comprises: Dividing the first memory into S second memory blocks, and placing the S second memory blocks into the second memory pool; Determining a first target memory block among the S second memory blocks; A first target memory node in the first target memory block is determined as the target memory.
6. The method according to claim 3, characterized in that The free memory node includes a first free memory node and / or a second free memory node, the first free memory node is a memory node that has not been occupied, or a memory node applied for by the first thread and released by the first thread, and the second free memory node is a memory node applied for by the first thread and not released by the first thread; The determining the target memory based on the M first memory blocks includes: In a case where the first idle memory node exists in the M first memory blocks, determining a second target memory node in the first idle memory node as the target memory; In a case where the first free memory node does not exist in the M first memory blocks and the second free memory node exists in the M first memory blocks, the third target memory node in the second free memory nodes is determined as the target memory.
7. The method according to claim 3, characterized in that The determining the target memory based on the first free memory block comprises: Determine a second target memory block in the first free memory block; A fourth target memory node in the second target memory block is determined as the target memory.
8. The method according to claim 1, characterized in that If the expected memory capacity is greater than the second memory capacity, determining the second memory pool to be the third memory pool, and the second memory capacity is less than the first memory capacity; If the expected memory capacity is less than or equal to the second memory capacity, the second memory pool is determined to be the fourth memory pool, and the capacity of the memory nodes included in the third memory pool is greater than the capacity of the memory nodes included in the fourth memory pool.
9. A memory allocation device, characterized in that: Applied to electronic equipment, the device comprises: A first receiving unit, configured to receive a memory allocation request, wherein the memory allocation request is used to request memory allocation for a first thread, and the memory allocation request carries an expected memory capacity and an address alignment condition; an allocating unit, configured to allocate a target memory to the first thread based on the expected memory capacity; a recording unit, configured to record attribute information of the target memory; wherein the attribute information of the target memory is stored in an attribute recording address, the attribute recording address is determined based on the address alignment condition and the starting address of the target memory, the attribute information includes the address of the target memory, the address of the memory block where the target memory is located, and the actual memory capacity applied for by the first thread, the actual memory capacity applied for is determined based on the address alignment condition, the expected memory capacity, and the memory capacity required by additional information, the additional information including control information and / or debugging information; A second receiving unit, configured to receive a memory release request, wherein the memory release request is used to release the target memory; A releasing unit, configured to release the target memory based on the attribute information; The attribute information includes a first target address of the target memory and a second target address of a third memory block where the target memory is located; In terms of releasing the target memory based on the attribute information, the release unit is specifically configured to, if the second target address is the first address, release the target memory using a second thread based on the first target address; wherein the first address is an address allocated by the first memory pool; if the second target address is not the first address, release the target memory using a third thread based on the first target address and the second target address; The memory allocation device further includes a definition unit, which is specifically used to define the public memory node included in the third memory block as a private memory node of the third memory block when the first thread and the third thread are not the same thread and the first thread is closed, after the target memory is released by the third thread; The releasing unit is further specifically configured to release the third memory block by using the third thread when the number of memory nodes allocated to the third memory block is a first number.
10. An electronic device, characterized in that: The electronic device comprises a processor, a memory and one or more programs, wherein the programs are stored in the memory and are configured to be executed by the processor, and the programs comprise instructions for executing the steps in the method according to any one of claims 1 to 8.
11. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, wherein the computer program is processed to execute the method according to any one of claims 1 to 8.
