mohamed
mohamed

Reputation: 31

OutOfMemoryError Exception in hibernate

I have a OutOfMemoryError when i run my springBoot application ,

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

Subfunction process file with 3000000 lines and create insert query to dataBase. The program aborted in line 1900000 with outOfMemory error.

When we analyze dump with Eclipse Memory analyzer we have that result : enter image description here

Any help will be useful , thank you .

Upvotes: 0

Views: 151

Answers (1)

Christian Beikov
Christian Beikov

Reputation: 16420

Hibernate keeps all entities that you persist in the persistence context. If you do not need these entities anymore, you can flush and clear them out in e.g. batches of 50 items.

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

Upvotes: 1

Related Questions