On Jan 30, 6:08 am, Daniel Janus <nath...@gmail.com> wrote: > Hi, > > I've recently heard about the locals clearing feature of Clojure 1.2 (from a > recent post by Ken Wesson), and decided to test-drive it. Here is a > contrived example: > > (defn garbage [] > (make-array Byte/TYPE 10485760)) > > (defn -main [& args] > (let [a (garbage) > b (garbage) > c (garbage) > d (garbage) > e (garbage) > f (garbage) > g (garbage) > h (garbage)] > (println "OK"))) > > Now, when I build this with Leiningen and try to run under -Xmx20M or so, it > bombs out on me with an OOME. Changing the let to a bunch of nested lets > doesn't help, nor does migrating to Clojure 1.3alpha4. > > Shouldn't the locals clearing feature detect that each of the a-h locals is > no longer needed and clear them right after allocation? Is this a > misconception on my part about how this works? Or is something weird going > on here? >
Locals clearing happens at the point of last use. Since those locals are never used, no clearing code is emitted. This java program fails similarly: public class Mem { public static void main (String[] args) { byte[] a = new byte[10485760]; byte[] b = new byte[10485760]; byte[] c = new byte[10485760]; byte[] d = new byte[10485760]; byte[] e = new byte[10485760]; byte[] f = new byte[10485760]; byte[] g = new byte[10485760]; byte[] h = new byte[10485760]; System.out.println ("Ok"); } } This works in Clojure due to locals clearing: (defn garbage [] (make-array Byte/TYPE 10485760)) (defn -main [& args] (let [a (garbage) b (when a (garbage)) c (when b (garbage)) d (when c (garbage)) e (when d (garbage)) f (when e (garbage)) g (when f (garbage)) h (when g (garbage))] (println "OK"))) Since unused locals serve no purpose, optimizing that case is not going to become a priority any time soon. (Note: I still think the JVM GC should be able to figure out that those locals are dead references. I'm just not going to work around it in this case). Rich -- 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