OutOfMemoryError之前的垃圾回收 [英] Garbage collection before OutOfMemoryError

查看:95
本文介绍了OutOfMemoryError之前的垃圾回收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于Java中垃圾回收的一个问题:如果一个人分配了一大块内存(使用new int [BIG_NUMBER]或其他),是否可以保证垃圾收集器在抛出OutOfMemoryError之前会收集垃圾?这是行为吗 垃圾收集器现在已经成为Java规范的一部分了?

我了解到,垃圾收集器本身可能会抛出此类异常,以防收集垃圾的时间太长,至少在Sun Hotspot Java虚拟机的情况下

«如果在垃圾回收上花费了太多时间,那么并行/并发收集器将抛出OutOfMemoryError:如果超过98%的总时间花费在了垃圾回收上,并且回收了不到2%的堆,则将抛出OutOfMemoryError.此功能旨在防止应用程序长时间运行,而由于堆太小而几乎没有进展,甚至没有进展.如有必要,可以通过在命令行中添加选项-XX:-UseGCOverheadLimit来禁用此功能. »

,但是根据引用,可以通过在命令中添加一个选项来禁用此功能 线.

解决方案

AFAIK,它一直是Spec的一部分.

如果VM的内存非常低,早期的OutOfMemoryError是Java 6的一项功能,它被引入来在VM变得不可用但还没有完全消失时停止VM.

我不会关闭此功能,您最好对系统进行工程设计,以使您永远无法获得接近98%的内存使用率.我建议30%是一个比较舒适的水平.

摘自Javadoc for Java 1.4.2(于2002年推出) http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/OutOfMemoryError.html

当Java虚拟机由于内存不足而无法分配对象,并且垃圾回收器无法再提供更多内存时,抛出该异常.

This is a question about garbage collection in java: if one allocates a large block of memory (using new int[BIG_NUMBER] or however), is there any guarantee that a garbage collector will collect the garbage before an OutOfMemoryError is thrown? Is this behaviour of garbage collector a part of the Java spec by now?

I have learnt that a garbage collector itself might throw such exception in case it takes too long to collect the garbage, at least in the case of Sun Hotspot Java Virtual Machine

«The parallel / concurrent collector will throw an OutOfMemoryError if too much time is being spent in garbage collection: if more than 98% of the total time is spent in garbage collection and less than 2% of the heap is recovered, an OutOfMemoryError will be thrown. This feature is designed to prevent applications from running for an extended period of time while making little or no progress because the heap is too small. If necessary, this feature can be disabled by adding the option -XX:-UseGCOverheadLimit to the command line. »

but this may be disabled, according to the citation, by adding an option to the command line.

解决方案

AFAIK, It has always been part of the Spec.

The early OutOfMemoryError if the VM is very low on memory is a Java 6 feature and was introduced to stop the VM when it becomes unusable but not quite dead.

I wouldn't turn this feature off, you are far better off engineering your system so you never get close to 98% memory usage. I would suggest 30% is a more comfortable level to run at.

From the Javadoc for Java 1.4.2 (introduced in 2002) http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/OutOfMemoryError.html

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.

这篇关于OutOfMemoryError之前的垃圾回收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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