While working with memory mapped file, you may get java.io.IOException: Map failed error, which is mainly caused by Caused by: java.lang.OutOfMemoryError: Map failed error as shown below. This error usually comes while mapping a big file in memory e.g. trying to map a file greater than 1 or 2GB
java.io.IOException: Map failed
at sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:888)
Caused by: java.lang.OutOfMemoryError: Map failed
at sun.nio.ch.FileChannelImpl.map0(Native Method)
at sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:885)
... 6 more
How to Solve java.io.IOException: Map failed

-d64
This JVM flag instruct virtual machine to run on 64-bit mode. Since 64-bit installation on Solaris and may be on other UNIX systems comes with 32-bit JVM as well, which may be efficient for small programs, due to small size of OOPS pointer as discusses on why use -XX:UseCompressedOOPS flag in 64-bit JVM. By providing this option to your JVM you make sure that it's running on 64-bit mode, allowing it to map more memory.
-XX:MaxDirectMemorySize
This option is not on official list of JVM flags but still a good option to try. It allows you configure memory used by direct buffers.
One more option, you can try is -XX:+UseLargePages.
For example, if your program is using 6GB of memory mapped file, you may want to pass
java -d64 -XX:MaxDirectMemorySize=6g -XX:+UseLargePages BigApp
That's all on how to solve java.io.IOException: Map failed and java.lang.OutOfMemoryError: Map failed. You can use any of these options or combination of them to see what works for you. Make sure you use 64-bit JVM if you are mapping large files in memory. Remember though both error are different, one is IOException and other is OutOfMemoryError, cause is more or less same, difference is IOException comes while using that file and OutOfMemoryError comes while mapping that file in memory.
Tidak ada komentar:
Posting Komentar