ib84
ib84

Reputation: 675

Why do I get "java.lang.OutOfMemoryError: GC overhead limit exceeded" here?

I wanted to hack a quick script that simply splits a nucleotide sequence (a string like "ATGAAGAAC...") into a List of triplets aka codons (Strings: "ATG", "AAG", "AAC", ...) to do other stuff with.

What's wrong with this one-liner, why do I get a "java.lang.OutOfMemoryError: GC overhead limit exceeded" ?

def tripletize(s:String, accu:List[String] = List.empty[String]):List[String] = tripletize(s.drop(3), s.take(3) :: accu)

I'm not asking what that is, but why I get it and how to avoid. Btw, I know this would give me the reverse order, but I don't know how to avoid that scala considers String as a collection itself. i.e. this:

scala> List.empty ++ "hello"
res6: List[Char] = List(h, e, l, l, o)

Upvotes: 0

Views: 697

Answers (1)

Rex Kerr
Rex Kerr

Reputation: 167871

When do you stop? drop won't terminate the recursion just because there's nothing more to drop. Also, what do you return? You never say. You need to do something like

= if (s.length==0) accu else tripletize(s.drop(3), s.take(3) :: accu)

Upvotes: 4

Related Questions