java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError

You can get your heap memory size through below programe.

方法一public class GetHeapSize {
    public static void main(String[] args) {
        long heapsize = Runtime.getRuntime().totalMemory();
        System.out.println("heapsize is :: " + heapsize);
    }

}


方法二 see Java VM options

-Xms<size>        set initial Java heap size-Xmx<size>        set maximum Java heap size

java -Xmx2g 

assign 2 gigabytes of ram as maximum to your app

But you should see if you don't have a memory leak first.

It depends on the program. Try spot memory leaks. This question would be to hard to answer. Lately you can profile using JConsole to try to find out where your memory is going to


原因:
常见的有以下几种:

1.内存中加载的数据量过于庞大,如一次从数据库取出过多数据;

2.集合类中有对对象的引用,使用完后未清空,使得JVM不能回收;

3.代码中存在死循环或循环产生过多重复的对象实体;

4.使用的第三方软件中的BUG;

5.启动参数内存值设定的过小;

常见错误提示:
1.tomcat:java.lang.OutOfMemoryError: PermGen space

2.tomcat:java.lang.OutOfMemoryError: Java heap space

3.weblogic:Root cause of ServletException java.lang.OutOfMemoryError

4.resin:java.lang.OutOfMemoryError

5.java:java.lang.OutOfMemoryError

解决;
1.应用服务器提示错误的解决:
把启动参数内存值设置足够大。

 

2.Java代码导致错误的解决:
重点排查以下几点:

1)检查代码中是否有死循环或递归调用。

2)检查是否有大循环重复产生新对象实体。

3)检查对数据库查询中,是否有一次获得全部数据的查询。一般来说,如果一次取十万条记录到内存,就可能引起内存溢出。这个问题比较隐蔽,在上线前,数据库中数据较少,不容易出问题,上线后,数据库中数据多了,一次查询就有可能引起内存溢出。因此对于数据库查询尽量采用分页的方式查询

4 )检查List、MAP等集合对象是否有使用完后,未清除的问题。List、MAP等集合对象会始终存有对对象的引用,使得这些对象不能被GC回收。


猜你喜欢

转载自blog.csdn.net/boguesfei/article/details/80610168