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

According to java operator precedence, which operator is considered to be with highest precedence?

606


What is meant by 'bit masking' in java?

651


How many bits is a 64 bit byte?

538


Program to Find the second largest element in an array.

591


What does the three dot emoji mean?

588






Can I learn java in 3 months?

546


How can we avoid including a header more than once?

614


What are the different collection views provided by maps?

573


What is the difference between stream and buffer?

521


What is set string?

599


Why are the objects immutable in java?

550


Is hashset ordered java?

590


What is object-oriented programming?

574


Can list be final in java?

491


What are the restrictions that are applied to the java static methods?

533