Java堆内存错误 [英] Java Heap Memory error

查看:206
本文介绍了Java堆内存错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到这个错误:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at com.mysql.jdbc.MysqlIO.nextRowFast(MysqlIO.java:1585)
    at com.mysql.jdbc.MysqlIO.nextRow(MysqlIO.java:1409)
    at com.mysql.jdbc.MysqlIO.readSingleRowSet(MysqlIO.java:2886)
    at com.mysql.jdbc.MysqlIO.getResultSet(MysqlIO.java:476)
    at com.mysql.jdbc.MysqlIO.readResultsForQueryOrUpdate(MysqlIO.java:2581)
    at com.mysql.jdbc.MysqlIO.readAllResults(MysqlIO.java:1757)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2171)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2562)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2512)
    at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1476)
    at DBase.connect(automateExport.java:31)
    at automateExport.main(automateExport.java:10)

我试图增加通过打开eclipse.ini文件然后更改

I tried to increase the heap memmory space by opening eclipse.ini file and then changing

-Xms 256m and -Xmx 512m

但这没有帮助。我尝试了512m和1024m,但最终给出错误:无法启动JVM,eclipse没有打开。

But that did not help. I tried 512m and 1024m but that ended up giving error: Failed to start JVM and eclipse did not open up.

我尝试在cmd行上执行相同操作:

I tried doing the same on cmd line:

java -Xms 256m and -Xmx 512m

还有eclipse -vmargs -Xms 256m和-Xmx 512m
但是还是没有帮助。
我基本上创建了一个JDBC连接来查询数据库的大量记录。
请帮助我

and also eclipse -vmargs -Xms 256m and -Xmx 512m But still no help. I am basically creating a JDBC connection to query the database for very large set of records. Please help me.

推荐答案

当您查询返回非常大集合时,mysql驱动程序将默认加载整个结果设置在内存中,然后给你控制权。这听起来像是你的内存不足,所以增加内存,例如-Xmx1024M

When you query returns very large sets, the mysql driver will by default load the entire result set in memory before giving you control back. It sounds like you're just running out of memory, so increase the memory furter, e.g. -Xmx1024M

另一个选择可能是在MySQL查询上启用流式传输结果 - 这样可以防止整个ResultSet一次性加载到内存中。这可以通过执行

The other alternative might be to enable streaming results on your MySQL queries- this prevents the entire ResultSet to be loaded into memory all at once. This can be done by doing

stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, 
           java.sql.ResultSet.CONCUR_READ_ONLY);//These are defaults though,I believe
stmt.setFetchSize(Integer.MIN_VALUE);

(请注意,除了Integer.MIN_VALUE之外的其他内容对MySQL驱动程序没有影响)

(Note that anything else but Integer.MIN_VALUE has no effect on the MySQL driver)

这篇关于Java堆内存错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆