About the behavior when doing a file map with java

About the behavior when doing a file map with java

I investigated the behavior when file mapping is done in Java, so I will summarize it.

Environment CentOS7.3.1611, openjdk-1.8.0.131

You can use the open + mmap system call in C to map files to memory, but I've always been concerned about using it in Java. I will actually try it.

Evaluate with the following sources.

mmap01.java


import java.lang.Thread;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class mmap01
{
  public static void main(String[] args) throws Exception
  {
    for (int argc = 0; argc < args.length; argc++ ) {
      File file = new File(args[argc]);
      long length = file.length();

      System.out.println("file:" + args[0] + "  size:" + length);
      MappedByteBuffer in = new RandomAccessFile(file, "r")
                                .getChannel().map(FileChannel.MapMode.READ_ONLY, 0, length);
      for (long i = 0; i < length; i++) {
        in.get();
      }
      System.out.println("Finished reading");
    }
    System.out.println("wait");
    Thread.sleep(1000000);
  }
}

Let's actually map it.

By the way, in the case of a file that exceeds Integer.MAX_VALUE (2 ^ 31), the following error will occur at the timing of map. I was able to map with a size of 2 ^ 31-1.

$ dd if=/dev/zero of=2gb bs=1 seek=2147483647 count=1
$ dd if=/dev/zero of=2gb-1 bs=1 seek=2147483646 count=1
$ dd if=/dev/zero of=2gb-2 bs=1 seek=2147483645 count=1
$ java mmap01 2gb
file:2gb  size:2147483648
Exception in thread "main" java.lang.IllegalArgumentException: Size exceeds Integer.MAX_VALUE
	at sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:869)
	at mmap01.main(mmap01.java:17)
$ java mmap01 2gb-1
file:2gb-1  size:2147483647
Finished reading
wait

Checking the memory map status and Java status of the java process at this time is as follows. It seems to be mapped with mmap. From the jstat results, it seems that the mapped area is out of the heap.

$ cat /proc/$(pidof java)/maps
(Abbreviation)
7f97c8000000-7f9848000000 r--s 00000000 00:26 5                          /mnt/hgfs/COMMON/Java_Mmap/2gb-1
(Abbreviation)
$ jstat -gc $(pidof java)
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT
1024.0 1024.0  0.0    0.0    8192.0   671.9    20480.0      0.0     4480.0 866.3  384.0   73.9       0    0.000   0      0.000    0.000

What happens when you map multiple files?

$ java mmap01 2gb-1 2gb-2
file:2gb-1  size:2147483647
Finished reading
file:2gb-1  size:2147483646
Finished reading
wait

Both files are properly mapped. The heap has not increased as much as the additionally read area (2 ^ 31-2).

$ cat /proc/$(pidof java)/maps
(Abbreviation)
7fd08c000000-7fd10c000000 r--s 00000000 00:26 6                          /mnt/hgfs/COMMON/Java_Mmap/2gb-2
7fd10c000000-7fd18c000000 r--s 00000000 00:26 5                          /mnt/hgfs/COMMON/Java_Mmap/2gb-1
(Abbreviation)
$ jstat -gc $(pidof java)
 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT
1024.0 1024.0  0.0    0.0    8192.0   835.7    20480.0      0.0     4480.0 866.3  384.0   73.9       0    0.000   0      0.000    0.000

But why did we set a 2GB limit on the mapable area? It is also difficult to handle because there is no explicit unmapping method. Notes on DirectByteBuffer and performance comparison also has a description of release.

reference

Recommended Posts

About the behavior when doing a file map with java
When a Java file created with the Atom editor is garbled when executed at the command prompt
A story about hitting the League Of Legends API with JAVA
Create a jar file with the command
[Java] Create a jar file with both compressed and uncompressed with the jar command
Read a string in a PDF file with Java
A story about the JDK in the Java 11 era
A story about trying to operate JAVA File
Create a multi-key map with the standard library
About the mechanism when displaying the GUI with Docker
About the map method
Check the behavior of Java Intrinsic Locks with bpftrace
[Java] Get the file path in the folder with List
I tried OCR processing a PDF file with Java
[Java] When writing the source ... A memorandum of understanding ①
A story about developing ROS called rosjava with java
I learned about the existence of a gemspec file
How to save a file with the specified extension under the directory specified in Java to the list
Error handling when the maximum file size is exceeded when uploading a file with Spring Boot
A note about the scope
About the File :: Stat class
[Java] Create a temporary file
Error when playing with java
I tried OCR processing a PDF file with Java part2
How to reduce the load on the program even a little when combining characters with JAVA
How to deal with the type that I thought about writing a Java program for 2 years
Create a simple web server with the Java standard library com.sun.net.httpserver
About what I did when creating a .clj file in Clojure
Read the file under the classpath as a character string with spring
Replace with a value according to the match with a Java regular expression
A story about having a hard time aligning a testing framework with Java 6
The point of addiction when performing basic authentication with Java URLConnection
Overwrite upload of file with the same name with BOX SDK (java)
[Gradle] Build a Java project with a configuration different from the convention
About the behavior of ruby Hash # ==
Build a Java project with Gradle
Learn about transaction savepoints (with Java)
Run a batch file from Java
Unzip the zip file in Java
About the current development environment (Java 8)
Make a list map with LazyMap
Follow the link with Selenium (Java)
EXCEL file update sample with JAVA
About file copy processing in Java
Java8 list conversion with Stream map
[IntelliJ IDEA] How to automatically add final when saving a Java file
For the time being, run the war file delivered in Java with Docker
The story of forgetting to close a file in Java and failing
The story of making a game launcher with automatic loading function [Java]
Let's express the result of analyzing Java bytecode with a class diagram
Create a Tokyo subway map from the CSV file of station data.jp
Memo: [Java] If a file is in the monitored directory, process it.
How to find out the Java version of a compiled class file
[Java small story] Monitor when a value is added to the List
Sample program that returns the hash value of a file in Java
About specifying the validity period when saving the cache in Redis with Spring Cache
<java> Split the address before and after the street address with a regular expression
Decrypt the entire bean in a properties file with some values encrypted
[Java] Get MimeType from the contents of the file with Apathce Tika [Kotlin]
About returning a reference in a Java Getter
When seeking multiple in a Java array