0

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 .

1
  • 2
    Please use the search as questions regarding batch inserting with JPA/Hibernate have been answered before. Commented Apr 19, 2021 at 13:09

1 Answer 1

1

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();
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks ,I try this but it didn't solve the problem,i will add that i use nativeQuery to insert in DB.
@ Christian Beikov Thanks ,I try this but it didn't solve the problem,i will add that i use nativeQuery to insert in DB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.