简体   繁体   English

java.lang.OutOfMemoryError,何时回收内存?

[英]java.lang.OutOfMemoryError, when is memory reclaimed?

Once this exception is thrown, is object that caused this thrown out of memory? 引发此异常后,导致此异常的对象是否被抛出内存? Does it happen right away? 它会马上发生吗?

In other words, if i am adding objects to a list, at some point this can no longer happen and OOM error is thrown. 换句话说,如果我将对象添加到列表中,则在某些时候不再会发生这种情况,并且会引发OOM错误。 At that time, does anything happen to the list itself? 那时,列表本身没有任何变化吗?

java.lang.OutOfMemoryError: Java heap space

This is thrown when a new object could not be created. 当无法创建新对象时,抛出此错误。 The current object will continue to exist. 当前对象将继续存在。

However, due to the nature of an error like this the current code will stop executing and it's likely that the current object will be garbage-collected soon. 但是,由于此类错误的性质,当前代码将停止执​​行,并且当前对象很可能很快会被垃圾回收。 It just depends on the structure of your code and whether references are still being held to your object. 它仅取决于代码的结构以及是否仍保留对对象的引用。

From documentation : 文档

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

According to http://docs.oracle.com/javase/6/docs/api/ , an OutOfMemoryError is: 根据http://docs.oracle.com/javase/6/docs/api/,OutOfMemoryError为:

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

So it happens as soon as the JVM sees that it does not have enough heapspace to allocate memory for the new object you're trying to create. 因此,一旦JVM看到它没有足够的堆空间来为您尝试创建的新对象分配内存,就会发生这种情况。 So the object never gets created because you don't have enough memory. 因此,永远不会创建该对象,因为您没有足够的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM