InterviewSolution
| 1. |
How Do You Trigger Garbage Collection From Java Code? |
|
Answer» You, as Java PROGRAMMER, can not force garbage collection in Java; it will only trigger if JVM THINKS it needs a garbage collection based on Java heap size. Before removing an object from MEMORY garbage collection thread invokes finalize()METHOD of that object and gives an opportunity to perform any sort of cleanup required. You can also invoke this method of an object code, however, there is no guarantee that garbage collection will occur when you call this method. Additionally, there are methods like System.gc() and Runtime.gc() which is used to send REQUEST of Garbage collection to JVM but it’s not guaranteed that garbage collection will happen. You, as Java programmer, can not force garbage collection in Java; it will only trigger if JVM thinks it needs a garbage collection based on Java heap size. Before removing an object from memory garbage collection thread invokes finalize()method of that object and gives an opportunity to perform any sort of cleanup required. You can also invoke this method of an object code, however, there is no guarantee that garbage collection will occur when you call this method. Additionally, there are methods like System.gc() and Runtime.gc() which is used to send request of Garbage collection to JVM but it’s not guaranteed that garbage collection will happen. |
|