The dose q over a simple lazy seq ends from a heap of space

When stress testing some Clojure code at work, I noticed that it ends from a heap of space when iterating over large data sets. In the end, I was able to track down problems to a combination of the Clojure doseq function and implementation for lazy sequences.

This is the smallest piece of code that causes Clojure to crash by running out of available heap space:

 (doseq [e (take 1000000000 (iterate inc 1))] (identity e)) 

The doseq documentation clearly states that it does not preserve the head of a lazy sequence, so I would expect the memory complexity above the code to be closer to O (1). Is there something I'm missing? What is the Clojure-idiomatic way to iterate over very large lazy sequences if doseq not up to work?

+2
source share
1 answer

When I run this sample, I see that memory usage reaches 2.0 Gigs, so maybe you really just don't have a bar.

it will probably take some time to start:

 user=> (time (doseq [e (take 1000000000 (iterate inc 1))] (identity e))) "Elapsed time: 266396.221132 msecs" 

form on top:

 23999 arthur 20 0 4001m 1.2g 5932 S 213 15.3 17:11.35 java 24017 arthur 20 0 3721m 740m 5548 S 88 9.3 13:49.95 java 
+2
source

Source: https://habr.com/ru/post/1238496/


All Articles