What happens when the JVM runs out of memory to allocate during run time?

前端 未结 5 1806
难免孤独
难免孤独 2020-12-15 05:37

After thinking for a long time of a generic way to pose this question (and failing to find one) I\'m just going to ask it as a concrete example:

Suppose I have a Lin

5条回答
  •  暖寄归人
    2020-12-15 06:21

    If you have so much occupied memory that the free space cannot even sustain an idle JVM, you would get either some error saying the program has not enough memory, or the JVM would crash.

    If you can run the JVM, you can specify the limit on heap space with -Xmx. That doesn't mean all the heap will be allocated by JVM on start - it is only an internal limit. If the JVM will want to increase the heap space, but there is not enough memory, or you need more heap than specified by -Xmx, you will get OutOfMemoryError in currently running Java programs.

    In a very extreme condition, you can run out of free memory while the JVM is running, and at the same time the JVM requires more memory for its internal operation (not the heap space) - then the JVM tells you it needed more memory, but could not get any, and terminates, or it will crash outright.

提交回复
热议问题