On Sat, Mar 29, 2014 at 10:09 AM, Ryan Waters <ryan.or...@gmail.com> wrote:

> I have some code that blows up the heap and I'm not sure why.  I've
> reduced it down to the following.
>
> I've tried to make sure the atom doesn't have boundless growth and I
> didn't think 'while' hangs on to the head of sequences so I'm embarrassed
> to say I'm stumped.
>
>
> (defn leaks-memory
>   []
>   (let [mem (atom [])
>         chunksize 1000
>         threshold 2000]
>   (while true
>     (swap! mem conj (rand-int 100))
>
>     ; every 'chunksize' item past 'threshold'
>     (when (and (= 0 (mod (count @mem) chunksize))
>                (> (count @mem) threshold))
>       (swap! mem subvec chunksize)))))
>
> (doc subvec)

Returns a persistent vector of the items in vector from
start (inclusive) to end (exclusive). If end is not supplied,
defaults to (count vector). This operation is O(1) and very fast, as
the resulting vector shares structure with the original and no
trimming is done.

subvec is fast, but it's not saving you any memory.

-- 
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/d/optout.

Reply via email to