Garbage collection is a way in which Java recollects the space occupied by loitering objects. By doing so, it [Java] ensures that your application never runs out of memory (though we cannot be assured that the program will ever run out of memory).
The garbage collection process is primarily governed by the configuration parameters of the heap. (Heap is that part of the physical memory which is used by the JVM to create objects). The configuration parameters of the heap are – Xms & Xmx. These can be used as –
java –Xms:256m Xmx:1g
Related: Set JVM Heap Size
This indicates that the minimum heap size (indicated by Xms) is 256Mb & the maximum heap size (indicated by Xmx) is 1024Mb.
Java Garbage Collection types
The garbage collection can be of 2 types namely:
To understand the concept of major/minor gc, we need to understand the concept of young generation.
Young Generation is the pool of temporary objects which are not fully garbage collected initially. When these objects become old, they become part of the Old generation (This is referred as Minor GC) which are then fully garbage collected (referred as Major GC).
How to identify Major/Minor GC?
The gc type can be identified using a java option as follows –
- suppose you start your application as –
to determine garbage collection, start your app as –
java –verbose:gc HelloWorld
The output of above change should be something like –
GC 325407K->83000K(776768K), 0.2300771 secs
- GC – Indicates that it was a minor collection (young generation). If it had said Full GC then that indicates that it was a major collection (tenured generation).
- 325407K – The combined size of live objects before garbage collection.
- 83000K – The combined size of live objects after garbage collection.
- (776768K) – the total available space, not counting the space in the permanent generation, which is the total heap minus one of the survivor spaces.
- 0.2300771 secs – time it took for garbage collection to occur.