JVM crashes with error='Cannot allocate memory' (errno=12)

后端 未结 3 1206
暖寄归人
暖寄归人 2020-12-09 17:11

My code crashes with this error message

Executing \"/usr/bin/java  com.utils.BotFilter\"
OpenJDK 64-Bit Server VM warning: INFO: 
os::commit_memory(0x0000000         


        
相关标签:
3条回答
  • 2020-12-09 17:50

    Another possibility (which I encountered just now) would be bad settings for "overcommit memory" on linux.

    In my situation, /proc/sys/vm/overcommit_memory was set to "2" and /proc/sys/vm/overcommit_ratio to "50" , meaning "don't ever overcommit and only allow allocation of 50% of the available RAM+Swap".

    That's a pretty deceptive problem, since there can be a lot of memory available, but allocations still fail for apparently no reason.

    The settings can be changed to the default (overcommit in a sensible way) for now (until a restart):

    echo 0 >/proc/sys/vm/overcommit_memory
    

    ... or permanently:

    echo "vm.overcommit_memory=0 >> /etc/sysctl.conf
    sysctl -p /etc/sysctl.conf # apply it immediately
    

    Note: this can also partly be diagnosed by looking at the output of /proc/meminfo:

    ...
    CommitLimit:    45329388 kB
    Committed_AS:   44818080 kB 
    ...
    

    In the example in the question, Committed_AS is much higher than CommitLimit, indicating (together with the fact that allocations fail) that overcommit is enabled, while here both values are close together, meaning that the limit is strictly enforced.

    An excellent detailed explanation of these settings and their effect (as well as when it makes sense to modify them) can be found in this pivotal blog entry. (Tl;dr: messing with overcommit is useful if you don't want critical processes to use swap)

    0 讨论(0)
  • 2020-12-09 17:56

    As Scary Wombat mentions, the JVM is trying to allocate 2712666112 bytes (2.7 Gb) of memory, and you only have 691424000 bytes (0.69 Gb) of free physical memory and nothing available on the swap.

    0 讨论(0)
  • 2020-12-09 18:00

    Possible solutions:

    • Reduce memory load on the system
    • Increase physical memory or swap space
    • Check if swap backing store is full
    • Use 64 bit Java on a 64 bit OS
    • Decrease Java heap size (-Xmx/-Xms)
    • Decrease number of Java threads
    • Decrease Java thread stack sizes (-Xss)
    • Set larger code cache with -XX:ReservedCodeCacheSize=
    • In case you have many contexts wars deployed on your tomcat try reduce them
    0 讨论(0)
提交回复
热议问题