A memory management method for operating system dynamic allocation and storage medium

By using dynamic memory management methods in embedded systems, a small amount of memory is initially allocated and dynamically expanded according to needs, solving the problem of wasted memory resources in embedded devices and achieving efficient utilization of memory resources and stable program operation.

CN120653439BActive Publication Date: 2025-12-09GUANGZHOU JINQILI INFORMATION TECH CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202510763407.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-06-09
Publication Date
2025-12-09
Estimated Expiration
2045-06-09

AI Technical Summary

Technical Problem

In existing technologies, embedded devices suffer from a scarcity of memory resources, leading to over-allocation during memory request, resulting in resource waste and low utilization.

Method used

The system adopts a dynamic memory allocation management method based on the operating system. A fixed small amount of memory is allocated initially, and then dynamically expanded according to actual needs. By modifying the memory allocation and operation interface, the program can run stably and avoid memory redundancy.

Benefits of technology

It improves memory resource utilization, supports more programs running simultaneously, reduces system maintenance costs, and ensures program stability and efficient use of memory resources.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120653439B_ABST
    Figure CN120653439B_ABST
Patent Text Reader

Abstract

The application relates to the technical field of embedded systems, in particular to a memory management method for dynamic allocation of an operating system and a storage medium, which realizes dynamic memory management by reforming an operating system memory allocation interface and an operation interface. A memory applicant adds a mark bit when initiating a demand application, the operating system executes an allocation operation according to the mark bit, and when the mark bit indicates that the demand is determined, a specified length is allocated, and when the demand is not determined, a fixed length memory block is first allocated. When executing a memory operation, the idle state of a subsequent memory block is detected according to a memory head mark bit, and the original memory block is expanded or is re-allocated and data is copied. The application can significantly reduce memory waste, improve memory use efficiency in a resource-limited scene such as an embedded system, guarantee stable operation of a program, enhance system flexibility through configuration file adjustment and a mark bit strategy, and facilitate memory management and maintenance in combination with detailed memory information recording.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The application belongs to the technical field of embedded systems, and particularly relates to a memory management method for dynamic allocation of an operating system and a storage medium. BACKGROUND

[0002] In the embedded industry, hardware resource constraints are a long-standing problem, especially memory resources are extremely scarce, and the device memory capacity is usually only a few hundred M, and some low-power or small devices even have less than a few hundred K. In the memory application process, the operating system and various application programs generally adopt a conservative memory allocation strategy for program stability considerations. To avoid program crashes, data loss and other abnormal situations caused by memory overflow, developers often apply for more memory space than the actual needs, especially when the memory usage changes dynamically or is difficult to accurately estimate, the phenomenon of over-application is more significant.

[0003] The existing patent (publication number CN109522121A) discloses a memory allocation method and device, comprising: dividing the memory space into a memory normal area and a memory reserved area; after receiving the memory application request of the application, judging whether the memory amount in the memory normal area reaches the memory amount applied by the application, if not, judging whether the application is an emergency application, if the application is an emergency application, allocating memory resources for the emergency application from the memory reserved area. The application prevents some key applications from running due to memory resource shortage, and ensures the stable operation of the system. Although the memory application strategy ensures the reliability of program operation to a certain extent, it inevitably causes excessive occupation and waste of memory resources, affecting the effective use of memory resources.

[0004] Therefore, the present application is proposed. SUMMARY

[0005] In order to solve the above technical problems in the prior art, the present application provides a memory management method for dynamic allocation of an operating system and a storage medium. In view of the problem of memory waste caused by excessive allocation when a program applies for memory in the prior art, the present application allocates only a small amount of memory at the first allocation, and then dynamically allocates memory according to the actual needs, so as to avoid allocating more memory than the actual needs on the premise of ensuring the normal operation of the program, and improve the effective utilization rate of memory resources.

[0006] To achieve the above purpose, the technical scheme of the present application is as follows:

[0007] In a first aspect, a memory management method for dynamic allocation of an operating system comprises:

[0008] The memory requester initiates a memory demand application, and the demand application includes a mark bit added according to whether the memory demand size is determined or not;

[0009] The operating system side performs a memory allocation operation based on the mark bit, wherein if the mark bit indicates that the memory demand size is determined, a memory block is allocated according to the length specified by the requester, and if the mark bit indicates that the memory demand size is not determined, a corresponding memory block is allocated to the requester according to the detected memory demand size.

[0010] Further, the specific steps of allocating a corresponding memory block according to the detected memory demand size include:

[0011] Obtaining the memory length required for operation, and detecting whether the memory blocks of a set length successively after the memory space are all idle;

[0012] If the memory blocks are idle, the idle memory blocks are applied and extended to the original memory, and the predetermined memory operation is performed;

[0013] If the memory blocks are not idle, a memory block of a matching size is searched for, a memory block of a preset length is allocated, the data on the original memory block is sequentially copied to the new memory block, the predetermined memory operation is performed, and finally the space of the original memory block is released and the first address of the new memory block is returned.

[0014] Further, when the mark bit indicates that the memory demand size is determined, the mark bit is set to 0, and the operating system allocates a memory block according to the length specified by the requester;

[0015] When the mark bit indicates that the memory demand size is not determined, the mark bit is set to 1, and the operating system ignores the length specified by the requester and allocates a memory block according to a first preset length.

[0016] Further, the specified length is 3 bytes or 10 bytes.

[0017] Further, when allocating memory, a memory of a second preset length is additionally applied to store the header information of the memory, and the value of the mark bit is stored in the header information of the memory regardless of whether the mark bit is 0 or 1.

[0018] Further, if the mark bit in the header information of the memory is 0, a memory block is directly allocated according to the length specified by the requester.

[0019] Further, if the mark bit in the header information of the memory is 1, the memory length required for operation is a third preset length, and it is detected whether the memory blocks successively after the memory space are all idle.

[0020] Further, the length of the successive memory blocks = the third preset length - the first preset length.

[0021] Further, if the detected memory block is idle, the idle memory block is applied and extended to the original memory, and then the memory operation is executed.

[0022] Further, if the detected memory block is not idle, a memory block with a length of the third preset length + the second preset length is allocated, the data on the original memory block is sequentially copied to the new memory block, and then the memory operation is executed.

[0023] Further, after the memory block is reallocated and the operation is executed, the space of the original memory block is released, and the first address of the new memory block is returned.

[0024] Further, the space of the original memory block is released by the operating system including the original memory block in the allocatable memory pool for subsequent memory allocation.

[0025] Further, whether the memory block is idle is determined by the operating system recording the usage state of the memory to determine whether the corresponding memory block is in an unused state.

[0026] In a second aspect, the application also provides a computer readable storage medium, the storage medium storing a computer program, and the computer program is executed by a processor to implement the memory management method of the operating system dynamic allocation.

[0027] Compared with the prior art, the application realizes dynamic memory management by modifying the operating system memory allocation interface and the operation interface, and has significant and multi-dimensional beneficial effects. For hardware resource limited scenarios such as embedded systems, when the memory demand is uncertain, the traditional mode of pre-allocating a large amount of memory is abandoned, and only a fixed small amount of memory is allocated at first, and then dynamically expanded according to the actual demand. This mechanism effectively avoids memory redundancy, maximizes the utilization rate of limited memory resources, can support more programs to run simultaneously, and significantly improves the use efficiency of memory resources. In terms of ensuring stable program operation, when the memory block is detected to be not idle during the execution of the memory operation, the appropriate memory block is found and the data is copied to ensure that the program will not cause an exception due to insufficient memory or overflow during the running process. Both the memory space required for normal program operation and the excessive occupation of memory resources are avoided, and a balance is achieved between stability and resource utilization. BRIEF DESCRIPTION OF DRAWINGS

[0028] Figure 1 The flowchart of the memory management method of the operating system dynamic allocation provided by the embodiment of the application. DETAILED DESCRIPTION

[0029] The technical solutions of the present application will be clearly described below with reference to the drawings. Obviously, the described embodiments are not all embodiments of the present application, and all other embodiments obtained by those of ordinary skill in the art without creative efforts fall within the protection scope of the present application.

[0030] It should be noted that, unless otherwise specified, the relative arrangement, numerical expression of the components and steps set forth in these embodiments should not be understood as limiting the scope of the present application.

[0031] The following description of exemplary embodiments is merely illustrative in nature and is in no way intended to limit the application or its application or use in any way. Techniques, methods, and devices known to those of ordinary skill in the relevant art may not be discussed in detail herein, but should be considered part of the present specification where applicable.

[0032] Reference Figure 1 A flowchart of a memory management method for dynamic allocation of an operating system according to the present application is shown in the figure. The problem of memory waste is solved by dynamic allocation of memory. When first allocated, only a small amount of memory length is allocated, and subsequent memory is dynamically allocated according to actual needs. This is achieved by modifying the memory allocation interface and memory operation interface in the operating system. The specific steps include:

[0033] S1, memory allocation interface modification

[0034] When the memory applicant initiates a memory demand application, a flag bit needs to be added in the application according to the determination of the size of the memory demand of the applicant. The operating system side performs memory allocation operation based on the flag bit, as follows:

[0035] S11, when the applicant knows the length of the memory needed and does not need redundancy, the flag bit is set to 0. At this time, the memory allocation interface of the operating system will allocate a memory block according to the length specified by the applicant.

[0036] For example, in a certain application scenario, the applicant calculates or knows the memory required by the program running, and specifies that 100 bytes of memory are needed. The operating system will strictly allocate a continuous memory block with a length of 100 bytes in the memory resource according to the application requirements. At the same time, when allocating memory, an additional memory with a fixed length, i.e. a second preset length, will be applied, which is assumed to be n bytes. The second preset length is usually 16 bytes or more, which may be slightly different for different operating systems, and is mainly used to store the header information of the memory, which can store the header information.

[0037] The extra memory is used to store the memory header information, which is an important data structure in memory management, containing the key attributes and management information of the memory block, such as the size of the memory block, the usage state, etc. At this time, the operating system will store the value of the mark bit, that is, 0, in the memory header information, which is convenient for subsequent identification and management of the memory block. After completing the allocation of the memory block and recording the information, the operating system returns the first address of the memory block to the applicant, and the applicant can access and operate the allocated memory through the first address.

[0038] S12, when the applicant is not sure about the actual memory length he needs, the mark bit is set to 1. At this time, the memory allocation interface of the operating system will ignore the length specified by the applicant, and instead allocate a memory block according to the first preset length. The first preset length is assumed to be h bytes, which can be determined by the user according to the actual application requirements and hardware environment, and can be flexibly configured through a configuration file; for example, to save more memory, the first preset length can be set as small as possible, even 0, but if the user feels that setting it to 5 is more appropriate, it can not only avoid the need to expand memory in most cases, but also achieve the effect of saving memory.

[0039] For example, in some embedded devices that are more cautious about memory usage, h can be set to 3 bytes; while in some scenarios where the initial memory demand is slightly larger, it can also be adjusted to 10 bytes. Taking a program with uncertain memory demand as an example, even if it specifies an arbitrary length when applying, the operating system will still allocate a memory block according to h bytes due to the mark bit being 1. Similarly, during the allocation of memory, n bytes of memory will be applied to store the memory header information, and the value 1 of the mark bit will be stored in the memory header information. Finally, the operating system returns the first address of the memory block to the applicant for subsequent use.

[0040] S2, memory operation interface modification

[0041] When the allocated memory needs to be operated, the operating system will read the mark bit from the memory header information that is about to be operated, and perform different operations according to the value of the mark bit:

[0042] S21, if the mark bit in the memory header information is 0, it indicates that the applicant has explicitly specified the memory demand and does not need subsequent adjustment when the memory block is allocated. At this time, the operating system will directly execute the established memory operation. For example, in a data processing program, if there is a data write operation, the operating system will directly write the data to the corresponding memory block storage location according to the first address of the memory block and the operation instruction, without the need for additional memory checking and adjustment operations.

[0043] S22, if the mark bit in the memory header information is read as 1, indicating that the applicant is uncertain about the actual memory requirement when the memory block is allocated, the operating system will perform the following steps:

[0044] S221, obtain the memory length to be operated: the operating system will obtain the memory length to be operated this time according to the current memory operation instruction and the program running requirement, and record it as a third preset length, assuming m bytes; the third preset length is determined by the user's operation, and the memory operation interface will obtain the length to be written when it needs to write to this segment of memory. This length m is dynamically determined according to the memory requirement generated in the actual running process of the program, for example, in the running process of the program, more memory space is needed to store data or run code due to the increase of data volume or the expansion of functional modules.

[0045] S222, detect the idle state of the memory block: after obtaining the length m, the operating system will detect whether the memory blocks with a length of (m-h) that are continuous after the memory space are all idle. The operating system internally maintains a set of memory usage state recording mechanism, usually in the form of data table or linked list and the like, which records the starting address, length, usage state and other information of each memory block in detail. By traversing and comparing these records, the operating system can judge whether the corresponding memory block is in an unused state, i.e. idle state.

[0046] S223, idle case processing: if it is detected that these continuous memory blocks are all idle, it indicates that the current memory space can meet the further memory requirement of the program. The operating system will initiate an application to the memory management module to obtain this idle memory block and extend it to the original memory block. For example, if the original memory block length is h bytes, after extension, the memory block length becomes m bytes, thereby providing sufficient memory space for the program. Then, the operating system can perform the intended memory operation, such as writing new data to the extended memory area.

[0047] S224, non-idle case processing: if it is detected that these continuous memory blocks are not all idle, it indicates that the current memory space cannot directly meet the memory requirement of the program. At this time, the operating system will start the memory reallocation mechanism to find a memory block with appropriate size in the memory resource. The length of the newly allocated memory block is (m+n) bytes, where m is the actual memory length required by the program, and n is the length for storing the memory header information.

[0048] The operating system will copy the data on the original memory block to the new memory block one by one according to the order of memory block data storage, ensuring the integrity and accuracy of the data. After the data copying is completed, the intended memory operation is performed to meet the running requirement of the program.

[0049] The operating system includes the original memory block into the allocable memory pool, updates the memory usage state record, marks the original memory block as a re-allocable state for subsequent other memory application, and returns the first address of the new memory block to the related program, so that the program can continue to operate the memory through the new address.

[0050] In the second aspect, the application provides a computer readable storage medium, wherein the storage medium stores a computer program, and the computer program is executed by a processor to realize the memory management method of the operating system dynamic allocation.

[0051] In summary, the application has the following advantages:

[0052] 1. Through the detection and reasonable processing of the idle state of the memory block, the program can have sufficient and appropriate memory space during running, and the program exception caused by insufficient memory or overflow can be avoided, thereby providing a solid guarantee for stable program running.

[0053] 2. The preset length of the memory allocation can be flexibly adjusted through the configuration file, so that the operating system can track and maintain the memory usage, and the flexibility and operability of the memory management are greatly improved.

[0054] 3. The high-efficiency management mechanism formed by the modified interface reduces the complexity of the memory management, so that the system can more conveniently locate and process the memory-related problems during running, and the system maintenance cost and difficulty are effectively reduced.

[0055] The above specific embodiments are only used to illustrate the technical solutions of the application rather than limit the application. Although the application has been described in detail with reference to the examples, it should be understood by those skilled in the art that the technical solutions of the application can be modified or replaced equivalently without departing from the scope of the technical solutions of the application, and all the modifications and replacements should be included in the scope of the claims of the application.

Claims

1. A method for managing memory dynamically allocated by an operating system, characterized by, The application relates to a memory management method for an operating system. The memory application initiates a memory demand application, and the demand application includes a mark bit added according to whether the memory demand size is determined; The operating system side performs a memory allocation operation based on the mark bit, wherein: if the mark bit indicates that the memory demand size is determined, a memory block is allocated according to the length specified by the application; if the mark bit indicates that the memory demand size is not determined, a corresponding memory block is allocated to the application according to the detected memory demand size, and the method specifically comprises the following steps: When the mark bit indicates that the memory demand size is determined, the mark bit is set to 0, and the operating system allocates a memory block according to the length specified by the application; when the mark bit indicates that the memory demand size is not determined, the mark bit is set to 1, and the operating system ignores the length specified by the application and allocates a memory block according to a first preset length; When the memory is allocated, a memory head information with a length of a second preset length is additionally applied, and the value of the mark bit is stored in the memory head information; when the allocated memory needs to be operated, the operating system reads the mark bit from the memory head information of the memory to be operated and performs different operations according to the value of the mark bit, and the method specifically comprises the following steps: If the mark bit in the memory head information is 0, the memory operation is directly performed; If the mark bit in the memory head information is 1, the length of the memory to be operated is a third preset length, and it is detected whether the memory blocks after the memory space are all idle; If the memory blocks after the memory space are all idle, idle memory blocks are applied and extended to the original memory, and then the memory operation is performed; If the memory blocks after the memory space are not all idle, a memory block with a length of the third preset length+the second preset length is allocated, data on the original memory block is sequentially copied to the new memory block, and then the memory operation is performed; After the memory block is re-allocated and the operation is performed, the space of the original memory block is released, and the first address of the new memory block is returned; The length of the continuous memory block is the third preset length-the first preset length.

2. The memory management method of dynamically allocating memory for an operating system according to claim 1, wherein, The first preset length is 3 bytes or 10 bytes.

3. The memory management method of dynamically allocating memory for an operating system according to claim 1, wherein, The space of the original memory block is released by the operating system, and the original memory block is included in the allocatable memory pool for subsequent memory allocation.

4. The memory management method of dynamically allocating memory for an operating system according to claim 1, wherein, Whether the memory block is idle is detected by recording the memory usage state of the operating system to determine whether the corresponding memory block is in an unused state.

5. A computer readable storage medium, characterized in that, The storage medium stores a computer program, and the computer program is executed by the processor to realize the memory management method of the operating system according to any one of claims 1-4.

Citation Information

Patent Citations

  • Memory application method and device, terminal and computer readable storage medium

    CN109522121A

  • Memory processing method and device, electronic equipment and storage medium

    CN118193222A

  • Memory allocation method and device and storage medium

    CN119440794A