Java Garbage Collection Simplified

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:

  • 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.

View Comments

  • 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

Share
Published by
Viral Patel
Tags: garbage collection java class file JVM jvm class format

Recent Posts

  • Java

Java URL Encoder/Decoder Example

Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…

4 years ago
  • General

How to Show Multiple Examples in OpenAPI Spec

Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…

4 years ago
  • General

How to Run Local WordPress using Docker

Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…

4 years ago
  • Java

Create and Validate JWT Token in Java using JJWT

1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…

4 years ago
  • Spring Boot

Spring Boot GraphQL Subscription Realtime API

GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…

4 years ago
  • Spring Boot

Spring Boot DynamoDB Integration Test using Testcontainers

1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…

4 years ago