How to convince Java Garbage Collector to run when working set is small?

后端 未结 4 1215
北海茫月
北海茫月 2021-02-01 18:32

This is yet another \"please tell me how to force the Java garbage collector to run\" question. In our application, I believe we have good reasons for doing this.

This i

4条回答
  •  遇见更好的自我
    2021-02-01 19:06

    Consider using non-managed memory, i.e., ByteBuffers in place of the byte arrays.

    I can only offer a hack which will need some tuning and then might or might not work. I'd first try the more sane solutions. When you want to force the GC, do it by allocating a lot of memory. Do this so that the memory can be immediately reclaimed, but so that the whole allocation can't be optimized away (something like sum += new byte[123456].hashCode() should do). You'll need to find a reliable method for determining when to stop. An object with a finalizer might tell you or maybe watching runtime.getFreeMemory could help.

提交回复
热议问题