Java内存不足使用PostgreSQL

标签 java postgresql memory

关于此还有另一个可能相关的问题,但没有答案,提问者也没有澄清任何问题。

所以当我在 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,但我宁愿不必这样做。

最佳答案

  1. 将获取大小设置为合理的大小 (st.setFetchSize(rowCount))。您可以对此进行试验。
  2. 将所有内容放入事务中并关闭自动提交 (con.setAutoCommit(false))。

参见 this explanationthe documentation .

关于Java内存不足使用PostgreSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11085973/

相关文章:

java - 排队多个下载,寻找生产者消费者 API

java - 文件可用后立即运行 Spring Batch 作业

database - Postgres 约束表的写入和删除顺序

sql - 从另一个表创建表

c++ - 在 C++ 中实现接口(interface)功能

c++ - std::weak_ptrs 是否会影响 std::make_shared 分配的内存何时被释放?

java - 给定根节点和目标节点的二叉树之和是多少?

java - middlegen 发生了什么,我今天用什么代替?

ruby-on-rails - Localhost :3000 claims "gem ' pg'"in not installed, Heroku 应用运行正常

c++ - 我如何处理队列前端和弹出功能