Heap: Java objects
heap also called as shared memory, where multiple threads share the same data. It is the runtime data area from which memory for class instances and arrays are allocated, created at the start-up.
-Xmx<> - set the maximum Java heap size, by default is 64MB
-Xms<> - set the initial Java heap size
Heap memory is reclaimed by Garbage Collector.
advantage: provides for dynamic storage management
disadvantage: inefficient and unreliable
Non-Heap: loaded classes, meta-data
Method Area: stores per-class structures, code for methods and constructors. Per-class structure means runtime constants and static fields.
Static Variables are stored in Method Area
Stack:
stacks stores frames. stacks are created private to a thread. every thread has a PC (Program Counter) and a java stack. PC use the stack to store the intermediate values, dynamic linking, return values for methods and dispatch exception
A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes.
advantage: recursion, conserve storage
disadvantage: inefficient reference, overhead of allocation and deallocation, subprograms cannot be history sensitive
Memory Generation:
HotSpot VM's GC seperate the JVM's memory into young generation and old generation
Young Generation:
Eden space and Survivor space. Shortlived objects will be available in Eden space, when GC happens, if the object is referenced, it will be moved to survivor space and for dereferenced objects will be removed
Old Generation:
Tenured and Permanent. GC moves live objects from survivor space to tenured generation. The permanent generation contains meta data of the VM, class and method objects.