Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have multiple heaps in java?

Is it possible to have multiple heaps in java? If it is possible then in which cases it does happen?

like image 363
Marcin Szymczak Avatar asked Dec 08 '22 05:12

Marcin Szymczak


1 Answers

There is already multiple memory regions, but only one Java heap as such.

Typically there are;

  • Java heap which might be broken into
    • Eden space,
    • Survivor Spaces,
    • Tenures space.
  • The native memory heap for small direct memory allocation. e.g. ByteBuffer.allocateDirect(4) See missing [heap] section in /proc/pid/maps for an interesting discussion of the [heap] region in native space.
  • Anonymous memory mapping for large direct allocations. ByteBuffer.allocateDirect(10000000)
  • Stack space for the threads. i.e. the local variables in a method.
  • Perm Gen or MetaSpace for the code. WHere you byte code and the JIT compiled code goes
  • Shared libraries for native code. This includes DLL/SO and why "hello world" appears to use so much memory.
  • Memory mapped file regions. e.g. FileChannel.map(..)
  • Other regions rarely used directly such as the VDSO. Only accessible via JNI or Unsafe.

When you say "the heap" this refers to the first one, the one you can naturally allocate Java object on and the Garbage Collector manages. In reality, there is also the native "heap" but this is not interchangeable and has a very different purpose, in fact most Java developers would never need to know it exists.

Note: this is how the JVM is implemented on Windows and Linux, there is nothing in the standard which says these have to exists for Java to work.

like image 134
Peter Lawrey Avatar answered Dec 11 '22 08:12

Peter Lawrey