java GC(分配失败)VS OutOfMemoryError 异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43862031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GC (Allocation Failure) VS OutOfMemoryError Exception
提问by user3024119
'OutOfMemoryError': Usually, this error is thrown when there is insufficient space to allocate an object in the Java heap.
'OutOfMemoryError':通常,当 Java 堆中没有足够的空间分配对象时会抛出此错误。
GC (Allocation Failure): Allocation Failure” means that there is an allocation request that is bigger than the available space in young generation.
GC(Allocation Failure):Allocation Failure”表示有一个大于年轻代可用空间的分配请求。
Does this mean Allocation Failure will be thrown when Young generation memory is full (Minor GC) and "OutOfMemoryError" is thrown in full GC?
这是否意味着当年轻代内存已满(Minor GC)并且在full GC中抛出“OutOfMemoryError”时会抛出Allocation Failure?
回答by Eugene
These couldbecome related as far as I can tell; but they are entirely different things.
据我所知,这些可能会变得相关;但它们是完全不同的东西。
OutOfMemory
is an error you can not recover from - the JVM will die at this point.
OutOfMemory
是一个您无法从中恢复的错误 - JVM 将在此时死亡。
GC (Allocation Failure): Allocation Failure
is the reason why GC will kick in (and do a minor collection). At this point some things might happen, like: enough space is freed for the new allocation to fit into young generation
. Or that did not happen and some objects will be promoted to the old generation
. If they can't be promoted
, a full GC
might be triggered - and if that does not free enough space an OutOfMemory
mightbe thrown.
GC (Allocation Failure): Allocation Failure
是 GC 将启动(并进行次要收集)的原因。此时可能会发生一些事情,例如:为新分配释放足够的空间以适应young generation
. 或者这没有发生,一些对象将被提升到old generation
. 如果他们can't be promoted
,afull GC
可能会被触发 - 如果这没有释放足够的空间,aOutOfMemory
可能会被抛出。
回答by killjoy
In general, an OutOfMemoryError
occurs when you have exceeded the maximum memory you have already allocated to the JVM. This amount can be changed when starting java using jvm parameters. e.g. -Xmx2G
. Note that this amount isn't used immediately. See below.
通常,OutOfMemoryError
当您超过已分配给 JVM 的最大内存时会发生。使用 jvm 参数启动 java 时可以更改此数量。例如-Xmx2G
。请注意,此金额不会立即使用。见下文。
GC (Allocation Failure) is similar, except it occurs when the garbage collector runs out of memory on the heap, and it attempts to allocate more. If your allocated memory is higher than your available system memory, this will fail. Essentially, the JVM tries to allocate memory which isn't there.
GC(分配失败)与此类似,不同之处在于它发生在垃圾收集器耗尽堆上的内存并尝试分配更多内存时。如果您分配的内存高于可用的系统内存,这将失败。本质上,JVM 会尝试分配不存在的内存。