Does GC release back memory to OS?

前端 未结 5 1030
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 08:44

When the garbage collector runs and releases memory does this memory go back to the OS or is it being kept as part of the process. I was under the strong impression that the

5条回答
  •  死守一世寂寞
    2020-11-22 09:21

    If you use the G1 collector and call System.gc() occasionally (I do it once a minute), Java will reliably shrink the heap and give memory back to the OS.

    Since Java 12, G1 does this automatically if the application is idle.

    I recommend using these options combined with the above suggestion for a very compact resident process size:

    -XX:+UseG1GC -XX:MaxHeapFreeRatio=30 -XX:MinHeapFreeRatio=10
    

    Been using these options daily for months with a big application (a whole Java-based guest OS) with dynamically loading and unloading classes - and the Java process almost always stays between 400 and 800 MB.

提交回复
热议问题