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

What is a newline character in java?

563


Can we inherit a class with private constructor?

566


what is synchronization? : Java thread

561


Which is better arraylist or vector?

567


What is the exception hierarchy in java?

483






Can substring create new object?

556


Is java developer a good career?

550


What do you understand by copy constructor in java?

490


What is lifetime variable?

521


Discuss 2D arrays.

607


Where is core java used?

577


What does math floor () do?

547


What is java objectoutputstream?

541


What are basic data types?

559


What is keyword in oop?

516