Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory usage of FileChannel#map

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?

like image 569
millimoose Avatar asked May 08 '09 10:05

millimoose


People also ask

What is Filechannel?

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.

What is memory mapped file Java?

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.


2 Answers

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.

like image 188
Tom Hawtin - tackline Avatar answered Sep 20 '22 15:09

Tom Hawtin - tackline


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.

like image 30
kohlerm Avatar answered Sep 18 '22 15:09

kohlerm