休眠中的 OutOfMemoryError 异常

时间:2021-04-19 12:59:51

标签: java spring-boot hibernate jpa out-of-memory

运行 springBoot 应用程序时出现 OutOfMemoryError,

@Transaction(propagation = Propagation.REQUIRED) 
void public function() {
   subFunction() ;
}

3000000 行的子函数处理文件并创建对数据库的插入查询。 程序在第 1900000 行中止,并出现 outOfMemory 错误。

当我们使用 Eclipse 内存分析器分析转储时,我们得到了这样的结果: enter image description here

任何帮助都会有用,谢谢。

1 个答案:

答案 0 :(得分:1)

Hibernate 会保留您在持久化上下文中持久化的所有实体。如果您不再需要这些实体,您可以在例如刷新和清除它们每批 50 件。

int lineCount = 0;
for (String line : lines) {
  // Your code
  entityManager.persist(entity);
  lineCount++;
  if ((lineCount % 50) == 0) {
      entityManager.flush();
      entityManager.clear();
  }
}