垃圾收集器是否保证在内存不足错误之前运行? [英] Is the garbage collector guaranteed to run before Out of Memory Error?

查看:176
本文介绍了垃圾收集器是否保证在内存不足错误之前运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果堆满了,JVM会抛出一个
的OutOfMemoryError 。但是可以确定的是,一个(完整的)垃圾回收总是在引发这样的异常之前发生?

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?

这意味着当引发异常时,内存只有强引用对象(或GC Roots可访问)才是完整的。

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

编辑:假设Sun JVM - HotSpot正在讨论中。

Assume the Sun JVM - HotSpot is in discussion.

推荐答案

The 6.3节中的Java机器规范(强调我的):

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


OutOfMemoryError :Java虚拟机实现已耗尽虚拟或物理内存
且自动存储管理器无法
回收足够的内存
来满足对象创建请求。

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.

因此,JVM的确保证它会尝试它所能做的在抛出OOME之前通过垃圾回收释放内存。

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.

这篇关于垃圾收集器是否保证在内存不足错误之前运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆