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.

  

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