Is the garbage collector guaranteed to run before Out of Memory Error?

后端 未结 5 1150
清酒与你
清酒与你 2020-12-01 21:12

In case the heap is full, the JVM throws an OutOfMemoryError. But is it assured that a (full) garbage collection always takes place before such an exception is thrown?

相关标签:
5条回答
  • 2020-12-01 21:23

    There's no guaranty that the last operation preceding OutOfMemoryError is garbage collection. Most probably not, since garbage collection would reduce the amount of used memory not increase it.

    0 讨论(0)
  • 2020-12-01 21:27

    The Java Machine Specification states in section 6.3 (emphasis mine):

    OutOfMemoryError: The Java virtual machine implementation has run out of either virtual or physical memory, and the automatic storage manager was unable to reclaim enough memory to satisfy an object creation request.

    So the JVM does give a guarantee that it will try what it can to free up memory through garbage collection before it throws an OOME.

    0 讨论(0)
  • 2020-12-01 21:28

    There are other additional factors to consider besides what people have already answered, for example the garbage collection policy you are using. Consider the throughput garbage collection, this will throw an out of memory exception if too much time is being spent collecting and not enough memory is being freed (although the recent changes might have changed things). Having said all this I would not assume garbage collection occurs at any time within the execution of an application that I'm writing...

    0 讨论(0)
  • 2020-12-01 21:32

    You are not guaranteed that a full garbage collection has been performed, but that the VM has tried to make enough memory available through garbage collection. You could have found that in the API documentation for the OutOfMemoryError class:

    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.

    Note that there are cases where the garbage collector can decide that enough memory is not available without actually trying to discard unreferenced object instances. The most obvious example is if you try to allocate more memory in one go (e.g. a large byte array) than the max heap size. In this case, an OutOfMemoryError may be thrown without the garbage collector being run at all.

    0 讨论(0)
  • 2020-12-01 21:49

    The garbage collector will usually be run before an OutOfMemoryError is thrown. However you might get an OOME without a GC if you

    • try to create a very large object (e.g. larger than the heap)
    • start a thread and there is not enough virtual memory or resources to start the thread.
    • older versions of Java would throw this error if you reached your maximum direct memory.
    0 讨论(0)
提交回复
热议问题