Java Memory Management
When does the garbage collector free up heap memory?
All is taken care of by JVM
Algorithm used by GC: Mark & Sweep
Mark: JVM mark those objects which are being referenced (Has Connection to Stack Memory).
Sweep: JVM sweep those objects which are no longer be used or referenced.
Normal Sweeping: It sweeps unmarked (red colored) objects and keep the gaps as it is. This might result OutOfMemoryError.
OutOfMemoryError:
Sweeping with compacting: It compacts the remaining gaps after sweeping unmarked objects. This is better than normal sweeping.
Sweeping with copying: It copies the marked objects instead of removing and compacting objects. It then makes original memory empty.
Different types of Memory:
Heap Memory:
Young generation : Copying objects from young generation to old generation. This is expensive operation.
a. Eden Space: New objects are created in Eden Space and as soon as it reaches a certain threshold, GC starts and moves objects still alive to the survivor space and cleaining up the Eden Space.
b. Survivor Space: When GC comes by then it moves all the alive objects to Old generation cleaning up the survivor space.
Old generation
Non-heap memory:
- Permanent-generation (Metaspace) or PermGen:
Types of Garbage Collectors:
Serial GC:
It uses mark and copy for Young generation & mark sweep compact for Old generation.
It runs on a single thread.
Parallel GC:
It uses mask and copy for Young generation & mark sweep compact for Old generation.
It runs on multiple threads.
Concurrent Mark Sweep GC:
It uses mark and copy for Young generation & mark sweep compact for Old generation.
It runs on multiple threads.
Garbage-First (G1) GC:
Divides heap into small regions
Keep track of amount of live and dead objects
Aims for shortest pauses possible
Runs on multiple threads
Z GC:
Sims for max 10ms pauses
Reference coloring
Sweep and copy
Load barriers
Runs on multiple threads
JVM Tuning:
JVM Tuning is typically the last step to improve the performance of an application
Before JVM tuning as yourself the following questions:
Is the memory functioning all right?
Is the latency all right?
Is the throughput normal?
Metaspace:
Once it reaches its limits, it expands
Set a miximum size
Set a threshold for GC
Set minimum and maximum free ratio
How to prevent memory leak / OutOfMemoryError in JVM ?
Set the objects to null in certain specific situations
Close resources such as streams, file connection, db connections
Avoid string concats; use string builder instead
Careful with static collections holding objects
Overwrite hashCode and equals (especially when custom objects get added to hash sets)
Subscribe to my newsletter
Read articles from Bikash Mainali directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Bikash Mainali
Bikash Mainali
Experienced Java/JavaScript full-stack developer over 6 years of extensive expertise serving key role on elite technical teams developing enterprise software for healthcare, apple ad-platform, banking, and e-commerce. Adaptable problem-solver with high levels of skill in Groovy, Java, Spring, Spring Boot, Hibernate, JavaScript, TypeScript, Angular, Node, Express, React, MongoDB, IBM DB2, Oracle, PL/SQL, Docker, Kubernetes, CI/CD pipelines, AWS, Micro-service and Agile/Scrum. Strong technical skills paired with business-savvy UI design expertise. Personable team player with experience collaborating with diverse cross-functional teams.