关于此还有另一个可能相关的问题,但没有答案,提问者也没有澄清任何问题。
所以当我在 postgres 中检查一个大的结果集时,java 似乎崩溃了。
我正在运行的查询试图获得大约 550 万行。基本上,我正在尝试从 Postgres 数据库填充 Neo4J 数据库。
除非有人问,否则我不会费心发布代码,因为我认为它非常通用。这是异常输出:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Unknown Source)
at java.lang.Class.getDeclaredField(Unknown Source)
at java.util.concurrent.atomic.AtomicReferenceFieldUpdater$AtomicReferenceFieldUpdaterImpl.<init>(Unknown Source)
at java.util.concurrent.atomic.AtomicReferenceFieldUpdater.newUpdater(Unknown Source)
at java.sql.SQLException.<clinit>(Unknown Source)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1818)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:512)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:374)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:254)
at sqlToGraph.SqlToGraph.main(SqlToGraph.java:52)
第 52 行只是查询,如下所示:
rs = st.executeQuery("select player_lo, player_hi, total_hands_lo, total_hands_hi, (pc_lo + pc_hi)/2 as avg_pc from pair");
所以我猜 rs 变得相当大了。我想我可以只使用 LIMIT 和 OFFSET 并运行程序几百次,或者让它循环重置 rs,但我宁愿不必这样做。
最佳答案
- 将获取大小设置为合理的大小 (
st.setFetchSize(rowCount)
)。您可以对此进行试验。 - 将所有内容放入事务中并关闭自动提交 (
con.setAutoCommit(false)
)。
关于Java内存不足使用PostgreSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11085973/