Is it possible to have multiple heaps in java? If it is possible then in which cases it does happen?
问题:
回答1:
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.
回答2:
As there is only one heap inside a Java virtual machine instance, you need multiple JVM'S for multiple heap
Java Virtual Machine Architecture - you can see Heap area is only one in Java virtual machine
refer link for JVM Internals