public class Garbage
{
int a=0;
public void add()
{
int c=10+20;
System.out.println(c);
System.out.println(a);
}
public static void main(String args[])
{
Garbage obj=new Garbage();
System.gc();
System.out.println("Garbage Collected");
obj.add();
}
}
Above is a code in java used for garbage collection. object
obj has been created for the class Garbage and system.gc
method is called. Then using that object add method is
called.System.gc method if called the obj should be garbage
collected?

Answer Posted / namita

First We cannot force the garbage collection to garbage the
object. Garbage collection can never be forced.

So by calling System.gc() will not ensure you that the
object will be garbage collected.

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How to convert string to byte array and vice versa?

588


what is thread in Java ?

631


What is the difference between the file and randomaccessfile classes?

539


How do you use compareto in java?

566


Which is fastest collection in java?

566






What are void methods?

576


What is bufferedwriter?

542


Explain runtime exceptions?

749


How objects of a class are created if no constructor is defined in the class?

583


What is the final field modifier?

550


What is skeleton and stub?

594


What is files manifesting?

1736


What is main difference between variable and constant?

536


What are the basic control structures?

501


What is the difference between equals() and == in java?

531