A memory allocation method

By introducing the combination of MallocPackage data blocks and operating system memory management in embedded systems, the problem of low memory management efficiency in embedded systems is solved, and system performance improvement and memory utilization improvement are achieved.

CN113946444BActive Publication Date: 2025-03-25HANGZHOU NATCHIP SCI & TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202111198865.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-10-14
Publication Date
2025-03-25
Estimated Expiration
2041-10-14

AI Technical Summary

Technical Problem

In embedded systems, it is difficult for the prior art to efficiently and reasonably manage memory under limited hardware performance, resulting in low system performance and low memory usage.

Method used

Using an operating system-based memory application release method, the MallocPackage data blocks are generated and managed, frequent system calls are reduced, and memory allocation and recycling are optimized.

Benefits of technology

When releasing the same size of memory blocks at high frequency, reduce system call overhead and improve system performance; when memory blocks with low heat value are insufficient, priority will be given to releasing memory blocks with low heat value to improve memory utilization.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113946444B_ABST
    Figure CN113946444B_ABST
Patent Text Reader

Abstract

The present invention discloses a memory allocation method. When a user applies for memory for the first time, a memory block is applied to the operating system, and a data block is generated at the same time, and a pointer to the applied memory block is retained in the data block; the data block contains an identifier, an applied memory block size, and a heat value; the data block is put into a use table; when the user applies for a memory block again, a data block with the same identifier and applied memory block size is searched from the free table; if found, the data block is returned to the user for use, and the data block is moved from the free table to the use table; if not found, this application is the first application. When the user releases the memory, the data block is moved from the use table to the free table. If the system memory is insufficient, data blocks with small heat values ​​are released first. By adopting the method of the present invention, when applying for the release of memory blocks of the same size at a high frequency, the operating system is no longer required every time, thereby avoiding the extra overhead caused by frequent system calls and improving memory utilization.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention belongs to the field of computer technology and relates to a memory allocation method, in particular to a method for managing memory allocation in embedded software. Background Art

[0002] Embedded systems are increasingly used, and many fields require dedicated embedded hardware and embedded software. In addition, the performance of the hardware running embedded software is not very strong. How to improve software efficiency under limited hardware performance is a very important research direction. Among them, how embedded software can efficiently and reasonably manage memory is one of the difficult problems. Generally speaking, in order to achieve memory utilization, a design is adopted to apply for memory from the operating system and release it after use. However, frequent application and release of memory by the operating system will bring additional system overhead. Sometimes, a fixed memory design method is adopted for performance, but this method brings the problem of low memory utilization. Therefore, a new memory allocation management method is urgently needed to avoid system calls when memory is frequently used, improve system performance, and allow the operating system to reclaim memory when memory is insufficient or when the module exits, thereby improving memory utilization. Summary of the invention

[0003] The purpose of the present invention is to address the deficiencies of the prior art and provide a new memory allocation method based on operating system memory application release.

[0004] The method of the present invention includes applying for memory, releasing memory, and clearing cache. The details are as follows:

[0005] When the user applies for memory for the first time, he applies for a memory block from the operating system and generates a data block MallocPackage. The pointer *P of the applied memory block is retained in the Malloc Package. The Malloc Package contains the application identifier ID passed in by the user, the size of the applied memory block Size, and the hot value Hot of the Malloc Package itself. The Malloc Package is put into the usage table Malloc.

[0006] When the user applies for a memory block again, the free table Free is searched for a MallocPackage with the same ID and Size as the current application; if found, the found Malloc Package is returned to the user for use, and the Malloc Package is moved from the Free table to the Malloc table; if not found, this application is the first application.

[0007] When the user releases memory, the Malloc Package is moved from the Malloc table to the Free table.

[0008] If the system memory is insufficient when the user applies for memory, the MallocPackage in the Free table is released to the system according to the hot value, and the Malloc Package with a small hot value is released first. The user is allowed to call the clear cache interface to release all Malloc Packages in the Free table.

[0009] By adopting the method of the present invention, when frequently requesting to release memory blocks of the same size, the operating system is no longer required every time, thus avoiding the extra overhead caused by frequent system calls. At the same time, when the memory is insufficient, the memory block can be returned to the operating system, thus improving the memory utilization rate. BRIEF DESCRIPTION OF THE DRAWINGS

[0010] Figure 1 A flowchart of applying for memory in the method of the present invention;

[0011] Figure 2 A flowchart of releasing memory in the method of the present invention;

[0012] Figure 3 The present invention is a flowchart of clearing the cache in the method of the present invention. DETAILED DESCRIPTION

[0013] The present invention is further described below in conjunction with the accompanying drawings.

[0014] The method of the invention comprises applying for memory, releasing memory and clearing cache.

[0015] When the user applies for memory for the first time, he applies for a memory block from the operating system and generates a data block MallocPackage. The pointer *P of the applied memory block is retained in the Malloc Package. The Malloc Package contains the application identifier ID passed in by the user, the size of the applied memory block Size, and the hot value Hot of the Malloc Package itself. The Malloc Package is put into the usage table Malloc.

[0016] When the user applies for a memory block again, the free table Free is searched for a MallocPackage with the same ID and Size as the current application; if found, the found Malloc Package is returned to the user for use, and the Malloc Package is moved from the Free table to the Malloc table; if not found, this application is the first application.

[0017] When the user releases memory, the Malloc Package is moved from the Malloc table to the Free table.

[0018] If the system memory is insufficient when the user applies for memory, the MallocPackage in the Free table is released to the system according to the hot value, and the Malloc Package with a small hot value is released first. The user is allowed to call the clear cache interface to release all Malloc Packages in the Free table.

[0019] like Figure 1 As shown, the specific steps for users to apply for memory are as follows:

[0020] Step (1) The user sends a memory request instruction;

[0021] Step (2) determines whether the data block Malloc Package generated by this application already exists in the Free table based on the ID and Size passed in by the user: if it does, execute steps (3) to (7); if it does not, execute step (8);

[0022] Step (3) Get the existing Malloc Package from the Free table;

[0023] Step (4) adding 1 to the heat value Hot in the obtained Malloc Package;

[0024] Step (5) remove the obtained Malloc Package from the Free list;

[0025] Step (6) adding the obtained Malloc Package to the Malloc table;

[0026] Step (7) returns the obtained Malloc Package to the user;

[0027] Step (8) applying for a memory block through the operating system and the Size passed in by the user;

[0028] Step (9) determines whether the pointer *P obtained from the operating system is NULL: if not, execute steps (10) to (13); if yes, execute step (14);

[0029] Step (10) generates a new Malloc Package, and fills the pointer *P obtained from the operating system into the new Malloc Package;

[0030] Step (11) generates a new ID and fills the new ID into the new Malloc Package;

[0031] Step (12) Initialize the heat value Hot in the new Malloc Package to 0;

[0032] Step (13) returns the new Malloc Package to the user;

[0033] Step (14) determines whether there is still a Malloc Package in the Free table. If so, execute step (15); if not, execute step (16);

[0034] Step (15) obtains the Malloc Package with the smallest hot value from the Free table and releases it, and returns to step (8);

[0035] Step (16) returns error information to the user.

[0036] like Figure 2 As shown, the specific steps for users to release memory are as follows:

[0037] Step (1) The user sends a memory release instruction and passes in the Malloc Package corresponding to the memory to be released that has been returned to the user;

[0038] Step (2) determines whether the Malloc Package corresponding to the memory to be released exists in the Malloc table: if it does, execute step (3); otherwise, return an error message to the user;

[0039] Step (3) remove the Malloc Package corresponding to the memory to be released from the Malloc table;

[0040] Step (4) adds the Malloc Package corresponding to the memory to be released to the Free table, and the release process ends.

[0041] like Figure 3 As shown, the specific steps for users to clear the cache are as follows:

[0042] Step (1) The user sends a cache clearing instruction;

[0043] Step (2) determines whether there is a Malloc Package in the Free table: if so, execute step (3); otherwise, the cache is cleared;

[0044] Step (3) obtain a Malloc Package in the Free table;

[0045] Step (4) releases the pointer *P in the acquired Malloc Package;

[0046] Step (5) releases the acquired Malloc Package; continue to execute step (2).

Claims

1. A memory allocation method, comprising applying for memory, releasing memory and clearing cache, characterized in that: The application memory is: When the user applies for memory for the first time, he applies for a memory block from the operating system and generates a data block Malloc Package. The pointer *P of the applied memory block is retained in the Malloc Package. The Malloc Package contains the application identifier ID and the size of the applied memory block Size passed in by the user, as well as the hot value Hot of the Malloc Package itself. The Malloc Package is put into the usage table Malloc. When the user applies for a memory block again, the free table Free is searched for a MallocPackage with the same ID and Size as the current application; if found, the found Malloc Package is returned to the user for use, and the Malloc Package is moved from the Free table to the Malloc table; if not found, this application is the first application; The memory release is as follows: when the user releases the memory, the Malloc Package is moved from the Malloc table to the Free table; The cache clearing is as follows: when the user applies for memory, if the system memory is insufficient, the Malloc Package in the Free table is released to the system according to the hot value, and the Malloc Package with a small hot value is released first; The steps for applying for memory are as follows: Step (1) The user sends a memory request instruction; Step (2) determines whether the data block Malloc Package generated by this application already exists in the Free table based on the ID and Size passed in by the user: if it does, execute steps (3) to (7); if it does not, execute step (8); Step (3) Get the existing Malloc Package from the Free table; Step (4) adding 1 to the heat value Hot in the obtained Malloc Package; Step (5) remove the obtained Malloc Package from the Free list; Step (6) adding the obtained Malloc Package to the Malloc table; Step (7) returns the obtained Malloc Package to the user; Step (8) applying for a memory block through the operating system and the Size passed in by the user; Step (9) determines whether the pointer *P obtained from the operating system is NULL: if not, execute steps (10) to (13); if yes, execute step (14); Step (10) generates a new Malloc Package, and fills the pointer *P obtained from the operating system into the new Malloc Package; Step (11) generates a new ID and fills the new ID into the new Malloc Package; Step (12) Initialize the heat value Hot in the new Malloc Package to 0; Step (13) returns the new Malloc Package to the user; Step (14) determines whether there is still a Malloc Package in the Free table. If so, execute step (15); if not, execute step (16); Step (15) obtains the Malloc Package with the smallest hot value from the Free table and releases it, and returns to step (8); Step (16) returns error information to the user; The steps of releasing memory are as follows: Step a. The user sends a memory release instruction and passes in the MallocPackage corresponding to the memory to be released that has been returned to the user; Step b. Determine whether the Malloc Package corresponding to the memory to be released exists in the Malloc table: if it does, execute step c, otherwise return an error message to the user; Step c. Remove the Malloc Package corresponding to the memory to be released from the Malloc table; Step d. Add the Malloc Package corresponding to the memory to be released to the Free table, and the release process ends; The steps to clear the cache are as follows: Step A. The user sends a cache clearing instruction; Step B. Determine whether there is a Malloc Package in the Free table: If so, execute step C, otherwise it means that the cache is cleared; Step C. Get a Malloc Package in the Free table; Step D. Release the pointer *P in the obtained Malloc Package; Step E. Release the acquired Malloc Package; continue to step B.

2. A memory allocation method as claimed in claim 1, characterized in that: In clearing the cache, the user is allowed to call the clear cache interface to release all Malloc Packages in the Free table.

Citation Information

Patent Citations

  • Memory control method for embedded systems

    CN102915276A

  • Heap memory management method and device, equipment and medium

    CN113296703A