Even with 7G of heap space, this will run out of memory.
import scala.collection.mutable.Set
class Foo() {
val anEmptySet: Set[Int] = Set()
def bar(ints: Traversable[Int]): Unit = {}
override def finalize() {
bar(anEmptySet)
super.finalize()
}
}
object FooTest {
def main(args: Array[String]): Unit = {
for (i <- 0 to 100000000) {
val f = new Foo()
}
}
}
What is causing the problem, and how can it be avoided? The problem seems to be the call to bar
in the finalize method, but I don't understand why it would leak memory. I know that typical classes don't need to override finalize, but it is necessary in the real version of this code.