Garbage Collection in Java - Basics
Automatic garbage collection process involves following steps:
In garbage collection, live (in use) objects are tracked and the rest are marked as un-used and discarded.
Live (in use) vs un-used objects?
An in use object (live or referenced object) means that some part of your program still maintains a pointer to that object.
An unused object (unreferenced object) is one which is no longer referenced by any part of your program. So the memory used by an unreferenced object can be reclaimed.
Process of Garbage Collection:
Step 1. Identify referenced and unreferenced objects
Step 2. Remove unreferenced objects. The memory allocator holds references to blocks of free space where new object can be allocated.
One could also compact the remaining objects (after deleting unreferenced objects), which would make memory allocation easier.
Time consuming?
Check all objects and mark only referenced objects could be time consuming.
Many objects actually have very short life and hence we have concept of JVM Generations.
JVM Generations:
The fewer objects that are alive, the less there is to be marked.
This is achieved by generational heap.
The Heap is divided into
Objects are usually created in the young area. Once an object has survived a couple of GC cycles it is tenured to the old generation.
The Permanent generation contains metadata required by the JVM to describe the classes and methods used in the application.
The permanent generation is populated by the JVM at runtime based on classes in use by the application.
In addition, Java SE library classes and methods may be stored here.
Automatic garbage collection process involves following steps:
- Looking at heap memory
- Find objects in use
- Delete the unused objects.
In garbage collection, live (in use) objects are tracked and the rest are marked as un-used and discarded.
Live (in use) vs un-used objects?
An in use object (live or referenced object) means that some part of your program still maintains a pointer to that object.
An unused object (unreferenced object) is one which is no longer referenced by any part of your program. So the memory used by an unreferenced object can be reclaimed.
Process of Garbage Collection:
Step 1. Identify referenced and unreferenced objects
Step 2. Remove unreferenced objects. The memory allocator holds references to blocks of free space where new object can be allocated.
One could also compact the remaining objects (after deleting unreferenced objects), which would make memory allocation easier.
Time consuming?
Check all objects and mark only referenced objects could be time consuming.
Many objects actually have very short life and hence we have concept of JVM Generations.
JVM Generations:
Memory Heap is categorized into smaller parts (generations).
One needs to ensure that there are fewer objects during the garbage collection phase to make the garbage collection process faster.
One needs to ensure that there are fewer objects during the garbage collection phase to make the garbage collection process faster.
The fewer objects that are alive, the less there is to be marked.
This is achieved by generational heap.
The Heap is divided into
- Young generation
- Old (Tenured) generation
- Permanent generation
Objects are usually created in the young area. Once an object has survived a couple of GC cycles it is tenured to the old generation.
Most allocated objects will not survive their first or second GC cycle.
If the old generation is not growing and therefore not running out of space, it requires no garbage-collection at all. There will be unreachable objects in the old generation, but as long as the memory is not needed, there is no reason to reclaim it.
If the old generation is not growing and therefore not running out of space, it requires no garbage-collection at all. There will be unreachable objects in the old generation, but as long as the memory is not needed, there is no reason to reclaim it.
To make this generational approach work, the young generation must be big enough to ensure that all temporary objects die there.
GC in young generation is called minor GC and in old generation is called major GC.
Both minor and major GC are stop-the-world Events, which means that all the application threads are stopped till the operation completes.
The Permanent generation contains metadata required by the JVM to describe the classes and methods used in the application.
The permanent generation is populated by the JVM at runtime based on classes in use by the application.
In addition, Java SE library classes and methods may be stored here.
No comments:
Post a Comment