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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With