JVM and Garbage Collections Interview Questions and Answers - 1

Question: 1

What is Garbage Collection and how to call it explicitly?

When an object is no longer referred to by any variable, java automatically reclaims memory used by that object.

This is known as garbage collection.

system.gc() method may be used to call it explicitly.

Question: 2

What is the purpose of finalization?

The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected.

Question: 3

What is finalize() method?

finalize() method is used just before an object is destroyed and can be called just prior to garbage collection.

Question: 4

Does garbage collection guarantee that a perform will not run out of memory?

Garbage collection does not guarantee that a program will not run out of memory. As garbage collection is JVM dependent then it is possible for programs to use memory resources faster than they are garbage collected.

Moreover garbage method will be run before an object is Garbage collected, it is called by the garbage collector on an object when garbage collection determines that there no more references to the object.

The garbage collection is controlled, it means you cannot predict when it will happen, you thus cannot predict exactly when the finalize method will run.

Once a variable is no longer referenced by anything it is available for garbage collection

Question: 5

When is an object subject to garbage collection?

An object is subject to garbage collection when it becomes unreachable to the program in which it is used.

Related Questions