3

From the Java docs,

The contents of direct buffers may reside outside of the normal garbage-collected heap, and so their impact upon the memory footprint of an application might not be obvious

Also from the Java docs,

MappedByteBuffer: A direct byte buffer whose content is a memory-mapped region of a file.

and

A mapped byte buffer and the file mapping that it represents remain valid until the buffer itself is garbage-collected.

I believe that off the heap memory allocations cannot be garbage-collected by the GC. In this case, these statements make me curious about memory management of a MappedByteBuffer. What happens if the direct ByteBuffer backing a MappedByteBuffer sits outside the normal heap?

  • Probably, the Java object is configured to clean up the native resources when it is garbage collected. This could be implemented using `Object#finalize` (deprecated since Java 9) or using [`java.lang.ref.Cleaner`](https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/ref/Cleaner.html) (or some similar internal mechanism). – Slaw May 09 '19 at 17:11
  • @Slaw Hmmm...was going through the implementation of `DirectByteBuffer`. Guess you might be right. The question can more generally be asked for a direct byte buffer but I have seen uses of `MappedByteBuffer` more often than direct byte buffers. – Viplav Kumar May 09 '19 at 17:35
  • https://stackoverflow.com/questions/29014757/are-memory-mapped-files-managed-by-the-os#comment46469073_29119039 – Leponzo Jun 19 '21 at 01:32

0 Answers0