On Sun, Dec 7, 2008 at 1:16 AM, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > I'm also running into, what I believe to be, the same problem. Every > time I run the following code I get "java.lang.OutOfMemoryError: Java > heap space". > > (use 'clojure.contrib.duck-streams) > (count (line-seq (reader "big.csv"))) > > If I change "count" to "dorun" then it will return without problem.
I think I can reproduce this one like so: user=> (count (take 15000 (iterate #(str % "more") "some"))) java.lang.OutOfMemoryError: Java heap space (NO_SOURCE_FILE:0) As with yours, I can replace 'count' with 'dorun' and it works fine. I can also use 'last': user=> (.length (last (take 15000 (iterate #(str % "more") "some"))))) 60000 I think the problem in this case is 'count', which for all IPersistentCollections (including lazy sequences) calls the 'count' method of the instance. ASeq's count method is a tight for() loop, but since it's an instance method it must retain a 'this' reference to the head of the seq. Fixing this is hard becasue RT.count() is holding onto the head as well. I've attached a patch that fixes the problem, but it's pretty ugly, perhaps only useful to demonstrate that this is the problem. --Chouser --~--~---------~--~----~------------~-------~--~----~ 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 To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---
diff --git a/src/jvm/clojure/lang/RT.java b/src/jvm/clojure/lang/RT.java index 49550b7..11dd151 100644 --- a/src/jvm/clojure/lang/RT.java +++ b/src/jvm/clojure/lang/RT.java @@ -482,6 +482,15 @@ static public IPersistentMap meta(Object x){ public static int count(Object o){ if(o == null) return 0; + else if(o instanceof LazyCons) + { + int i = 0; + for( ; o != null; o = ((ISeq )o).rest(), i++) + ; + return i; + } + else if(o instanceof IPersistentCollection) + return ((IPersistentCollection) o).count(); else if(o instanceof IPersistentCollection) return ((IPersistentCollection) o).count(); else if(o instanceof String)