Clojure has a feature called locals clearing, which sets 'coll to nil
before calling reduce in test1, because the compiler can prove it won't be
used afterwards.
In test2, coll has to be retained, because reduce is called a second time
on it.


2013/2/5 N8Dawgrr <nathan.r.matth...@gmail.com>

> If the head is retained on a lazy sequence we have a potential memory leak.
>
> I set my JVM memory low, 64mb and ran the following:
>
> user> (defn test1 [coll] (reduce + coll))
> #'user/test1
> user> (test1 (take 10000000 (iterate inc 0)))
> 49999995000000
> user>
>
> Now if we do:
>
> user> (defn test2 [coll] [(reduce + coll) (reduce + coll)])
> #'user/test2
> user> (test2 (take 10000000 (iterate inc 0)))
> OutOfMemoryError Java heap space  [trace missing]
> user>
>
> Which OOMs as expected. The question is, why doens't the first example
> blow up? What magics happening?
>
> I would expect coll which is a function argument to be retained by the
> garbage collector and hence blow up.
>
>  --
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to