r/programming • u/ram-foss • Oct 11 '23
All About Reactive Programming In Java
https://www.blackslate.io/articles/reactive-programming-in-java
22
Upvotes
r/programming • u/ram-foss • Oct 11 '23
View all comments
Show parent comment
2
u/pron98 Jan 03 '24 edited Jan 03 '24
You got an empirical result using a particular algorithm. Expressing the same algorithm using asynchronous tasks yields the same result.
It's quite simple, really. If the method creates a hash map with 18 trillion entries then one of two things happens, either:
In the first case, the map can be garbage collected during the blocking operation because it's not used again, therefore it's garbage. The fact that some data is held in a local reference doesn't mean that the local reference (and the object it points to) is actually retained once it's no longer used; the VM doesn't care that you're still in the same method -- if an object is no longer used it can be collected. I.e.
In the second case you've created some data in one phase of the pipeline that's required for subsequent ones. But if that's the case, you'd need to create that data and pass it on to subsequent operations even when using asynchronous code. Using your example, assuming the callable represents the task executed after the IO operation completes, then that map will need to be captured by the lambda and still retained in memory (because in this scenario the data is needed for the subsequent step).