JVM HeapMemory中Used, Committed and Max的區別

首先要明確的是used < committed < max,單位是bytes;


  • init:JVM啓動時從操作系統申請的初始內存,也即JVM參數中-Xms設置的值
  • used: 實際使用的內存,包括未被垃圾回收期回收的不可達對象佔用的內存,它可以比初始(init)內存小
  • committed:操作系統層面爲當前JVM進程保留的內存
    • 可能等於或大於used內存;JVM可以從操作系統申請很多內存,但是不一定真正使用它,但是操作系統可以爲java進程保留相關內存
    • 可能小於init內存,因爲JVM可以回收內存並將其歸還給操作系統
    • 如果JVM需要更多內存,它將嘗試從操作系統申請,此時commited會變大
    • 如果創建一個新對象,並且此時used < committed,這時JVM不需要直接從操作系統申請內存,而是直接使用已經申請好的committed內存,確保新對象能創建成功
    • 如果創建一個新對象,並且總內存使用量已經操作committed值,JVM在創建對象前需要向操作系統申請額外的內存,並且不保證能申請成功,可能出現OOM
  • max:JVM能從操作系統申請的最大內存
    • 該值一般通過JVM參數-Xmx設置
    • 操作系統可能不會爲JVM申請這麼多的內存,因爲操作系統還要爲其他進程保留一定的內存,此時可能會導致OOM







used < committed < max, and the unit of measure for all of them is bytes

init: the initial amount of memory that the JVM requested from the operating system at startup.
this is controlled by the -Xms cli option of the java command. See 2
used: amount of memory that is actually in use, so the memory consumed by all objects including the objects that are not reachable but haven't been garbaged collected yet.
it can be lower than init
committed: amount of memory that is reserved at the operating system level for the JVM process at the moment.
It can be equal or greater than the used, the JVM can request/allocated more memory from the OS and not really use it, but the OS is reserving that memory for the java process anyway.
it can go down, it maybe be even lower than init because the JVM can release memory back to the operating system.
if the JVM needs more memory will try to allocate more from the OS and then the committed will go up, but it's always possible that the OS runs of memory even if the amount of memory requested is lower that max
if you are trying to create new objects and the used < committed then the JVM does not need to request more memory from the OS and thus is guaranteed that it will succeed.
if you are trying to create new objects and the total amount of memory will go over committed then the JVM needs to allocate more memory from the OS before creating the object, that is not guaranteed to succeed (the OS may also run out of memory)
max: the maximum amount of memory that the JVM will ever try to request / allocate from the operating system
controlled by the -Xmx cli options. See 2
it is not guaranteed that the JVM will be able to allocate this much, the operating system may run out of memory because other processes reserved it.
So in the OP example

used is 3.8G
committed and max are 8.6G
that means that the JVM can allocate objects in the heap up to 8.6G and that is guaranteed, it won't have to ask the operating system for that since it was already allocated. If the JVM at some point requires more memory than that, because it needs to allocate more objects and it can't release any memory via garbage collection then it will fail with OOM since 8.6G is already the max it's allowed to request (I guess because it was started with -Xmx8600M.


還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.