In Java, there is a method called System.gc()
, which calls the garbage collector (or rather, “suggests” to the JVM to run the GC).
Are there valid reasons to call this method? I’m thinking that it would be evident of bad software design.
2
How about a benchmarking suite? Rather than letting the GC run at any random time, you might want to make it run between sets of tests, to (possibly) create more consistent conditions for each set.
5
What about a game where you want to control gc pauses. You could then call gc right before a level starts after assets are loaded.
1
In general, there isn’t a good reason to call System.gc(). Especially since the system might ignore it. But here are some plausible reasons.
- As mentioned, in a benchmark
- You just freed up a ton of large Objects and otherwise “things are quiet”, the queues are empty, etc… Now is a good time to get the maintenance work done.
- You are about to allocate a ton of large Objects and otherwise “things are quiet”.
- You are about to enter speed sensitive code where low latency is desired. Get the garbage collection “out of the way” now, before the climactic fight vs. the super-fast 28 Days Later Zombies in 3D and 4K resolution. 🙂