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 / madhu
Garbage Collector is called by the JVM. and it will collect
the objects which has no reference, in the above case obj
has reachability,so JVM won't force the garbage collector to
collect the obj object.
Is This Answer Correct ? | 2 Yes | 0 No |
Post New Answer View All Answers
Will the compiler creates a default constructor if I have a parameterized constructor in the class?
How many bytes is a string?
What is the Difference between Final Class && Abstract Class?
Why put method is idempotent?
Can singleton class be inherited in java?
Discuss different types of errors that generally occur while programming.
What is meant by binding in rmi?
What is contract between hashcode and equal method?
What do you understand by garbage collection in Java? Can it be forced to run?
What is java command?
What’s meant by anonymous class?
what is aggregation in java?
Under what conditions is an object’s finalize() method invoked by the garbage collector?
What is difference between char array and string?
Is arraylist ordered in java?