The first version completes without problems.
Make sure your JVM has plenty of memory available to garbage collect.
Look at the -Xmx setting (for instance with leiningen   :jvm-opts ["-
Xmx2g" "-server"])

REPL started; server listening on localhost port 18885
user=>   (def atoms '(a b c))
#'user/atoms
user=>
user=>   (defn get-ch [n] (map #(str n %) atoms))
#'user/get-ch
user=>   (defn add-ch
    ([] (apply concat (iterate add-ch atoms)))
    ([n] (mapcat get-ch n)))
#'user/add-ch
user=> (time (dorun (take 20000000 (add-ch))))
"Elapsed time: 89922.105 msecs"
nil
user=>

On Jun 7, 10:53 am, Tom Hume <twh...@gmail.com> wrote:
> Hi
>
> I'm trying to generate a sequence which corresponds to a breadth-first
> search of a very wide, deep tree... and I'm hitting memory problems when I
> go too far along the sequence. Having asked around on the IRC channel and
> looked here, the number 1 cause of such problems is inadvertently holding
> onto the head; but I can't see where I'm doing this.
>
> The code is quite simple; here's a version which displays the problem:
>
> (def atoms '(a b c))
>
> (defn get-ch [n] (map #(str n %) atoms))
>
> (defn add-ch
>   ([] (apply concat (iterate add-ch atoms)))
>   ([n] (mapcat get-ch n)))
>
> (dorun (take 20000000 (add-ch)))
> And here's another version (which is the one I started out with before
> getting help from #clojure), which displays the same issue:
>
> (def atoms '(a b c))
>
> (defn get-children [n] (map #(str n %) atoms))
>
> (defn add-layer
>   ([] (add-layer atoms))
>   ([n] (let [child-nodes (mapcat get-children n) ]
>       (lazy-seq (concat n (add-layer child-nodes))))))
>
> (dorun (take 20000000 (add-layer)))
> Both give me an "OutOfMemoryError Java heap space". I'm running them from
> the REPL in Eclipse/CounterClockwise, on a Macbook Air.
>
> I'm pretty new to Clojure, so after beating my head against this for a day
> I'm hoping that this is something trivial I'm overlooking. I realise I
> could up my heap-size to make the issue less likely to occur, but the
> sequences I ultimately want to process are so vast I don't think this is
> going to help me...
>
> Any suggestions warmly welcomed.
>
> Thanks
> Tom

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