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 / bhudeep

Java provides us one method System.gc()to call garbage
collection forcefully.But by calling System.gc() will not
ensure you that the object will be garbage collected.

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is meant by singleton class?

809


Write a java program to check if a number is prime or not?

774


Is null or empty java?

765


Is it possible to use Semaphore/ Mutex in an Interrupt Handler?

771


Can we override constructor?

786


How do you clear an arraylist in java?

724


Why does the integer quotient -0/3 yield 0, but the double quotient -0.0/3.0 yields – 0.0?

836


What if the main() method is declared as private? What happens when the static modifier is removed from the signature of the main() method?

951


how to one war file class to another war file class?

1790


take an array with -ve and +ve value both.find out the nearest value of 0(zero).if two values are same like(-2 and +2)then extract +2 is nearest of 0(zero).

1782


What modifiers are allowed for methods in an interface?

894


Why packages are used?

889


How many threads does a core java have?

797


Why there are some null interface in JAVA? What does it mean? Give some null interface in JAVA?

869


What do you know about the garbage collector in java?

831