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
Maverickhari,
Garbage collector is system created thread which runs
automatically.
We are not sure when the garbage collection is going to
happen. this totally depends upon the JVM. Like connection
pool all the the objects are created in pool JVM will check
if there is no free memory in pool then it searches for the
objects which are no longer in use and will garbage collect
that and allocate to some other object.
hope this will clear
| Is This Answer Correct ? | 1 Yes | 0 No |
Post New Answer View All Answers
What are the rules for variable declaration?
What package is math in java?
Explain method overloading?
Java Compiler is stored in JDK, JRE or JVM?
Discuss about garbage collector in Java.
Can we execute java program without main method?
What is singletonlist in java?
What is function and method in java?
How do you sing an Applet ?
Explain about static nested classes in java?
Can we define package statement after import statement in java?
What is an interface in java? Explain
What is array length?
What restrictions are placed on method overloading in java programming?
What is difference between null and void?