Garbage Collection

  • The JVM specification states that garbage collection must occur on the heap, but it does not specify how this is to happen (i.e. reference counting vs. mark and sweep)
  • Current JVMs typically use mark and sweep.
  • The garbage collector runs periodically, freeing up unreferenced objects.
  • There is no guarantee when garbage collection will occur.
  • Setting an object reference to null may speed up garbage collection.
  • The garbage collector is responsible for running the objects' finalize method before freeing it.

Explicitly requesting garbage collection


Request garbage collection, if possible, if there is no other thread with a higher priority on the whole computer.
System.gc();
Runtime.gc();