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 / 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 |
Post New Answer View All Answers
Can a source file contain more than one class declaration?
Is there any sort function in java?
What is an enumeration?
What is a ternary operator in java?
Give a brief description of java socket programming?
What is foreach loop in java?
Explain importance of throws keyword in java?
When will we use them?
What is basic syntax?
Difference between ‘is-a’ and ‘has-a’ relationship in java?
Can we use catch statement for checked exceptions when there is no chance of raising exception in our code?
What is the role of garbage collector in java?
how we can use debug in myeclipse 6.0 in order solve the problems that exist in our program when there are 900 to 1000 pages in a web application
can used Protected Class outside Function.?
What is a method in coding?