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?
A file channel is a SeekableByteChannel that is connected to a file. It has a current position within its file which can be both queried and modified . The file itself contains a variable-length sequence of bytes that can be read and written and whose current size can be queried.
Reading and writing in the memory-mapped file is generally done by the operating system to write content into a disk. Prefer Direct buffer to Indirect Buffer for better performance. Memory used to load File is outside Java heap and reside on shared memory which allows us to two different ways to access the file.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With