Memory management method and memory management device of embedded system
By specifying the continuous address space of RAM as dynamic memory space in an embedded system and using resource files to manage memory allocation, the problems of inefficient memory management and fragmentation in the existing technology are solved, and more efficient memory utilization and system stability are achieved.
Patent Information
- Application Number
- CN202411728340.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-28
- Publication Date
- 2025-03-18
AI Technical Summary
Existing embedded systems are less efficient in memory management, which can easily lead to memory fragmentation and affect system performance and stability.
By specifying a continuous address space in RAM as a dynamic memory space and using resource files to record and manage memory allocation data, flexible allocation and real-time tracking of memory resources can be achieved.
It effectively improves the memory utilization rate of embedded systems, ensures the efficient operation and stability of the system, and reduces memory fragmentation.
Smart Images

Figure CN119645641A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of smart cards, and in particular to a memory management method and a memory management device for an embedded system. Background Art
[0002] With the widespread application of embedded systems in smart devices and the Internet of Things, the demand for efficient management of memory resources has become increasingly important. In the prior art, embedded systems usually use static memory allocation or simple dynamic allocation methods to pre-allocate fixed-size memory space or allocate memory on demand to meet application requirements. These methods are inefficient in memory usage and easily lead to memory fragmentation, which affects system performance and stability. Summary of the invention
[0003] In view of this, an object of the present invention is to provide a memory management method and a memory management device for an embedded system, which can make memory space management more flexible, thereby reducing memory fragmentation, and further improving memory utilization and operation stability.
[0004] In a first aspect, an embodiment of the present invention provides a memory management method for an embedded system, wherein the embedded system includes at least one functional module; the method includes: designating a continuous address space in a RAM as a dynamic memory space; when a memory space application request from a functional module is received, identifying whether an application corresponding to the memory space application request includes a resource file; wherein the resource file is a variable-length record file, and includes resource management data for describing the memory space allocation situation; the memory space application request includes a first memory space capacity required for an object data set to store an object data set; the object data set includes object data and object information; if it is identified that the application includes a resource file, obtaining a first remaining memory space capacity of the dynamic memory space, and determining whether the first memory space capacity is less than or equal to the first remaining memory space capacity; wherein the first remaining memory space capacity is an unused portion of the dynamic memory space; if the first memory space capacity is less than or equal to the first remaining memory space capacity, storing the object data set in the dynamic memory space; generating first space allocation data based on a memory address of the object data set in the dynamic memory space; recording the first space allocation data in the resource file, and updating the resource management data based on the first memory space capacity and the first space allocation data; and returning the memory address of the object data set in the dynamic memory space to the functional module.
[0005] Furthermore, each functional module is correspondingly provided with a resource file, and the method also includes: when receiving a request for releasing memory space from the functional module, based on the deletion start address in the request for releasing memory space, traversing each resource file, and determining the second space allocation data corresponding to the deletion start address; deleting the second space allocation data, and based on the memory address in the second space allocation data, deleting the object data set corresponding to the second space allocation data in the dynamic memory space; and updating the resource management data corresponding to the functional module based on the second space allocation data and the second memory space capacity corresponding to the second space allocation data.
[0006] Furthermore, after updating the resource management data corresponding to the functional module, the method also includes: traversing each resource file, and rewriting the remaining object data sets after deleting the starting address into the dynamic memory space in turn to sort out the memory fragments generated by the request to release the memory space; based on the updated memory address of the remaining object data set in the dynamic memory space, updating the remaining second space allocation data in each resource file; and returning the updated memory address of the remaining object data set in the dynamic memory space to the corresponding functional module.
[0007] Furthermore, after the step of identifying whether the application corresponding to the memory space application request includes resource files, the method further includes: if it is identified that the application does not include resource files, creating an application directory for the application and generating resource files in the application directory.
[0008] Furthermore, each first space allocation data corresponds to a record number; if after identifying that the function module includes a step of including a resource file, the method also includes: determining whether any record number in the resource file is equal to a preset maximum record number, and whether each record number in the resource file is a continuous record number; if a record number in the resource file is equal to a preset maximum record number, and each record number is a connected record number, generating the next resource file in the application directory corresponding to the application.
[0009] Further, the type of the first memory space capacity is DTR space capacity; the resource management data is the associated and saved allocated DTR space capacity, allocated RTR space capacity, the number of DTR occupied records and the number of RTR occupied records; the initial value of the allocated DTR space capacity, the allocated RTR space capacity, the initial value of the number of DTR occupied records and the initial value of the number of RTR occupied records are all 0; the memory address includes the DTR starting address and the DTR ending address; the first space allocation data is recorded to the resource file, and the steps of updating the resource management data based on the first memory space capacity and the first space allocation data include: obtaining the DTR starting address and the DTR space capacity corresponding to the object data set; determining the DTR ending address based on the DTR starting address and the DTR space capacity; generating the first space allocation data based on the DTR starting address, the DTR space capacity and the DTR ending address; obtaining the file ID corresponding to the resource file, determining the first record number corresponding to the first space allocation data based on the preset record order, and recording the first record number and the first space allocation data in association with the resource file corresponding to the file ID; updating the allocated DTR space capacity based on the DTR space capacity, and adding 1 to the number of DTR occupied records.
[0010] Furthermore, the type of the second memory space capacity is DTR space capacity; based on the second space allocation data and the second memory space capacity corresponding to the second space allocation data, the step of updating the resource management data corresponding to the functional module includes: reducing the DTR space capacity in the allocated DTR space capacity, and reducing the number of DTR occupied records by 1.
[0011] Further, the type of the first memory space capacity is RTR space capacity; the resource management data is the associated and saved allocated DTR space capacity, allocated RTR space capacity, the number of DTR occupied records and the number of RTR occupied records; the initial value of the allocated DTR space capacity, the allocated RTR space capacity, the initial value of the number of DTR occupied records and the initial value of the number of RTR occupied records are all 0; the memory address includes the RTR starting address and the RTR ending address; the first space allocation data is recorded to the resource file, and the steps of updating the resource management data based on the first memory space capacity and the first space allocation data include: obtaining the RTR starting address and RTR space capacity corresponding to the object data set; determining the RTR ending address based on the RTR starting address and the RTR space capacity; generating the first space allocation data based on the RTR starting address, the RTR space capacity and the RTR ending address; obtaining the file ID corresponding to the resource file, determining the first record number corresponding to the first space allocation data based on the preset record order, and associating the first record number and the first space allocation data to the resource file corresponding to the file ID; updating the allocated RTR space capacity based on the RTR space capacity, and adding 1 to the number of RTR occupied records.
[0012] Furthermore, the type of the second memory space capacity is RTR space capacity; based on the second space allocation data and the second memory space capacity corresponding to the second space allocation data, the step of updating the resource management data corresponding to the functional module includes: reducing the RTR space capacity in the allocated RTR space capacity, and reducing the number of RTR occupancy records by 1.
[0013] Furthermore, the type of the first memory space capacity is DTR space capacity or RTR space capacity; the step of storing the object data set in the dynamic memory space includes: when the type of the first memory space capacity is DTR space capacity, storing the object data set in the order from high address to low address of the dynamic memory space; when the type of the first memory space capacity is RTR space capacity, storing the object data set in the order from low address to high address of the dynamic memory space.
[0014] In a second aspect, an embodiment of the present invention provides a memory management device for an embedded system, the memory management device comprising: a dynamic memory space module, used to designate a continuous address space in a RAM as a dynamic memory space; a storage module, used to identify whether a function module includes a resource file when receiving a memory space application request from a function module; wherein the resource file is a variable-length record file, containing resource management data for describing the memory space allocation; the memory space application request includes a first memory space capacity required for an object data set to store an object data set; the object data set includes object data and object information; the storage module is also used to obtain a first remaining memory of the dynamic memory space if it is identified that the function module includes a resource file space capacity, and determines whether the first memory space capacity is less than or equal to the first remaining memory space capacity; wherein the first remaining memory space capacity is the unused portion of the dynamic memory space; the storage module is also used to store the object data set in the dynamic memory space if the first memory space capacity is less than or equal to the first remaining memory space capacity; the data management module is used to generate the first space allocation data based on the memory address of the object data set in the dynamic memory space; the data management module is also used to record the first space allocation data to the resource file, and update the resource management data based on the first memory space capacity and the first space allocation data; the result return module is used to return the memory address of the object data set in the dynamic memory space to the functional module.
[0015] The embodiment of the present invention provides a memory management method and a memory management device for an embedded system, wherein the embedded system includes at least one functional module; the method includes: designating a continuous address space in a RAM as a dynamic memory space; when receiving a memory space application request from a functional module, identifying whether an application corresponding to the memory space application request includes a resource file; wherein the resource file is a variable-length record file, and includes resource management data for describing the memory space allocation situation; the memory space application request includes a first memory space capacity required for an object data set to store the object data set; the object data set includes object data and object information; if it is identified that the application includes a resource file, obtaining a first remaining memory space capacity of the dynamic memory space, and determining whether the first memory space capacity is less than or equal to the first remaining memory space capacity; wherein the first remaining memory space capacity is an unused portion of the dynamic memory space; if the first memory space capacity is less than or equal to the first remaining memory space capacity, storing the object data set in the dynamic memory space; generating first space allocation data based on a memory address of the object data set in the dynamic memory space; recording the first space allocation data in the resource file, and updating the resource management data based on the first memory space capacity and the first space allocation data; and returning the memory address of the object data set in the dynamic memory space to the functional module. In this method, by designating a continuous address space of RAM as dynamic memory space and combining it with resource files to record and manage memory allocation data, flexible allocation and real-time tracking of memory resources can be achieved, thereby effectively improving the memory utilization of the embedded system and ensuring the efficient operation and stability of the embedded system.
[0016] Other features and advantages of the present invention will be described in the following description, and partly become apparent from the description, or understood by practicing the present invention. The purpose and other advantages of the present invention are realized and obtained by the structures particularly pointed out in the description, claims and drawings.
[0017] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, preferred embodiments are given below and described in detail with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] In order to more clearly illustrate the specific implementation methods of the present invention or the technical solutions in the prior art, the drawings required for use in the specific implementation methods or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some implementation methods of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0019] Figure 1 A flow chart of a memory management method for an embedded system provided by an embodiment of the present invention;
[0020] Figure 2 A flow chart of a method for managing memory deletion in an embedded system provided in Embodiment 1 of the present invention;
[0021] Figure 3 A flow chart of a method for managing memory after data deletion provided in Embodiment 1 of the present invention;
[0022] Figure 4 A flow chart of a method for updating resource management data when the type of the first memory space capacity provided in the first embodiment of the present invention is the DTR space capacity;
[0023] Figure 5 A flow chart of a method for updating resource management data when the type of the first memory space capacity provided in the first embodiment of the present invention is the RTR space capacity;
[0024] Figure 6 A schematic diagram of a memory management device for an embedded system provided by an embodiment of the present invention;
[0025] Figure 7 A schematic diagram of a memory management device for another embedded system provided by an embodiment of the present invention.
[0026] Icons: 1-dynamic memory space module; 2-storage module; 3-data management module; 4-result return module; 5-deletion module. DETAILED DESCRIPTION
[0027] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the accompanying drawings. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0028] To facilitate understanding of this embodiment, the embodiment of the present invention is described in detail below.
[0029] Embodiment 1:
[0030] Figure 1 A flow chart of a memory management method for an embedded system provided by an embodiment of the present invention.
[0031] The memory management method of the embedded system is applied to the embedded system, and the embedded system includes at least one functional module.
[0032] Here, in embedded systems, especially in the environment of running native COS (Chip Operating System), functional modules refer to software components in embedded systems that perform specific tasks or functions. These modules can be independently running applications or services, responsible for handling specific functional requirements, and require memory allocation to store the data or temporary calculation results required for their operation.
[0033] Reference Figure 1 , a memory management method for an embedded system, including:
[0034] Step S101, designate a continuous address space in the RAM as a dynamic memory space.
[0035] Here, in embedded systems, especially smart cards, the types of applications are becoming increasingly diverse. The RAM (random access memory) space in the smart card that can be occupied by the application can be dynamically loaded according to the application, and the relevant space can be dynamically applied for and restored.
[0036] Dynamic memory space includes RTR space (CLEAR_ON_RESET RAM) and DTR space (CLEAR_ON_DESELECT RAM). RTR space will be erased when the smart card is reset. Multiple applications can apply for this space. Once applied, each application has exclusive use of the applied space, so it can also be referred to as exclusive memory. DTR space will be erased when the application is deselected. In addition, this space will also be erased when the smart card is reset. Multiple applications can apply for this space. Once applied, multiple applications share this space, so it can also be referred to as shared memory.
[0037] Dynamic memory space is a continuous address space divided from RAM and used to store dynamically allocated data.
[0038] Among them, the DTR space starts from the starting address of the dynamic memory space and grows toward the higher address, and the RTR space starts from the ending address of the dynamic memory space and decreases toward the lower address.
[0039] During initialization, the embedded system allocates a section of RAM address space as dynamic memory space to ensure that the DTR and RTR areas are clearly divided.
[0040] Step S102, when receiving a memory space application request from a functional module, identify whether the application corresponding to the memory space application request includes a resource file; wherein the resource file is a variable-length record file, containing resource management data for describing the memory space allocation situation; the memory space application request includes a first memory space capacity required for an object data set to store the object data set; the object data set includes object data and object information.
[0041] Here, the function module includes multiple applications, and each application is set with a resource file (variable-length record file). The resource file is used to record the memory allocation information of the current function module, such as the size of the allocated space, the number of occupied records, etc. Among them, the variable-length record file does not need to specify the file size in advance, and supports the addition and deletion of a single record.
[0042] Each application may be configured with one or more resource files. Different resource files have different file IDs. Each resource file may record up to 255 data items.
[0043] In one embodiment, after the step of identifying whether the application corresponding to the memory space application request includes a resource file in step S102, the method further includes:
[0044] If it is identified that the application does not include resource files, an application directory is created for the application and resource files are generated in the application directory.
[0045] Here, when receiving a memory space application request, first check whether the requesting application already has the corresponding resource file. If the resource file does not exist in the application, a new resource file is created for the application and the resource management data is initialized, thereby ensuring that the memory allocation record of each application is managed independently to avoid conflicts.
[0046] Step S103, if it is recognized that the application includes resource files, obtain the first remaining memory space capacity of the dynamic memory space, and determine whether the first memory space capacity is less than or equal to the first remaining memory space capacity; wherein the first remaining memory space capacity is the unused portion of the dynamic memory space.
[0047] Here, the remaining capacity of the current dynamic memory space is calculated through the record data in the resource file. If the requested memory space is larger than the remaining capacity, the allocation fails.
[0048] The calculation of the remaining capacity is based on the allocation of DTR and RTR. The remaining capacity is the portion from the end of the allocated space of the DTR space to the beginning of the allocated space of the RTR space.
[0049] The remaining capacity may be determined by traversing the allocated DTR space capacity and the allocated RTR space capacity in each resource file.
[0050] If the requested space exceeds the remaining capacity, an allocation failure error message will be returned.
[0051] In one embodiment, each first space allocation data corresponds to a record number.
[0052] Here, in the resource file, each memory allocation operation generates a first space allocation data, recording the allocated memory address, allocation size and associated functional modules, and each allocation data is provided with a unique record number to facilitate the tracking and organization of the memory status.
[0053] The memory allocation status is recorded by the resource file, and the binding of each first space allocation data with the record number helps to accurately locate the memory allocation information and avoid conflicts and errors.
[0054] In step S103, after identifying that the application includes a resource file, the method further includes:
[0055] Determine whether any record number in the resource file is equal to the preset maximum record number, and whether each record number in the resource file is a consecutive record number.
[0056] Here, the preset maximum record number is used to indicate the capacity limit of the resource file, and is preset according to actual conditions, and can be set to 255. The resource file may also not be set with a capacity limit.
[0057] When the resource file has a capacity limit and the record number reaches the maximum value, a new resource file is generated.
[0058] If data is deleted, the record number is discontinuous, and a record number whose storage content is empty is determined, and the space allocation data to be saved is associated and saved to this record number.
[0059] If a record number in the resource file is equal to the preset maximum record number, and each record number is a connection record number, the next resource file is generated in the application directory corresponding to the application.
[0060] Here, if a record number in the resource file is equal to the preset maximum record number, and each record number is a connection record number, a new resource file is generated for the application, and subsequent memory allocation operations are recorded in the new resource file.
[0061] New resource files are associated with existing files in the application directory by serial number, forming a logical continuous management structure.
[0062] Step S104: if the capacity of the first memory space is less than or equal to the capacity of the first remaining memory space, the object data set is stored in the dynamic memory space.
[0063] Here, the object data set (including object data and object information) is stored in the dynamic memory space, the DTR area is stored from low address to high address, and the RTR area is stored from high address to low address.
[0064] The object information includes the environment, size and type of the object.
[0065] The object dataset is stored in an appropriate area according to the applied space type (DTR or RTR).
[0066] Step S105: Generate first space allocation data based on the memory address of the object data set in the dynamic memory space.
[0067] Here, the first space allocation data includes a start address, an end address and related object information of the object data set.
[0068] The first space allocation data may only record the starting address and size of the object.
[0069] Step S106: Record the first space allocation data into the resource file, and update the resource management data based on the first memory space capacity and the first space allocation data.
[0070] Here, a new record is added to the resource file, including the allocated memory address, object information, etc. At the same time, the first record of the resource file is updated, such as the total size of the allocated DTR / RTR space and the number of occupied records.
[0071] The update of resource files ensures the real-time management of memory allocation by the system. The record numbers of newly added records are automatically assigned to ensure continuity and uniqueness.
[0072] The record number may not be allocated.
[0073] Step S107: Return the memory address of the object data set in the dynamic memory space to the functional module.
[0074] Here, the allocated memory address is returned to the functional module, and the functional module uses the address to access or operate the allocated memory. The return of the address indicates that the memory allocation is complete, and the functional module can directly use the allocated memory block.
[0075] In one embodiment, referring to Figure 2 , the method further comprises:
[0076] Step S201, when receiving a request for releasing memory space from a functional module, traverse each resource file based on a deletion start address in the request for releasing memory space, and determine the second space allocation data corresponding to the deletion start address.
[0077] Here, each functional module needs to dynamically allocate memory space during operation. When a functional module completes the current task or needs to release part of the memory, it will initiate a request to release the memory space.
[0078] The request contains the deletion start address, which is used to mark the starting position of the memory area that needs to be released.
[0079] By traversing the resource files of each functional module, the second space allocation data that matches the deletion start address is found.
[0080] The second space allocation data stores allocation records of dynamic memory, including memory addresses and corresponding allocation information.
[0081] Step S202: delete the second space allocation data, and based on the memory address in the second space allocation data, delete the object data set corresponding to the second space allocation data in the dynamic memory space.
[0082] Here, once the second space allocation data to be deleted is determined, the relevant record is deleted from the resource file. At the same time, the memory address in the second space allocation data is used to locate and clear the corresponding object data set in the dynamic memory space to release the physical memory.
[0083] The memory allocation is recorded through the resource file. After deleting the relevant data, the memory usage can be updated synchronously to ensure the integrity of memory management.
[0084] Step S203: updating resource management data corresponding to the functional module based on the second space allocation data and the second memory space capacity corresponding to the second space allocation data.
[0085] Here, according to the deleted second space allocation data and the corresponding memory space capacity, the resource management data of the functional module is adjusted to reflect the current memory usage status, for example, reducing the amount of allocated memory recorded and increasing the amount of available memory.
[0086] Resource management data is used as a real-time monitoring tool for memory allocation and deallocation. After each memory release, resource management data is updated to ensure global consistency of memory management.
[0087] In one embodiment, referring to Figure 3 In step S203, after the step of updating the resource management data corresponding to the functional module, the method further includes:
[0088] Step S301, traverse each resource file, and rewrite the remaining object data sets after the deletion start address into the dynamic memory space in turn to sort out the memory fragments generated by the request to release the memory space.
[0089] Here, in dynamic memory management, since the deletion operation will leave discontinuous fragments in the memory space, it may cause subsequent memory allocation failure or reduced efficiency. To this end, each resource file is traversed and the object data sets that remain after the deletion operation are rewritten into the dynamic memory space in sequence, so that these data are stored continuously in the memory space, thereby defragmenting.
[0090] The memory allocation and data storage of each application are recorded through the resource file. The second space allocation data in the resource file stores the specific address information of dynamic memory allocation.
[0091] By traversing resource files, memory fragments can be effectively identified and sorted to achieve optimal utilization of memory space.
[0092] Step S302: updating the remaining second space allocation data in each resource file based on the updated memory address of the remaining object data set in the dynamic memory space.
[0093] Here, for the object data set that is rewritten into the dynamic memory space, its memory address has changed, and the records in each resource file need to be updated synchronously.
[0094] The updated content includes the new memory address of each object data set and the corresponding allocation size information.
[0095] The resource file acts as a mapping table for dynamic memory, recording the memory usage status of each application. When the address of a data set changes, the update of the resource file ensures data consistency and the accuracy of subsequent operations.
[0096] Step S303: returning the updated memory addresses of the remaining object data sets in the dynamic memory space to the corresponding functional modules.
[0097] Here, the updated object dataset memory address is returned to the corresponding application through a specific interface, notifying it that the data has been migrated to the new memory location, ensuring that the application can continue to access its data normally without having to worry about the internal implementation of memory management.
[0098] In one embodiment, the type of the first memory space capacity is DTR space capacity; the resource management data is the associated saved allocated DTR space capacity, allocated RTR space capacity, the number of DTR occupied records and the number of RTR occupied records; the initial value of the allocated DTR space capacity, the allocated RTR space capacity, the initial value of the number of DTR occupied records and the initial value of the number of RTR occupied records are all 0; the memory address includes the DTR start address and the DTR end address.
[0099] The resource management data may also include the allocated DTR space capacity and the allocated RTR space capacity that are stored in association.
[0100] Reference Figure 4 , step S106, specifically includes the following steps S401 to S406.
[0101] Step S401, obtaining the DTR start address and DTR space capacity corresponding to the object data set.
[0102] Here, the object data set is a logical unit allocated in the memory by the functional module. According to the requirements of the object data set, the starting address and DTR space capacity of the corresponding DTR are obtained.
[0103] The DTR space capacity indicates the size of the memory space allocated to the object data set, which is usually determined dynamically by the application's requirements and system resources.
[0104] Step S402, determining the DTR end address based on the DTR start address and the DTR space capacity.
[0105] Here, the DTR end address is calculated based on the DTR start address and the allocated space capacity: DTR end address = DTR start address + DTR space capacity.
[0106] The end address marks the memory range of the data set for subsequent memory access and management.
[0107] Through precise recording of resource files, the memory range of each object data set is strictly managed to avoid memory allocation overlap or conflict.
[0108] Step S403, generating first space allocation data based on the DTR start address, the DTR space capacity and the DTR end address.
[0109] Here, the first space allocation data includes the memory allocation information of the object data set, such as the start address, end address, allocation capacity, etc. The specific format is as follows:
[0110] Field 1: DTR starting address.
[0111] Field 2: DTR end address.
[0112] Field 3: DTR space capacity.
[0113] Field 4: Identifier of the associated application or dataset.
[0114] Step S404, obtaining a file ID corresponding to the resource file, determining a first record number corresponding to the first space allocation data based on a preset record order, and associating the first record number and the first space allocation data to record in the resource file corresponding to the file ID.
[0115] Here, each application corresponds to at least one resource file, and the corresponding file ID is obtained through the module identifier.
[0116] According to the record sequence in the resource file, a unique record number is assigned to the first space allocation data to ensure that the record numbers are continuous and unique.
[0117] The first space allocation data is bound to its record number and written into the resource file.
[0118] Step S405, updating the allocated DTR space capacity based on the DTR space capacity, and increasing the number of DTR occupied records by 1.
[0119] Here, the allocated DTR space capacity is accumulated, and the allocated DTR space capacity = the allocated DTR space capacity + the current DTR space capacity. The number of DTR occupied records is increased by 1 to reflect the number of DTR records in the current resource file.
[0120] Monitor memory usage in real time by dynamically updating resource management data to ensure that allocation records are consistent with actual memory status.
[0121] When the resource management data only includes the associated and saved allocated DTR space capacity and allocated RTR space capacity, the DTR space capacity does not need to be updated.
[0122] In one embodiment, the type of the second memory space capacity is DTR space capacity.
[0123] Here, when the functional module releases memory, the update of the DTR space capacity needs to reflect the released memory area and adjust the overall memory usage.
[0124] Step S302 specifically includes:
[0125] Reduce the DTR space capacity in the allocated DTR space capacity and reduce the number of DTR occupied records by 1.
[0126] Here, based on the second space allocation data of the released memory, the size that needs to be reduced from the allocated DTR space capacity is calculated: the updated allocated DTR space capacity = the current allocated DTR space capacity - the released DTR space capacity.
[0127] When the function module releases the memory, the corresponding second space allocation data is deleted, and the number of DTR occupied records is reduced by 1: the updated number of DTR occupied records = the current number of DTR occupied records - 1.
[0128] In one embodiment, the type of the first memory space capacity is RTR space capacity; the resource management data is the associated and saved allocated DTR space capacity, allocated RTR space capacity, number of DTR occupied records and number of RTR occupied records; the initial value of the allocated DTR space capacity, the allocated RTR space capacity, the initial value of the number of DTR occupied records and the initial value of the number of RTR occupied records are all 0; the memory address includes the RTR start address and the RTR end address. Figure 5 , step S106, specifically includes the following steps S501 to S505.
[0129] Step S501, obtaining the RTR start address and RTR space capacity corresponding to the object data set.
[0130] Here, according to the application's request, a section of RTR space is allocated, and the allocated RTR start address and the required RTR space capacity are determined.
[0131] The RTR start address is the starting point of memory allocation and is used to mark the storage location of the object data set; the RTR space capacity indicates the size of the allocated memory.
[0132] Step S502, determining the RTR end address based on the RTR start address and the RTR space capacity.
[0133] Here, the RTR end address is calculated based on the RTR start address and the allocated space capacity: RTR end address = RTR start address + RTR space capacity.
[0134] The RTR end address marks the range of allocated memory, which facilitates subsequent management and recovery.
[0135] Use resource files to record the RTR start and end addresses to ensure that the memory range of each allocation operation is clear.
[0136] Step S503: Generate first space allocation data based on the RTR start address, the RTR space capacity and the RTR end address.
[0137] Here, the first space allocation data will be used as a record in the resource file for memory allocation state management.
[0138] The first space allocation data contains the memory allocation information of the object data set, such as the start address, end address, allocation capacity, etc. The specific format is as follows:
[0139] Field 1: RTR starting address.
[0140] Field 2: RTR end address.
[0141] Field 3: RTR space capacity.
[0142] Field 4: Identifier of the associated application or dataset.
[0143] Step S504, obtaining a file ID corresponding to the resource file, determining a first record number corresponding to the first space allocation data based on a preset record order, and associating the first record number and the first space allocation data to record in the resource file corresponding to the file ID.
[0144] Here, the unique file ID of the corresponding resource file is obtained according to the identifier of the application.
[0145] Assign a unique first record number in the preset record sequence of the resource file to ensure that each record can be indexed and tracked by the record number.
[0146] The first record number is bound to the first space allocation data and written into a corresponding resource file.
[0147] Step S505: Update the allocated RTR space capacity based on the RTR space capacity, and increase the number of RTR occupancy records by 1.
[0148] Here, the allocated RTR space capacity is updated: the allocated RTR space capacity is added to the allocated capacity: the allocated RTR space capacity = the allocated RTR space capacity + the currently allocated RTR space capacity.
[0149] Record the amount of memory allocated each time: RTR occupancy record number + 1.
[0150] In one embodiment, the type of the second memory space capacity is RTR space capacity.
[0151] Step S302 specifically includes:
[0152] Update the allocated RTR space capacity based on the RTR space capacity, and increase the number of RTR occupancy records by 1.
[0153] Here, when the functional module releases memory, the system determines the value to be reduced from the allocated RTR space capacity according to the second space allocation data of the release request. The calculation formula is: updated allocated RTR space capacity = current allocated RTR space capacity - released RTR space capacity.
[0154] The RTR start address and capacity included in the second space allocation data are used to identify and calculate the space capacity that needs to be reduced.
[0155] After reducing the allocated capacity, the resource management data is updated in real time to reflect the current memory status.
[0156] The RTR capacity of each allocation is recorded in the resource file. When a release operation occurs, the corresponding RTR space capacity is synchronously reduced from the resource management data.
[0157] Check whether the allocated RTR capacity after reduction is a non-negative value to ensure that no data exception or logic error is caused.
[0158] If the released capacity exceeds the allocated capacity, the system needs to trigger an exception handling mechanism to prevent erroneous release operations.
[0159] When the release operation is completed, the corresponding allocation record is deleted and the number of RTR occupied records is reduced: the updated number of RTR occupied records = the current number of RTR occupied records - 1.
[0160] The number of RTR occupied records corresponds one-to-one to the allocation records in the resource file. During the release operation, the allocation data is deleted and the number of records is updated at the same time.
[0161] In one embodiment, the type of the first memory space capacity is DTR space capacity or RTR space capacity.
[0162] In step S104, the step of storing the object data set into the dynamic memory space includes:
[0163] When the type of the first memory space capacity is DTR space capacity, the object data set is stored in the order from high address to low address of the dynamic memory space; when the type of the first memory space capacity is RTR space capacity, the object data set is stored in the order from low address to high address of the dynamic memory space.
[0164] Here, the formula distinguishes the allocation and access order of DTR and RTR spaces, and manages these two different types of memory spaces through resource files. Different storage orders are adopted according to the different types of memory spaces.
[0165] When the type of the first memory space capacity is DTR space capacity, the object data set is stored in the order of high address to low address of the dynamic memory space. DTR space is usually used to store temporary data. The storage order of high address to low address can effectively avoid the fragmentation problem in the dynamic memory and ensure that the free space in the high address area will not be occupied too early during storage. High address to low address storage can ensure that the newly stored object data set will not interfere with the already stored data, and it is also helpful for cleaning and sorting when memory is recycled.
[0166] When the type of the first memory space capacity is RTR space capacity, the object data set is stored in the order from low address to high address of the dynamic memory space. The RTR space is used for real-time data storage, and the storage order from low address to high address helps to speed up the real-time response of the system. The low address space is used first to ensure that the real-time data can be accessed and processed quickly. The low address area usually has a faster access speed, and the storage order can ensure the priority processing of real-time tasks while avoiding memory access delays.
[0167] The embodiment of the present invention provides a memory management method for an embedded system, wherein the embedded system includes at least one functional module; the method includes: designating a continuous address space in a RAM as a dynamic memory space; when receiving a memory space application request from a functional module, identifying whether an application corresponding to the memory space application request includes a resource file; wherein the resource file is a variable-length record file, and includes resource management data for describing the memory space allocation situation; the memory space application request includes a first memory space capacity required for an object data set to store the object data set; the object data set includes object data and object information; if it is identified that the application includes a resource file, obtaining a first remaining memory space capacity of the dynamic memory space, and determining whether the first memory space capacity is less than or equal to the first remaining memory space capacity; wherein the first remaining memory space capacity is an unused portion of the dynamic memory space; if the first memory space capacity is less than or equal to the first remaining memory space capacity, storing the object data set in the dynamic memory space; generating first space allocation data based on a memory address of the object data set in the dynamic memory space; recording the first space allocation data in the resource file, and updating the resource management data based on the first memory space capacity and the first space allocation data; and returning the memory address of the object data set in the dynamic memory space to the functional module. In this method, by designating a continuous address space of RAM as dynamic memory space and combining it with resource files to record and manage memory allocation data, flexible allocation and real-time tracking of memory resources can be achieved, thereby effectively improving the memory utilization of the embedded system and ensuring the efficient operation and stability of the embedded system.
[0168] Embodiment 2:
[0169] Figure 6 A schematic diagram of a memory management device for an embedded system provided by an embodiment of the present invention.
[0170] Reference Figure 6 , a memory management device for an embedded system, comprising:
[0171] The dynamic memory space module 1 is used to designate a continuous address space in the RAM as a dynamic memory space.
[0172] Storage module 2 is used to identify whether the function module includes a resource file when receiving a memory space application request from the function module; wherein the resource file is a variable-length record file, which contains resource management data for describing the memory space allocation situation; the memory space application request includes the first memory space capacity required for the object data set to store the object data set; the object data set includes object data and object information.
[0173] Storage module 2 is also used to obtain the first remaining memory space capacity of the dynamic memory space if it is recognized that the function module includes a resource file, and to determine whether the first memory space capacity is less than or equal to the first remaining memory space capacity; wherein the first remaining memory space capacity is the unused portion of the dynamic memory space.
[0174] The storage module 2 is further configured to store the object data set into the dynamic memory space if the capacity of the first memory space is less than or equal to the capacity of the first remaining memory space.
[0175] The data management module 3 is used to generate first space allocation data based on the memory address of the object data set in the dynamic memory space.
[0176] The data management module 3 is further used to record the first space allocation data into the resource file, and update the resource management data based on the first memory space capacity and the first space allocation data.
[0177] The result returning module 4 is used to return the memory address of the object data set in the dynamic memory space to the function module.
[0178] In one embodiment, each functional module is provided with a corresponding resource file. Figure 7 The memory management device of the embedded system further includes a deletion module 5, which is used to:
[0179] When a request for releasing memory space is received from a functional module, each resource file is traversed based on the deletion start address in the request for releasing memory space to determine the second space allocation data corresponding to the deletion start address.
[0180] The second space allocation data is deleted, and based on the memory address in the second space allocation data, the object data set corresponding to the second space allocation data in the dynamic memory space is deleted.
[0181] Based on the second space allocation data and the second memory space capacity corresponding to the second space allocation data, resource management data corresponding to the functional module is updated.
[0182] In one embodiment, referring to Figure 7 , the deletion module 5 is also used to:
[0183] Traverse each resource file and rewrite the remaining object data sets after the deletion start address into the dynamic memory space in turn to sort out the memory fragments generated by the memory space release request.
[0184] Based on the updated memory addresses of the remaining object data sets in the dynamic memory space, the remaining second space allocation data in each resource file is updated.
[0185] The updated memory addresses of the remaining object data sets in the dynamic memory space are returned to the corresponding functional modules.
[0186] In one embodiment, referring to Figure 6 and Figure 7 , the storage module 2 is also used for:
[0187] If it is identified that the application does not include resource files, an application directory is created for the application and resource files are generated in the application directory.
[0188] In one embodiment, each first space allocation data corresponds to a record number. Figure 6 and Figure 7 , the storage module 2 is also used for:
[0189] Determine whether any record number in the resource file is equal to the preset maximum record number, and whether each record number in the resource file is a consecutive record number.
[0190] If a record number in the resource file is equal to the preset maximum record number, and each record number is a connection record number, the next resource file is generated in the application directory corresponding to the application.
[0191] In one embodiment, the type of the first memory space capacity is DTR space capacity; the resource management data is the associated and saved allocated DTR space capacity, allocated RTR space capacity, number of DTR occupied records and number of RTR occupied records; the initial value of the allocated DTR space capacity, the allocated RTR space capacity, the initial value of the number of DTR occupied records and the initial value of the number of RTR occupied records are all 0; the memory address includes the DTR start address and the DTR end address. Figure 6 and Figure 7 , the data management module 3 is also used for:
[0192] Get the DTR start address and DTR space capacity corresponding to the object data set.
[0193] Based on the DTR start address and the DTR space capacity, determine the DTR end address.
[0194] Based on the DTR start address, the DTR space capacity and the DTR end address, first space allocation data is generated.
[0195] A file ID corresponding to the resource file is obtained, a first record number corresponding to the first space allocation data is determined based on a preset record order, and the first record number and the first space allocation data are associated and recorded in the resource file corresponding to the file ID.
[0196] Update the allocated DTR space capacity based on the DTR space capacity, and increase the number of DTR occupied records by 1.
[0197] In one embodiment, the type of the second memory space capacity is DTR space capacity. Figure 6 and Figure 7 , the data management module 3 is also used for:
[0198] Reduce the DTR space capacity in the allocated DTR space capacity and reduce the number of DTR occupied records by 1.
[0199] In one embodiment, the type of the first memory space capacity is RTR space capacity; the resource management data is the associated and saved allocated DTR space capacity, allocated RTR space capacity, number of DTR occupied records and number of RTR occupied records; the initial value of the allocated DTR space capacity, the allocated RTR space capacity, the initial value of the number of DTR occupied records and the initial value of the number of RTR occupied records are all 0; the memory address includes the RTR start address and the RTR end address. Figure 6 and Figure 7 , the data management module 3 is also used for:
[0200] Get the RTR start address and RTR space capacity corresponding to the object data set.
[0201] Based on the RTR start address and the RTR space capacity, the RTR end address is determined.
[0202] Based on the RTR start address, the RTR space capacity and the RTR end address, first space allocation data is generated.
[0203] A file ID corresponding to the resource file is obtained, a first record number corresponding to the first space allocation data is determined based on a preset record order, and the first record number and the first space allocation data are associated and recorded in the resource file corresponding to the file ID.
[0204] Update the allocated RTR space capacity based on the RTR space capacity, and increase the number of RTR occupancy records by 1.
[0205] In one embodiment, the type of the second memory space capacity is RTR space capacity. Figure 6 and Figure 7 , the data management module 3 is also used for:
[0206] Reduce the RTR space capacity in the allocated RTR space capacity and reduce the number of RTR occupied records by 1.
[0207] In one embodiment, the type of the first memory space capacity is DTR space capacity or RTR space capacity. Storage module 2 is also used for:
[0208] When the type of the first memory space capacity is DTR space capacity, the object data set is stored in the order from high address to low address of the dynamic memory space; when the type of the first memory space capacity is RTR space capacity, the object data set is stored in the order from low address to high address of the dynamic memory space.
[0209] An embodiment of the present invention provides a memory management device for an embedded system. In this method, by designating a section of continuous address space of RAM as dynamic memory space and combining resource files to record and manage memory allocation data, flexible allocation and real-time tracking of memory resources can be achieved, thereby effectively improving the memory utilization of the embedded system, thereby ensuring the efficient operation and stability of the embedded system.
[0210] The computer program product provided in the embodiment of the present invention includes a computer-readable storage medium storing program code. The instructions included in the program code can be used to execute the method described in the previous method embodiment. The specific implementation can be found in the method embodiment and will not be repeated here.
[0211] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the system and device described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.
[0212] In addition, in the description of the embodiments of the present invention, unless otherwise clearly specified and limited, the terms "installed", "connected", and "connected" should be understood in a broad sense, for example, it can be a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be a direct connection or an indirect connection through an intermediate medium, or it can be the internal communication of two components. For ordinary technicians in this field, the specific meanings of the above terms in the present invention can be understood according to specific circumstances.
[0213] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the methods described in various embodiments of the present invention. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0214] In the description of the present invention, it should be noted that the terms "center", "upper", "lower", "left", "right", "vertical", "horizontal", "inner", "outer", etc., indicating the orientation or positional relationship, are based on the orientation or positional relationship shown in the drawings, and are only for the convenience of describing the present invention and simplifying the description, rather than indicating or implying that the device or element referred to must have a specific orientation, be constructed and operated in a specific orientation, and therefore cannot be understood as limiting the present invention. In addition, the terms "first", "second", and "third" are used for descriptive purposes only, and cannot be understood as indicating or implying relative importance.
[0215] Finally, it should be noted that the above-described embodiments are only specific implementations of the present invention, which are used to illustrate the technical solutions of the present invention, rather than to limit them. The protection scope of the present invention is not limited thereto. Although the present invention is described in detail with reference to the above-described embodiments, ordinary technicians in the field should understand that any technician familiar with the technical field can still modify the technical solutions recorded in the above-described embodiments within the technical scope disclosed by the present invention, or can easily think of changes, or make equivalent replacements for some of the technical features therein; and these modifications, changes or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present invention, and should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention shall be based on the protection scope of the claims.
Claims
1. A memory management method for an embedded system, characterized in that: The embedded system includes at least one functional module; the method includes: Designate a continuous address space in RAM as dynamic memory space; When receiving a memory space application request from the functional module, identifying whether the application corresponding to the memory space application request includes a resource file; wherein the resource file is a variable-length record file, including resource management data for describing memory space allocation; the memory space application request includes a first memory space capacity required by an object data set to store the object data set; the object data set includes object data and object information; If it is recognized that the application includes the resource file, obtaining a first remaining memory space capacity of the dynamic memory space, and determining whether the first memory space capacity is less than or equal to the first remaining memory space capacity; wherein the first remaining memory space capacity is an unused portion of the dynamic memory space; If the capacity of the first memory space is less than or equal to the capacity of the first remaining memory space, storing the object data set in the dynamic memory space; generating first space allocation data based on a memory address of the object data set in the dynamic memory space; Recording the first space allocation data into the resource file, and updating the resource management data based on the first memory space capacity and the first space allocation data; The memory address of the object data set in the dynamic memory space is returned to the functional module.
2. The memory management method of the embedded system according to claim 1, characterized in that: Each of the functional modules is correspondingly provided with a resource file, and the method further comprises: When receiving a request for releasing memory space from the functional module, traversing each resource file based on the deletion start address in the request for releasing memory space, and determining the second space allocation data corresponding to the deletion start address; deleting the second space allocation data, and based on the memory address in the second space allocation data, deleting the object data set corresponding to the second space allocation data in the dynamic memory space; Based on the second space allocation data and a second memory space capacity corresponding to the second space allocation data, the resource management data corresponding to the functional module is updated.
3. The memory management method of the embedded system according to claim 2, characterized in that: After updating the resource management data corresponding to the functional module, the method further includes: Traversing each of the resource files, and rewriting the remaining object data sets after the deletion start address into the dynamic memory space in turn, so as to sort out the memory fragments generated by the memory space release request; Based on the updated memory address of the remaining object data set in the dynamic memory space, updating the remaining second space allocation data in each of the resource files; The updated memory address of the remaining object data set in the dynamic memory space is returned to the corresponding functional module.
4. The memory management method of an embedded system according to claim 1, characterized in that: After the step of identifying whether the application corresponding to the memory space application request includes a resource file, the method further includes: If it is identified that the application does not include the resource file, an application directory is created for the application, and the resource file is generated in the application directory.
5. The memory management method of the embedded system according to claim 4, characterized in that: Each of the first space allocation data corresponds to a record number; After the step of identifying that the function module includes the resource file, the method further includes: Determine whether any of the record numbers in the resource file is equal to a preset maximum record number, and whether each of the record numbers in the resource file is a consecutive record number; If one of the record numbers in the resource file is equal to the preset maximum record number, and each of the record numbers is a connection record number, the next resource file is generated in the application directory corresponding to the application.
6. The memory management method of the embedded system according to claim 2, characterized in that: The type of the first memory space capacity is DTR space capacity; the resource management data is the associated and saved allocated DTR space capacity, allocated RTR space capacity, number of DTR occupied records and number of RTR occupied records; the initial value of the allocated DTR space capacity, the allocated RTR space capacity, the initial value of the DTR occupied records and the initial value of the RTR occupied records are all 0; the memory address includes a DTR start address and a DTR end address; The step of recording the first space allocation data into the resource file and updating the resource management data based on the first memory space capacity and the first space allocation data comprises: Obtain the DTR start address and the DTR space capacity corresponding to the object data set; Determine the DTR end address based on the DTR start address and the DTR space capacity; Generate the first space allocation data based on the DTR start address, the DTR space capacity and the DTR end address; Obtaining a file ID corresponding to the resource file, determining a first record number corresponding to the first space allocation data based on a preset record order, and associating the first record number and the first space allocation data to record in the resource file corresponding to the file ID; The allocated DTR space capacity is updated based on the DTR space capacity, and the number of DTR occupied records is increased by 1.
7. The memory management method of the embedded system according to claim 6, characterized in that: The type of the second memory space capacity is the DTR space capacity; The step of updating the resource management data corresponding to the functional module based on the second space allocation data and the second memory space capacity corresponding to the second space allocation data comprises: The DTR space capacity is reduced from the allocated DTR space capacity, and the number of DTR occupied records is reduced by 1.
8. The memory management method of the embedded system according to claim 2, characterized in that: The type of the first memory space capacity is RTR space capacity; the resource management data is the associated and saved allocated DTR space capacity, allocated RTR space capacity, number of DTR occupied records and number of RTR occupied records; the initial value of the allocated DTR space capacity, the allocated RTR space capacity, the initial value of the DTR occupied records and the initial value of the RTR occupied records are all 0; the memory address includes an RTR start address and an RTR end address; The step of recording the first space allocation data into the resource file and updating the resource management data based on the first memory space capacity and the first space allocation data comprises: Obtaining the RTR start address and the RTR space capacity corresponding to the object data set; Determine the RTR end address based on the RTR start address and the RTR space capacity; Generate the first space allocation data based on the RTR start address, the RTR space capacity and the RTR end address; Obtaining a file ID corresponding to the resource file, determining a first record number corresponding to the first space allocation data based on a preset record order, and associating the first record number and the first space allocation data to record in the resource file corresponding to the file ID; The allocated RTR space capacity is updated based on the RTR space capacity, and the number of RTR occupancy records is increased by 1.
9. The memory management method of the embedded system according to claim 8, characterized in that: The type of the second memory space capacity is the RTR space capacity; The step of updating the resource management data corresponding to the functional module based on the second space allocation data and the second memory space capacity corresponding to the second space allocation data comprises: The RTR space capacity is reduced in the allocated RTR space capacity, and the number of RTR occupancy records is reduced by 1.
10. The memory management method of the embedded system according to claim 1, characterized in that: The type of the first memory space capacity is DTR space capacity or RTR space capacity; The step of storing the object data set in the dynamic memory space comprises: When the type of the first memory space capacity is the DTR space capacity, the object data set is stored in the order from high address to low address of the dynamic memory space; when the type of the first memory space capacity is the RTR space capacity, the object data set is stored in the order from low address to high address of the dynamic memory space.
11. A memory management device for an embedded system, characterized in that: The memory management device comprises: Dynamic memory space module, used to designate a continuous address space in RAM as dynamic memory space; A storage module, configured to identify whether a resource file is included in the function module when receiving a memory space application request from the function module; wherein the resource file is a variable-length record file, including resource management data for describing memory space allocation; the memory space application request includes a first memory space capacity required by the object data set to store the object data set; the object data set includes object data and object information; The storage module is further configured to obtain a first remaining memory space capacity of the dynamic memory space if it is identified that the function module includes the resource file, and determine whether the first memory space capacity is less than or equal to the first remaining memory space capacity; wherein the first remaining memory space capacity is an unused portion of the dynamic memory space; The storage module is further configured to store the object data set in the dynamic memory space if the capacity of the first memory space is less than or equal to the capacity of the first remaining memory space; A data management module, configured to generate first space allocation data based on a memory address of the object data set in the dynamic memory space; The data management module is further used to record the first space allocation data into the resource file, and update the resource management data based on the first memory space capacity and the first space allocation data; The result returning module is used to return the memory address of the object data set in the dynamic memory space to the function module.
Citation Information
Patent Citations
Method and system for dynamically managing JavaCard transient resources
CN105426237A
Java Card transient object storage space distribution method and device
CN107807846A
Memory control method and device and electronic equipment
CN119003182A
Internal memory monitoring method
CN1936859A
Dynamic memory managing method and dynamic memory management information processing device
JP2003248620A