Re: Losing your head with side effects

2011-10-25 Thread Mike
It appears I'm not understanding how something works in the REPL (or
maybe deeper).  For instance:

(def big (range 1000))
; memory is small now
(count big) = 1000
; memory is huge now
(System/gc)
; memory is still huge now
(def big nil)
(System/gc)
; memory is small again

So somehow when count realizes big, I'm guessing it gets memoized
and stuck in some sort of cache attached to the var big.

If I:

(do (System/gc) (count (range 1000)) (System/gc))
; memory is small before and after

I think the version I wrote originally is fine; I just wasn't testing
it properly.  Sorry to bother folks!

Mike

-- 
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


Re: Losing your head with side effects

2011-10-25 Thread Linus Ericsson
Range is lazy.

First you define a range, which is just a promise that the variable will
be rendering a seq with 10M when asked for the elements.

When you count the sequence you realize it and it takes up much space. When
you let go if it it's of course GCed.

The problem is that you hold on to the head (first element) of the sequence.
If you do

(count (range 1000))

clojure will go through it lazily and throw away elements as they are
counted, hence much less memory usage.

Let go of the head! :)

/Linus

2011/10/25 Mike cki...@gmail.com

 It appears I'm not understanding how something works in the REPL (or
 maybe deeper).  For instance:

 (def big (range 1000))
 ; memory is small now
 (count big) = 1000
 ; memory is huge now
 (System/gc)
 ; memory is still huge now
 (def big nil)
 (System/gc)
 ; memory is small again

 So somehow when count realizes big, I'm guessing it gets memoized
 and stuck in some sort of cache attached to the var big.

 If I:

 (do (System/gc) (count (range 1000)) (System/gc))
 ; memory is small before and after

 I think the version I wrote originally is fine; I just wasn't testing
 it properly.  Sorry to bother folks!

 Mike

 --
 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 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