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

后端 未结 5 1214
清酒与你
清酒与你 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: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.

提交回复
热议问题