Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
496 views
in Technique[技术] by (71.8m points)

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

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?

This would mean that the memory is full only with strong referenced objects (or reachable by GC Roots) when the exception is thrown.

Edit: Assume the Sun JVM - HotSpot is in discussion.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...