Lightweight memory dynamic management method and system

By using mapping arrays and linked list mechanisms, the problems of complex configuration and fragmentation in dynamic memory management are solved, achieving efficient memory allocation and release, improving memory utilization and system reliability, and making it suitable for resource-constrained embedded systems.

CN120973508APending Publication Date: 2025-11-18SHENZHEN CHENG TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510872381.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-06-26
Publication Date
2025-11-18

AI Technical Summary

Technical Problem

Existing dynamic memory management mechanisms are complex to configure and cumbersome to implement, resulting in memory fragmentation, long allocation and release times with unpredictable execution durations, and additional synchronization mechanisms are required in multi-threaded programs to ensure the correctness and consistency of concurrent operations.

Method used

By employing a mapping array and linked list mechanism, memory request information is identified by initializing the mapping array, managed memory, and free segment information on memory. Based on the request type, memory allocation is performed by searching for physical contiguous segments or extracting linked list segments. Combined with pointer adjustment and free information update, efficient management of contiguous and non-contiguous memory is achieved.

Benefits of technology

It improves memory utilization, reduces memory fragmentation, lowers management overhead, supports concurrent operations in a multi-threaded environment, and is suitable for resource-constrained embedded or lightweight systems.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120973508A_ABST
    Figure CN120973508A_ABST
Patent Text Reader

Abstract

The invention relates to a lightweight memory dynamic management method and system, and the method comprises the steps: initializing a memory, and recognizing memory application information; if the memory application information is a continuous memory segment application, the minimum unit number is converted according to the application length, a physically continuous minimum allocation unit segment is searched, and a linked list pointer is adjusted to separate the minimum allocation unit segment; if the memory application information is non-continuous memory segment application, intercepting a target linked list segment corresponding to the application length from the head of the idle linked list, and adjusting a linked list pointer to separate the target linked list segment; when a read-write instruction is received, performing read-write directly based on a memory address obtained by the application as a base address for a continuous memory segment application, and performing cross-unit read-write for a discontinuous memory segment application, positioning a first element of a mapping array, positioning byte positions one by one, and executing cross-unit read-write; and when a memory segment release instruction is received, positioning the first element of the mapping array according to the to-be-released first address, and combining the to-be-released linked list and the idle linked list into an ascending-order linked list.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The application relates to the technical field of memory management, in particular to a lightweight memory dynamic management method and system. BACKGROUND

[0002] The memory dynamic management mechanism has important significance in computer programs. It is used for dynamic allocation and release of memory, so that the program can dynamically obtain the required memory space at runtime. This is very useful for cases where dynamic data structures such as variables, arrays, and structures need to be created and operated during program execution. The memory dynamic management mechanism can expand or shrink the required memory according to real-time conditions. This flexibility allows the program to manage memory according to actual needs, improving memory utilization. The memory dynamic management mechanism allocates the required memory space for each variable or data structure precisely, avoiding the waste caused by static memory allocation. Through the memory dynamic management mechanism, larger data structures such as large arrays and complex objects can be placed in managed memory rather than stack memory. This can avoid stack overflow problems, as managed memory is usually larger than stack memory. The memory dynamic management mechanism can implement a thread-safe memory management mechanism to support concurrent execution of multi-threaded programs. The significance of the memory dynamic management mechanism is that it provides a flexible, dynamic, and efficient method for managing memory, allowing the program to fully utilize computer resources and meet the needs of dynamically creating and operating variables and data structures.

[0003] The current memory dynamic management mechanism is complex to configure and tedious to implement, which can cause memory fragmentation problems, long allocation and release times, and unpredictable execution times. In addition, in multi-threaded programs, an additional synchronization mechanism is needed to ensure the correctness and consistency of memory allocation during concurrent operations.

[0004] Based on this, the application provides a lightweight memory dynamic management method and system. SUMMARY

[0005] To improve the problem that the memory dynamic management mechanism is complex to configure and tedious to implement, which can cause memory fragmentation problems, long allocation and release times, and unpredictable execution times, and in multi-threaded programs, an additional synchronization mechanism is needed to ensure the correctness and consistency of memory allocation during concurrent operations, the application provides a lightweight memory dynamic management method and system.

[0006] In a first aspect, the application provides a lightweight memory dynamic management method, which adopts the following technical solution: comprising:

[0007] Initializing the mapping array on the memory, the managed memory, and the free segment information, and identifying the memory application information;

[0008] If the memory application information is a continuous memory segment application, then the number of minimum units is converted according to the application length, a physically continuous minimum allocation unit segment is searched based on the free segment information, the link table pointer is adjusted to separate the minimum allocation unit segment, the free segment information is updated, and the segment starting address is returned;

[0009] If the memory application information is a non-continuous memory segment application, then a target link table segment corresponding to the application length is cut from the head of the free link table based on the free segment information and the application length, the link table pointer is adjusted to separate the target link table segment, the free segment information is updated, and the segment starting address is returned.

[0010] When a read-write instruction is received, for the continuous memory segment application, the memory address obtained based on the application is directly used as the base address for read-write, and for the non-continuous memory segment application, the first element of the mapping array is located based on the virtual starting address, the offset, and the data length, the byte position is located one unit at a time through the offset and the unit size, and the cross-unit read-write operation is performed.

[0011] When a memory segment release instruction is received, the first element of the mapping array is located according to the to-be-released starting address, and the to-be-released link table and the free link table are merged into an ascending link table, and the free link table length is updated.

[0012] Preferably, the mapping array on the memory, the managed memory, and the free segment information are initialized, and the memory application information is identified, including:

[0013] The memory application information is received, the memory application information includes the application length and the application type identifier, and according to the application type identifier, it is judged that the memory application information is the continuous memory segment application or the non-continuous memory segment application.

[0014] The managed memory pool is cleared on demand to ensure that there is no residual data in the initial state.

[0015] The mapping array is an integer array, which corresponds one-to-one to the minimum allocation unit in the managed memory, the value of each element is the subscript of the next element, and a one-way link table is formed, and the element is negative one when the link table ends.

[0016] The free segment information includes the free link table head subscript, the free link table length, and the historical minimum length, the free link table head subscript is set to zero, the free link table length is set to the total length of the mapping array, and the historical minimum length is set to the free link table length.

[0017] Preferably, if the memory application information is a continuous memory segment application, the number of minimum units is converted according to the application length, a physically continuous minimum allocation unit segment is searched based on the free segment information, the link table pointer is adjusted to separate the minimum allocation unit segment, the free segment information is updated, and the segment starting address is returned, including:

[0018] The required number of minimum allocation units is calculated according to the application length and the minimum allocation unit size;

[0019] If the length of the free link table in the free segment information is not less than the required number of units, the mapping array is traversed to find a target element whose element subscript is less than the element value by one, and an element segment whose continuous number of target elements is greater than the minimum number of units is set as a continuous intercept segment.

[0020] If the continuous intercept segment has a previous element, the value of the previous element is updated to the original value of the last element of the continuous intercept segment, and the last element of the continuous intercept segment is assigned a value of -1.

[0021] If the continuous intercept segment does not have a previous element, the subscript of the head of the free link table is updated to the original value of the last element of the continuous intercept segment.

[0022] The length of the free link table is reduced by the number of intercepted units to obtain the current length of the free link table, and if the current length of the free link table is less than the historical minimum length, the historical minimum length is updated to the current length of the free link table, and the memory address corresponding to the first element of the continuous intercept segment is returned.

[0023] Preferably, if the memory application information is a non-continuous memory segment application, the target link table segment corresponding to the application length is intercepted from the head of the free link table based on the free segment information and the application length, the link table pointer is adjusted to separate the target link table segment, the free segment information is updated, and the segment starting address is returned, including:

[0024] If the length of the free link table is not less than the minimum number of units, the target link table segment with a length corresponding to the number of units of the application length is intercepted from the head of the free link table;

[0025] The next element of the last element of the intercepted segment is set to the original subscript of the head of the free link table, and the last element of the intercepted segment is assigned a value of -1.

[0026] If the current length of the free link table is less than the historical minimum length, the historical minimum length is updated to the current length of the free link table.

[0027] The memory address corresponding to the first element of the intercepted segment is returned.

[0028] Preferably, when receiving a read-write instruction, for the continuous memory segment application, read-write is directly based on the obtained memory address as the base address; for the non-continuous memory segment application, the first element of the mapping array is located based on the virtual first address, offset and data length, the byte position is located one unit at a time through the offset and unit size, and the cross-unit read-write operation is performed, including:

[0029] When receiving a read-write instruction, for the continuous memory segment application, read-write is directly based on the obtained memory address as the base address;

[0030] For the non-continuous memory segment application, the first address of the virtual continuous memory segment, the offset and the data length to be read and written are provided;

[0031] The mapping array element corresponding to the virtual first address is converted, and it is confirmed that the mapping array element is the first element of the non-continuous memory linked list;

[0032] According to the offset and the minimum allocation unit size, the minimum allocation unit in which the data to be read and written is located and the byte position in the unit are determined;

[0033] The read-write operation is performed one unit at a time, until the read-write of the data to be read and written is completed.

[0034] Preferably, when receiving a memory segment release instruction, the first element of the mapping array is located according to the to-be-released first address, and the to-be-released linked list and the free linked list are merged into an ascending linked list, the free linked list length is updated, including:

[0035] According to the first address of the to-be-released memory, the first element of the mapping array corresponding to the first address is calculated, and it is confirmed that the first element of the mapping array is the first element of the to-be-released memory linked list;

[0036] The elements of the to-be-released linked list and the free linked list are compared one by one, and the element pointing is adjusted;

[0037] The free linked list length is added to the length of the released linked list, and the free linked list length is updated.

[0038] Preferably, according to the free linked list length and the mapping array length, the current allocated memory size and the to-be-allocated memory size are calculated.

[0039] According to the historical minimum length and the mapping array length, the historical maximum allocated memory size is calculated, and the historical maximum memory usage is obtained.

[0040] In a second aspect, a lightweight memory dynamic management device is provided, which adopts the following technical scheme, including:

[0041] An initial module for initializing mapping array, managed memory and free segment information on memory, and identifying memory application information;

[0042] A continuous module for, if the memory application information is a continuous memory segment application, converting minimum unit number according to application length, searching for a physically continuous minimum allocation unit segment based on the free segment information, adjusting link table pointer to separate the minimum allocation unit segment, and updating the free segment information, and returning a segment start address;

[0043] A disconnection module for, if the memory application information is a non-continuous memory segment application, based on the free segment information and the application length, cutting a target link table segment corresponding to the application length from a head of a free link table, adjusting link table pointer to separate the target link table segment, updating the free segment information, and returning a segment start address;

[0044] A read-write module for, when receiving a read-write instruction, for the continuous memory segment application, directly performing read-write based on a memory address obtained by application as a base address, and for the non-continuous memory segment application, based on a virtual start address, an offset and a data length, locating a mapping array first element, positioning byte positions by the offset and unit size one by one, and performing cross-unit read-write operation;

[0045] A release module for, when receiving a memory segment release instruction, locating the mapping array first element according to a to-be-released start address, and merging a to-be-released link table and the free link table into an ascending link table, and updating a free link table length.

[0046] In a third aspect, the application further provides a control device, which comprises:

[0047] The device comprises a memory and a processor, and the memory stores a computer program capable of being loaded and executed by the processor and performing the above lightweight memory dynamic management method.

[0048] In a fourth aspect, the application further provides a computer readable storage medium storing a computer program capable of being loaded and executed by a processor and performing the above lightweight memory dynamic management method.

[0049] In summary, through the mapping array, linked list mechanism and standardized process, the efficient dynamic allocation, release and read-write management of memory are realized in the application. The effects are as follows: in the initialization stage, the initial state of the mapping array (constructing the free linked list), the managed memory (clearing to prevent residue) and the free segment information (recording the linked list head, length, etc.) is determined, laying a foundation for subsequent operations; for continuous and non-continuous memory application, the allocation is realized by searching the physically continuous segment or intercepting the linked list segment, the target segment is separated combined with pointer adjustment and the free information is updated, taking into account the physical continuity requirement and flexible allocation scene; in the read-write operation, the continuous memory is directly accessed based on the address and the non-continuous memory is positioned across the unit through the mapping array, ensuring the efficient access of different allocation types; when the memory is released, the to-be-released linked list and the free linked list are combined into an ascending linked list, reducing the fragmentation and updating the length; the additional memory usage statistics can monitor the resource state in real time. Through the lightweight linked list operation, the management overhead is reduced, the memory demand in multiple scenes is supported, the memory utilization and system reliability are improved, and the method is suitable for resource-limited embedded or lightweight system environment. BRIEF DESCRIPTION OF DRAWINGS

[0050] Figure 1 FIG. 1 is a flow diagram of a lightweight memory dynamic management method.

[0051] Figure 2 FIG. 2 is a memory mapping diagram of a lightweight memory dynamic management method.

[0052] Figure 3 FIG. 3 is a continuous memory segment application diagram of a lightweight memory dynamic management method.

[0053] Figure 4 FIG. 4 is a non-continuous memory segment application diagram of a lightweight memory dynamic management method.

[0054] Figure 5 FIG. 5 is a memory folding application diagram of a lightweight memory dynamic management method.

[0055] Figure 6 FIG. 6 is a memory query diagram of a lightweight memory dynamic management method.

[0056] Figure 7 FIG. 7 is a memory release diagram of a lightweight memory dynamic management method.

[0057] Figure 8 FIG. 8 is a structural block diagram of a lightweight memory dynamic management device. DETAILED DESCRIPTION

[0058] The application will be further described in detail below. Figure 1 Figure 8

[0059] ​​The memory dynamic management mechanism supports continuous memory application and non-continuous memory application. The continuous memory application is a traditional use method, and the non-continuous memory application can use small memory blocks split from the continuous memory block that has been applied, thereby increasing the memory use efficiency. The memory dynamic management mechanism does not depend on specific operating systems or memory management unit hardware. The memory dynamic management mechanism can be used on small platforms with relatively tight resources, and the use method is simple and has no special restrictions, and the portability is high.

[0060] The memory dynamic management mechanism has controllable execution time, does not generate memory fragmentation, and has no application times limit. The initialization, application, release, query and other operations have no high time complexity, and the execution time is predictable. The released memory is merged back to the idle memory, and does not cause memory fragmentation and cannot be applied. When applying, there is no need to add new memory to record the information of the memory that has been applied. The total memory overhead is determined, and there is no application times limit. There is no problem that the idle memory cannot be applied due to too many application times.

[0061] Reference Figure 1 and Figure 2 The embodiment of the application at least includes steps S10 to S50.

[0062] S10, initialize the mapping array on the memory, the managed memory and the idle segment information, and identify the memory application information.

[0063] S20, if the memory application information is a continuous memory segment application, then according to the application length, the minimum unit number is converted, based on the idle segment information, the physically continuous minimum allocation unit segment is searched, the linked list pointer is adjusted to separate the minimum allocation unit segment, and the idle segment information is updated, and the segment starting address is returned.

[0064] S30, if the memory application information is a non-continuous memory segment application, then based on the idle segment information and the application length, the target linked list segment corresponding to the application length is cut from the head of the idle linked list, the linked list pointer is adjusted to separate the target linked list segment, the idle segment information is updated, and the segment starting address is returned.

[0065] S40, when receiving the read-write instruction, for the continuous memory segment application, the memory address obtained based on the application is directly used as the base address for reading and writing. For the non-continuous memory segment application, the virtual starting address, the offset and the data length are used to locate the first element of the mapping array, the byte position is located one unit at a time through the offset and the unit size, and the cross-unit read-write operation is performed.

[0066] S50, when receiving the memory segment release instruction, the first element of the mapping array is located according to the to-be-released starting address, and the to-be-released linked list and the idle linked list are merged into an ascending linked list, and the length of the idle linked list is updated.

[0067] Specifically, by initializing the mapping array, the managed memory and the free segment information, and identifying the application type, the physical continuous segment or the truncated linked list segment is searched for allocation and updating of the free information for continuous / non-continuous memory application, the continuous memory is directly read and written, the non-continuous memory is read and written through the mapping array cross-cell positioning, and the to-be-released linked list and the free linked list are combined and the length is updated when released. The role is to realize efficient dynamic management through light linked list operation, take into account different memory allocation needs, reduce fragmentation, improve memory utilization, and be suitable for resource-limited embedded or lightweight system environment.

[0068] In some embodiments, referring to Figure 2 , step S10 specifically includes the following steps: receiving memory application information, the memory application information including application length and application type identifier, judging the memory application information as continuous memory segment application or non-continuous memory segment application; clearing the managed memory pool as needed to ensure that there is no residual data in the initial state; the mapping array is an integer array corresponding to the smallest allocation unit in the managed memory, the value of each element is the subscript of the next element, forming a one-way linked list, and the element is negative one when the linked list ends; the free segment information includes the free linked list head subscript, the free linked list length and the historical minimum length, the free linked list head subscript is set to zero, the free linked list length is set to the total length of the mapping array, and the historical minimum length is set to the free linked list length.

[0069] For easy understanding, taking 1M Bytes as an example, the minimum allocation unit is a fixed size, and for easy understanding of the present application, 32 Bytes is taken as the minimum unit, and the array length of the mapping array is 32,768.

[0070] Specifically, the memory application information including application length and type identifier is received, and judged as continuous or non-continuous application; the 1M Bytes of managed memory pool is cleared as needed to prevent residual; the mapping array (length 32,768, corresponding to 1M / 32 Bytes minimum unit) is defined, each element is the subscript of the next unit (-1 indicates the end of the linked list), forming a one-way linked list; the free segment information (head subscript 0, length 32,768, historical minimum length same as current length) is initialized. The role is to lay the foundation for dynamic memory management: efficiently mark the free unit through the mapping array linked list structure, clear to ensure that there is no residual in the initial state, record the resource state through the free segment information, support subsequent on-demand allocation, release and read-write operation, and realize reliable management of lightweight memory.

[0071] In some embodiments, referring to Figure 3, step S20 specifically includes the following steps: according to the application length and the minimum allocation unit size, calculating the required minimum allocation unit number; if the free list length in the free segment information is not less than the required unit number, traversing the mapping array, finding the target element whose element subscript is less than the element value by one in the mapping array, and setting the element segment of the target element whose continuous number is greater than the minimum unit number as the continuous interception segment; if the continuous interception segment has a previous element, updating the value of the previous element to the original value of the last element of the continuous interception segment, and assigning the last element of the continuous interception segment to -1; if the continuous interception segment has no previous element, updating the free list head subscript to the original value of the last element of the continuous interception segment. Subtract the unit number of the intercepted segment from the free list length to obtain the current free list length, and if the current free list length is less than the historical minimum length, update the historical minimum length to the current free list length, and return the memory address corresponding to the first element of the continuous interception segment.

[0072] Specifically, first, according to the application length (such as 128 Bytes required) and the minimum unit size (such as 32 Bytes), the required minimum unit number (128 / 32=4) is calculated; then check if the free list length is sufficient (if the free list has ≥4 units, continue); then traverse the mapping array, find the continuous segment (such as the units from subscript i to i+3, whose values are i+1, i+2, i+3, i+4 in turn, and there are 4 continuous units) whose element subscript is less than the value by one (i.e. value=subscript+1, representing physical continuity) and the continuous number is ≥ the required unit number; if the segment has a predecessor element (i.e. there is an element j pointing to i), change the value of j to the original value of the last element (i+4), if the segment is the first segment, update the free list head to i+4, and finally set the last element of the segment to -1 (mark the end of the segment). The role is to efficiently allocate continuous memory through accurate search of physical continuous segments and adjustment of chain table pointers, meet the demand of continuous memory of hardware or specific scenarios, while maintaining the accuracy of the free list and reducing memory fragmentation.

[0073] In some embodiments, with reference to Figure 4 , step S30 specifically includes the following steps: if the free list length is not less than the minimum unit number, intercepting a target chain table segment with a length corresponding to the unit number of the application length from the free list head; setting the next element of the last element of the intercepted segment to the original free list head subscript, and assigning the last element of the intercepted segment to -1; if the current free list length is less than the historical minimum length, updating the historical minimum length to the current free list length; returning the memory address corresponding to the first element of the intercepted segment.

[0074] Specifically, if the length of the free chain table is greater than or equal to the required minimum number of units (for example, 4 units of 32 bytes are required for an application of 128 bytes), a target chain table segment with a length of the required number of units is directly cut from the head of the free chain table (the first 4 units are cut); the next element of the last element of the cut segment is set as the original free chain table head index (to keep the remaining free chain table continuous), and the last element is assigned a value of -1 (to mark the end of the segment); if the current length of the free chain table (the original length minus the number of cut units) is less than the historical minimum length, the historical minimum length is updated (to record the lowest remaining state of memory usage); and finally, the memory address corresponding to the first element of the cut segment is returned. The function is to simplify the non-continuous memory allocation logic by cutting from the head, efficiently meet the scattered memory requirements, adjust the pointer to keep the remaining free chain table integrity, reduce fragmentation, update the historical minimum length to monitor the memory usage peak, provide data support for system optimization, and improve the flexibility and reliability of memory management.

[0075] In some embodiments, with reference to Figure 5 and Figure 6 , step S40 specifically includes the following steps: upon receiving a read-write instruction, for a continuous memory segment application, directly performing read-write based on the obtained memory address as the base address; for a non-continuous memory segment application, providing the first address of the virtual continuous memory segment, the offset, and the data length to be read and written; converting the mapping array element corresponding to the virtual first address, and confirming that the mapping array element is the first element of the non-continuous memory chain table; determining the minimum allocation unit and the byte position in the unit where the data to be read and written is located according to the offset and the minimum allocation unit size; and performing read-write operation by jumping from one unit to another until the read-write of the data to be read and written is completed.

[0076] Specifically, for a continuous memory segment, the physical address is continuous, and read-write is directly performed starting from the obtained base address, which is efficient; for a non-continuous memory segment, the virtual continuous first address is perceived, the system needs to locate the mapping array first element (i.e. the head of the non-continuous chain table) in combination with the offset and data length, and then calculate the data cross-unit position (e.g. 0-32 bytes of the second unit and 0-0 bytes of the third unit) according to the offset (e.g. 64 bytes) and the minimum unit size (e.g. 32 bytes), jump from one unit to another to access the physical unit (e.g. read and write the second unit first, and then read and write the third unit), until the data read-write is completed. The function is to consider the access requirements of different memory allocation methods: continuous memory direct operation ensures efficiency, and non-continuous memory realizes transparent conversion of "continuous" on the user side and "scattered" on the physical side through virtual address mapping and cross-unit positioning, improves memory utilization, meets the dependence of hardware on continuous memory, and meets the requirements of software on flexible memory.

[0077] In some embodiments, with reference to Figure 7S50 specifically comprises the following steps: according to the first address of the memory to be released, the corresponding first element of the mapping array is calculated, and it is confirmed that the first element of the mapping array is the first element of the memory list to be released; the element indexes of the to-be-released list and the free list are compared one by one, and the element pointers are adjusted; the length of the free list is added to the length of the released list, and the length of the free list is updated.

[0078] Specifically, first, according to the first address of the memory to be released (such as the starting position of the memory released by the user), the corresponding first element of the memory in the mapping array (i.e. the head node of the memory list) is calculated, and it is confirmed that the element is the starting point of the to-be-released list; then the element indexes of the to-be-released list and the current free list are compared one by one (such as checking whether the released memory block is adjacent to the free block), the pointers of the list elements are adjusted (such as merging adjacent free blocks and updating the pointer connection relationship); finally, the length of the free list is added to the length of the released list (such as the original free list has 100 units, and 50 units are released, then it is updated to 150 units), and the update of the free list state is completed. Its role is to accurately locate the released memory, adjust the list pointer to merge the free block, reduce the memory fragmentation, update the length of the free list to maintain the accurate statistics of the system on the available memory, provide reliable free resource information for subsequent memory allocation, and improve the efficiency and stability of memory management.

[0079] In some embodiments, the corresponding processing steps are as follows: according to the length of the free list and the length of the mapping array, the size of the currently allocated memory and the size of the to-be-allocated memory are calculated. According to the historical minimum length and the length of the mapping array, the historical maximum allocated memory size is calculated, and the historical maximum memory usage is obtained.

[0080] Specifically, the current allocated memory is calculated by the length of the free list (the number of available units) and the length of the mapping array (the total number of units): current allocated memory = total number of units - number of free units; to-be-allocated memory = number of free units x unit size. In combination with the historical minimum free length (the minimum value of the free list), the historical maximum allocated = total number of units - historical minimum free, and the historical maximum usage = (historical maximum allocated / total number of units) x 100%. The role is to monitor the memory usage state in real time, master the historical peak value, provide data support for system optimization of memory allocation and prevention of resource shortage, and improve the efficiency of memory management.

[0081] The implementation principle of a lightweight dynamic memory management method in this application embodiment is as follows: the system achieves efficient dynamic allocation, release, and read / write management of memory through a mapping array, linked list mechanism, and standardized process. Its functions are reflected in: during the initialization phase, the initial states of the mapping array (constructing a free linked list), managed memory (zeroing to prevent residual memory), and free segment information (recording the linked list head, length, etc.) are clearly defined, laying the foundation for subsequent operations; for contiguous and non-contiguous memory requests, allocation is achieved by searching for physically contiguous segments or truncating linked list segments, respectively, combined with pointer adjustment to separate the target segment and update free information, balancing the need for physical contiguity and flexible allocation scenarios; during read / write operations, contiguous memory is accessed directly based on address, while non-contiguous memory is located across units through the mapping array, ensuring efficient access for different allocation types; when releasing memory, the unreleased linked list and the free linked list are merged into an ascending linked list, reducing fragmentation and updating the length; additional memory usage statistics can monitor resource status in real time. By reducing management overhead through lightweight linked list operations, it supports memory requirements in multiple scenarios, improves memory utilization and system reliability, and is suitable for resource-constrained embedded or lightweight system environments.

[0082] Figure 1 This is a flowchart illustrating a lightweight dynamic memory management method in one embodiment. It should be understood that, although... Figure 1 The steps in the flowchart are shown sequentially as indicated by the arrows, but these steps are not necessarily executed in the order indicated by the arrows; unless explicitly stated otherwise, there is no strict order requirement for the execution of these steps, and they can be executed in other orders; and Figure 1 At least some of the steps in the process may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these sub-steps or stages is not necessarily sequential, but can be executed in turn or alternately with other steps or at least some of the sub-steps or stages of other steps.

[0083] Based on the same technical concept, referring to Figure 8 This application also provides a lightweight dynamic memory management device, which adopts the following technical solution: the device includes:

[0084] The initialization module is used to initialize the mapped arrays, managed memory, and free segment information in memory, and to identify memory request information;

[0085] The contiguous module is used to calculate the minimum number of units based on the requested length if the memory request information is for a contiguous memory segment. Based on the free segment information, it searches for a physically contiguous minimum allocation unit segment, adjusts the linked list pointers to separate the minimum allocation unit segment, updates the free segment information, and returns the segment head address.

[0086] The disconnection module is configured to, if the memory application information is a non-continuous memory segment application, based on the free segment information and the application length, cut a target linked list segment with a corresponding application length from a head of a free linked list, adjust a linked list pointer to separate the target linked list segment, update the free segment information, and return a segment start address;

[0087] The read-write module is configured to, when receiving a read-write instruction, for a continuous memory segment application, directly perform read-write based on a memory address obtained by the application as a base address, and for a non-continuous memory segment application, based on a virtual start address, an offset and a data length, locate a first element of a mapping array, locate a byte position by the offset and a unit size one by one, and perform a cross-unit read-write operation.

[0088] The release module is configured to, when receiving a memory segment release instruction, locate a first element of a mapping array according to a to-be-released start address, and combine a to-be-released linked list and a free linked list into an ascending linked list, and update a free linked list length.

[0089] In some embodiments, the initial module is specifically configured to receive memory application information, the memory application information including an application length and an application type identifier, and determine whether the memory application information is a continuous memory segment application or a non-continuous memory segment application according to the application type identifier.

[0090] The managed memory pool is cleared on demand to ensure that there is no residual data in the initial state.

[0091] The mapping array is an integer array, which corresponds to the smallest allocation unit in the managed memory one by one, and the value of each element is the subscript of the next element, forming a one-way linked list, and the element is negative one when the linked list ends.

[0092] The free segment information includes a free linked list head subscript, a free linked list length and a historical minimum length, the free linked list head subscript is set to zero, the free linked list length is set to the total length of the mapping array, and the historical minimum length is set to the free linked list length.

[0093] In some embodiments, the continuous module is specifically configured to calculate the number of required smallest allocation units according to the application length and the size of the smallest allocation unit.

[0094] If the free linked list length in the free segment information is not less than the required number of units, the mapping array is traversed to find a target element whose element subscript is less than the element value by one, and an element segment with a target element continuous number greater than the minimum unit number is set as a continuous cutting segment.

[0095] If the continuous cutting segment has a previous element, the value of the previous element is updated to the original value of the last element of the continuous cutting segment, and the last element of the continuous cutting segment is assigned a value of -1.

[0096] If the continuous cutting segment does not have a previous element, the free linked list head subscript is updated to the original value of the last element of the continuous cutting segment.

[0097] Subtract the number of units of the truncated segment from the length of the free list to obtain a current length of the free list, and if the current length of the free list is less than the historical minimum length, update the historical minimum length as the current length of the free list, and return a memory address corresponding to a first element of the continuous truncated segment.

[0098] In some embodiments, the intermittent module is specifically configured to, if the length of the free list is not less than the minimum number of units, truncate a target list segment with a length corresponding to the number of units of the application length from a head of the free list;

[0099] Set a next element of a last element of the truncated segment as a head subscript of the original free list, and assign the last element of the truncated segment as -1.

[0100] If the current length of the free list is less than the historical minimum length, update the historical minimum length as the current length of the free list.

[0101] Return a memory address corresponding to a first element of the truncated segment.

[0102] In some embodiments, the read-write module is specifically configured to, when receiving a read-write instruction, for a continuous memory segment application, directly perform read-write based on a memory address obtained by the application as a base address.

[0103] For a non-continuous memory segment application, provide a first address of a virtual continuous memory segment, an offset, and a data length to be read and written.

[0104] Convert a mapping array element corresponding to the virtual first address, and confirm that the mapping array element is a first element of a non-continuous memory list.

[0105] According to the offset and the minimum allocation unit size, determine a minimum allocation unit and a byte position in the unit where the data to be read and written is located.

[0106] Jump to perform read-write operation unit by unit until the read-write of the data to be read and written is completed.

[0107] In some embodiments, the release module is specifically configured to, according to a first address of the memory to be released, calculate a mapping array first element corresponding to the first address, and confirm that the mapping array first element is a first element of a memory list to be released.

[0108] Compare the element subscripts of the list to be released and the free list one by one, and adjust the element pointers.

[0109] Add the length of the free list to the length of the released list to update the length of the free list.

[0110] In some embodiments, the releasing module is further configured to calculate a current allocated memory size and a to-be-allocated memory size according to the free list length and the mapping array length. A historical maximum allocated memory size is calculated according to the historical minimum length and the mapping array length, and a historical maximum memory usage is obtained.

[0111] The embodiment of the present application further discloses a control device.

[0112] Specifically, the control device comprises a memory and a processor, and the memory stores a computer program capable of being loaded and executed by the processor to perform the above-mentioned lightweight memory dynamic management method.

[0113] The embodiment of the present application further discloses a computer readable storage medium.

[0114] Specifically, the computer readable storage medium stores a computer program capable of being loaded and executed by the processor to perform the above-mentioned lightweight memory dynamic management method, and the computer readable storage medium comprises various storage media capable of storing program codes, for example, a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.

[0115] The above are preferred embodiments of the present application, and are not intended to limit the protection scope of the present application, therefore: any equivalent changes made according to the structure, shape, principle of the present application should be covered within the protection scope of the present application.

Claims

1. A lightweight dynamic memory management method, characterized in that, include: Initialize the mapped array, managed memory, and free segment information in memory, and identify memory request information; If the memory request information is a contiguous memory segment request, then the minimum number of units is calculated based on the request length. Based on the free segment information, a physically contiguous minimum allocation unit segment is searched, the linked list pointers are adjusted to separate the minimum allocation unit segment, the free segment information is updated, and the segment head address is returned. If the memory request information is a non-contiguous memory segment request, then based on the free segment information and the request length, the target linked list segment corresponding to the request length is extracted from the head of the free linked list, the linked list pointer is adjusted to separate the target linked list segment, the free segment information is updated, and the segment head address is returned; When a read / write instruction is received, for the application of a contiguous memory segment, the read / write operation is performed directly based on the memory address obtained from the application as the base address. For the application of a non-contiguous memory segment, the first element of the mapping array is located based on the virtual starting address, offset, and data length. The byte position is located unit by unit through the offset and unit size, and cross-unit read / write operations are performed. Upon receiving a memory segment release instruction, the first element of the mapping array is located based on the address to be released, and the list to be released and the free list are merged into an ascending list, and the length of the free list is updated.

2. The lightweight dynamic memory management method according to claim 1, characterized in that, The initialization of the memory mapping array, managed memory, and free segment information, and the identification of memory request information, includes: Receive the memory request information, which includes the request length and the request type identifier. Based on the request type identifier, determine whether the memory request information is a contiguous memory segment request or a non-contiguous memory segment request. The managed memory pool is cleared as needed to ensure that there is no residual data in the initial state; The mapping array is an integer array that corresponds one-to-one with the smallest allocation unit in the managed memory. The value of each element is the index of the next element, forming a singly linked list. The linked list ends when the element is negative one. The free segment information includes the free list head index, the free list length, and the historical minimum length. The free list head index is set to zero, the free list length is set to the total length of the mapping array, and the historical minimum length is set to the free list length.

3. The lightweight dynamic memory management method according to claim 2, characterized in that, If the memory request information is for a contiguous memory segment, then the minimum number of units is calculated based on the request length. Based on the free segment information, a physically contiguous minimum allocation unit segment is searched. The linked list pointers are adjusted to separate the minimum allocation unit segment, and the free segment information is updated. The segment head address is then returned, including: Calculate the required number of minimum allocation units based on the application length and the minimum allocation unit size; If the length of the free linked list in the free segment information is not less than the required number of units, then traverse the mapping array, find the target element in the mapping array whose element index is one less than the element value, and set the segment of elements whose consecutive number of target elements is greater than the minimum number of units as a continuous segment. If the continuous segment has a preceding element, update the value of the preceding element to the original value of the last element of the continuous segment, and assign the value of the last element of the continuous segment to -1. If a consecutive segment does not contain a preceding element, then the head index of the free list is updated to the original value of the last element of the consecutive segment. The length of the free list is obtained by subtracting the number of segments from the length of the free list. If the length of the current free list is less than the historical minimum length, the historical minimum length is updated to the current length of the free list, and the memory address corresponding to the first element of the consecutive segments is returned.

4. The lightweight dynamic memory management method according to claim 3, characterized in that, If the memory request information is for a non-contiguous memory segment, then based on the free segment information and the request length, a target linked list segment corresponding to the request length is extracted from the head of the free linked list, the linked list pointer is adjusted to separate the target linked list segment, the free segment information is updated, and the segment head address is returned, including: If the length of the free list is not less than the minimum number of units, then the target list segment with a length equal to the number of units corresponding to the requested length is truncated from the head of the free list. Set the next element after the last element of the truncated segment as the index of the head of the original free list, and assign the value -1 to the last element of the truncated segment; If the current length of the free list is less than the historical minimum length, then update the historical minimum length to the current length of the free list; Returns the memory address of the first element of the extracted segment.

5. The lightweight dynamic memory management method according to claim 4, characterized in that, Upon receiving a read / write instruction, for the contiguous memory segment request, read / write is performed directly based on the memory address obtained from the request as the base address. For the non-contiguous memory segment request, the first element of the mapping array is located based on the virtual starting address, offset, and data length. The byte position is located unit by unit using the offset and unit size, and cross-unit read / write operations are performed, including: Upon receiving a read / write instruction, for the requested contiguous memory segment, read / write operations are performed directly based on the memory address obtained from the request, using that address as the base address. For the non-contiguous memory segment request, the starting address of the virtual contiguous memory segment, the offset, and the length of the data to be read or written are provided; Calculate the mapping array element corresponding to the virtual starting address, and confirm that the mapping array element is the first element of a non-contiguous memory linked list; Based on the offset and the size of the minimum allocation unit, determine the minimum allocation unit where the data to be read or written is located and the byte position within the unit; The read and write operations are performed one unit at a time until the read and write of the data to be read and written are completed.

6. The lightweight dynamic memory management method according to claim 5, characterized in that, Upon receiving a memory segment release instruction, the process of locating the first element of the mapping array based on the address to be released, merging the list to be released and the free list into an ascending list, and updating the length of the free list includes: Based on the starting address of the memory to be released, calculate the first element of the corresponding mapping array, and confirm that the first element of the mapping array is the first element of the linked list of memory to be released; Compare the element indices of the linked list to be released and the free linked list one by one, and adjust the element pointers accordingly; The length of the free list is updated by adding the length of the released list to the length of the free list.

7. A lightweight dynamic memory management method according to claim 5, characterized in that, The method further includes: Calculate the current allocated memory size and the memory size to be allocated based on the length of the free list and the length of the mapping array. Based on the historical minimum length and the length of the mapping array, calculate the historical maximum allocated memory size and obtain the historical maximum memory utilization rate.

8. A lightweight dynamic memory management device, characterized in that, The device includes: The initialization module is used to initialize the mapped arrays, managed memory, and free segment information in memory, and to identify memory request information; The contiguous module is used to calculate the minimum number of units based on the request length if the memory request information is a contiguous memory segment request, search for physically contiguous minimum allocation unit segments based on the free segment information, adjust the linked list pointers to separate the minimum allocation unit segments, update the free segment information, and return the segment head address; The disconnection module is used to, if the memory request information is a non-contiguous memory segment request, extract a target linked list segment corresponding to the request length from the head of the free linked list based on the free segment information and the request length, adjust the linked list pointer to separate the target linked list segment, update the free segment information, and return the segment head address; The read / write module is used to, upon receiving a read / write instruction, directly read / write the contiguous memory segment request based on the memory address obtained from the request as the base address, and for the non-contiguous memory segment request, locate the first element of the mapping array based on the virtual starting address, offset, and data length, locate the byte position unit by unit through the offset and unit size, and perform cross-unit read / write operations. The release module is used to locate the first element of the mapping array according to the starting address to be released when a memory segment release instruction is received, and to merge the list to be released and the free list into an ascending list and update the length of the free list.

9. A control device, characterized in that, The device includes: It includes a memory and a processor, wherein the memory stores a computer program that can be loaded by the processor and executed as described in any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that, The computer program is stored that can be loaded by a processor and executed as described in any one of claims 1 to 7.