内存越界异常或错误?

Is memory out of bound exception or error?

是否内存越界异常或错误?我们通常在服务器上部署项目时得到这个。这可能是一个基本问题。我用谷歌搜索,但找不到相关答案,所以在这里发帖。

我得到的错误:

Invocation of init method failed; nested exception is java.lang.OutOfMemoryError: allocLargeObjectOrArray - Object size: 8216, Num elements: 2049

我们该如何处理?

java.lang.OutOfMemoryError 扩展 java.lang.Error 并且 java.lang.Exception

抓住Exception你会错过的

try{
....
}catch(Exception ex){
 //will not catch OutOfMemoryError, since it does not extend Exception
}

抓住 Throwable,你会同时击中他们..

try{
....
}catch(Throwable ex){
 //will catch both Exception and OutOfMemoryError, they both extend this
}

抓不抓Throwable是另一个问题看这个Is it a bad practice to catch Throwable?(感谢@Dawnkeeper link)