Suggested fix for JDK-4724038 (Add unmap method to MappedByteBuffer)
hearn at vinumeris.com
Tue Sep 8 13:03:05 UTC 2015
> If you're already doing this, why not skip the level of indirection and
> mprotect the entire mapped region to PROT_NONE when the user unmaps?
> ....... Then you do the real unmap when the buffer is GC'd (maybe via
Because the reason users are working around this is Windows, where they
need the ability to do:
If the unmap is still asynchronous it doesn't solve the problem.
Wait, or does it?
The primary place this matters is Windows. I must say that a
of the time that I find or write Java that doesn't run across platforms
properly, it's due to differences between the platforms file locking
behaviours and in particular because Java does not open files with a
reasonable share mode on Windows.
The bug to change this is here:
The comment (from 2007) says that there's a new file IO API (presumably
this meant NIO) that uses the right share mode, but that it still doesn't
help for deleting a mapped file because Windows doesn't allow that.
But MSDN is a little contradictory:
These *calls to CloseHandle succeed even when there are file views that are
> still open*. However, leaving file views mapped causes memory leaks.
The *DeleteFile* function fails if an application attempts to delete a file
> that has other handles open for normal I/O or as a memory-mapped file (
> *FILE_SHARE_DELETE* must have been specified when other handles were
So it suggests that you CAN delete an mmapped buffer as long as the handles
were all opened correctly. If true then this would allow the following
- Java always opens files with FILE_SHARE_DELETE. As any app that is not
expecting this behaviour is not portable anyway (would fail on UNIX), the
compatibility issues would hopefully not be too extreme.
- An attempt to close, delete or rename a file firstly remaps any mappings
of it to trigger access violations, then closes all underlying Win32
HANDLEs owned by the JVM, then returns. The actual unmap can be done
asynchronously as today once the GC comes along but it wouldn't matter
anymore because it's no longer stopping other file operations from working.
More information about the core-libs-dev