Kam
Kam

Reputation: 6008

Stack/Heap in JVM

I come from C/C++ background, where a process memory is divided into:

I am trying to understand how JVM works, I looked at different resources, I gathered that the JVM memory is divided into heap and stack as well plus few other things.

I want to wrap my mind around this, when I read heap and stack in JVM are we talking about concepts of stack and heap? and that the actual memory of the entire JVM resides on the heap (and here I mean the C++ concept of a Heap)?

Upvotes: 2

Views: 1522

Answers (1)

nanofarad
nanofarad

Reputation: 41281

I want to wrap my mind around this, when I read heap and stack in JVM are we talking about concepts of stack and heap?

Yes, in general this is the case. Each thread has its own per-thread stack, which is used to store local variables in stack frames (corresponding to method calls). The stack need not be located in a location related to the per-thread stack at the OS level. If the stack attempts to grow past a size as specified by -Xss or a default set by the implementation, a StackOverflowError will be thrown.

The stack can exist in C/C++ heap memory, and need not be contiguous (JVM spec v7):

Each Java Virtual Machine thread has a private Java Virtual Machine stack, created at the same time as the thread. A Java Virtual Machine stack stores frames (§2.6). A Java Virtual Machine stack is analogous to the stack of a conventional language such as C: it holds local variables and partial results, and plays a part in method invocation and return. Because the Java Virtual Machine stack is never manipulated directly except to push and pop frames, frames may be heap allocated. The memory for a Java Virtual Machine stack does not need to be contiguous.

The Java heap is a means of storing objects, including automatic garbage collection when objects are no longer reachable via strong references. It is shared between all threads running on a JVM.

The Java Virtual Machine has a heap that is shared among all Java Virtual Machine threads. The heap is the run-time data area from which memory for all class instances and arrays is allocated.

The heap is created on virtual machine start-up. Heap storage for objects is reclaimed by an automatic storage management system (known as a garbage collector); objects are never explicitly deallocated. The Java Virtual Machine assumes no particular type of automatic storage management system, and the storage management technique may be chosen according to the implementor's system requirements. The heap may be of a fixed size or may be expanded as required by the computation and may be contracted if a larger heap becomes unnecessary. The memory for the heap does not need to be contiguous.

By simply calling a constructor (e.g. HashMap foo = new HashMap()) the JVM will allocate the requisite memory on the heap for this object (or throw an OutOfMemoryError if that is not possible). It's also important to note that objects never live on the stack--only references to them do. Additionally, non-primitive fields also always contain references to objects.

It's also possible to allocate memory off-heap through sun.misc.Unsafe on some JVMs, some NIO classes that allocate direct buffers, and through the use of JNI. This memory is not part of the JVM heap and does not undergo automatic garbage-collection (meaning that it would need to be released through means such as delete, but it may be a part of heap memory as C++ may refer to it.

Upvotes: 2

Related Questions