gc() method

The gc() method is used to invoke the garbage collector to perform cleanup processing. The gc() is found in System and Runtime classes.


\begin{lstlisting}
public static void gc(){}
\end{lstlisting}

Garbage collection is performed by a daemon thread called Garbage Collector(GC). This thread calls the finalize() method before object is garbage collected. Following is the simple Example of garbage collection in java.


\begin{lstlisting}
public class TestGarbage1{
public void finalize(){System.ou...
...ew TestGarbage1();
s1=null;
s2=null;
System.gc();
}
}
\end{lstlisting}
Output
\begin{lstlisting}
object is garbage collected
object is garbage collected
\end{lstlisting}