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
What is the use of join method?
Difference between operator overloading and function overloading
What is meant by call by reference?
How many bits is a string?
What is the hashcode () and equals () used for?
What is an accessor?
What is a modifier?
What are the types of inner classes (non-static nested class) used in java?
Can we write multiple catch blocks under single try block?
How to create an immutable class?
Why synchronization is important?
What modifiers may be used with an inner class that is a member of an outer class in java programming?
Can we override tostring method in java?
When the constructor of a class is invoked?
What is mean by collections in java?