Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can JPA stream() cause OutOfMemoryError?

Tags:

stream

spring

jpa

In my Spring MVC project, I have used pagination where I fetch too many records from tables.

Currently, I have planned to use JPA Stream instead.

Like below:

Stream<User> findAll();

I don't collect them to list or search for max value. I just need to operate on each value (forEach) on the stream.

Can it cause OutOfMemoryError if table has too many records ? (Say, more than 10 million)

like image 439
valijon Avatar asked Sep 16 '25 10:09

valijon


1 Answers

When using Hibernate as a JPA provider the Stream<User> findAll() will result in a lazy cursor (see this question as well). This means it will only fetch the amount of records that is specified by the JDBC fetch-size. It will not put all records from the database (if there are more then 1000) into memory, like what happens with a List<User> findAll().

So unless you are holding onto the User object in your code and collect them into a collection/array the memory should be released periodically.

like image 197
M. Deinum Avatar answered Sep 18 '25 09:09

M. Deinum