Sunday, February 22, 2009

Memory mapped files in Java? Easy!?

Heh. Not so easy :) In NIO there is ability to work with memory mapped files, which is provided via java.nio.MappedByteBuffer. I was really happy with idea using memory mapped files, which means icreasing of performace of working with with big files. But... well, it is not so good, as could be. Generally speaking, it works, but has some serious pitfalls, which are well-known and described (and discussed) here:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6417205 (fixed in 1.6)

In two words, you can't unmap file from memory, which is causing all other issues. It is released only when GC collects that buffer. Such limitation doesn't allow you to use that feature in any application which need to release file or work with mappings really intensive (create lots of new ones).

No comments: