【问题标题】:How can i reduce heap usage for DataNucleus + Derby如何减少 DataNucleus + Derby 的堆使用量
【发布时间】:2012-08-06 19:56:07
【问题描述】:

我有一个 Java 桌面应用程序,它从服务中分批下载 50K 的 700K 条目并将它们存储在 Derby 库中

问题是这只有在我设置了 -Xmx1024 时才有效,否则应用程序崩溃并显示堆错误消息。虽然最终数据库的大小在100M左右

您能否建议一种方法来优化以下代码以使用更少的内存

下载的代码差不多是这样的

public static final void getItems() {
  ItemsRequest req = new ItemsRequest();
  Gson g = new Gson();
  int next_index_to_read = 0;
  int max_read_entry_count = 50000;
  req.setLimit(max_read_entry_count);
  ItemsResponse resp = null;
  do{
    resp = g.fromJson(postRequest(g.toJson(req)), ItemsResponse.class);
    resp.saveItems();
    next_index_to_read += max_read_entry_count;
    req.setFrom(next_index_to_read);
  }while(resp.getTotalItemsEntryCount()>next_index_to_read);
}

而负责保存数据的代码是

public class ItemsResponse
  public void saveItems() {
    PersistenceManagerFactory pmf = PMF.getPmf();

    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();

    try {
      tx.begin();

      if (data != null) {
        for (Item item : data) {
          Item item = null;
          try {
            item = pm.getObjectById(Item.class, item.getItemId());
          } catch (Exception e) {
            continue;
          }
          pm.makePersistent(item);
        }
      }
      tx.commit();
    } catch (Exception e) {
      Logger.getLogger("com.example").error("ItemResponse.saveData", e);
    } finally {
      if (tx.isActive()) {
        tx.rollback();
      }
    }
    pm.close();
  }

【问题讨论】:

    标签: java heap-memory derby datanucleus


    【解决方案1】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多