Jumat, 19 Desember 2014

java.io.IOException: Map failed and java.lang.OutOfMemoryError: Map failed



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


Depending upon which operating system you are working, e.g. Windows or UNIX, you can try different things to solve this error. One of the quick way to solve this by reducing size, which you want to map into memory. For example if you are trying to map 1GB into memory, you may want to only map 512MB or lower, if your code uses region based approach, this may work file. Alternatively you can also use -d64 and -XX:MaxDirectMemorySize JVM option to enable large direct buffers :



-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.























Source:http://javarevisited.blogspot.com/2014/11/javaioioexception-map-failed-javalangoutofmemoryerror.html

Tidak ada komentar:

Posting Komentar