Link to this article: Introduction to Android Mmap file mapping to memory

In Android development, we may need to document some files. For example, log files. If you use streams to write files, frequent manipulation of file IO can cause performance problems. To reduce the frequency of writing files, we might cache a certain number of logs and write them to the file at once. If the app exits unexpectedly, we may lose log information in memory. So what’s a safe way to write files that keeps IO down while keeping data written to the file as much as possible?

Mmap profile

Mmap concept

Mmap is a method of memory-mapping files. A file or other object is mapped to the address space of a process to achieve the mapping between the file disk address and a segment of virtual address in the process virtual address space.

Features: After this mapping is implemented, the process can use Pointers to read and write the memory, and the system will automatically write back dirty pages to the corresponding file disk, that is, the file operation is completed without calling system call functions such as read and write. Conversely, changes made by the kernel space to this area directly reflect user space, allowing file sharing between different processes. As shown below:

Mmap Memory mapping principle

The implementation process of MMAP memory mapping can be divided into three stages:

The application process starts mapping, and searches for an idle continuous virtual address in the virtual address space of the process as the mapping area. The system function Mmap is called to realize the mapping between the physical address of the file and the virtual address of the process. The application process accesses the mapping area, causing page failure exceptions, and realizing the copy of file contents to physical memory (main memory).

Mmap pros and cons

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

Both parent and unrelated processes can map their own user space to the same file or anonymously to the same area. Therefore, the purpose of interprocess communication and interprocess sharing can be achieved by changing the mapping region.

At the same time, if both process A and process B map region C, when A reads C for the first time, the file page is copied from disk to memory through the missing page. However, when USER B reads the same page from user C again, a page error will occur. However, the file data stored in the memory can be directly used instead of copying the file from the disk.

Mmap attention

Memory mapping is faster than normal I/O streams for large files, not necessarily for small files; Don’t always call the mappedbyteBuffer.force () method, which forces the operating system to write the contents of memory to disk, so if you call force() every time you write to a memory-mapped file, you won’t really benefit from it, just like disk IO. The operating system is responsible for reading and writing memory-mapped files, so even if your Java program dies after writing to memory, data will be written to disk as long as the operating system is working properly. If the power fails or the host crashes, it is possible that the memory-mapped files have not been written to disk, which means that some critical data may be lost.

reference

  • Stackoverflow.com/questions/2…
  • www.jianshu.com/p/187eada7b…
  • Juejin. Cn/post / 684490…
  • Stackoverflow.com/questions/3…
  • Stackoverflow.com/questions/3…

Binder in Android also leverages MMap. Binder transfers data to another process only by copying it once. See Introduction to the Binder mechanism

Android uses Mmap

Android uses Mmap, which can be combined with MappedByteBuffer via RandomAccessFile. Refer to the development record of “Drone”

Through the randomAccessFile. GetChannel (). The map is available MappedByteBuffer. The put method of ByteBuffer is then called to add the data.

See rustfisher.com for more articles