Add unmap method to MappedByteBuffer - http://bugs.sun.com/bugdata...
"We at Sun have given this problem a lot of thought, both during the original development of NIO and in the time since. We have yet to come up with a way to implement an unmap() method that's safe, efficient, and plausibly portable across operating systems. We've explored several other alternatives aside from the two described above, but all of them were even more problematic. We'd be thrilled if someone could come up with a workable solution, so we'll leave this bug open in the hope that it will attract attention from someone more clever than we are." - Sanjeev Singh
"Suppose that a thread operating on behalf of Alice maps a file into memory and then unmaps it. A second thread operating on behalf of Bob then maps some other file that the underlying operating system happens to assign to the same memory address. Now Alice's thread can read, and possibly even modify, the contents of Bob's file." - Paul Buchheit
Seems kind of dumb. Suppose that the threads are all under my control and I really do want to unmap a file? It's like they are still thinking strictly in terms of browser applets instead of normal server-side code, which is really the only thing people use java for anyway. - Paul Buchheit
It is kind of dumb. Fortunately the workaround (if you use a few, large files) is not that bad. Call System.gc(), System.runFinalization() and try again..... - Sanjeev Singh
Who designed a file API with an Open command but no Close? It seems almost useless to me. Paul: a browser would not have threads running on behalf of different users; a server would. - Gabe
If they're unwilling the pay the cost to check whether the mapping is valid, how do they ever manage to stomach the cost of bounds checking the get() calls on the MappedByteBuffer? I suppose they probably consider the cost of bounds checking inevitable, and maybe it can even be optimized out with a sufficiently clever compiler. They could at the very least offer a slower unmappable flavor that does make the validity check. - ⓞnor
When I do use C++, it's not that C++ compiles to something faster than Java, but rather because C and C++ give me direct control over memory and other resources. Most languages focus on the common case, and make things easier 99% of the time, and then make that 1% tear-out-hair frustrating. C++ makes things harder 100% of the time, but it's predictably hard and I never feel like I'm going to run into something like the unmap issue. - Amit Patel
Gabe, the real issue is who controls the code. On the server, it's typically all my code and I'm not worried about complex security schemes to protect one thread from attack by another. The idea with applets was that you could run untrusted code from third-parties and they would all be kept in a secure sandbox. Of course such code would never be allowed to mmap, which is why "security" is a really dumb reason to exclude this feature. - Paul Buchheit
Even if you disregard the security, there's the safety issue to consider. Having another file mapped there is harmless, but mappings can be read/write. If you unmap and then some other part of the system (possibly a C library) allocates memory there you can end up crashing the program. Not good. - Larry Greenfield
What's wrong with mapping in /dev/null instead? Doesn't mmap normally replace existing mappings of the same address? - Jim Norris
Jim: there is a race condition where some other thread in the process could allocate the memory between when you unmap the file and map in /dev/null. - Gabe
Larry, unmap is unsafe in the same way that free/delete are in c/c++. It's not ideal, but I'd rather have an unsafe op that I need to be careful with than not have it at all. They can call it "reallyDangerousAndScaryUnmapThatYouProbablyShouldNotUse()". - Paul Buchheit
Hmm, surprisingly you don't seem to be able to write your own mmap/munmap in JNI, so I can't say just use JNI nyah nyah. - Larry Greenfield
If Sun didn't implement this in JNI, how else could they have done it? JVM changes? - Gabe
Larry, you can use JNI to do mmap/unmap. One of the JNI examples does that: http://java.sun.com/develop... - Sanjeev Singh
Sanjeev, that example appears to be simply copying the bytes of the mmaped file into a java byte array: jb=(*env)->NewByteArray(env, finfo.st_size); (*env)->SetByteArrayRegion(env, jb, 0, finfo.st_size, (jbyte *)m); close(fd); - Paul Buchheit
I take it back. It is possible, using NewDirectByteBuffer (I misread the definition of this call when I first looked at the JNI documentation). That should be sufficient to implement an unsafe unmmap for Java code. http://java.sun.com/j2se... - Larry Greenfield