A memory persistence method and system

By automatically saving memory data through file mapping, the problem of easy loss of program memory data is solved, development efficiency is improved, the data saving process is simplified, and the use of third-party tools is avoided.

CN114817138BActive Publication Date: 2026-05-22JINAN XINTONG ELECTRIC TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
JINAN XINTONG ELECTRIC TECH CO LTD
Filing Date
2022-05-20
Publication Date
2026-05-22

AI Technical Summary

Technical Problem

Existing technologies suffer from data loss issues when handling persistent data in program memory, and require the introduction of third-party tools for read and write operations, which affects development efficiency.

Method used

By using file mapping, a portion of the memory data is mapped to a file, and the system API is used to automatically save the data, avoiding dependence on third-party tools.

Benefits of technology

It enables automatic persistence of in-memory data, improves development efficiency, simplifies the data saving process, and reduces reliance on third-party tools.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114817138B_ABST
    Figure CN114817138B_ABST
Patent Text Reader

Abstract

The application provides a memory persistence method and system, and belongs to the technical field of memory persistence control. The method uses a file mapping mode to process memory data persistence. Part of the content of a file is mapped to memory. Modifications made by a program to the part of the mapped memory can be automatically saved to the file. The scheme is convenient for reading and writing, does not need to introduce a third-party tool, and greatly improves development efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of memory persistence control technology, and in particular to a memory persistence method and system. Background Technology

[0002] The statements in this section are merely background information related to the present invention and do not necessarily constitute prior art.

[0003] The runtime data (heap, stack, etc.) in program memory is easily lost. In order to ensure that the data is not lost, a persistence method is needed to save the data to the hard disk and then read the data into memory when the program starts.

[0004] The current persistence methods include the following:

[0005] (1) Save the data to a file in a certain format (such as ini, json, xml, etc.). When the program starts, it parses the data and saves it into memory. This method is generally only suitable for configuration information where the data is not modified.

[0006] (2) Save the data to a database (mysql, sqlserver, sqlite, etc.). This method is suitable for most running data. The disadvantage is that third-party tools are required when reading / writing data. Summary of the Invention

[0007] To address the shortcomings of existing technologies, this invention provides a memory persistence method and system. The method uses file mapping to handle memory data persistence, mapping a portion of a file's content into memory. Modifications made by the program to this mapped memory portion can be automatically saved to the file. This facilitates reading and writing without requiring the introduction of third-party tools.

[0008] To achieve the above objectives, the present invention adopts the following technical solution:

[0009] The first aspect of this invention provides a memory persistence method.

[0010] A memory persistence method includes the following process:

[0011] Get the size X of the structure;

[0012] Create a .dat file, ensuring the file size is at least X;

[0013] The system API is called to map the portion of size X in the .dat file into memory, and the mapped address is obtained;

[0014] Reading or writing to memory of size X starting from the mapped address automatically saves the modifications to the dat file.

[0015] As an optional implementation, the file mapping API is mmap in Linux systems and CreateFileMapping in Windows systems.

[0016] A second aspect of the present invention provides a memory persistence system.

[0017] A memory persistence system, comprising:

[0018] The data acquisition module is configured to: acquire the size X of the structure;

[0019] The dat file creation module is configured to: create dat files, ensuring the file size is no less than X;

[0020] The mapping module is configured to call the system API to map a portion of size X in the .dat file into memory and obtain the mapping address.

[0021] The storage module has been modified and configured to read or write memory of size X starting from the mapped address, and modifications to this memory are automatically saved to the dat file.

[0022] A third aspect of the present invention provides a memory persistence method.

[0023] An in-memory persistence method for list data includes the following steps:

[0024] If the size of the control data is Y, and the size of each instance in the list is X, then the size to be mapped is: M = Y + nX, where n is the number of instances in the list, and the memory address of the i-th data location is: P. i =0x123456+(i-1)*Y;

[0025] Create a .dat file, ensuring its size is at least MB.

[0026] Map the contents of file M to memory, and the mapped address is 0X123456;

[0027] By iterating through the bitmap, we calculate the address of the first free location, copy the data to this location, and update the bitmap and the used count.

[0028] As an optional implementation, the i-th data is the data to be deleted. The memory space of size Y starting from the memory address of the i-th data is cleared to zero, and the bitmap and the used count are updated.

[0029] As an optional implementation, such as updating the data of the person object at the i-th position, first obtain the memory address of the i-th position P2 = 0x123456 + Y, and then modify the data of the person object at P2.

[0030] As an optional implementation, the location of valid data is determined based on the bitmap used. For each location of valid data, the data is checked to determine whether it is the data to be searched.

[0031] A fourth aspect of the present invention provides a memory persistence system.

[0032] An in-memory persistence system for list data includes:

[0033] The data acquisition module is configured such that: the data size is Y, the size of each instance in the list is X, and the size to be mapped is M = Y + nX, where n is the number of instances in the list, and the memory address of the i-th data location is P. i =0x123456+(i-1)*Y;

[0034] The .dat file creation module is configured to create .dat files with a file size of at least 1 MB.

[0035] The mapping module is configured to map the contents of a file of size M into memory, resulting in a mapping address of 0x123456.

[0036] The data insertion module is configured to: traverse the bitmap, calculate the address of the first free position, copy the data to this position, and update the bitmap and the used count.

[0037] The fifth aspect of the present invention provides a computer-readable storage medium having a program stored thereon that, when executed by a processor, implements the steps of the memory persistence method as described in the first or third aspect.

[0038] The sixth aspect of the present invention provides an electronic device, including a memory, a processor, and a program stored in the memory and executable on the processor, wherein the processor, when executing the program, implements the steps in the memory persistence method as described in the first or third aspect.

[0039] Compared with the prior art, the beneficial effects of the present invention are:

[0040] 1. This invention uses a file mapping method to handle memory data persistence. A portion of the file content is mapped into memory, and modifications made by the program to this mapped memory portion can be automatically saved to the file. This facilitates reading and writing without the need for third-party tools.

[0041] 2. In the past, after developers modified configuration data or some runtime data, they needed to manually persist the modified data to prevent data loss due to program restart. This invention uses a file mapping method to process such data. When developers modify the mapped memory, they modify the corresponding file. This eliminates the need for developers to consider the persistence of modified data and allow them to focus on functional development, thus improving development efficiency. Attached Figure Description

[0042] The accompanying drawings, which form part of this invention, are used to provide a further understanding of the invention. The illustrative embodiments of the invention and their descriptions are used to explain the invention and do not constitute an improper limitation of the invention.

[0043] Figure 1 This is a schematic diagram of fixed-size mapping data provided in Embodiment 1 of the present invention.

[0044] Figure 2 This is a schematic diagram of the non-fixed size mapping data provided in Embodiment 2 of the present invention.

[0045] Figure 3 This is a schematic diagram of inserting data according to Embodiment 2 of the present invention. Detailed Implementation

[0046] The present invention will be further described below with reference to the accompanying drawings and embodiments.

[0047] It should be noted that the following detailed description is illustrative and intended to provide further explanation of the invention. Unless otherwise specified, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this invention pertains.

[0048] It should be noted that the terminology used herein is for the purpose of describing particular embodiments only and is not intended to limit the scope of exemplary embodiments according to the invention. As used herein, the singular form is intended to include the plural form as well, unless the context clearly indicates otherwise. Furthermore, it should be understood that when the terms "comprising" and / or "including" are used in this specification, they indicate the presence of features, steps, operations, devices, components, and / or combinations thereof.

[0049] Where there is no conflict, the embodiments and features in the embodiments of the present invention can be combined with each other.

[0050] Example 1:

[0051] like Figure 1 As shown, Embodiment 1 of the present invention provides a memory persistence method, including the following process:

[0052] Define a structure: person, and calculate the size X of the structure;

[0053] In Linux systems, the API for file mapping is mmap; in Windows systems, the API is CreateFileMapping.

[0054] Create a file named test.dat, ensuring its size is at least X. Call the system API to map the portion of test.dat of size X into memory, obtaining the mapped address 0x123456. The program can then read from and write to the memory segment of size X starting at address 0x123456, automatically saving any modifications to this memory segment to the file test.dat.

[0055] Example 2:

[0056] Embodiment 2 of the present invention provides a memory persistence system, comprising:

[0057] The data acquisition module is configured to: acquire the size X of the structure;

[0058] The dat file creation module is configured to: create dat files, ensuring the file size is no less than X;

[0059] The mapping module is configured to call the system API to map a portion of size X in the .dat file into memory and obtain the mapping address.

[0060] The storage module has been modified and configured to read or write memory of size X starting from the mapped address, and modifications to this memory are automatically saved to the dat file.

[0061] Example 3:

[0062] like Figure 2 and Figure 3 As shown, Embodiment 1 of the present invention provides a memory persistence method for data with variable sizes, such as list data. Figure 2 The following process will be used as an example:

[0063] If the control data size is Y, and the size of each person instance in the list is X, then the size to be mapped is: M = Y + 5X; the memory address of the i-th data location is: P i =0x123456+(i-1)*Y;

[0064] Create a file named test.dat, ensuring the file size is at least M. Map the contents of the M-sized file into memory, obtaining the mapped address 0X123456.

[0065] Inserting data: By traversing the bitmap, we find that the first position is unused, and calculate the address P1 of the first position. Copy the data to the first position, update the bitmap and the used count, and obtain... Figure 3 ;

[0066] Deleting data: If calculations determine that the first piece of data is about to be deleted, then... Figures 3 to 2 This will give you the result of deleting the first data; clear the memory space of size Y starting from the memory address P1 of the first data to 0, and update the bitmap and the used count;

[0067] Update data: For example, to update the age of the person object in the second position to 31 years old, first get the memory address of the second position P2 = 0x123456 + Y, and then change the age of the person object at P2 to 31 years old.

[0068] Data retrieval: Based on the bitmap used, determine the locations where valid data exists. For each location where valid data exists, check the name and age to determine if it is the data to be searched.

[0069] Example 4:

[0070] Embodiment 4 of the present invention provides a memory persistence system for list data, including:

[0071] The data acquisition module is configured such that: the data size is Y, the size of each instance in the list is X, and the size to be mapped is M = Y + nX, where n is the number of instances in the list, and the memory address of the i-th data location is P. i =0x123456+(i-1)*Y;

[0072] The .dat file creation module is configured to create .dat files with a file size of at least 1 MB.

[0073] The mapping module is configured to map the contents of a file of size M into memory, resulting in a mapping address of 0x123456.

[0074] The data insertion module is configured to: traverse the bitmap, calculate the address of the first free position, copy the data to this position, and update the bitmap and the used count.

[0075] Also includes:

[0076] The data deletion module is configured such that, if calculations determine that the first piece of data is about to be deleted, it will be deleted by... Figures 3 to 2 This will give you the result of deleting the first data item. Clear the memory space of size Y starting at memory address P1 of the first data item to 0, and update the bitmap and the used count.

[0077] The data update module is configured as follows: if the age of the person object in the second position is updated to 31 years old, first obtain the memory address of the second position P2 = 0x123456 + Y, and then change the age of the person object at P2 to 31 years old.

[0078] The data query module is configured to: determine the location of valid data based on the bitmap used; and for each location of valid data, check the name and age to determine if it is the data to be searched.

[0079] Example 5:

[0080] Embodiment 5 of the present invention provides a computer-readable storage medium having a program stored thereon, which, when executed by a processor, implements the steps in the memory persistence method as described in Embodiment 1 or Embodiment 3.

[0081] Example 6:

[0082] Embodiment 6 of the present invention provides an electronic device, including a memory, a processor, and a program stored in the memory and executable on the processor. When the processor executes the program, it implements the steps in the memory persistence method as described in Embodiment 1 or Embodiment 3.

[0083] Those skilled in the art will understand that embodiments of the present invention can be provided as methods, systems, or computer program products. Therefore, the present invention can take the form of hardware embodiments, software embodiments, or embodiments combining software and hardware aspects. Furthermore, the present invention can take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage and optical storage) containing computer-usable program code.

[0084] This invention is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart illustrations and / or block diagrams. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.

[0085] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.

[0086] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.

[0087] Those skilled in the art will understand that all or part of the processes in the above embodiments can be implemented by a computer program instructing related hardware. The program can be stored in a computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. The storage medium can be a magnetic disk, optical disk, read-only memory (ROM), or random access memory (RAM), etc.

[0088] The above description is merely a preferred embodiment of the present invention and is not intended to limit the invention. Various modifications and variations can be made to the present invention by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.

Claims

1. A memory persistence method, characterized in that: The file mapping method is used to handle memory data persistence, including the following processes: When applied to a structure, the size X of the structure is obtained, a dat file is created, ensuring that the file size is no less than X, the system API is called to map the part of the dat file of size X into memory, the mapping address is obtained, and the memory of size X starting from the mapping address is read or written. Modifications to this memory are automatically saved to the dat file. When applied to list data, with the control data size being Y and the size of each instance in the list being X, the required mapping size is: M = Y + nX, where n is the number of instances in the list, and the memory address of the i-th data location is: P. i =0x123456+(i-1)*Y; Create a .dat file with a size no smaller than M; Map the contents of file M to memory, obtaining the mapped address 0x123456; traverse the bitmap and calculate the address of the first free location, copy the data to this location, and update the bitmap and the used count; based on the used bitmap, determine the locations where valid data exists, and for each location where valid data exists, check the data to determine if it is the data to be searched; The i-th data is the data to be deleted. Clear the memory space of size Y starting from the memory address of the i-th data, update the bitmap and the used count. If the data of the person object at the i-th position is to be updated, first get the memory address of the i-th position P2=0X123456+Y, and modify the data of the person object at P2.

2. The memory persistence method as described in claim 1, characterized in that: In Linux systems, the API for file mapping is mmap; in Windows systems, the API for file mapping is Create FileMapping.

3. A memory persistence system, characterized in that, File mapping is used to handle memory data persistence; When applied to a structure, it includes: The data acquisition module is configured to: acquire the size X of the structure; The dat file creation module is configured to: create dat files, ensuring the file size is no less than X; The mapping module is configured to call the system API to map a portion of size X in the .dat file into memory and obtain the mapping address. The storage module has been modified and configured to read or write memory of size X starting from the mapped address, and modifications to this memory are automatically saved to the dat file. When applied to list data, it includes: The data acquisition module is configured such that: the data size is Y, the size of each instance in the list is X, and the size to be mapped is M = Y + nX, where n is the number of instances in the list, and the memory address of the i-th data location is P. i =0x123456+(i-1)*Y; The .dat file creation module is configured to create .dat files with a file size of at least 1 MB. The mapping module is configured to map the contents of a file of size M into memory, resulting in a mapping address of 0x123456. The data insertion module is configured to: traverse the bitmap, calculate the address of the first free location, copy the data to this location, update the bitmap and the used count, determine the location where valid data exists based on the bitmap, and for each location where valid data exists, check the data to determine if it is the data to be searched. The i-th data is the data to be deleted. Clear the memory space of size Y starting from the memory address of the i-th data, update the bitmap and the used count. If the data of the person object at the i-th position is to be updated, first get the memory address of the i-th position P2=0x123456+Y, and modify the data of the person object at P2.

4. A computer-readable storage medium having a program stored thereon, characterized in that, When the program is executed by the processor, it implements the steps of the memory persistence method as described in any one of claims 1-2.

5. An electronic device comprising a memory, a processor, and a program stored in the memory and executable on the processor, characterized in that, When the processor executes the program, it implements the steps in the memory persistence method as described in any one of claims 1-2.