
Is Memory Mapped File This Fast in Java? - Tulik
http://sherxon.com/blog/fastest-io-in-java
======
ro_sharp
Memory mapped files are great fun in C, but Java can't safely unmap files!
Refer to this bug that's been open since 2002:
[http://bugs.java.com/view_bug.do?bug_id=4724038](http://bugs.java.com/view_bug.do?bug_id=4724038)

Quote (December 2016): "I have a plan to implement this, but it requires
changes to the Java Memory Model and to the compilers. I didn't get it done in
time for JDK 9 because of being distracted by a ton of other things, but I
hope I'll get it done by JDK 10."

------
tomfitz
If you find ByteBuffer's API awkward/insufficient, you might want to consider
one of the replacements:

* Netty's: [https://netty.io/4.0/api/io/netty/buffer/ByteBuf.html](https://netty.io/4.0/api/io/netty/buffer/ByteBuf.html) as used by Netty

* Agrona's, as used by Aeron and SBE: [http://insightfullogic.com/2015/Apr/18/agronas-threadsafe-of...](http://insightfullogic.com/2015/Apr/18/agronas-threadsafe-offheap-buffers/)

------
RandyRanderson
You should never read() one byte at a time unless you like waiting. The C
idiom is still true for Java:

byte[] buf=new byte[4096]; // this matters

while(true) {

    
    
      int num=in.read(buf,0,buf.length);
    
      ...
    

}

Also MemMapped files in Java have an int index so you can't really use them
for large files without using another method as well.

Finally typically one has to parse the files to a string of a certain charset.
This poses still more issues in terms of how you read files; esp multi and
variable byte charsets.

------
i336_
When was MappedByteBuffer introduced in Java?

Furthermore, in what situations is mmap() (or MappedByteBuffer, if the
situation is Java-specific) worse than read() et al?

I ask because Java is on my "I should probably know at least a little bit"
list and I'm interested to pick up all the tidbits and tricks that I can - and
this article screams "just use MappedByteBuffer everywhere you need I/O!", so
I want to know if there are any exceptions.

~~~
elFarto
I was slightly confused by the claim of the article saying:

"Memory mapped files are new feature in Java nio package"

They were introduced in Java 1.4, 15 years ago.

~~~
i336_
Thanks.

