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?
Answers were Sorted based on User's Feedback
Answer / namita
First We cannot force the garbage collection to garbage the
object. Garbage collection can never be forced.
So by calling System.gc() will not ensure you that the
object will be garbage collected.
| Is This Answer Correct ? | 2 Yes | 0 No |
Answer / 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 |
Answer / 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 |
Answer / 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 |
Can we write multiple catch blocks under single try block?
How does java enable high performance?
Is it possible to use Semaphore/ Mutex in an Interrupt Handler?
Explain the importance of join() method in thread class?
What is method reference?
what are the major differences between jdk1.4 and jdk1.5?
2 Answers Cap Gemini, Crimson Logic, Infosys, TCS, Tenth Planet,
How do you check if two strings are equal in java?
How hashset works internally in java?
Suppose if we have variable ' I ' in run method, if I can create one or more thread each thread will occupy a separate copy or same variable will be shared?
What are the notations in Java?
Is it possible to use string in the switch case?
What is the role of garbage collector in java?