Java 7
Young Generation
Eden
Survivors: S0, S1
- When Eden is full, Minor GC
- minor GC
Old Generation
Major GC
string pool was relocated to the heap
String pool values are garbage collected
Young Generation
Eden
Survivors: S0, S1
- When Eden is full, Minor GC
- minor GC
Old Generation
Major GC
string pool was relocated to the heap
String pool values are garbage collected
Internal architecture of the Java virtual machine:
Heap
One JVM instance has only one heap, it is created on JVM start-up and shared among all Java virtual machine threads.
The heap is the runtime data area from which memory for all class instances and arrays is allocated.
Its memory is reclaimed using the Garbage Collection.
Heap is also used to store Method Area.
Method Area
Method Area is logically part of the heap.
It stores information about loaded types, and per-class structures such as class static variables, constant pool, field and method data, and the code for methods and constructors, including the special methods used in class and instance initialization and interface type initialization
It is created on virtual machine start-up and shared among all Java virtual machine threads.
Runtime Constant Pool
Runtime constant Pool memory is allocated from the Method Area.
It is created when the class or interface is loaded by the Java virtual machine.
It stores different kinds of constants, ranging from numeric literals known at compile time to method and field references that must be resolved at run time.
JVM stack
Another runtime memory area types is JVM stack.
JVM stack is created when a new thread is launched. Data stored in the JVM stack in private and only accessible to that thread.
A Java stack stores a thread's state in discrete stack frames.
JVM stack holds local variables and partial results, and plays a part in method invocation and return.
The Stack Frame
The stack frame stores local variables, operand stack, and frame data.
When a thread invokes a Java method, the virtual machine creates and pushes a new frame onto the thread's Java stack. This new frame then becomes the current frame. As the method executes, it uses the frame to store parameters, local variables, intermediate computations, and other data.
Native Method Stacks
These stacks are provided by JVM that support native methods. They are created per thread.
Instance variables are stored in Heap
Static variables are stored in Method Area
Arrays are stored on heap
For local primitive variables are stored on stack, for non-primitive variables, its memory is allocated in heap, stack only stores a reference to that object.
When you instantiate an object in Java you are actually allocating two pieces of memory - one on the stack and one on the heap.
The memory of the stack is used to store a reference and the memory on the heap is used to store the actual object. The primitive data types (int, long, char etc.) are only stored on the stack.
Resources