Android MMAP file mapping to memory introduction

Android 2020

Link to this article: introduction to Android MMAP file mapping to memory

During Android development, we may need to record some files. For example, log file. If you use streaming to write files, frequent manipulation of files IO may cause performance problems. In order to reduce the frequency of writing files, we may cache a certain number of logs and write them to the file at one time. If the app exits abnormally, we may lose the log information in memory. So what is a more secure way to write files, which can not only reduce IO, but also ensure that data is written to files as much as possible?

MMAP is a memory mapping file method, that is, a file or other object is mapped to the process address space to realize the one-to-one mapping relationship between the file disk address and a virtual address in the process virtual address space.

Features: after implementing such a mapping relationship, the process can read and write this section of memory in the form of pointer, and the system will automatically write back the dirty page to the corresponding file disk, that is, the operation of the file is completed without calling read, write and other system call functions. On the contrary, the modification of this area in kernel space also directly reflects user space, so that file sharing between different processes can be realized. As shown in the figure below:

Generally speaking, the implementation process of MMAP memory mapping can be divided into three stages:

The application process starts mapping, and looks for an idle continuous virtual address that meets the requirements as the mapping area in the virtual address space of the process; Call the system function MMAP to realize the one-to-one mapping between the file physical address and the process virtual address; When the application process accesses the mapping area, it will cause page missing exception and copy the file content to physical memory (main memory).

Only one copy of data: when a page missing exception occurs, the data is directly copied from the disk to the user space of the process, skipping the page cache. It realizes the efficient interaction between user space and kernel space: the respective modification operations of the two spaces can be directly reflected in the mapped area, so as to be captured by the other space in time. Provides a way for processes to share memory and communicate with each other.

Both parent-child processes and unrelated processes can map their own user space to the same file or anonymously to the same area. Thus, by changing the mapping area, the purpose of inter process communication and inter process sharing can be achieved.

At the same time, if both process a and process B map area C, when a reads C for the first time, it copies the file page from the disk to the memory through the missing page; However, when B reads the same page of C again, although page missing exceptions will also occur, it is no longer necessary to copy files from disk, but can directly use the file data already saved in memory.

For large files, memory mapping is faster than ordinary IO streams, but not for small files; Don't call mappedbytebuffer. Force() method often. This method forces the operating system to write the contents of memory to the hard disk. Therefore, if you call force() method every time you write a memory mapping file, you can't really benefit from the memory mapping file, but it's similar to disk IO. The operating system is responsible for reading and writing memory mapped files. Therefore, even if your Java program hangs after writing to memory, as long as the operating system works normally, the data will be written to disk. If the power supply fails or the host is paralyzed, it is possible that the memory mapping file has not been written to the disk, which means that some key data may be lost.

Binder in Android also uses MMAP. When binder transfers data, it only needs to copy once to transfer the data to another process. Refer to binder mechanism introduction

MMAP is used in Android, which can be matched with mappedbytebuffer through RandomAccessFile. Refer to the drone development log tool

Get mappedbytebuffer through RandomAccessFile. Getchannel(). Map. Then call the put method of ByteBuffer to add data.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>