java - 如果 new 失败了怎么办?

标签 java memory-management

在 C++ 和 C# 中,当 new 无法分配足够的内存时,它会抛出异常。

我找不到有关 new 在 Java 中的行为的任何信息。 那么如果在Java中new失败(内存不足)会怎样呢?

最佳答案

假设您具体是指内存分配失败,那么它应该抛出 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.

Error 的所有子类一样,它通常是不可恢复的情况,即使从技术上讲您可以捕获它:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.

A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur.

关于java - 如果 new 失败了怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4785343/

相关文章:

c++ - 这怎么解释? "new"-ed 数组未初始化...?

c++ - 重载 New 时,New 与 Malloc

c++ - Microsoft C++ 异常:std::bad_alloc。是什么原因造成的?

java - 结合版本 1 和版本 4 UUID 的想法是个好主意吗?

java.lang.IllegalAccessError : org. apache.commons.dbcp.DelegatingPreparedStatement.isClosed()Z

java - 使用 DataStream 的 addData 点燃事务。可能的?

ios - 插入数千条记录时 CoreData(swift) 内存问题

java - 如何将 INTEGER/NULL 绑定(bind)到 ? Android 中带有 rawQuery SelectionArgs 的占位符?

java - SPRING BOOT注释: are they required

c# - 在 C# 中获取内存中下一个对象大小的空间