April 11, 2020
Estimated Post Reading Time ~

JVM: Java Heap & Stack

When does Heap gets created?
Heap gets create on the startup of JVM.

What is Heap structure (young space/nursery and old space)? 
When an object created it is first stored and placed on young space or nursery and if it lives there for longer time it is moved to old space. The design goal behind young space and old space is that newly created objects are short lived and since they are placed on young space their access is faster.

How memory is allocated to Objects?
While assigning memory on Heap, Objects are first identified whether they are small or large? Small objects are allocated in Thread Local Areas (TLA). TLA are free chunk of space reserved from Heap and is given to thread for exclusive use. Thread can then allocate objects in its TLA without synchronizing with other threads. When the TLA becomes full, the thread simply requests a new TLA. The TLAs are reserved from the nursery if such exists; otherwise they are reserved anywhere in the heap. Large objects that don’t fit inside a TLA are allocated directly on the heap.

What are Shallow and retained sizes?
Shallow size of an object is the amount of memory allocated to store the object itself, not taking into account the referenced objects. Shallow size of a regular (non-array) object depends on the number and types of its fields. Shallow size of an array depends on the array length and the type of its elements (objects, primitive types). Shallow size of a set of objects represents the sum of shallow sizes of all objects in the set.

Retained size of an object is its shallow size plus the shallow sizes of the objects that are accessible, directly or indirectly, only from this object. In other words, the retained size represents the amount of memory that will be freed by the garbage collector when this object is collected.

What is allocated on stack and heap?
'Local variables' like function arguments or variables inside a function are only 'allocated' on the stack (primitive value or reference). Object Instance and static variables are stored on the heap.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.