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


Please Help Members By Posting Answers For Below Questions

Will the compiler creates a default constructor if I have a parameterized constructor in the class?

582


How many bytes is a string?

558


What is the Difference between Final Class && Abstract Class?

613


Why put method is idempotent?

457


Can singleton class be inherited in java?

513






Discuss different types of errors that generally occur while programming.

575


What is meant by binding in rmi?

546


What is contract between hashcode and equal method?

566


What do you understand by garbage collection in Java? Can it be forced to run?

550


What is java command?

567


What’s meant by anonymous class?

594


what is aggregation in java?

584


Under what conditions is an object’s finalize() method invoked by the garbage collector?

573


What is difference between char array and string?

525


Is arraylist ordered in java?

570