Memory management method and device, equipment, medium and program product
By obtaining the release address and memory capacity of the memory to be released, determining the target memory block category, and recording the sub-memory block status in the corresponding status record area, the problem of memory fragmentation and lock competition is solved, and the efficiency of memory allocation and release is improved. Improved system performance.
Patent Information
- Application Number
- CN202411075524.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-07
- Publication Date
- 2024-10-01
AI Technical Summary
In the existing technology, there are lock competition problems caused by memory fragmentation, delayed memory release, and cross-thread release, resulting in a waste of system resources, affecting memory allocation and release efficiency, and reducing program running speed.
By obtaining the release address and memory capacity of the memory to be released, the target memory block category is determined, and the status of the sub-memory block is recorded in the first state record area or the second state record area of the target initial memory block to achieve cross-thread and intra-thread Memory release to avoid lock competition and improve memory allocation and release efficiency.
It simplifies the memory block layout and the memory allocation process of the allocator, quickly locates the memory to be released, reduces search time, avoids lock competition, improves memory allocation and release efficiency, and improves system performance.
Smart Images

Figure CN118733279A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the fields of computer technology and financial technology, and specifically to a memory management method, apparatus, device, medium and program product. Background Art
[0002] When a computer program is running, it needs to store data in memory, and the memory is managed by the memory allocator, which provides two core operations: allocating memory and releasing memory. When a program needs to store data, the memory allocator will allocate a memory area of a specified size from the memory pool for the program to use. When a program no longer needs a certain memory area, it can return the memory to the memory allocator by calling the memory release function so that other programs or other parts of the current program can reuse the memory.
[0003] In the process of implementing the concept of the present disclosure, the inventors discovered that the related technologies have problems such as memory fragmentation, delayed memory release, and lock contention caused by cross-thread release, which will cause waste of system resources, affect the efficiency of memory allocation and memory release, and reduce the program running speed. Summary of the invention
[0004] In view of the above problems, the present disclosure provides a memory management method, apparatus, device, medium and program product for improving the efficiency of memory allocation and memory release.
[0005] According to a first aspect of the present disclosure, there is provided a memory management method, comprising:
[0006] Get the release address and memory capacity of the memory to be released;
[0007] According to the memory capacity, determine the target memory block category of the memory to be released;
[0008] According to the release address and the memory capacity, a plurality of target sub-memory blocks occupied by the memory to be released are determined from the target initial memory block corresponding to the target memory block category;
[0009] In the case where it is determined that the release process of the target sub-memory block belongs to a cross-thread release process, modifying the states of the plurality of target sub-memory blocks in the first state recording area of the target initial memory block to complete the release of the plurality of target sub-memory blocks;
[0010] When it is determined that the release process of the target sub-memory block does not belong to a cross-thread release process, the states of the multiple target sub-memory blocks are modified in the second state recording area of the target initial memory block to complete the release of the multiple target sub-memory blocks.
[0011] According to an embodiment of the present disclosure, the memory block category includes a first memory block category and a second memory block category, the target memory block category is one of the first memory block category and the second memory block category, and the method further includes:
[0012] Determine a first state recording area and / or a second state recording area of the first initial memory block according to a memory capacity of the first initial memory block corresponding to the first memory block category and a memory capacity of the first initial sub-memory block corresponding to the first memory block category;
[0013] Determine the first status record area and / or the second status record area of the second initial memory block based on the memory capacity of the second initial memory block corresponding to the second memory block category and the memory capacity of the second initial sub-memory block corresponding to the second memory block category, wherein the memory capacity of the second initial sub-memory block is the memory capacity of the system memory page.
[0014] According to an embodiment of the present disclosure, the method further includes:
[0015] Scanning the information storage area of the target initial memory block to generate a first scanning result;
[0016] According to the first scanning result, it is determined whether the release process of the target sub-memory block belongs to the cross-thread release process or does not belong to the cross-thread release process.
[0017] According to an embodiment of the present disclosure, after releasing the multiple target sub-memory blocks, the method further includes:
[0018] Scan the first state record area and the second state record area of the target initial memory block;
[0019] When all initial sub-memory blocks in the target initial memory block are in idle state, the memory capacity of the target initial memory block is returned to the parent memory block of the target initial memory block, wherein the memory capacity of the parent memory block is an integer multiple of the memory capacity of the target initial memory block.
[0020] According to an embodiment of the present disclosure, according to the release address and the memory capacity, a plurality of target sub-memory blocks occupied by the memory to be released are determined from the target initial memory block corresponding to the target memory block category, including:
[0021] Determine a target initial memory block corresponding to the target memory block category according to the release address;
[0022] According to the memory capacity, a plurality of target sub-memory blocks occupied by the to-be-released memory are determined from a plurality of initial sub-memory blocks corresponding to the target initial memory block.
[0023] According to an embodiment of the present disclosure, determining a target initial memory block corresponding to a target memory block category according to a release address includes:
[0024] According to the release address and the memory capacity of the target initial memory block, determining the initial address of the target initial memory block occupied by the memory to be released and the target initial memory block from the multiple initial memory blocks;
[0025] According to the initial address and the memory capacity of the memory to be released, a plurality of target sub-memory blocks occupied by the memory to be released are determined from a plurality of initial sub-memory blocks corresponding to the target initial memory block.
[0026] According to an embodiment of the present disclosure, according to the release address and the memory capacity of the target initial memory block, determining the initial address of the target initial memory block occupied by the memory to be released and the target initial memory block from multiple initial memory blocks includes:
[0027] Based on the release address, the memory capacity of the initial memory block is rounded down to determine the initial address information of the target initial memory block and the target initial memory block.
[0028] According to an embodiment of the present disclosure, the memory management method further includes:
[0029] In response to a memory allocation request of an application thread, obtaining a memory capacity of the memory to be allocated in the allocation request;
[0030] Determine the target memory block category to be occupied by the memory to be allocated according to the memory capacity of the memory to be allocated;
[0031] According to the memory capacity of the memory to be allocated and the target memory block category, determining a plurality of sub-memory blocks to be allocated that are to be occupied by the memory to be allocated from a plurality of initial sub-memory blocks corresponding to the target initial memory block;
[0032] The states of the plurality of sub-memory blocks to be allocated are modified in the second state recording area of the target initial memory block to allocate the plurality of sub-memory blocks to be allocated to the requesting thread.
[0033] According to an embodiment of the present disclosure, according to the memory capacity of the memory to be allocated and the target memory block category, a plurality of sub-memory blocks to be allocated that are to be occupied by the memory to be allocated are determined from a plurality of initial sub-memory blocks corresponding to the target initial memory block, including:
[0034] Scanning the first state recording area and / or the second state recording area of the target initial memory block to generate a second scanning result;
[0035] According to the second scanning result, a plurality of initial sub-memory blocks in an idle state that satisfy the memory capacity are determined as sub-memory blocks to be allocated.
[0036] A second aspect of the present disclosure provides a memory management device, including:
[0037] A first acquisition module is used to acquire a release address and a memory capacity of a memory to be released;
[0038] A first determining module, used to determine the target memory block category of the memory to be released according to the memory capacity;
[0039] A second determination module is used to determine a plurality of target sub-memory blocks occupied by the to-be-released memory from a target initial memory block corresponding to the target memory block category according to the release address and the memory capacity;
[0040] a first releasing module, configured to modify the states of the plurality of target sub-memory blocks in the first state recording area of the target initial memory block to complete the release of the plurality of target sub-memory blocks when it is determined that the release process of the target sub-memory block belongs to a cross-thread release process; and
[0041] The second release module is used to modify the status of multiple target sub-memory blocks in the second status record area of the target initial memory block to complete the release of multiple target sub-memory blocks when it is determined that the release process of the target sub-memory block does not belong to a cross-thread release process.
[0042] A third aspect of the present disclosure provides an electronic device, comprising: one or more processors; and a memory for storing one or more computer programs, wherein the one or more processors execute the one or more computer programs to implement the steps of the above method.
[0043] The fourth aspect of the present disclosure further provides a computer-readable storage medium having a computer program or instructions stored thereon, which implements the steps of the above method when the above computer program or instructions are executed by a processor.
[0044] The fifth aspect of the present disclosure further provides a computer program product, including a computer program or instructions, which implement the steps of the above method when the above computer program or instructions are executed by a processor.
[0045] According to an embodiment of the present disclosure, in response to a release request of memory to be released, a memory release function is called according to the release address and memory size of the memory to be released. In memory management, it is not necessary to store the memory capacity of the allocated memory in a memory block, which simplifies the layout of the memory block and simplifies the process of allocating memory by the memory allocator, thereby improving the efficiency of memory release. The category of the memory block is determined according to the memory capacity, and the target initial memory block to which the memory to be released belongs and its corresponding target initial sub-memory block can be quickly located according to the memory capacity, thereby reducing the search time. Furthermore, different release strategies are adopted for the cases where the memory to be released belongs to cross-thread release and does not belong to cross-thread release. The first state recording area is used to record the state of the memory block released across threads, and the second state recording area is used to record the state of the memory block released within the thread. When the thread of the memory to be released is different from the thread at the time of allocation, the state of the memory to be released is recorded in the first state recording area; when the thread of the memory to be released is the same as the thread at the time of allocation, the state of the memory to be released is recorded in the second state recording area. Therefore, when the thread to release the memory is different from the thread at the time of allocation, there is no need to consider whether the thread at the time of allocation is locked or occupied by other threads. Instead, it is executed directly by modifying the first state record area, avoiding the waiting problem caused by lock competition, reducing time consumption, and improving the efficiency of memory allocation and release. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] The above contents and other objects, features and advantages of the present disclosure will become more apparent through the following description of the embodiments of the present disclosure with reference to the accompanying drawings, in which:
[0047] Figure 1 Schematically illustrates an exemplary system architecture that can be applied to the memory management method and apparatus of the present disclosure;
[0048] Figure 2 A flowchart of a memory management method according to an embodiment of the present disclosure is schematically shown;
[0049] Figure 3 A schematic diagram schematically shows the correspondence between memory block categories and initial sub-memory blocks according to an embodiment of the present disclosure;
[0050] Figure 4 A schematic diagram schematically shows the correspondence between threads and memory blocks according to an embodiment of the present disclosure;
[0051] Figure 5 A schematic diagram schematically shows a status recording area according to an embodiment of the present disclosure;
[0052] Fig. 6A and Figure 6B A schematic diagram schematically shows the corresponding relationship between various memory block categories according to an embodiment of the present disclosure;
[0053] Figure 7 The flowchart of the memory release method according to the embodiment of the present disclosure is schematically shown;
[0054] Figure 8 The flowchart of the memory management method according to another embodiment of the present disclosure is schematically shown;
[0055] Fig. 9 The flowchart of a memory management method according to another embodiment of the present disclosure is schematically shown;
[0056] Fig.10 A structural block diagram of a memory management device according to an embodiment of the present disclosure is schematically shown; and
[0057] Fig.11 A block diagram of an electronic device suitable for implementing a memory management method according to an embodiment of the present disclosure is schematically shown. DETAILED DESCRIPTION
[0058] Hereinafter, embodiments of the present disclosure will be described with reference to the accompanying drawings. However, it should be understood that these descriptions are exemplary only and are not intended to limit the scope of the present disclosure. In the following detailed description, for ease of explanation, many specific details are set forth to provide a comprehensive understanding of the embodiments of the present disclosure. However, it is apparent that one or more embodiments may also be implemented without these specific details. In addition, in the following description, descriptions of known structures and technologies are omitted to avoid unnecessary confusion of the concepts of the present disclosure.
[0059] The terms used herein are only for describing specific embodiments and are not intended to limit the present disclosure. The terms "comprise", "include", etc. used herein indicate the existence of the features, steps, operations and / or components, but do not exclude the existence or addition of one or more other features, steps, operations or components.
[0060] All terms (including technical and scientific terms) used herein have the meanings commonly understood by those skilled in the art unless otherwise defined. It should be noted that the terms used herein should be interpreted as having a meaning consistent with the context of this specification and should not be interpreted in an idealized or overly rigid manner.
[0061] When using expressions such as "at least one of A, B, and C, etc.", they should generally be interpreted according to the meaning of the expression commonly understood by those skilled in the art (for example, "a system having at least one of A, B, and C" should include but is not limited to a system having A alone, B alone, C alone, A and B, A and C, B and C, and / or A, B, C, etc.).
[0062] In the embodiments of the present disclosure, the user's authorization or consent is obtained before obtaining or collecting the user's personal information.
[0063] It should be noted that the memory management method and device disclosed herein can be used in the fields of computer technology and financial technology, and can also be used in any field other than the fields of computer technology and financial technology. The application field of the memory management method and device disclosed herein is not limited.
[0064] It should be noted that in the embodiments of the present disclosure, certain software, components, models and other existing solutions in the industry may be mentioned, and they should be regarded as exemplary. Their purpose is only to illustrate the feasibility of implementing the technical solution of the present disclosure, but it does not mean that the applicant has or will necessarily use the solution.
[0065] In the technical solution of the present disclosure, the user information (including but not limited to user personal information, user image information, user device information, such as location information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved are all information and data authorized by the user or fully authorized by all parties, and the collection, storage, use, processing, transmission, provision, disclosure and application of the relevant data comply with relevant laws, regulations and standards, take necessary confidentiality measures, do not violate public order and good morals, and provide corresponding operation entrances for users to choose to authorize or refuse.
[0066] In traditional memory allocation and memory release methods, when a program allocates and releases a large amount of small memory, the memory allocator usually starts delayed release, that is, it caches these small memories first and does not release them to the operating system immediately. If there are a large number of delayed release operations in the system and there are continuous new memory allocation requests, the available memory may be exhausted faster, thereby triggering insufficient memory earlier and affecting the performance of the entire system.
[0067] On the other hand, if the memory allocation operation is performed in one thread, and the memory release operation is performed in another different thread. Since multiple threads operating memory at the same time may cause confusion and errors, it is necessary to use a mutex lock or spin lock mechanism for coordination and protection to ensure that only one thread can access and operate the relevant memory area at the same time to ensure data consistency and correctness. However, the use of these locks will bring additional overhead and waiting time. For example, when a thread wants to release memory, but the lock is occupied by other threads at this time, it needs to wait for the lock to be released, which will cause the thread to block or idle (for spin locks), thereby increasing time consumption and reducing performance. If there are a large number of such cross-thread memory release situations in the program, frequent lock competition and waiting will significantly slow down the entire memory allocation and release process, which will seriously affect the overall performance of the memory allocator and reduce the running efficiency of the program.
[0068] In order to at least partially solve the above technical problems, an embodiment of the present disclosure provides a memory management method, including:
[0069] Get the release address and memory capacity of the memory to be released;
[0070] According to the memory capacity, determine the target memory block category of the memory to be released;
[0071] According to the release address and the memory capacity, a plurality of target sub-memory blocks occupied by the memory to be released are determined from the target initial memory block corresponding to the target memory block category;
[0072] In the case where it is determined that the release process of the target sub-memory block belongs to a cross-thread release process, modifying the states of the plurality of target sub-memory blocks in the first state recording area of the target initial memory block to complete the release of the plurality of target sub-memory blocks;
[0073] When it is determined that the release process of the target sub-memory block does not belong to a cross-thread release process, the states of the multiple target sub-memory blocks are modified in the second state recording area of the target initial memory block to complete the release of the multiple target sub-memory blocks.
[0074] Figure 1 The following schematically shows an exemplary system architecture that can be applied to the memory management method and device of the present disclosure. Figure 1 As shown, the data processing device 100 includes a memory 110 and a processor 120 , and the processor 120 may include a memory allocator 121 for memory allocation.
[0075] According to an embodiment of the present disclosure, the memory 110 may store relevant data generated by the application. For example, the memory 110 may store the running data of the application and the attribute data of the application. For example, the running data may be generated by the application when it is running, and the running data may be temporary data. For example, the attribute data may be a parameter describing the running attributes of the application, and the attribute parameter may be long-term valid data.
[0076] According to an embodiment of the present disclosure, when an application generates relevant data, the memory allocator 121 in the processor 120 may apply for storage space of the memory 110 for the relevant data and store the relevant data in the storage space of the memory.
[0077] According to an embodiment of the present disclosure, the memory allocator 121 in the processor 120 responds to a memory release request from an applying thread or an alternative thread, determines a target memory block category and a plurality of initial sub-memory blocks corresponding to the category according to the memory capacity to be released in the memory release request, and further, according to the release address, determines a target sub-memory block occupied by the memory to be released from the plurality of initial sub-memory blocks determined in the memory 110, and releases the target sub-memory block in a timely manner.
[0078] According to an embodiment of the present disclosure, the memory allocator 121 in the processor 120 responds to the memory allocation request of the applying thread, determines the target memory block category according to the memory capacity to be allocated in the memory allocation request, and determines the initial sub-memory block in the initial sub-memory block corresponding to the target memory block category determined in the memory 110 as the sub-memory block to be allocated, and allocates the sub-memory block to be allocated to the applying thread.
[0079] The memory management method provided in the embodiment of the present disclosure may generally be executed by the memory allocator 121. Accordingly, the memory management device provided in the embodiment of the present disclosure may generally be arranged in the memory allocator 121.
[0080] It should be understood that Figure 1 The number of memory allocators, memories, and processors in the embodiment is only for illustration. Any number of memory allocators, memories, and processors may be provided as required for implementation.
[0081] The following will be based on Figure 1 The system architecture provided by Figure 2~Figure 9 The memory management method of the disclosed embodiment is described in detail.
[0082] Figure 2 The flowchart of the memory management method according to the embodiment of the present disclosure is schematically shown. Figure 3 The following is a schematic diagram showing the correspondence between the memory block category and the initial sub-memory block according to an embodiment of the present disclosure. Figure 2 and Figure 3 Provide explanation.
[0083] like Figure 2 As shown, the memory management of this embodiment includes operations S210 to S250.
[0084] In operation S210, a release address and memory capacity of a memory to be released are obtained.
[0085] According to an embodiment of the present disclosure, in response to a request from a thread to release memory, the release address p and memory capacity n of the memory to be released by the thread are obtained, wherein the location information of the memory block occupied by the memory to be released can be determined according to the release address p, and the specific occupied memory block can be determined according to the memory capacity n. The release address p and memory capacity n are known before the memory is released, that is, when the memory is allocated to the thread. The memory manager passes the memory capacity as a parameter to the release function each time the memory is released.
[0086] In operation S220, a target memory block category of the memory to be released is determined according to the memory capacity.
[0087] According to an embodiment of the present disclosure, a memory block category is determined according to a memory capacity. The number of memory block categories may be determined according to a predetermined memory capacity, and one predetermined memory capacity corresponds to one memory block category. For example, as required, the predetermined memory capacity is 2KB, 30KB, 100KB, 2M, and 10M. In this example, five memory block categories are included. Further, a target memory block category matching the memory capacity is determined from the plurality of memory block categories.
[0088] According to an embodiment of the present disclosure, a target memory block category of memory to be released is determined from multiple memory block categories according to memory capacity, including steps 11 and 12:
[0089] Step 11: Determine whether the memory capacity n is less than or equal to a first threshold N, wherein the first threshold can be set based on experience, for example, the first threshold N is 128KB or 64KB.
[0090] Step 12: When the memory capacity n is less than or equal to the first threshold N, determine whether the memory capacity n is less than or equal to a second threshold, wherein the second threshold is set according to the memory capacity of the system memory page.
[0091] The embodiments of the present disclosure are mainly aimed at the allocation and release of smaller memory blocks. Therefore, the size of the first threshold value N is first set based on experience, and the memory that satisfies the memory capacity n less than or equal to the first threshold value N is adopted by the memory management method of the embodiments of the present disclosure. For the memory whose memory capacity n is greater than the first threshold value N, the memory manager provided by the standard library of the operating system or programming language can be directly used for memory allocation and release, which will not be repeated here. When the memory capacity is less than or equal to the first threshold value N, it indicates that the memory capacity to be released belongs to the smaller memory block defined in the embodiments of the present disclosure. Therefore, the analysis is mainly carried out for the case where it is less than or equal to the first threshold value N.
[0092] According to an embodiment of the present disclosure, according to the judgment result of step 12, the target memory block category of the memory to be released is determined.
[0093] In operation S230, a plurality of target sub-memory blocks occupied by the to-be-released memory are determined from a target initial memory block corresponding to the target memory block category according to the release address and the memory capacity.
[0094] According to the embodiments of the present disclosure, Figure 3 As shown, each memory block category corresponds to one or more initial memory blocks, and the memory capacity of the initial memory blocks of the same memory block category is the same (for example, the memory capacity of initial memory block 1 and initial memory block 2 is the same), and each initial memory block corresponds to multiple initial sub-memory blocks, and the memory capacity of the initial sub-memory blocks of the same memory block category is the same. After determining the target memory block category to which the memory to be released belongs, the target initial memory block occupied by the memory to be released is determined from the multiple initial memory blocks corresponding to the target memory block category (for example, Figure 3 Medium grey initial memory block 2).
[0095] According to an embodiment of the present disclosure, the target initial memory block can also be divided into multiple initial sub-memory blocks. After determining the target initial memory block, the target initial sub-memory block occupied by the to-be-released memory is determined from the multiple initial sub-memory blocks corresponding to the target initial memory block.
[0096] According to an embodiment of the present disclosure, threads include an application thread and a candidate thread, and each initial memory block associated with the application thread and the candidate thread includes a first state recording area, a second state recording area, and an information storage area. The application thread is used to represent the thread that applies for memory in the memory allocation phase, and the candidate thread is used to represent other threads related to the application thread.
[0097] Figure 4 The diagram schematically shows the correspondence between threads and memory blocks according to an embodiment of the present disclosure.
[0098] According to an embodiment of the present disclosure, the memory allocator 121 can divide the corresponding memory space for the application thread or the candidate thread in the memory 110, such as Figure 4 As shown, a corresponding memory space is divided for each thread, and each memory space includes at least a plurality of first initial memory blocks S and a plurality of second initial memory blocks L2. A third memory block L1 may also be included. The memory blocks in each memory block category are linked in a linked list. For example, thread local storage (TLS) technology may be used to divide the corresponding memory space for each thread.
[0099] According to an embodiment of the present disclosure, the fourth memory block H is virtual memory, and the system is not required to reserve physical memory of corresponding size when allocating the fourth memory block H. The third memory block L1 or the second initial memory block L2 is physical memory, and the system is required to reserve physical memory of corresponding size.
[0100] According to an embodiment of the present disclosure, a first state recording area, a second state recording area and an information storage area are configured in the first initial memory block S, the second initial memory block L2 or the third memory block L1 in the memory space corresponding to each thread.
[0101] Figure 5 The diagram schematically shows a status recording area according to an embodiment of the present disclosure.
[0102] like Figure 5 As shown, the first state recording area is used to record the state of memory blocks released across threads. The second state recording area is used to record the state of memory blocks released within a thread. The information storage area is used to record the thread information of the thread to which each initial memory block belongs.
[0103] According to an embodiment of the present disclosure, the information storage area may also include linked list information.
[0104] According to an embodiment of the present disclosure, Figure 5 The positional relationship between the first state recording area, the second state recording area, and the information storage area shown in is only an exemplary description, and the embodiments of the present disclosure do not limit the positional relationship between the first state recording area, the second state recording area, and the information storage area. For example, the configuration may be in the order of the first state recording area, the information storage area, and the second state recording area. Figure 5 The schematic diagram shows that the first state recording area, the second state recording area and the information storage area are configured at the head of the memory block. According to actual conditions, the first state recording area, the second state recording area and the information storage area can also be configured at the tail of each memory block, which is not limited in the embodiment of the present disclosure.
[0105] According to an embodiment of the present disclosure, the first state recording area, the second state recording area and the information storage area are configured in the head area of the memory block. The memory capacity occupied by the first state recording area, the second state recording area and the information storage area can be set based on experience.
[0106] In operation S240, when it is determined that the release process of the target sub-memory block belongs to a cross-thread release process, the states of the multiple target sub-memory blocks are modified in the first state recording area of the target initial memory block to complete the release of the multiple target sub-memory blocks.
[0107] According to the embodiments of the present disclosure, Figure 5 For example, when the thread information to which the target sub-memory blocks B3 and B4 currently belong is different from the thread information to which they belong when they are allocated, the first state record area of the target initial memory block to which the target sub-memory blocks B3 and B4 belong is modified, that is, Figure 5 The first status record area of the first initial memory block S shown in is modified, and the bits corresponding to the target sub-memory blocks B3 and B4 are set to 1 (1 is used in the first status record area to indicate that the sub-memory block is in an idle state), thereby completing the release of multiple target sub-memory blocks.
[0108] In operation S250, when it is determined that the release process of the target sub-memory block does not belong to a cross-thread release process, the states of multiple target sub-memory blocks are modified in the second state recording area of the target initial memory block to complete the release of the multiple target sub-memory blocks.
[0109] According to the embodiments of the present disclosure, Figure 5 For example, when the thread information to which the target sub-memory blocks B3 and B4 currently belong is the same as the thread information to which they belong when they are allocated, the second state record area of the target initial memory block to which the target sub-memory blocks B3 and B4 belong is modified, that is, Figure 5 The second status record area of the first initial memory block S shown in is modified, and the bits corresponding to the target sub-memory blocks B3 and B4 are set to 0 (0 is used in the second status record area to indicate that the sub-memory block is in an idle state), thus completing the release of multiple target sub-memory blocks.
[0110] According to an embodiment of the present disclosure, since multiple candidate threads may write to the first state record area, when executing the above-mentioned cross-thread memory release operation, the corresponding bit of the first state record area of the target initial memory block to which the target sub-memory block belongs is set to 1 through an atomic operation, thereby ensuring the consistency and correctness of the data operation in a multi-thread or concurrent environment. The first state record area is pre-configured in the initial memory block to record the information of cross-thread memory release, and the consistency of information reading and writing is guaranteed in combination with atomic operations. Compared with the method of using various locks to achieve cross-thread release, the waiting problem caused by lock competition is avoided and the memory release time is shortened.
[0111] According to an embodiment of the present disclosure, in response to a release request of memory to be released, a memory release function is called according to the release address and memory size of the memory to be released. In memory management, it is not necessary to store the memory capacity of the allocated memory in a memory block, which simplifies the layout of the memory block and simplifies the process of allocating memory by the memory allocator, thereby improving the efficiency of memory release. The category of the memory block is determined according to the memory capacity, and the target initial memory block to which the memory to be released belongs and its corresponding target initial sub-memory block can be quickly located according to the memory capacity, thereby reducing the search time. Furthermore, different release strategies are adopted for the cases where the memory to be released belongs to cross-thread release and does not belong to cross-thread release. The first state recording area is used to record the state of the memory block released across threads, and the second state recording area is used to record the state of the memory block released within the thread. When the thread of the memory to be released is different from the thread at the time of allocation, the state of the memory to be released is recorded in the first state recording area; when the thread of the memory to be released is the same as the thread at the time of allocation, the state of the memory to be released is recorded in the second state recording area. Therefore, when the thread to release the memory is different from the thread at the time of allocation, there is no need to consider whether the thread at the time of allocation is locked or occupied by other threads. Instead, it is executed directly by modifying the first state record area, avoiding the waiting problem caused by lock competition, reducing time consumption, and improving the efficiency of memory allocation and release.
[0112] According to an embodiment of the present disclosure, the memory block category includes a first memory block category and a second memory block category, and the target memory block category is one of the first memory block category or the second memory block category.
[0113] According to an embodiment of the present disclosure, the memory capacity of the first initial memory block S corresponding to the first memory block category is smaller than the second initial memory block L2 corresponding to the second memory block category. For example, the first initial memory block S=32KB, and the second initial memory block L2=2MB. After the memory blocks are preliminarily screened according to the first threshold N, the memory blocks that are less than or equal to the first threshold N are taken as the main research objects of the embodiment of the present disclosure. Furthermore, the first memory block category is further divided into two categories of relatively large and relatively small memory blocks according to a predetermined memory block size. And according to the memory capacity, it is determined that the memory block occupied by the memory to be allocated belongs to the larger memory block L2 or the smaller memory block S, so that the memory block category occupied by the memory to be released or the memory block to be allocated of the memory to be allocated can be accurately located.
[0114] Fig. 6A and Figure 6B A schematic diagram schematically shows the correspondence between memory block categories according to an embodiment of the present disclosure.
[0115] like Fig. 6AAs shown, the memory block category also includes a third memory block category and a fourth memory block category. Among them, the third memory block L1 corresponding to the third memory block category is the upper memory block of the first initial memory block S of the first memory block category; the fourth memory block H corresponding to the fourth memory block category is the upper memory block of the third memory block L1 corresponding to the third memory block category.
[0116] like Figure 6B As shown, the fourth memory block H corresponding to the fourth memory block category is the upper-level memory block of the second initial memory block L2 corresponding to the second memory block category.
[0117] According to an embodiment of the present disclosure, Fig. 6A and Figure 6B The number of memory blocks corresponding to each category shown in is only an exemplary description, and more memory blocks may be included, which will not be repeated here.
[0118] According to an embodiment of the present disclosure, the memory capacity of the upper memory block is an integer multiple of the memory capacity of the lower memory block. Specifically, the multiple can be determined according to the bit width of the computer processor, for example, the memory capacity of the upper memory block is 64 times or 32 times the memory capacity of the lower memory block. Fig. 6A , the memory capacity of the fourth memory block H is an integer multiple of the memory capacity of the third memory block L1; the memory capacity of the third memory block L1 is an integer multiple of the memory capacity of the first initial memory block S. Exemplarily, the memory capacity of the fourth memory block H is 128MB, the memory capacity of the third memory block L1 is 2MB, and the memory capacity of the first initial memory block S is 32KB.
[0119] exist Figure 6B , the memory capacity of the fourth memory block H is an integral multiple of the memory capacity of the second initial memory block L2. Exemplarily, the memory capacity of the fourth memory block H is 128 MB, and the memory capacity of the second initial memory block L2 is 2 MB.
[0120] According to an embodiment of the present disclosure, the memory 110 is classified according to the size of the memory capacity, and the memory blocks corresponding to each category are set according to a multiple relationship, which is conducive to memory alignment of each category of memory blocks and improves the efficiency of the processor accessing the memory. At the same time, when the memory block size is in a multiple relationship, it is easier to find a memory block of the appropriate size when releasing and reallocating the memory, reducing the generation of memory fragments; and reducing the time to find a suitable memory block, thereby improving the efficiency of memory allocation. It can also reduce the unused space generated by the mismatch of memory block sizes, thereby improving the utilization of memory. Further, according to the bit width of the computer processor, the multiple relationship is set to 64 times or 32 times, which is more conducive to rapid positioning when the processor accesses the memory.
[0121] According to an embodiment of the present disclosure, the method for determining the first state recording area and / or the second state recording area specifically includes step 21 and step 22.
[0122] In step 21, the first status record area and / or the second status record area of the first initial memory block is determined according to the memory capacity of the first initial memory block S corresponding to the first memory block category and the memory capacity of the first initial sub-memory block B corresponding to the first memory block category.
[0123] According to an embodiment of the present disclosure, according to the memory capacity of the first initial memory block S and the memory capacity of the first initial sub-memory block B, the number of corresponding first initial sub-memory blocks B in the first initial memory block S is determined, and the position of the memory capacity of each first initial sub-memory block B is set to a bit. According to the above example, when S=32KB and B=16B, there are 2048 bits. According to the number of bits, the number of bytes required for the first state record area or the second state record area is calculated, that is, 256 bytes. It can be obtained that the first state record area and the second state record area of the first initial memory block respectively occupy 256 bytes of the first initial memory block.
[0124] In step 22, the first status record area and / or the second status record area of the second initial memory block is determined based on the memory capacity of the second initial memory block corresponding to the second memory block category and the memory capacity of the second initial sub-memory block corresponding to the second memory block category, wherein the memory capacity of the second initial sub-memory block is the memory capacity of the system memory page.
[0125] According to an embodiment of the present disclosure, according to the memory capacity of the second initial memory block L2 and the memory capacity of the second initial sub-memory block Q, the number of corresponding second initial sub-memory blocks Q in the second initial memory block L2 is determined, and the position of the memory capacity of each second initial sub-memory block Q is set to a bit. According to the above example, when L2=2MB and Q=4KB, there are 512 bits. According to the number of bits, the number of bytes required for the first state record area or the second state record area is calculated, that is, 64 bytes. It can be obtained that the first state record area and the second state record area of the second initial memory block respectively occupy 64 bytes of the second initial memory block.
[0126] According to the embodiments of the present disclosure, in the above two embodiments, as Figure 5 As shown, the position of the information storage area can be determined first, and the number of bytes occupied by the first state recording area and the second state recording area after the information storage area, respectively. The positions of the first state recording area and the second state recording area can be interchanged.
[0127] According to an embodiment of the present disclosure, since the determination of the first state record area or the second state record area is calculated based on the predefined memory capacity of the first initial sub-memory block B or the memory capacity of the second initial sub-memory block Q, and the memory capacity of the first initial sub-memory block B or the memory capacity of the second initial sub-memory block Q is also predefined, the specific memory capacity values mentioned in the above examples are only used as examples, and the present disclosure is not limited to this.
[0128] According to the embodiment of the present disclosure, the number of bytes occupied by the first status recording area or the second status recording area may also be predefined based on experience, which will not be described in detail here.
[0129] According to the embodiment of the present disclosure, in addition to dividing the memory blocks into the following types according to the integer multiples of the memory capacity: Fig. 6A The first memory block category, the third memory block category, the fourth memory block category, and the Figure 6B In addition to the second memory block category and the fourth memory block category shown, the first initial memory block S or the second initial memory block L2 may also be divided into initial sub-memory blocks with smaller unit memory capacities according to a predetermined memory capacity.
[0130] Specifically, the memory capacity of the first initial sub-memory block B is predefined, and the first initial memory block S is divided into a plurality of first initial sub-memory blocks B, such as Fig. 6A For example, the memory capacity of the first initial memory block S is 32KB, and the memory capacity of the first initial sub-memory block B is 16B. That is, one first initial sub-memory block B corresponds to 2048 first initial sub-memory blocks B. The memory capacity of the second initial sub-memory block Q is predefined, and the second initial memory block L2 is divided into a plurality of second initial sub-memory blocks Q, such as Figure 6B As shown. For example, the memory capacity of the second initial memory block L2 is 2MB, and the memory capacity of the second initial sub-memory block Q is 4KB. That is, one second initial memory block L2 corresponds to 512 second initial sub-memory blocks Q. Among them, the memory capacity of the first initial sub-memory block B can be set according to experience, for example, it can also be 32B, 66B, etc.; the memory capacity of the second initial sub-memory block Q is determined according to the memory capacity of the system memory page, for example, it can also be 8KB, etc.
[0131] Figure 7 The flowchart of the memory release method according to the embodiment of the present disclosure is schematically shown.
[0132] like Figure 7 As shown, the memory release method also includes operations S710 to S720.
[0133] In operation S710, an information storage area of a target initial memory block is scanned to generate a first scanning result.
[0134] According to an embodiment of the present disclosure, the memory capacity of the lower-level memory block in each memory block may also be used as the information storage area. Exemplarily, the information storage area of the third memory block L1 is set to be a memory of the size of the first S memory blocks.
[0135] According to an embodiment of the present disclosure, the memory capacity of the information storage area may also be set to be a multiple of 64 or 128.
[0136] According to the embodiments of the present disclosure, since the upper memory block and the lower memory block have a multiple relationship of 64 or 32, the memory capacity of the lower memory block is used as the information storage area, or the memory capacity of the information storage area is set to be a multiple of 64 or 128. Memory alignment is achieved, so that the CPU can directly read one or two S blocks to obtain the linked list information and the thread information to which the S block belongs, so that the complete data can be read at one time, and the access efficiency is improved.
[0137] According to the embodiments of the present disclosure, Figure 5 For example, if the target initial memory block to which multiple target sub-memory blocks belong is Figure 5 The first initial memory block S shown in , scans the information storage area of the first initial memory block S to generate a scanning result.
[0138] When an application thread applies for memory, the system allocates part of the memory to the application thread and records the thread information to which the part of the memory belongs in the information storage area. Figure 5 The memory blocks B2, B3, and B4 in are allocated to the request thread 1. For example, the target sub-memory blocks are determined to be B3 and B4. When releasing the memory, Figure 5 The information storage area of the first initial memory block S shown in can determine whether the thread information to which the target sub-memory blocks B3 and B4 currently belong is the same as the thread information to which they belonged when they were allocated, and generate a first scanning result.
[0139] In operation S720, according to the first scanning result, it is determined whether the release process of the target sub-memory block belongs to the cross-thread release process or does not belong to the cross-thread release process.
[0140] According to an embodiment of the present disclosure, when the thread of the memory to be released is different from the thread at the time of allocation, it is determined that the release process of the target sub-memory block belongs to a cross-thread release process; when the thread of the memory to be released is the same as the thread at the time of allocation, it is determined that the release process of the target sub-memory block does not belong to a cross-thread release process.
[0141] Figure 8 The flowchart of a memory management method according to another embodiment of the present disclosure is schematically shown.
[0142] like Figure 8As shown, after completing the release of multiple target sub-memory blocks, the memory management method further includes operations S810 to S820.
[0143] In operation S810, a first status recording area and a second status recording area of a target initial memory block are scanned.
[0144] According to an embodiment of the present disclosure, the second status record area of the target initial memory block is scanned, and whether the initial sub-memories in the target initial memory block are all in the idle state is determined based on the scanning result; and the first status record area of the target initial memory block is scanned, and whether the initial sub-memories in the target initial memory block are all in the idle state is determined based on the scanning result.
[0145] In operation S820, when all initial sub-memory blocks in the target initial memory block are in idle state, the memory capacity of the target initial memory block is returned to the parent memory block of the target initial memory block, wherein the memory capacity of the parent memory block is an integer multiple of the memory capacity of the target initial memory block.
[0146] According to the embodiment of the present disclosure, a scan of the target initial memory block is performed after each memory release, so as to obtain the status of the updated target initial memory block in time. Since in the embodiment of the present disclosure, the upper-level memory block is a multiple of the lower-level memory block, when the memory is released, the status information of the sub-memory blocks in the current memory block can be obtained in time by scanning the information storage area of the current memory block (i.e., the first status record area or the second status record area), so as to return the current memory block to the upper-level memory block in time when all the sub-memory blocks in the current memory block are in the idle state.
[0147] For example, Fig. 6A As shown, when all the first initial sub-memory blocks B corresponding to a first initial memory block S are in an idle state, the first initial memory block S is returned upward to the third memory block L1; when all the initial memory blocks S corresponding to a third memory block L1 are in an idle state, the third memory block L1 is returned upward to the fourth memory block H, and so on. Figure 6B As shown, when all second initial sub-memory blocks Q corresponding to a second initial memory block L2 are in an idle state, the second initial memory block L2 is returned upward to the fourth memory block H, and so on.
[0148] According to an embodiment of the present disclosure, when releasing memory, a complete initial memory block in an idle state can be returned to the parent memory block, avoiding the occurrence of fragmented and irregular memory blocks, making memory management more orderly and efficient, and achieving the purpose of returning the released memory to the operating system as quickly as possible.
[0149] According to an embodiment of the present disclosure, based on the release address and the memory capacity, multiple target sub-memory blocks occupied by the memory to be released are determined from the target initial memory block corresponding to the target memory block category, including: based on the release address, determining the target initial memory block corresponding to the target memory block category; and, based on the memory capacity, determining multiple target sub-memory blocks occupied by the memory to be released from the multiple initial sub-memory blocks corresponding to the target initial memory block.
[0150] According to an embodiment of the present disclosure, a target initial memory block (eg, Figure 3 The number of memory blocks in the figure is only for example, and more initial memory blocks or initial sub-memory blocks may also be included.
[0151] According to an embodiment of the present disclosure, after determining the target initial memory block, multiple target sub-memory blocks occupied by the memory to be released are determined from multiple initial sub-memory blocks based on the memory capacity of the memory to be released and the memory capacity of each initial sub-memory block. Specifically, based on the configuration information of the initial memory block, the position information of the first target sub-memory block is determined. For example, the first initial sub-memory block of the target initial memory block (initial memory block 2) can be used as the position information of the first target sub-memory block, or the positions of the first X initial sub-memory blocks of the target initial memory block (initial memory block 2) are reserved, and the X+1th initial sub-memory block is used as the position information of the first target sub-memory block, where X is a positive integer greater than 0.
[0152] According to an embodiment of the present disclosure, multiple target sub-memory blocks occupied by the memory to be released are determined from multiple initial sub-memory blocks according to the memory capacity of the memory to be released, the memory capacity of the initial sub-memory block, and the position information of the first target sub-memory block. Exemplarily, the memory capacity of the memory to be released is 5KB (5120B), the memory capacity of each initial sub-memory block is 16B, and the fifth initial sub-memory block is the first target sub-memory block. Then, the target sub-memory blocks occupied by the memory to be released are: 5120 / 16=30 blocks, that is, the memory to be released occupies 30 initial sub-memory blocks, and because it is occupied from the 5th initial sub-memory block, therefore, the target sub-memory occupied by the memory to be released is the 5th to 34th initial sub-memory blocks, then the 5th to 34th initial sub-memory blocks in the target initial memory block (initial memory block 2) are determined as the target sub-memory blocks occupied by the memory to be released.
[0153] According to an embodiment of the present disclosure, a plurality of initial sub-memory blocks corresponding to a target memory block category are determined according to a release address, including steps 31 and 32.
[0154] In step 31, according to the memory capacity of the initial memory block corresponding to the release address and the target memory block category, the initial address of the target memory block occupied by the memory to be released and the target memory are determined from multiple initial memory blocks.
[0155] According to an embodiment of the present disclosure, based on the release address, the memory capacity of the initial memory block is rounded down to determine the initial address information of the target memory block and the target memory block.
[0156] Exemplarily, the release address p=66KB, the memory capacity of the first initial memory block S is 32KB, and the memory capacity of S is rounded down, that is, the integer multiple of 32KB is 64KB, which indicates that the initial address information of the target initial memory block is the 64KB among the multiple first initial memory blocks S. Therefore, the initial memory block starting from 64KB is the target memory block.
[0157] In step 32, based on the initial address and the memory capacity, a plurality of target sub-memory blocks occupied by the to-be-released memory are determined from a plurality of initial sub-memory blocks corresponding to the target memory block.
[0158] According to an embodiment of the present disclosure, starting from an initial address, a plurality of target sub-memory blocks are determined according to a memory capacity of an initial sub-memory block and a memory capacity of a memory to be released.
[0159] Exemplarily, the initial address is 64KB obtained in the above embodiment, the memory capacity of the initial sub-memory block is 8KB, and the memory capacity of the memory to be released is 22KB. According to the memory capacity of the initial sub-memory block, rounding up to an integer multiple of 8KB, 24KB is obtained. Then, the memory blocks from 64KB to 88KB can be determined as target sub-memory blocks, that is, the memory to be released occupies 3 initial sub-memory blocks.
[0160] According to an embodiment of the present disclosure, by rounding down the memory block, it can be ensured that the first address (initial address information) of the memory block is the minimum possible value of the current position, thereby saving memory space to a certain extent.
[0161] Fig. 9 The flowchart of a memory management method according to another embodiment of the present disclosure is schematically shown.
[0162] like Fig. 9 As shown, the memory management method also includes operations S910 to S940.
[0163] In operation S910, in response to a memory allocation request of an application thread, a memory capacity of a memory to be allocated in the allocation request is obtained.
[0164] In operation S920, a target memory block category to be occupied by the memory to be allocated is determined according to the memory capacity of the memory to be allocated.
[0165] According to an embodiment of the present disclosure, a target memory block category to be occupied by the memory to be allocated is determined from multiple memory block categories according to the memory capacity, including steps 41 and 42:
[0166] Step 41: Determine whether the memory capacity n is less than or equal to a first threshold N, wherein the first threshold can be set based on experience, for example, the first threshold N is 128KB or 64KB.
[0167] Step 42: When the memory capacity n is less than or equal to the first threshold N, determine whether the memory capacity n is less than or equal to a second threshold, wherein the second threshold is set according to the memory capacity of the system memory page. For example, the second threshold is set to Q / 2.
[0168] According to an embodiment of the present disclosure, the target memory block category of the memory to be released is determined according to the judgment result performed in step 42. Specifically, when the memory capacity n is less than or equal to the first threshold N, the target memory block category of the memory to be released is determined to be the first memory block category; when the memory capacity n is greater than the first threshold N, the target memory block category of the memory to be released is determined to be the second memory block category.
[0169] Exemplarily, when the memory capacity n=3KB and Q=8KB, the memory capacity n is less than or equal to the second threshold value Q / 2, and the target memory block category to be occupied by the memory to be allocated is the first memory block category, that is, the memory to be allocated is to occupy a first initial memory block S. When the memory capacity n=7KB and Q=8KB, the memory capacity n is less than or equal to the second threshold value Q / 2, and the target memory block category to be occupied by the memory to be allocated is the second memory block category, that is, the memory to be allocated is to occupy a second initial memory block L2.
[0170] In operation S930, a plurality of to-be-allocated sub-memory blocks to be occupied by the to-be-allocated memory are determined from a plurality of initial sub-memory blocks corresponding to the target initial memory block according to the memory capacity of the to-be-allocated memory and the target memory block category.
[0171] According to an embodiment of the present disclosure, multiple initial memory blocks corresponding to the target memory block category are scanned, and initial sub-memory blocks that have memory capacity to be allocated and are in an idle state are determined as multiple sub-memory blocks to be allocated that are intended to be occupied by the allocated memory.
[0172] In operation S940, the states of the plurality of sub-memory blocks to be allocated are modified in the second state recording area of the target initial memory block to allocate the plurality of sub-memory blocks to be allocated to the requesting thread.
[0173] According to an embodiment of the present disclosure, the initial memory block to which the multiple sub-memory blocks to be allocated belong is the memory block to be allocated. In the second status recording area of the memory block to be allocated, the bits of the multiple sub-memory blocks to be allocated are set to 1, which is used to indicate that the multiple sub-memory blocks to be allocated are allocated and are in a non-idle state.
[0174] According to an embodiment of the present disclosure, based on the memory capacity of the memory to be allocated and the target memory block category, multiple sub-memory blocks to be occupied by the memory to be allocated are determined from multiple initial sub-memory blocks corresponding to the target initial memory block, including steps 51 to 52.
[0175] In step 51, the first status record area and / or the second status record area of the target initial memory block is scanned to generate a second scanning result.
[0176] According to an embodiment of the present disclosure, the second status record area of a memory block to be allocated to which a plurality of sub-memory blocks to be allocated belong is scanned to obtain a result of whether there is an initial sub-memory block in the memory block to be allocated that meets the memory capacity and is in an idle state; if not, the first status record area is continued to be scanned to obtain a result of whether there is an initial sub-memory block in the memory block to be allocated that meets the memory capacity and is in an idle state.
[0177] In step 52, according to the second scanning result, a plurality of initial sub-memory blocks in idle state that satisfy the memory capacity are determined as sub-memory blocks to be allocated.
[0178] According to an embodiment of the present disclosure, when memory allocation is required, a memory allocation function is called according to the memory size of the memory to be allocated, and the category of the memory block is set according to the memory capacity, so that the memory blocks are classified according to the size of the memory capacity, and the memory block to be allocated and its corresponding sub-memory block to be allocated to which the memory to be allocated belongs can be quickly found according to the memory capacity, thereby reducing the search time and further improving the memory allocation efficiency. Furthermore, by presetting the first state record area and the second state record area in the initial memory block, it is possible to directly scan the specified area without traversing each sub-memory block, thus saving the search time and further improving the memory allocation efficiency. In addition, setting both the first state record area and the second state record area at the head position of the initial memory block is more conducive to improving the scanning rate.
[0179] Based on the above memory management method, the present disclosure also provides a memory management device. Fig.10 The device is described in detail.
[0180] Fig.10 The structural block diagram of the memory management device according to an embodiment of the present disclosure is schematically shown.
[0181] like Fig.10As shown, the memory management device 1000 of this embodiment includes a first acquisition module 1010 , a first determination module 1020 , a second determination module 1030 , a first release module 1040 and a second release module 1050 .
[0182] The first acquisition module 1010 is used to acquire the release address and memory capacity of the memory to be released. In one embodiment, the first acquisition module 1010 can be used to perform the operation S210 described above, which will not be described in detail here.
[0183] The first determination module 1020 is used to determine the target memory block category of the memory to be released according to the memory capacity. In one embodiment, the first determination module 1020 can be used to perform the operation S220 described above, which will not be repeated here.
[0184] The second determination module 1030 is used to determine multiple target sub-memory blocks occupied by the to-be-released memory from the target initial memory block corresponding to the target memory block category according to the release address and the memory capacity. In one embodiment, the second determination module 1030 can be used to perform the operation S230 described above, which will not be repeated here.
[0185] The first release module 1040 is used to determine multiple target sub-memory blocks occupied by the to-be-released memory from multiple initial sub-memory blocks according to the memory capacity. In one embodiment, the first release module 1040 can be used to perform the operation S240 described above, which will not be described in detail here.
[0186] The second release module 1050 is used to release the memory occupied by the multiple target sub-memory blocks to the thread. In one embodiment, the second release module 1050 can be used to perform the operation S250 described above, which will not be repeated here.
[0187] According to an embodiment of the present disclosure, in response to a release request of memory to be released, a memory release function is called according to the release address and memory size of the memory to be released. In memory management, it is not necessary to store the memory capacity of the allocated memory in a memory block, which simplifies the layout of the memory block and simplifies the process of memory allocation by the memory allocator, thereby improving the efficiency of memory release. The category of the memory block is determined according to the memory capacity, and the target initial memory block to which the memory to be released belongs and its corresponding target initial sub-memory block can be quickly located according to the memory capacity, thereby reducing the search time. Furthermore, different release strategies are adopted for the cases where the memory to be released belongs to cross-thread release and does not belong to cross-thread release. The first state recording area is used to record the state of the memory block released across threads, and the second state recording area is used to record the state of the memory block released within the thread. When the thread of the memory to be released is different from the thread at the time of allocation, the first release module records the state of the memory to be released in the first state recording area; when the thread of the memory to be released is the same as the thread at the time of allocation, the second release module records the state of the memory to be released in the second state recording area. Therefore, when the thread to release the memory is different from the thread at the time of allocation, there is no need to consider whether the thread at the time of allocation is locked or occupied by other threads. Instead, it is executed directly by modifying the first state record area, avoiding the waiting problem caused by lock competition, reducing time consumption, and improving the efficiency of memory allocation and release.
[0188] According to an embodiment of the present disclosure, the memory block category includes a first memory block category and a second memory block category, the target memory block category is one of the first memory block category and the second memory block category, and the memory management device also includes: a third determination module and a fourth determination module.
[0189] A third determination module, configured to determine a first state recording area and / or a second state recording area of the first initial memory block according to a memory capacity of the first initial memory block corresponding to the first memory block category and a memory capacity of the first initial sub-memory block corresponding to the first memory block category;
[0190] A fourth determination module is used to determine the first status record area and / or the second status record area of the second initial memory block based on the memory capacity of the second initial memory block corresponding to the second memory block category and the memory capacity of the second initial sub-memory block corresponding to the second memory block category, wherein the memory capacity of the second initial sub-memory block is the memory capacity of the system memory page.
[0191] According to an embodiment of the present disclosure, the memory management device further includes: a first scanning module and a fifth determining module.
[0192] A first scanning module, used for scanning the information storage area of the target initial memory block to generate a first scanning result;
[0193] The fifth determination module is used to determine whether the release process of the target sub-memory block belongs to the cross-thread release process or does not belong to the cross-thread release process according to the first scanning result.
[0194] According to an embodiment of the present disclosure, after completing the release of multiple target sub-memory blocks, the memory management device further includes: a second scanning module and a return module.
[0195] A second scanning module, used for scanning the first state recording area and the second state recording area of the target initial memory block;
[0196] A return module is used to return the memory capacity of the target initial memory block to the parent memory block of the target initial memory block when all the initial sub-memory blocks in the target initial memory block are in an idle state, wherein the memory capacity of the parent memory block is an integer multiple of the memory capacity of the target initial memory block.
[0197] According to an embodiment of the present disclosure, the second determination module 1030 includes: a first determination submodule and a second determination submodule.
[0198] A first determination submodule, configured to determine a target initial memory block corresponding to a target memory block category according to a release address;
[0199] The second determining submodule is used to determine, according to the memory capacity, a plurality of target sub-memory blocks occupied by the to-be-released memory from a plurality of initial sub-memory blocks corresponding to the target initial memory block.
[0200] According to an embodiment of the present disclosure, the first determining submodule includes: a first determining unit and a second determining unit.
[0201] A first determining unit is used to determine, from a plurality of initial memory blocks, an initial address of a target initial memory block occupied by the memory to be released and a target initial memory block according to the release address and the memory capacity of the target initial memory block;
[0202] The second determining unit is used to determine a plurality of target sub-memory blocks occupied by the memory to be released from a plurality of initial sub-memory blocks corresponding to the target initial memory block according to the initial address and the memory capacity of the memory to be released.
[0203] According to an embodiment of the present disclosure, the first determining unit includes: a determining subunit.
[0204] The determination subunit is used to round down the memory capacity of the initial memory block based on the release address, and determine the initial address information of the target initial memory block and the target initial memory block.
[0205] According to an embodiment of the present disclosure, the memory management device further includes: a second acquisition module, a sixth determination module, a seventh determination module and an allocation module.
[0206] A second acquisition module, configured to respond to a memory allocation request of an application thread and acquire a memory capacity of the memory to be allocated in the allocation request;
[0207] A sixth determining module, used to determine a target memory block category to be occupied by the memory to be allocated according to the memory capacity of the memory to be allocated;
[0208] A seventh determination module, configured to determine, according to the memory capacity of the memory to be allocated and the target memory block category, a plurality of to-be-allocated sub-memory blocks to be occupied by the to-be-allocated memory from a plurality of initial sub-memory blocks corresponding to the target initial memory block;
[0209] The allocation module is used to modify the states of multiple sub-memory blocks to be allocated in the second state recording area of the target initial memory block to allocate the multiple sub-memory blocks to be allocated to the application thread.
[0210] According to an embodiment of the present disclosure, the seventh determination module includes: a scanning submodule and a third determination submodule.
[0211] A scanning submodule, used for scanning the first state recording area and / or the second state recording area of the target initial memory block to generate a second scanning result;
[0212] The third determining submodule is used to determine, according to the second scanning result, a plurality of initial sub-memory blocks in an idle state that satisfy the memory capacity as sub-memory blocks to be allocated.
[0213] According to an embodiment of the present disclosure, any multiple modules of the first acquisition module 1010, the first determination module 1020, the second determination module 1030, the first release module 1040, and the second release module 1050 can be combined into one module for implementation, or any one of the modules can be split into multiple modules. Alternatively, at least part of the functions of one or more of these modules can be combined with at least part of the functions of other modules and implemented in one module. According to an embodiment of the present disclosure, at least one of the first acquisition module 1010, the first determination module 1020, the second determination module 1030, the first release module 1040, and the second release module 1050 can be at least partially implemented as a hardware circuit, such as a field programmable gate array (FPGA), a programmable logic array (PLA), a system on a chip, a system on a substrate, a system on a package, an application specific integrated circuit (ASIC), or can be implemented by hardware or firmware such as any other reasonable way of integrating or packaging the circuit, or implemented in any one of the three implementation modes of software, hardware, and firmware, or in a suitable combination of any of them. Alternatively, at least one of the first acquisition module 1010, the first determination module 1020, the second determination module 1030, the first release module 1040 and the second release module 1050 may be at least partially implemented as a computer program module, which may perform corresponding functions when executed.
[0214] Fig.11 A block diagram of an electronic device suitable for implementing a memory management method according to an embodiment of the present disclosure is schematically shown.
[0215] like Fig.11 As shown, the electronic device 1100 according to an embodiment of the present disclosure includes a processor 1101, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 1102 or a program loaded from a storage part 1108 to a random access memory (RAM) 1103. The processor 1101 may include, for example, a general-purpose microprocessor (such as a CPU), an instruction set processor and / or a related chipset and / or a special-purpose microprocessor (for example, an application-specific integrated circuit (ASIC)), etc. The processor 1101 may also include an onboard memory for caching purposes. The processor 1101 may include a single processing unit or multiple processing units for performing different actions of the method flow according to an embodiment of the present disclosure.
[0216] In RAM1103, various programs and data required for the operation of electronic device 1100 are stored. Processor 1101, ROM1102 and RAM1103 are connected to each other via bus 1104. Processor 1101 performs various operations of the method flow according to the embodiment of the present disclosure by executing the program in ROM1102 and / or RAM1103. It should be noted that the program may also be stored in one or more memories other than ROM1102 and RAM1103. Processor 1101 may also perform various operations of the method flow according to the embodiment of the present disclosure by executing the program stored in the one or more memories.
[0217] According to an embodiment of the present disclosure, the electronic device 1100 may further include an input / output (I / O) interface 1105, which is also connected to the bus 1104. The electronic device 1100 may further include one or more of the following components connected to the input / output (I / O) interface 1105: an input portion 1106 including a keyboard, a mouse, etc.; an output portion 1107 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage portion 1108 including a hard disk, etc.; and a communication portion 1109 including a network interface card such as a LAN card, a modem, etc. The communication portion 1109 performs communication processing via a network such as the Internet. A drive 1110 is also connected to the input / output (I / O) interface 1105 as needed. A removable medium 1111, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 1110 as needed, so that a computer program read therefrom is installed into the storage portion 1108 as needed.
[0218] The present disclosure also provides a computer-readable storage medium, which may be included in the device / apparatus / system described in the above embodiments; or may exist independently without being assembled into the device / apparatus / system. The above computer-readable storage medium carries one or more programs, and when the above one or more programs are executed, the method according to the embodiment of the present disclosure is implemented.
[0219] According to an embodiment of the present disclosure, a computer-readable storage medium may be a non-volatile computer-readable storage medium, for example, it may include but is not limited to: a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present disclosure, a computer-readable storage medium may be any tangible medium containing or storing a program, which may be used by or in combination with an instruction execution system, an apparatus or a device. For example, according to an embodiment of the present disclosure, a computer-readable storage medium may include the ROM1102 and / or RAM1103 described above and / or one or more memories other than ROM1102 and RAM1103.
[0220] The embodiment of the present disclosure also includes a computer program product, which includes a computer program, and the computer program contains program code for executing the method shown in the flowchart. When the computer program product is run in a computer system, the program code is used to enable the computer system to implement the memory management method provided by the embodiment of the present disclosure.
[0221] The above functions defined in the system / device of the embodiment of the present disclosure are performed when the computer program is executed by the processor 1101. According to the embodiment of the present disclosure, the system, device, module, unit, etc. described above can be implemented by a computer program module.
[0222] In one embodiment, the computer program may rely on tangible storage media such as optical storage devices, magnetic storage devices, etc. In another embodiment, the computer program may also be transmitted and distributed in the form of signals on a network medium, and downloaded and installed through the communication part 1109, and / or installed from a removable medium 1111. The program code contained in the computer program may be transmitted using any appropriate network medium, including but not limited to: wireless, wired, etc., or any suitable combination of the above.
[0223] In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 1109, and / or installed from the removable medium 1111. When the computer program is executed by the processor 1101, the above functions defined in the system of the embodiment of the present disclosure are performed. According to the embodiment of the present disclosure, the system, device, means, module, unit, etc. described above can be implemented by a computer program module.
[0224] According to an embodiment of the present disclosure, the program code for executing the computer program provided by the embodiment of the present disclosure can be written in any combination of one or more programming languages. Specifically, these computing programs can be implemented using high-level process and / or object-oriented programming languages, and / or assembly / machine languages. Programming languages include, but are not limited to, Java, C++, python, "C" language or similar programming languages. The program code can be executed entirely on the user computing device, partially on the user device, partially on the remote computing device, or entirely on the remote computing device or server. In the case of a remote computing device, the remote computing device can be connected to the user computing device through any type of network, including a local area network (LAN) or a wide area network (WAN), or can be connected to an external computing device (for example, using an Internet service provider to connect through the Internet).
[0225] The flow charts and block diagrams in the accompanying drawings illustrate the possible architecture, functions and operations of the systems, methods and computer program products according to various embodiments of the present disclosure. In this regard, each box in the flow chart or block diagram can represent a module, a program segment, or a part of a code, and the above-mentioned module, program segment, or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0226] It will be appreciated by those skilled in the art that the features described in the various embodiments of the present disclosure may be combined and / or combined in a variety of ways, even if such combinations or combinations are not explicitly described in the present disclosure. In particular, without departing from the spirit and teachings of the present disclosure, the features described in the various embodiments of the present disclosure may be combined and / or combined in a variety of ways. All of these combinations and / or combinations fall within the scope of the present disclosure.
[0227] The embodiments of the present disclosure are described above. However, these embodiments are only for illustrative purposes and are not intended to limit the scope of the present disclosure. Although the embodiments are described above, this does not mean that the measures in the various embodiments cannot be used in combination to advantage. Without departing from the scope of the present disclosure, those skilled in the art may make a variety of substitutions and modifications, which should all fall within the scope of the present disclosure.
Claims
1. A memory management method, characterized in that: The method comprises: Get the release address and memory capacity of the memory to be released; Determining a target memory block category of the memory to be released according to the memory capacity; Determining, according to the release address and the memory capacity, a plurality of target sub-memory blocks occupied by the memory to be released from a target initial memory block corresponding to the target memory block category; In the case where it is determined that the release process of the target sub-memory block belongs to a cross-thread release process, modifying the states of the multiple target sub-memory blocks in the first state recording area of the target initial memory block to complete the release of the multiple target sub-memory blocks; When it is determined that the release process of the target sub-memory block does not belong to a cross-thread release process, the states of the multiple target sub-memory blocks are modified in the second state record area of the target initial memory block to complete the release of the multiple target sub-memory blocks.
2. The method according to claim 1, characterized in that The memory block category includes a first memory block category and a second memory block category, the target memory block category is one of the first memory block category and the second memory block category, and the method further includes: Determine a first status recording area and / or a second status recording area of the first initial memory block according to a memory capacity of a first initial memory block corresponding to the first memory block category and a memory capacity of a first initial sub-memory block corresponding to the first memory block category; Determine the first status record area and / or the second status record area of the second initial memory block based on the memory capacity of the second initial memory block corresponding to the second memory block category and the memory capacity of the second initial sub-memory block corresponding to the second memory block category, wherein the memory capacity of the second initial sub-memory block is the memory capacity of the system memory page.
3. The method according to claim 1, characterized in that The method further comprises: Scanning the information storage area of the target initial memory block to generate a first scanning result; According to the first scanning result, it is determined whether the release process of the target sub-memory block belongs to a cross-thread release process or does not belong to a cross-thread release process.
4. The method according to claim 1, characterized in that: After completing the release of the multiple target sub-memory blocks, the method further includes: Scan the first status record area and the second status record area of the target initial memory block; When all the initial sub-memory blocks in the target initial memory block are in idle state, the memory capacity of the target initial memory block is returned to the parent memory block of the target initial memory block, wherein the memory capacity of the parent memory block is an integer multiple of the memory capacity of the target initial memory block.
5. According to any one of the methods of claim 1 to 4, determining a plurality of target sub-memory blocks occupied by the memory to be released from a target initial memory block corresponding to the target memory block category according to the release address and the memory capacity, comprising: Determine, according to the release address, a target initial memory block corresponding to the target memory block category; According to the memory capacity, a plurality of target sub-memory blocks occupied by the to-be-released memory are determined from a plurality of initial sub-memory blocks corresponding to the target initial memory block.
6. The method according to claim 5, characterized in that Determining a target initial memory block corresponding to the target memory block category according to the release address includes: According to the release address and the memory capacity of the target initial memory block, determining the initial address of the target initial memory block occupied by the memory to be released and the target initial memory block from a plurality of initial memory blocks; According to the initial address and the memory capacity of the memory to be released, a plurality of target sub-memory blocks occupied by the memory to be released are determined from a plurality of initial sub-memory blocks corresponding to the target initial memory block.
7. The method according to claim 6, characterized in that According to the release address and the memory capacity of the target initial memory block, determining the initial address of the target initial memory block occupied by the memory to be released and the target initial memory block from multiple initial memory blocks, including: Based on the released address, the memory capacity of the initial memory block is rounded down to obtain the initial address information of the target initial memory block and the target initial memory block.
8. The method according to claim 1, characterized in that The method further comprises: In response to a memory allocation request of an application thread, obtaining a memory capacity of the memory to be allocated in the allocation request; Determining the target memory block category to be occupied by the memory to be allocated according to the memory capacity of the memory to be allocated; According to the memory capacity of the memory to be allocated and the category of the target memory block, determining a plurality of sub-memory blocks to be allocated that are to be occupied by the memory to be allocated from a plurality of initial sub-memory blocks corresponding to the target initial memory block; The states of the plurality of sub-memory blocks to be allocated are modified in the second state recording area of the target initial memory block to allocate the plurality of sub-memory blocks to be allocated to the application thread.
9. The method according to claim 8, characterized in that According to the memory capacity of the memory to be allocated and the category of the target memory block, determining a plurality of sub-memory blocks to be allocated that are to be occupied by the memory to be allocated from a plurality of initial sub-memory blocks corresponding to the target initial memory block, including: Scanning the first state recording area and / or the second state recording area of the target initial memory block to generate a second scanning result; According to the second scanning result, a plurality of initial sub-memory blocks in idle state that satisfy the memory capacity are determined as sub-memory blocks to be allocated.
10. A memory management device, characterized in that: The device comprises: A first acquisition module is used to acquire a release address and a memory capacity of a memory to be released; A first determining module, used to determine the target memory block category of the memory to be released according to the memory capacity; A second determining module is used to determine, according to the release address and the memory capacity, a plurality of target sub-memory blocks occupied by the memory to be released from the target initial memory block corresponding to the target memory block category; A first releasing module is configured to modify the states of the plurality of target sub-memory blocks in the first state recording area of the target initial memory block to complete the release of the plurality of target sub-memory blocks when it is determined that the release process of the target sub-memory block belongs to a cross-thread release process; and The second release module is used to modify the status of the multiple target sub-memory blocks in the second status record area of the target initial memory block when it is determined that the release process of the target sub-memory block does not belong to a cross-thread release process, so as to complete the release of the multiple target sub-memory blocks.
11. An electronic device, comprising: one or more processors; a memory for storing one or more computer programs, It is characterized in that the one or more processors execute the one or more computer programs to implement the steps of the method according to any one of claims 1 to 9.
12. A computer-readable storage medium having a computer program or instruction stored thereon, characterized in that: When the computer program or instruction is executed by a processor, the steps of the method according to any one of claims 1 to 9 are implemented.
13. A computer program product comprising a computer program or instructions, characterized in that When the computer program or instruction is executed by a processor, the steps of the method according to any one of claims 1 to 9 are implemented.
Citation Information
Cited By
Memory allocation system and method based on execution flow awareness and hardware topology and medium
CN122777322A
