How can I force garbage collection to take place?

Finlay McWalter

Strictly, you can't force it.

You can, however, call System.gc(), which is a "hint" to the runtime engine that now might be a good time to run the GC. Some implementations take that hint to heart and some don't.