When does System.gc() do something?

后端 未结 16 2151
傲寒
傲寒 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:54

    Normally, the VM would do a garbage collection automatically before throwing an OutOfMemoryException, so adding an explicit call shouldn't help except in that it perhaps moves the performance hit to an earlier moment in time.

    However, I think I encountered a case where it might be relevant. I'm not sure though, as I have yet to test whether it has any effect:

    When you memory-map a file, I believe the map() call throws an IOException when a large enough block of memory is not available. A garbage collection just before the map() file might help prevent that, I think. What do you think?

提交回复
热议问题