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 / 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 |
Post New Answer View All Answers
Give example to differentiate between call by value and call by reference.
What is composition in java?
What is the disadvantage of synchronization?
Write a program to print the pattern given below
Discuss about garbage collector in Java.
Why runnable interface is used in java?
Why string is called as immutable?
What is the access scope of a protected method?
What is the difference between array and array list in java?
Is java type safe?
Can static method access instance variables ?
How many ways can we create the string object?
What is difference between hashset and hashmap in java?
What is slash r?
How do you compare two objects?