A RAM usage optimization method, device and equipment in an embedded system

By adding custom segments and heap allocation/recycling functions to the embedded system, dynamically managing RAM and ROM storage is solved, and efficient memory management and flexible expansion of embedded systems are achieved.

CN118760534BActive Publication Date: 2024-11-22CHENGDU ZHONGSEN COMM TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411254820.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-09-09
Publication Date
2024-11-22
Estimated Expiration
2044-09-09

AI Technical Summary

Technical Problem

In embedded systems, the RAM capacity is limited, resulting in program crashes, performance degradation or functions that cannot be run. It is difficult for the prior art to achieve optimal RAM usage efficiency, especially poor flexibility and expansion.

Method used

Add custom segments to embedded systems, automatically put global variables into custom segment space through the compiler, form continuous memory segments adjacent to the RAM heap, define heap allocation and recycling functions, dynamically allocate and free memory, and in software At startup, the infrequently used global variables are transferred to the ROM to store, and the program is read from the ROM when it is run.

Benefits of technology

It realizes efficient use of RAM, dynamically expands ROM storage, maximizes the use of RAM heap space, eliminates the need to estimate data structures, and improves the memory management efficiency of embedded systems.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118760534B_ABST
    Figure CN118760534B_ABST
Patent Text Reader

Abstract

The present application relates to a method, device and equipment for optimizing RAM usage in an embedded system. The method comprises: adding a custom segment in the embedded system for storing global variables declared with the custom segment in RAM, and the custom segment is adjacent to the freely allocated RAM heap in the embedded system and forms a group of continuous memory segments. Furthermore, a heap allocation function and a heap recovery function are defined. For the heap space in the memory segment, the heap space memory segmentation allocation can be implemented based on the heap allocation function, and the heap space memory merging and release can be implemented based on the heap recovery function. For the space of the custom segment in the memory segment, the global variables in the custom segment can be written into ROM for storage by constructing a retrieval index, and when the software program needs to access the global variable, the global variable can be directly read based on the retrieval index for access. The use of this method can realize the efficient use of RAM memory in an embedded system.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of memory management technology, and in particular to a method, device and equipment for optimizing RAM usage in an embedded system. Background Art

[0002] In the process of embedded system software development, the capacity of RAM (Random Access Memory) is usually limited. This means that when dealing with slightly more complex programs, various functional defects may occur due to insufficient RAM. For example, the program may crash, performance may degrade, or some functions may not run due to insufficient memory. Therefore, improving RAM utilization becomes a key issue in embedded system software development.

[0003] An existing technical solution is to estimate the size of RAM data that is not frequently used in the software, segment the RAM, and write these infrequently used RAM segments into ROM (read-only memory) for storage. ROM is a non-volatile memory in which data will not be lost due to power failure. This method can theoretically free up part of the RAM for more frequently used data and functions. However, in practice, this method requires estimating the data structure in the RAM in advance, which makes it difficult to use in practice. In addition, this method is limited in terms of flexible scalability, making it difficult to achieve optimal RAM usage efficiency. Summary of the invention

[0004] Based on this, it is necessary to provide a RAM usage optimization method, device and equipment in an embedded system to address the above technical problems.

[0005] A method for optimizing RAM usage in an embedded system, the method comprising:

[0006] Add a custom segment in the embedded system, and specify the declaration of the custom segment in any one or more global variables in the RAM of the embedded system, and automatically put all global variables declared with the custom segment into the space of the custom segment through the compiler; wherein the custom segment is adjacent to the freely allocated RAM heap in the embedded system, and the space of the custom segment and the adjacent heap space form a continuous memory segment;

[0007] Define heap allocation function and heap recovery function, and initialize the memory segment;

[0008] For the heap space in the memory segment, when the software program in the embedded system is running, the heap allocation function is continuously called according to the single allocation space required for the current program to allocate the memory segment of the heap space; after the software program is completed, the heap recovery function is called to merge and release the memory of the heap space;

[0009] For the space of the custom segment in the memory segment, each time the software program in the embedded system is started, a retrieval index of the global variables declared with the custom segment is constructed, and based on the retrieval index, the global variables declared with the custom segment stored in the space of the custom segment are transferred to the ROM in the embedded system for storage, and when the global variables declared with the custom segment need to be accessed during the running of the software program, the corresponding global variables are read from the ROM according to the retrieval index for access.

[0010] In one embodiment, initializing the memory segment includes:

[0011] The space of the custom segment and the heap space in the memory segment are defined as memory structures respectively, and the space of the custom segment defined as the memory structure is set to a used heap space state, and the heap space defined as the memory structure is set to an unused heap space state;

[0012] A bidirectional linked list of memory segments is constructed based on the space of the custom segment defined as a memory structure and the heap space defined as a memory structure to complete the initialization processing of the memory segment; wherein, after the initialization processing of the memory segment is completed, the constructed bidirectional linked list contains two nodes, namely, a custom segment node in a used heap space state and a heap node in an unused heap space state, and both the custom segment node and the heap node are defined as memory structures.

[0013] In one embodiment, when a software program in an embedded system is running, a heap allocation function is continuously called to allocate memory segments of the heap space according to the single allocation space currently required for the program to run, including:

[0014] When the software program in the embedded system is running, the heap allocation function is called to traverse the two nodes in the bidirectional linked list constructed after the memory segment is initialized, and a heap node that meets the single allocation space size required for the current running of the program and is in an unused heap space state is found;

[0015] Split the heap node into two segments according to the single allocation space size required for the current operation of the program and the remaining memory size of the heap node itself, add the two segments obtained by the split into a two-way linked list as new heap nodes to obtain a new two-way linked list, maintain the relationship between adjacent nodes in the new two-way linked list, set the new heap node in the new two-way linked list whose space is consistent with the single allocation space size required for the current operation of the program to the used heap space state, and return the memory space address corresponding to the new heap node in the used heap space state to the program for use, and the new heap node in the new two-way linked list whose space is consistent with the initial heap node's own remaining memory size is still in the unused heap space state; wherein, the new two-way linked list contains 3 nodes, namely, 1 custom segment node in the used heap space state, 1 new heap node in the used heap space state, and 1 new heap node in the unused heap space state, and each node is defined as a memory structure;

[0016] According to the single allocation space required for the current running of the program, the heap allocation function is continuously called to continue to allocate memory segments for the new heap nodes in the new doubly linked list in the unused heap space state, until the memory space of the new heap nodes obtained by splitting does not meet the single allocation space size required for the current running of the program, then the heap space memory segmentation allocation is stopped.

[0017] In one embodiment, after the software program is completed, the heap space is merged and released by calling a heap recovery function, including:

[0018] After the software program is finished running, the heap recovery function is called to traverse all the nodes in the latest doubly linked list. The software program's current memory space addresses are found to exist in the latest nodes of the doubly linked list, and the fixed header information of the node is checked to see if it is correct. When a heap node with the software program's current memory space address and accurate fixed header information is found, the state of the heap node is restored to the unused heap space state, and the latest doubly linked list is checked to see if the adjacent upper and lower heap nodes are both in the unused heap space state. If so, the adjacent heap nodes that are both in the unused heap space state are merged and released.

[0019] In one of the embodiments, the memory structure includes fixed header information, usage status, and addresses of adjacent upper and lower segments.

[0020] In one embodiment, the heap space is much larger than the single allocation space required for the current execution of the program.

[0021] In one embodiment, building a search index of a global variable declared with a custom segment includes:

[0022] The base address, occupied space and CRC check value of each global variable declared with a custom segment are used as retrieval indexes.

[0023] In one embodiment, the global variables declared with the custom segment stored in the space of the custom segment are transferred to the ROM in the embedded system for storage based on the search index, and when the global variables declared with the custom segment need to be accessed during the running of the software program, the corresponding global variables are read from the ROM according to the search index for access, including:

[0024] Searching whether there is a global variable declared with a custom segment with the same search index in the ROM of the embedded system, if not, transferring the global variable declared with a custom segment stored in the space of the custom segment to the ROM of the embedded system and storing it according to a set format; if it exists, releasing the global variable declared with a custom segment in the space of the custom segment; by transferring and storing all the global variables declared with a custom segment stored in the space of the custom segment to the ROM, the space of the custom segment that is in a used heap space state after initialization processing is restored to an unused heap space state, thereby increasing the heap space in the unused heap space state that can be called when the software program is running in the embedded system;

[0025] When a global variable declared with a custom segment needs to be accessed during the running of a software program, the base address, occupied space, and CRC check value of the global variable are used as retrieval indexes, and the global variable with the same retrieval index is directly read from the ROM for access.

[0026] A RAM usage optimization device in an embedded system, the device comprising:

[0027] A custom segment declaration module is used to add a custom segment in an embedded system, and specify the declaration of the custom segment to be added to any one or more global variables in the RAM of the embedded system, and automatically put all global variables declared with custom segments into the space of the custom segment through the compiler; wherein the custom segment is adjacent to the freely allocated RAM heap in the embedded system, and the space of the custom segment and the adjacent heap space form a continuous memory segment;

[0028] The initialization module is used to define the heap allocation function and the heap recovery function, and initialize the memory segment;

[0029] The heap space allocation and release module is used to allocate memory segments of the heap space in the memory segment by continuously calling the heap allocation function according to the single allocation space required for the current operation of the program when the software program in the embedded system is running; after the software program is completed, the heap space is merged and released by calling the heap recovery function;

[0030] A custom segment transfer storage module is used to build a retrieval index for global variables declared with custom segments for the space of custom segments in the memory segment each time a software program in the embedded system is started, and based on the retrieval index, the global variables declared with custom segments stored in the space of the custom segment are transferred to the ROM in the embedded system for storage, and when the global variables declared with custom segments need to be accessed during the running of the software program, the corresponding global variables are read from the ROM according to the retrieval index for access.

[0031] A computer device comprises a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, the following steps are implemented:

[0032] Add a custom segment in the embedded system, and specify the declaration of the custom segment in any one or more global variables in the RAM of the embedded system, and automatically put all global variables declared with the custom segment into the space of the custom segment through the compiler; wherein the custom segment is adjacent to the freely allocated RAM heap in the embedded system, and the space of the custom segment and the adjacent heap space form a continuous memory segment;

[0033] Define heap allocation function and heap recovery function, and initialize the memory segment;

[0034] For the heap space in the memory segment, when the software program in the embedded system is running, the heap allocation function is continuously called according to the single allocation space required for the current program to allocate the memory segment of the heap space; after the software program is completed, the heap recovery function is called to merge and release the memory of the heap space;

[0035] For the space of the custom segment in the memory segment, each time the software program in the embedded system is started, a retrieval index of the global variables declared with the custom segment is constructed, and based on the retrieval index, the global variables declared with the custom segment stored in the space of the custom segment are transferred to the ROM in the embedded system for storage, and when the global variables declared with the custom segment need to be accessed during the running of the software program, the corresponding global variables are read from the ROM according to the retrieval index for access.

[0036] The above-mentioned RAM usage optimization method, device and equipment in an embedded system add a custom segment in the embedded system, the space of the custom segment is used to store global variables declared in the custom segment in RAM, and the custom segment is adjacent to the freely allocated RAM heap in the embedded system and forms a group of continuous memory segments. Furthermore, a heap allocation function and a heap recovery function are defined. For the heap space in the memory segment, the heap space memory segmentation allocation when the software program is running can be implemented based on the heap allocation function, and the heap space memory merging and release when the software program is running can be implemented based on the heap recovery function. In addition, for the space of the custom segment in the memory segment, by constructing a retrieval index of the global variables declared in the custom segment in the custom segment, the global variables in the custom segment can be written into ROM for storage, and when the software program needs to access the global variable, the global variable can be directly read based on the retrieval index for access.

[0037] Compared with the prior art, the present application can dynamically expand the global variables written in the ROM and increase the capacity of the allocatable heap space based on the custom segment space, and realize the segmented memory allocation and memory merging release of the heap space by defining the heap allocation function and the regression function, thereby realizing the maximum and effective utilization of the RAM heap space memory when the software program is running, without the need to estimate the data structure in the RAM, and realizing the efficient use of RAM memory in the embedded system. BRIEF DESCRIPTION OF THE DRAWINGS

[0038] Figure 1 A schematic diagram of a flow chart of a method for optimizing RAM usage in an embedded system in one embodiment;

[0039] Figure 2 A schematic diagram of a bidirectional link in one embodiment;

[0040] Figure 3 A schematic diagram of segmented allocation of heap space and memory merging and releasing in one embodiment;

[0041] Figure 4 FIG. 4 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION

[0042] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.

[0043] In one embodiment, Figure 1 As shown, a method for optimizing RAM usage in an embedded system is provided, comprising the following steps:

[0044] Step S1, adding a custom segment in the embedded system, and specifying to add a declaration of the custom segment to any one or more global variables in the RAM of the embedded system, and automatically placing all global variables declared with the custom segment into the space of the custom segment through the compiler; wherein the custom segment is adjacent to the freely allocated RAM heap in the embedded system, and the space of the custom segment and the adjacent heap space constitute a continuous memory segment.

[0045] Step S2, defining a heap allocation function and a heap recovery function, and initializing the memory segment.

[0046] The process of initializing the memory segment includes:

[0047] The space of the custom segment and the heap space in the memory segment are defined as memory structures respectively, and the space of the custom segment defined as the memory structure is set to a used heap space state, and the heap space defined as the memory structure is set to an unused heap space state;

[0048] Based on the space of the custom segment defined as a memory structure and the heap space defined as a memory structure, a bidirectional linked list of memory segments is constructed to complete the initialization processing of the memory segments; wherein, after the initialization processing of the memory segments is completed, the constructed bidirectional linked list contains two nodes, namely, a custom segment node in a used heap space state and a heap node in an unused heap space state, and both the custom segment node and the heap node are defined as memory structures. wherein the memory structure includes fixed header information, usage status, and addresses of adjacent upper and lower segments.

[0049] A doubly linked list such as Figure 2 As shown, when the content segment is initialized, the bidirectional linked list has only two nodes. After multiple heap node splits, the number of nodes increases dynamically to n and n>2. Each node in the bidirectional linked list is defined as a memory structure.

[0050] Step S3, for the heap space in the memory segment, when the software program in the embedded system is running, the heap allocation function is continuously called to allocate memory segments to the heap space according to the single allocation space required for the current running of the program; after the software program is completed, the heap space is merged and released by calling the heap recovery function.

[0051] like Figure 3 As shown in the figure, the process of allocating memory segments to the heap space includes:

[0052] When the software program in the embedded system is running, the heap allocation function is called to traverse the two nodes in the bidirectional linked list constructed after the memory segment is initialized, and a heap node that meets the single allocation space size required for the current running of the program and is in an unused heap space state is found;

[0053] Split the heap node into two segments according to the single allocation space size required for the current operation of the program and the remaining memory size of the heap node itself, add the two segments obtained by the split into a two-way linked list as new heap nodes to obtain a new two-way linked list, maintain the relationship between adjacent nodes in the new two-way linked list, set the new heap node in the new two-way linked list whose space is consistent with the single allocation space size required for the current operation of the program to the used heap space state, and return the memory space address corresponding to the new heap node in the used heap space state to the program for use, and the new heap node in the new two-way linked list whose space is consistent with the initial heap node's own remaining memory size is still in the unused heap space state; wherein, the new two-way linked list contains 3 nodes, namely, 1 custom segment node in the used heap space state, 1 new heap node in the used heap space state, and 1 new heap node in the unused heap space state, and each node is defined as a memory structure;

[0054] According to the single allocation space required for the current running of the program, the heap allocation function is continuously called to continue to allocate memory segments for the new heap nodes in the new doubly linked list in the unused heap space state, until the memory space of the new heap nodes obtained by splitting does not meet the single allocation space size required for the current running of the program, then the heap space memory segmentation allocation is stopped.

[0055] It should be noted that the heap space is much larger than the single allocation space required for the current program to run.

[0056] The process of merging and releasing memory in the heap space includes:

[0057] After the software program is finished running, the heap recovery function is called to traverse all the nodes in the latest doubly linked list. The software program's current memory space addresses are found to exist in the latest nodes of the doubly linked list, and the fixed header information of the node is checked to see if it is correct. When a heap node with the software program's current memory space address and accurate fixed header information is found, the state of the heap node is restored to the unused heap space state, and the latest doubly linked list is checked to see if the adjacent upper and lower heap nodes are both in the unused heap space state. If so, the adjacent heap nodes that are both in the unused heap space state are merged and released.

[0058] Step S4, for the space of the custom segment in the memory segment, each time the software program in the embedded system is started, a retrieval index of the global variables declared with the custom segment is constructed, and based on the retrieval index, the global variables declared with the custom segment stored in the space of the custom segment are transferred to the ROM in the embedded system for storage, and when the global variables declared with the custom segment need to be accessed during the running of the software program, the corresponding global variables are read from the ROM according to the retrieval index for access.

[0059] Specifically, the base address, occupied space and CRC check value of each global variable declared with a custom segment are used as retrieval indexes;

[0060] Searching whether there is a global variable declared with a custom segment with the same search index in the ROM of the embedded system, if not, transferring the global variable declared with a custom segment stored in the space of the custom segment to the ROM of the embedded system and storing it according to a set format; if it exists, releasing the global variable declared with a custom segment in the space of the custom segment; by transferring and storing all the global variables declared with a custom segment stored in the space of the custom segment to the ROM, the space of the custom segment that is in a used heap space state after initialization processing is restored to an unused heap space state, thereby increasing the heap space in the unused heap space state that can be called when the software program is running in the embedded system;

[0061] When a global variable declared with a custom segment needs to be accessed during the running of a software program, the base address, occupied space, and CRC check value of the global variable are used as retrieval indexes, and the global variable with the same retrieval index is directly read from the ROM for access.

[0062] It should be understood that although Figure 1 and Figure 3 The steps in the flowchart are shown in sequence as indicated by the arrows, but these steps are not necessarily executed in the order indicated by the arrows. Unless otherwise specified in this document, there is no strict order restriction for the execution of these steps, and these steps can be executed in other orders. Moreover, Figure 1 and Figure 3 At least part of the steps may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily executed 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 part of the sub-steps or stages of other steps.

[0063] In one embodiment, a RAM usage optimization device in an embedded system is provided, comprising:

[0064] A custom segment declaration module is used to add a custom segment in an embedded system, and specify the declaration of the custom segment to be added to any one or more global variables in the RAM of the embedded system, and automatically put all global variables declared with custom segments into the space of the custom segment through the compiler; wherein the custom segment is adjacent to the freely allocated RAM heap in the embedded system, and the space of the custom segment and the adjacent heap space form a continuous memory segment;

[0065] The initialization module is used to define the heap allocation function and the heap recovery function, and initialize the memory segment;

[0066] The heap space allocation and release module is used to allocate memory segments of the heap space in the memory segment by continuously calling the heap allocation function according to the single allocation space required for the current operation of the program when the software program in the embedded system is running; after the software program is completed, the heap space is merged and released by calling the heap recovery function;

[0067] A custom segment transfer storage module is used to build a retrieval index for global variables declared with custom segments for the space of custom segments in the memory segment each time a software program in the embedded system is started, and based on the retrieval index, the global variables declared with custom segments stored in the space of the custom segment are transferred to the ROM in the embedded system for storage, and when the global variables declared with custom segments need to be accessed during the running of the software program, the corresponding global variables are read from the ROM according to the retrieval index for access.

[0068] For the specific definition of the RAM usage optimization device under the embedded system, please refer to the definition of the RAM usage optimization method under the embedded system above, which will not be repeated here. Each module in the RAM usage optimization device under the above embedded system can be implemented in whole or in part by software, hardware and a combination thereof. The above modules can be embedded in or independent of the processor in the computer device in the form of hardware, or can be stored in the memory of the computer device in the form of software, so that the processor can call and execute the operations corresponding to the above modules.

[0069] In one embodiment, a computer device is provided. The computer device may be a terminal, and its internal structure diagram may be as follows: Figure 4 As shown. The computer device includes a processor, a memory, a network interface, a display screen and an input device connected through a system bus. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The network interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a RAM utilization optimization method under an embedded system is implemented. The display screen of the computer device can be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer device can be a touch layer covered on the display screen, or a key, trackball or touchpad set on the computer device housing, or an external keyboard, touchpad or mouse, etc.

[0070] Those skilled in the art will understand that Figure 4 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0071] In one embodiment, a computer device is provided, including a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, the following steps are implemented:

[0072] Add a custom segment in the embedded system, and specify the declaration of the custom segment in any one or more global variables in the RAM of the embedded system, and automatically put all global variables declared with the custom segment into the space of the custom segment through the compiler; wherein the custom segment is adjacent to the freely allocated RAM heap in the embedded system, and the space of the custom segment and the adjacent heap space form a continuous memory segment;

[0073] Define heap allocation function and heap recovery function, and initialize the memory segment;

[0074] For the heap space in the memory segment, when the software program in the embedded system is running, the heap allocation function is continuously called according to the single allocation space required for the current program to allocate the memory segment of the heap space; after the software program is completed, the heap recovery function is called to merge and release the memory of the heap space;

[0075] For the space of the custom segment in the memory segment, each time the software program in the embedded system is started, a retrieval index of the global variables declared with the custom segment is constructed, and based on the retrieval index, the global variables declared with the custom segment stored in the space of the custom segment are transferred to the ROM in the embedded system for storage, and when the global variables declared with the custom segment need to be accessed during the running of the software program, the corresponding global variables are read from the ROM according to the retrieval index for access.

[0076] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0077] The above-described embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.

Claims

1. A RAM usage optimization method in an embedded system, characterized in that: The method comprises: A custom segment is added to the embedded system, and a declaration of the custom segment is added to any one or more global variables in the RAM of the embedded system, and all global variables declared with the custom segment are automatically placed into the space of the custom segment through the compiler; wherein the custom segment is adjacent to the freely allocated RAM heap in the embedded system, and the space of the custom segment and the adjacent heap space form a continuous memory segment; Define a heap allocation function and a heap recovery function, and initialize the memory segment; For the heap space in the memory segment, when the software program in the embedded system is running, the heap allocation function is continuously called according to the single allocation space required for the current running of the program to allocate memory segments to the heap space; after the software program is finished running, the heap recovery function is called to merge and release the memory of the heap space; For the space of the custom segment in the memory segment, each time the software program in the embedded system is started, a search index of the global variable declared with the custom segment is constructed, and based on the search index, the global variable declared with the custom segment stored in the space of the custom segment is transferred to the ROM in the embedded system for storage, and when the global variable declared with the custom segment needs to be accessed during the running of the software program, the corresponding global variable is read from the ROM according to the search index for access; The method includes transferring the global variables declared with the custom segment stored in the space of the custom segment to the ROM in the embedded system for storage based on the search index, and reading the corresponding global variables from the ROM for access according to the search index when the global variables declared with the custom segment need to be accessed during the running of the software program, including: Searching in the ROM of the embedded system whether there is a global variable with the same search index and declared with a custom segment; if not, transferring the global variable with the custom segment stored in the space of the custom segment to the ROM of the embedded system and storing it in a set format; if it exists, releasing the global variable with the custom segment in the space of the custom segment; restoring the space of the custom segment that has been initialized to a used heap space state to an unused heap space state by transferring and storing all the global variables with the custom segment stored in the space of the custom segment to the ROM, thereby increasing the heap space in the unused heap space state that can be called when the software program in the embedded system is running; When the global variable declared with the custom segment needs to be accessed during the running of the software program, the base address, occupied space and CRC check value of the global variable are used as the retrieval index, and the global variable with the same retrieval index is directly read from the ROM for access.

2. The method according to claim 1, characterized in that Initializing the memory segment includes: The space of the custom segment and the heap space in the memory segment are respectively defined as memory structures, and the space of the custom segment defined as the memory structure is set to a used heap space state, and the heap space defined as the memory structure is set to an unused heap space state; A bidirectional linked list of memory segments is constructed based on the space of the custom segment defined as a memory structure and the heap space defined as a memory structure to complete the initialization processing of the memory segment; wherein, after the initialization processing of the memory segment is completed, the constructed bidirectional linked list contains two nodes, namely, a custom segment node in a used heap space state and a heap node in an unused heap space state, and both the custom segment node and the heap node are defined as memory structures.

3. The method according to claim 1, characterized in that When a software program in an embedded system is running, the heap allocation function is continuously called according to the single allocation space required for the current running of the program to allocate memory segments to the heap space, including: When the software program in the embedded system is running, the heap allocation function is called to traverse the two nodes in the bidirectional linked list constructed after the memory segment is initialized, and a heap node that meets the single allocation space size required for the current running of the program and is in an unused heap space state is found; The heap node is split into two segments according to the single allocation space size required for the current operation of the program and the remaining memory size of the heap node itself, and the two segments obtained by the split are added into a bidirectional linked list as new heap nodes to obtain a new bidirectional linked list, and the relationship between adjacent nodes in the new bidirectional linked list is maintained, and the new heap node in the new bidirectional linked list whose space is consistent with the single allocation space size required for the current operation of the program is set to a used heap space state, and the memory space address corresponding to the new heap node in the used heap space state is returned to the program for use, and the new heap node in the new bidirectional linked list whose space is consistent with the initial heap node's own remaining memory size is still in an unused heap space state; wherein, the new bidirectional linked list contains 3 nodes, namely, 1 custom segment node in the used heap space state, 1 new heap node in the used heap space state, and 1 new heap node in the unused heap space state, and each node is defined as a memory structure; According to the single allocation space required for the current running of the program, the heap allocation function is continuously called to continue to allocate memory segments for the new heap nodes in the new doubly linked list in the unused heap space state, until the memory space of the new heap nodes obtained by splitting does not meet the single allocation space size required for the current running of the program, then the heap space memory segmentation allocation is stopped.

4. The method according to claim 3, characterized in that After the software program is finished running, the heap space is released by calling the heap recovery function, including: After the software program is finished running, the heap recovery function is called to traverse all the nodes in the latest doubly linked list. The search is performed to see whether all the current memory space addresses of the software program exist in the nodes of the latest doubly linked list, and to check whether the fixed header information of the node is correct. When a heap node with the current memory space address of the software program and accurate fixed header information is found, the state of the heap node is restored to the unused heap space state, and the latest doubly linked list is checked to see whether the adjacent upper and lower heap nodes are both in the unused heap space state. If so, the adjacent heap nodes that are both in the unused heap space state are merged and released.

5. The method according to claim 2 or 4, characterized in that: The memory structure includes fixed header information, usage status, and addresses of adjacent upper and lower segments.

6. The method according to claim 1, characterized in that The heap space is much larger than the single allocation space required for the current program to run.

7. The method according to claim 1, characterized in that Construct a search index for the global variable declared with the custom segment, including: The base address, occupied space and CRC check value of each global variable declared with the custom segment are used as retrieval indexes.

8. A RAM usage optimization device in an embedded system, characterized in that: The device comprises: A custom segment declaration module, used for adding a custom segment in an embedded system, and specifying the declaration of the custom segment to be added to any one or more global variables in the RAM of the embedded system, and automatically placing all global variables declared with the custom segment into the space of the custom segment through a compiler; wherein the custom segment is adjacent to a freely allocated RAM heap in the embedded system, and the space of the custom segment and the adjacent heap space form a continuous memory segment; An initialization module, used to define a heap allocation function and a heap recovery function, and to initialize the memory segment; A heap space allocation and release module is used for, for the heap space in the memory segment, when the software program in the embedded system is running, continuously calling the heap allocation function to allocate memory segments to the heap space according to the single allocation space required for the current running of the program; after the software program is finished running, the heap space is merged and released by calling the heap recovery function; A custom segment transfer storage module is used to construct a retrieval index of the global variable declared with the custom segment for the space of the custom segment in the memory segment each time the software program in the embedded system is started, transfer the global variable declared with the custom segment stored in the space of the custom segment to the ROM in the embedded system for storage based on the retrieval index, and read the corresponding global variable from the ROM according to the retrieval index for access when the global variable declared with the custom segment needs to be accessed during the running of the software program; The method includes transferring the global variables declared with the custom segment stored in the space of the custom segment to the ROM in the embedded system for storage based on the search index, and reading the corresponding global variables from the ROM for access according to the search index when the global variables declared with the custom segment need to be accessed during the running of the software program, including: Searching in the ROM of the embedded system whether there is a global variable with the same search index and declared with a custom segment; if not, transferring the global variable with the custom segment stored in the space of the custom segment to the ROM of the embedded system and storing it in a set format; if it exists, releasing the global variable with the custom segment in the space of the custom segment; restoring the space of the custom segment that has been initialized to a used heap space state to an unused heap space state by transferring and storing all the global variables with the custom segment stored in the space of the custom segment to the ROM, thereby increasing the heap space in the unused heap space state that can be called when the software program in the embedded system is running; When the global variable declared with the custom segment needs to be accessed during the running of the software program, the base address, occupied space and CRC check value of the global variable are used as the retrieval index, and the global variable with the same retrieval index is directly read from the ROM for access.

9. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 7 are implemented.