Garbage collection is the method through which Java programs maintain their memory automatically.
working:
There are numerous options:
Student s=new Student();
s=null;
Student s1=new Student();
Student s2=new Student();
s1=s2;//now the first object referred by e1 is available for garbage collection
new Student();
Before the object is garbage collected, the finalize() function is called. This strategy can be used to tidy up your data. This method is defined as follows in the Object class:
protected void finalize(){}
Some Key Points to Keep in Mind:
The garbage collector is called using the gc() method, which performs cleanup operations. The gc() method is available in the System and Runtime classes.
public static void gc(){}
public class TestGarbage1
{
public void finalize()
{
System.out.println("object is garbage collected");
}
public static void main(String args[])
{
TestGarbage1 s1=new TestGarbage1();
TestGarbage1 s2=new TestGarbage1();
s1=null;
s2=null;
System.gc();
}
}
object is garbage collected
object is garbage collected
Note: also read about this keyword in Java
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…