Core Java for Interviews
Stack and Heap
Stack
Stack Memory in Java is used for static memory allocation and the execution of a thread. It contains:
- Primitive values that are specific to a method
- References to objects referred from the method that are in a heap.
Stack has preallocated size, with the default being 1 MB in most modern operating systems and architectures. This can be customised with -Xss
tuning flag. Once the stack size is exhausted, the JVM will throw StackOverFlowError
.
-Xss1m
-Xss1024k
Stack access is performed in LIFO order. After the execution of a function, the stack frame is flushed and the flow goes back to the calling method. Memory space is automatically reclaimed.
Heap
Heap space is used for the dynamic memory allocation of Java objects and JRE classes at runtime. New objects are always created in heap space. These objects can be accessed from anywhere, any threads, in the application.
Heap in Java are divided into three parts, called generation:
- Young generation. This is where all new objects are allocated and aged. A minor Garbage collection occurs when this fills up. This is further divided into two portions:
- Eden space: when we create an object, the memory will be allocated from the Eden Space.
- Survivor space: this contains the objects that have survived from the Young garbage collection or Minor garbage collection. We have two equally divided survivor spaces called S0 and S1.
- Old or tenured generation. This is where long surviving objects are stored. When objects are stored in the Young Generation, a threshold for the object’s age is set, and when that threshold is reached, the object is moved to the old generation.
- Permanent generation. This consists of JVM metadata for the runtime classes and application methods.
When the heap space is full, Java throws OutOfMemoryError
. Memory space is reclaimed by Garbage Collection
. Tuning heap space can be done with several JVM flags:
- Minimum heap
-Xms
- Maximum heap
-Xmx
- JVM grow and shrinking ratio based on available free space ratio
-XX:MinHeapFreeRatio
and-XX:MaxHeapFreeRatio
- Initial size of young generation
-XX:NewSize
or-Xmns
- Maximum size of young generation
-XX:MaxNewSize
or-Xmnx
- Size of young generation
-Xmn
the combination of-Xmns
and-Xmnx
setting it to the same value - Ratio between young and tenured generation
-XX:NewRatio
, default is2
meaning the tenured generation occupies 2/3 of the heap, while the young generation occupies 1/3 of the heap - Ratio between eden space and survivor space
-XX:SurvivorRatio
, default is8
meaning the ratio between survivor and eden space is 1:8. Each survivor space is 1/10 of the young generation size (2 survivor space, 1 eden space –> 2 * 1 + 8 –> 10)
Garbage Collection
PECS
Multithreading
References
- https://www.baeldung.com/jvm-configure-stack-sizes
- https://www.baeldung.com/java-stack-heap
- https://dzone.com/articles/understanding-the-java-memory-model-and-the-garbag
- https://docs.oracle.com/cd/E19900-01/819-4742/abeik/index.html
- https://sematext.com/blog/jvm-performance-tuning/
- https://www.baeldung.com/java-generics-pecs