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

后端 未结 4 1203
北海茫月
北海茫月 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:10

    Rather than answer your question directly (I can't), I'd like to offer a possible alternative.

    It sounds like you are allocating a large number of large byte arrays during your analysis run, and then allowing them to be garbage collected at the end of the run (or attempting to force them to be garbage collected just before the next run).

    Instead, if possible, try managing your own pool of byte arrays, so that, in the best case, you allocate all of the needed arrays once when the application is first started, and then they live for the lifetime of the application, and don't need to garbage collected.

    This idea can, of course, be extended to more complex data structures and object instances.

    This is all quite a bit more work than just allocating memory when you need it, and 'freeing' it when you don't, but should cut down considerably on the work that the garbage collector needs to do.

提交回复
热议问题