Reputation: 39950
Does FileChannel#map
allocate all the memory needed for the resulting ByteBuffer
immediately, or is it only allocated on-demand during reads from the buffer?
I just tried mapping all of a 500+ MB file in a trivial test program, and looked at the memory usage of the process. (Using both Runtime#totalMemory
and eyeballing it in the OS X Activity Monitor for a groovysh process.) The memory usage never passed 30-ish MB.
So, could a Java implementation "hide" some of its memory usage in native calls? And if so, is there a way to find out how much that is on OS X?
Upvotes: 5
Views: 2084
Reputation: 1709
java.nio.MappedByteBuffer#load will trigger loading of all file contents into memory (otherwise it's on demand):
This method makes a best effort to ensure that, when it returns, * this buffer's content is resident in physical memory. Invoking this * method may cause some number of page faults and I/O operations to * occur.
Upvotes: 1
Reputation: 2624
Yes. It's not part of the heap. Still, depending on the OS there will still be memory reserved. On OS X, because it's UNIX-like you should be able to use top or ps to check how much memory it needs.
Upvotes: 1
Reputation: 147164
Memory usage is never straightforward. The actual buffer used by FileChannel.map
is not part of the Java heap. Indeed the memory might be shared with other processes. The file may not even be read from the disc until the pages are touched.
Upvotes: 5