System.gc() vs GC button in JVisualVM/JConsole

后端 未结 4 795
醉话见心
醉话见心 2021-02-09 17:33

I\'m currently testing my proof of concept prototype dealing with XML schema, and built around a very memory consuming external library for tree automata (for which I\'ve got th

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-09 18:19

    I have quite a bit of positive experience with this trivial approach:

    System.gc();
    Thread.sleep(500);
    System.gc();
    

    One GC run is often not enough due to object finalization issues, where an object may be resurrected in finalization. Therefore additional memory is released in the second GC run.

    Do note that this, as well as ony other, seemingly "smarter", approaches, are all heuristics and quite dependant on the exact version of JVM, including its GC configuration. But in many cases you will not be so much interested in generality: if it works right now and allows you to do your measurements, it is the way to go.

提交回复
热议问题