サービスから 50K のバッチで 700K のエントリをダウンロードし、それらを Derby ベースに保存する Java デスクトップ アプリケーションがあります。
問題は、これが -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();
}