java - 如何减少 DataNucleus + Derby 的堆使用量

标签 java heap-memory derby datanucleus

我有一个 Java 桌面应用程序,它从服务中批量下载 700K 条目,每批 50K,并将它们存储在 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 - 如何减少 DataNucleus + Derby 的堆使用量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11885992/

相关文章:

java - 线程安全类 许多方法 ReentrantLock

c - 如何fwrite到指针的指针?

带有嵌入式 DERBY DB 的 Java 应用程序

java - 使用 EvaluationContextExtensionSupport 和自定义 PermissionEvaluator 将 Spring Boot 2.0.6 迁移到 2.1.0 时出现“无效的 bean 定义”

java - 成为一名优秀的富互联网应用程序 (RIA) 开发人员需要哪些重大的思维转变?

java : Get heap dump without jmap or without hanging the application

sql - Java DB/Derby-表名中的空白-如何转义?

java - 关闭 Netbeans 后无法访问我的 Derby 数据库

java - 使用 TreeTranslator 重命名不适用于 Kotlin 的函数

C - 返回一个没有 malloc 的 char 指针