Empty should be empty!

The shock and surprise when I saw the memory cost of a Stream created from an empty List in #Java was audible.
https://donraab.medium.com/empty-should-be-empty-c09e21edc205?source=friends_link&sk=c7809f108441527f48b2ef173bc7fbda

Empty Should be Empty

Why does empty cost so much in Java sometimes?

Medium
@TheDonRaab hi! You cannot return a singleton stream from an empty list, as any stream has a state. It tracks whether it's consumed, so you get IllegalStateException when trying to consume it twice. Also, you can register close handlers via onClose(). I believe, LazyIterable doesn't have such a (arguably not very useful but still) feature.
@TheDonRaab in fact, streams should be short-lived, unlike collections. If you have more than hundred of stream instances in your heap at any random point of time, likely you are doing something wrong. Streams are born to die quickly, they likely do not survive the first GC evacuation. Hence, it's usually not a problem if you allocate some more bytes.
@TheDonRaab This becomes mostly problematic with flatMap, when you create tons of small streams. E.g., adding .flatMap(Optional::stream) to flatten a stream of optionals may cause a performance issue. But now, you can replace this with .<T>mapMulti(Optional::ifPresent) to avoid intermediate streams.
@tagir_valeev Hi, I was about to go to sleep after publishing and then went and updated my blog because I remembered that Streams can't be used more than once as you rightly point out here. Then I saw your post with a bunch of great points. Thanks for the replies and advice!
@TheDonRaab the stream would be much simpler without parallelism support. It would be possible to have separate sequential stream and parallel stream implementations. As parallel is used rarely, many people would benefit in terms of CPU+allocations when using sequential only. However, .parallel() returns the same instance with a flag set, and I believe existing code already relies on this unspecified behavior...
@TheDonRaab great blog. Thank you. I learned quite a few bits.