Java gc()

Amansingh Javatpoint
1 min readJun 18, 2021

--

Garbage collection

Java language provides different ways to perform the task of memory management. In Java, objects are declared and assigned references. Once lifeof an object is completed it is dereferenced and destroyed to free up memory space. The task is performed by a garbage collector that resides inside the JVM (Java Virtual Machine). When dereferenced objects are shifted from the young generation to the old generation, the garbage collector is executed.

The JVM can perform garbage collection operation during the execution program. But we don’t know when it’s going to be executed. Hence, we can make JVM run a garbage collector at least once throughout the execution of the program by using System.gc() method.

How to mark an object for garbage collection?

There are different ways to make an unused object eligible for garbage collection.

  1. By making a reference null
  2. Assigning an old reference to new reference
  3. With the use of anonymous object

--

--

No responses yet