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

前端 未结 5 1802
难免孤独
难免孤独 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:12

    Suppose I have a Linux machine which has 1 Gb of memory which it can allocate to processes (physical and swap totals 1 Gb).

    My first response would be, unless you are talking about a phone, I would get more memory. You can buy 16 GB (b = bit, B = byte) for less than $100.

    does the JVM fail to start right away because it will try to allocate all of the 512 Mb of memory and fail (due to the fact that there's not enough available memory at the moment)?

    This can happen if your system does not have 512 MB (plus some overhead) as it allocates the continuous virtual memory used for the heap on startup.

    Even if you have 550 MB free, the program could fail to start as it need to load more than just the heap.

    will the Java process fail with an OutOfMemoryError?

    This can happen if your program uses 512 MB while running, regardless of the amount of memory your machine has. This error will only occur after your JVM has started. You won't get this error if it cannot start.

    will it fail with some other (standard) error?

    This is possible if you run out of swap space after the program has started. It is rare and only happens on a severely overloaded machine. What I have seen is the JVM crashes due to a low level OS failure to allocate memory.

    Java 6 Update 25 VM crash: insufficient memory

    0 讨论(0)
  • 2020-12-15 06:20

    The JVM process will run in virtual memory, so the question of allocation of other processes running is relevant, but not completely determinitive.

    When the JVM cannot allocate more memory (for whatever reason), the process itself doesn't terminate, but rather starts throwing OutOfMemoryError within the JVM, but not external to the JVM. In other words, the JVM continues to run, but the programs running within the JVM will usually fail, because most don't handle low memory conditions adequately. In this fairly common case, when the program does not do anything to handle the error, the JVM will terminate the program and exit. Ultimately, this is from the memory allocation, but not directly so. It's possible for a piece of code to scale itself back under low memory condition and continue to run.

    And others have pointed out, it happens sometimes that the JVM itself doesn't handle low memory well, but this is a pretty extreme condition.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-15 06:26

    -Xmx just defines the maximum size of the heap. It makes no guarantee on wether there is so much memory or not. It only ensures that the heap will never be bigger then the given value. That said, Option B.) will happen, an outOfMemoryError will be thrown.

    0 讨论(0)
  • 2020-12-15 06:27

    OutOfMemroyError will be "Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector."

    So, in essence, "B. The Java process fail with an OutOfMemroyError".

    0 讨论(0)
提交回复
热议问题