2010/12/22 Jeff Palmucci <jpalmu...@gmail.com>

> I've worked around this sort of thing in the past by wrapping the
> initialization in a closure. My macros:
>
> (defmacro once-fn "Define a function that should only be called once.
> Releases local storage earlier"
>  [args & body]
>  `(^{:once true} fn* ~args ~...@body))
>
> (defmacro top-level-run "work around a memory leak in the repl"
>  [& body]
>  `((once-fn []
>      ~...@body)))
>
> You'll find that:
>
> (def out_of_mem (top-level-run (reduce + 0 (range 500000000))))
>
> does not run out of memory.
>

Couldn't just it be a wrap with (let [] ), and let the choice of running it
once or not by choosing either def or defonce :

user=> (def x (reduce + (range 50000000)))
java.lang.OutOfMemoryError: Java heap space (NO_SOURCE_FILE:1)
user=> (def x (let [] (reduce + (range 50000000))))
#'user/x
user=> (defonce y (let [] (reduce + (range 50000000))))
#'user/y
user=> (defonce y (let [] (reduce + (range 50000000))))
nil
user=> (defonce z (reduce + (range 50000000)))
#'user/z
user=> (defonce z (reduce + (range 50000000)))
nil
user=> x
1249999975000000
user=> y
1249999975000000
user=> z
1249999975000000
user=>

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

Reply via email to