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
According to java operator precedence, which operator is considered to be with highest precedence?
What is meant by 'bit masking' in java?
How many bits is a 64 bit byte?
Program to Find the second largest element in an array.
What does the three dot emoji mean?
Can I learn java in 3 months?
How can we avoid including a header more than once?
What are the different collection views provided by maps?
What is the difference between stream and buffer?
What is set string?
Why are the objects immutable in java?
Is hashset ordered java?
What is object-oriented programming?
Can list be final in java?
What are the restrictions that are applied to the java static methods?