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
What is lifetime variable?
Why does java have two ways to create child threads?
How big is a gigabyte?
What is meant by JVM? Is JVM platform independent or not?
How does regex work?
What is the purpose of using break in each case of switch statement?
Write a java program to find the route that connects between Red and Green Cells. General Rules for traversal 1. You can traverse from one cell to another vertically, horizontally or diagonally. 2. You cannot traverse through Black cells. 3. There should be only one Red and Green cell and at least one of each should be present. Otherwise the array is invalid. 4. You cannot revisit a cell that you have already traversed. 5. The maze need not be in the same as given in the above example
What are java threads?
Which one of the following suits the description of a string better: derived or primitive?
Is hashmap thread safe?
What's the base class in java from which all classes are derived?
What is Java Annotations?
Explain the private field modifier?
Why is a constant variable important?
What is the purpose of using javap?