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

Maverickhari,

Garbage collector is system created thread which runs
automatically.

We are not sure when the garbage collection is going to
happen. this totally depends upon the JVM. Like connection
pool all the the objects are created in pool JVM will check
if there is no free memory in pool then it searches for the
objects which are no longer in use and will garbage collect
that and allocate to some other object.

hope this will clear

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Mention some features of java?

546


Is singleton class immutable?

541


What are the main differences between the java platform and other platforms?

534


Can you instantiate the math class in Java?

620


How to create a base64 decoder in java8?

561






Does set allows null in java?

510


What all access modifiers are allowed for top class ?

715


What is autoboxing and unboxing?

589


Explain about complier design(phases)

632


What is mysql driver class name?

556


Can we use this () and super () in a method?

535


what is thread? What are the high-level thread states? : Java thread

794


What is the difference between this() and super() in java?

545


What are benefits of java?

562


Explain the difference between an Interface and an Abstract class?

638