Java Garbage collector under the hood
In this article we understand how the java garbage collector works under the hood and what are the other memory management techniques used in other programming languages.
- Before we understand Java (GC) we need to understand the memory management, in the case of other programming languages, and then we see the stand in Java
Memory management is a crucial part in programming lanuage’s how it consumes the memory and then releases it whenever it does not need anymore different types of programming language take a different approach
Types of memory management
- ) Manual Memory management
- ) Automatic Memory management
Manual Memory management
Some system languages offer manual memory management to get mucch better control over memory example’s would be: C and C++
Advantages of Manual Memory management
Fine control over allocating and deallocating memory by ourselves
Superior Performance because of managing memory manually, the program doesn’t need to check what the object need to free up and what doesn’t
Disadvantages
Improper handling of allocating / deallocating memory leads to critical security bugs leading to memory leaks.
A pointer with an uninitialized memory address lead to “Wild Pointer” that causes system failure / crashes.
But dynamic memory allocation helps us to minimize these types of problems. The C standard library provide some group of functions ex: malloc you can see full list of funtion’s here
The Rust Programming language take different approach named Ownership and borrowing you can read about here
Automatic Memory management
High level programming languages provides Garbage Collection (GC) to automatically remove the objects that no longer need ex: Java, Python, C#, Go, Ruby
There were certain benefits in automatic memory management by using (GC) compared to the Manual approach
Memory leaks are fewer compared to manual memory management so better change to avoid crtical bugs
Also, it is not guaranteed the memory leak shouldn’t happen on (GC)
Let’s see what the approach does java do for better Garbage collection (GC)
Step: 1 Marking the Objects
First, java marks the object which is used/referenced in a program and which is not
Note: This is a time consuming process where system need to scan all the objects
Step: 2 Grouping together
The objects which are referenced in the program that all grouped together. This make new memory allocation fast and efficient. The old objects that are not referenced in our program they are moved to garbage collector and new objects can now enter into this heap memory
The heap is place where our object are stored and then it manages by GC
As, i previously mentioned marking the objects and grouping referencing objects these are very time consuming process that lead to very long GC times.
How Java overcome this problem
Empirical analysis of applications has shown that most newer objects (younger objects) are shot lived
Generational Garbage Collection
The information learned from object allocation behaviour, We use this knowledge to further improve the JVM performance.
So, the heap memory is further divided into the Young generation, the Old generation, and the Permanent generation
Young generation
The Young generation is a place where new objects are allocated When the young generation is filled with objects that causes: Minor GC
Old generation
The Old generation is used to store long surviving objects that is survived from Young garbage collection. Old generation objects that collect by GC is called Major GC
Note: The Minor and Major Garbage collection are stop-the-world event it means the all application threads are stopped Until the process finished.
Permanent generation
This contains metadata for JVM and also Java Standard Library classes and functions may be stored here.
Additional considerations
Memory Leak: A memory leak is a resources leak where the object is not garbage collected when it no longer needed it leads to Higher memory consumption when it often occurs, it unnecessarily increasing java heap memory that cause OutofMemoryError and our program crashes.
Summary
In this article I explained how Java the garbage collector works internally and what are the types of memory management in programming languages. If you want to learn further about Java (GC) i highly recommend visiting the official java docs here.