Java Garbage Collection Simplified

Java Garbage CollectionGarbage 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:

  • Major
  • Minor

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 –

  1. suppose you start your application as –
    java HelloWorld
  2. 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.
Get our Articles via Email. Enter your email address.

You may also like...

2 Comments

  1. Anjani says:

    nice concept provided in this tutorial about GC.
    I have a query :
    Enable Sweeping
    allow classes to be unloaded so your PermGen never runs out:

    -XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled

    where should I write the above command?
    Actually my program is showing sometimes permgen space outofmemory error , so i want to enable permGenSweepingEnable.
    I have windows server with tomcat 7 n java 7

    Thanks & Regards,
    Anjani

  2. Mark says:

    Very helpful — clear and brief. Thank you for posting.

Leave a Reply

Your email address will not be published. Required fields are marked *