# \[Tutorial\] What Is Garbage Collection In Java With Example!

**URL:** <https://onehack.st/t/tutorial-what-is-garbage-collection-in-java-with-example/58427>\
**Category:** Tutorials & Methods\
**Tags:** freebies, tips-tricks\
**Created:** [September 21, 2019, 11:38am UTC](https://onehack.st/t/tutorial-what-is-garbage-collection-in-java-with-example/58427 "2019-09-21T11:38:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aina](https://onehack.st/user_avatar/onehack.st/aina/32/167469_2.png) [@Aina](https://onehack.st/u/Aina)\
**Post date:** [September 21, 2019, 11:38am UTC](https://onehack.st/t/tutorial-what-is-garbage-collection-in-java-with-example/58427/1 "2019-09-21T11:38:31Z")

</div>

## What is Garbage Collector?

Garbage Collector is a program that manages memory automatically wherein de-allocation of objects is handled by Java rather than the programmer. In the Java programming language, dynamic allocation of objects is achieved using the **new** operator. An object once created uses some memory and the memory remains allocated till there are references for the use of the object.

When there are no references to an object, it is assumed to be no longer needed, and the memory, occupied by the object can be reclaimed. There is no explicit need to destroy an object as Java handles the de-allocation automatically.

The technique that accomplishes this is known as **Garbage Collection**. Programs that do not de-allocate memory can eventually crash when there is no memory left in the system to allocate. These programs are said to have _memory leaks._

**Garbage collection in Java happens automatically** during the lifetime of the program, eliminating the need to de-allocate memory and thereby avoiding memory leaks. In C language, it is the programmer’s responsibility to de-allocate memory allocated dynamically using free() function. This is where Java memory management leads.

Note: All objects are created in **Heap** Section of memory. More on this in a later tutorial.

## Example: To Learn Garbage Collector Mechanism in Java

**Step 1)** Copy the following code into an editor.

```auto
class Student{
int a;
int b;

  public void setData(int c,int d){
    a=c;
    b=d;
  }
  public void showData(){
    System.out.println("Value of a = "+a);
    System.out.println("Value of b = "+b);
  }
  public static void main(String args[]){
    Student s1 = new Student();
    Student s2 = new Student();
    s1.setData(1,2);
    s2.setData(3,4);
    s1.showData();
    s2.showData();
    //Student s3;
    //s3=s2;
    //s3.showData();
    //s2=null;
    //s3.showData();
    //s3=null;
    //s3.showData();
  }
}
```

**Step 2)** Save, Compile and Run the code. As shown in the diagram, two objects and two reference variables are created.

 ![1](https://onehack.st/uploads/default/original/3X/9/c/9c29865e7a1c76de6222b7e3bdd4707687807e1a.jpeg)

**Step 3)** Uncomment line # 20,21,22. Save, compile & run the code.

**Step 4)** As shown in the diagram below, two reference variables are pointing to the same object.

 ![2](https://onehack.st/uploads/default/original/3X/7/9/79137f19437c4ea20600b813f2a91a6f66623c55.jpeg)

**Step 5)** Uncomment line # 23 & 24. Compile, Save & Run the code

**Step 6)** As show in diagram below, s2 becomes null, but s3 is still pointing to the object and is not eligible for java garbage collection.

 ![3](https://onehack.st/uploads/default/original/3X/d/3/d3aacff50262d3259f5a8c8bb394cf81a6478f3c.jpeg)

**Step 7)** Uncomment line # 25 & 26. Save, Compile & Run the Code

**Step 8)** At this point there are no references pointing to the object and becomes eligible for garbage collection. It will be removed from memory, and there is no way of retrieving it back.

 ![4](https://onehack.st/uploads/default/original/3X/f/f/ff833c567d24e87dfdc6adf3d28798aa8e717959.jpeg)

## How to delete an object in Java?

1. If you want to make your object eligible for Garbage Collection, assign its reference variable to null.

2. Primitive types are not objects. They cannot be assigned null.

 ![5](https://onehack.st/uploads/default/original/3X/b/9/b9f38f0d6857b81c3fd7a22f4fdceb76b4729ad2.jpeg)

## ENJOY & HAPPY LEARNING! 👍

---

<div class="post-metadata">

**Author:** ![Ak\_Malviya](https://onehack.st/user_avatar/onehack.st/ak_malviya/32/23188_2.png) [@Ak\_Malviya](https://onehack.st/u/Ak_Malviya)\
**Post date:** [September 21, 2019, 5:20pm UTC](https://onehack.st/t/tutorial-what-is-garbage-collection-in-java-with-example/58427/2 "2019-09-21T17:20:55Z")

</div>

Thank you @Aina

---

<div class="post-metadata">

**Author:** ![arzak21st](https://onehack.st/user_avatar/onehack.st/arzak21st/32/134283_2.png) [@arzak21st](https://onehack.st/u/arzak21st)\
**Post date:** [May 6, 2024, 2:00pm UTC](https://onehack.st/t/tutorial-what-is-garbage-collection-in-java-with-example/58427/3 "2024-05-06T14:00:03Z")

</div>

I’m a Java developer and I have never came across such wonderful and clear explanations.  
Thank you @Aina ! I really appreciate your effort.
