When does System.gc() do something?

后端 未结 16 2174
傲寒
傲寒 2020-11-22 05:02

I know that garbage collection is automated in Java. But I understood that if you call System.gc() in your code that the JVM may or may not decide to perform ga

16条回答
  •  梦如初夏
    2020-11-22 05:40

    System.gc() is implemented by the VM, and what it does is implementation specific. The implementer could simply return and do nothing, for instance.

    As for when to issue a manual collect, the only time when you may want to do this is when you abandon a large collection containing loads of smaller collections--a Map> for instance--and you want to try and take the perf hit then and there, but for the most part, you shouldn't worry about it. The GC knows better than you--sadly--most of the time.

提交回复
热议问题