Java garbage collection

前端 未结 7 2151
生来不讨喜
生来不讨喜 2020-12-05 09:44

Java automatically calls garbage collector, then why we need manual calls for garbage collection? When should use System.gc()

7条回答
  •  悲哀的现实
    2020-12-05 10:03

    There isn't a need to call for garbage collection explicitly and calling System.gc() is only a suggestion, the JVM can ignore your suggestion.

    The only practical uses I can think of is

    1. During debugging, forcing a collection can expose a memory leak
    2. If the program goes through predictable cycles of intense computation followed by no computation (like a turn based game), during the no-computation period the CPU could be utilized for a suggested garbage collection to prevent jitter during the intense computation portions.

提交回复
热议问题