Garbage Collection

This is a very interesting and important to understand the role of garbage collection in Java.

When the JVM starts up, it creates a heap memory area, which is also called a dynamic and runtime data area. All the objects and instances will be stored in the heap area.

It works well for managing heap memory effectively. It collects all the unreferenced data, destroys it and frees the memory.

System.gc()

We can request it to run using the System.gc() method.

finalize()

Every time an object is garbage collected, the finalize() method is called.

It will totally depend on the garbage collector when to run. below is the example for the same:

Syntax:

OUTPUT:

Below are the options to make any object eligible for garbage collection:

  1. Assign object to null value:

2. Anonymous objects are always ready for collecting garbage.

3. An object will be ready for collecting garbage if it is no longer in use.

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan