Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Effect of Scala class definitions on perm gen space

Tags:

permgen

scala

A standard pattern used in Scala class library is definition of classes within classes and traits. And most of the operations of objects of parent classes result in objects of those inner classes being created. Each inner class is different for each object.

e.g. See source for scala.io.Source and LineIterator. I think this is the simplest one in standard library.

As documents suggest below are two different classes.

val s1:Source = ...
val s2:Source = ...
s1.getLines.getClass != s2.getLines.getClass //true if s1 != s2

Meaning two classes are created.

As the whole collection library is using the same pattern, what are the effects on permgen space for long running processes?

like image 373
Khurram Ijaz Avatar asked Aug 18 '11 19:08

Khurram Ijaz


1 Answers

I'm not sure how you concluded that if s1 != s2, then s1.getLines.getClass != s2.getLines.getClass. If I create two instances of BufferedSource using Source.fromFile, then both will return an instance of the same class scala.io.BufferedSource$BufferedLineIterator when I call getLines.

scala> s1 == s2
res6: Boolean = false

scala> s1.getLines.getClass == s2.getLines.getClass
res7: Boolean = true

It's true that Scala creates a lot of classes, but this is done at compile-time, not runtime, so perm gen should not be more of a issue for long-running processes.

like image 101
Ben James Avatar answered Sep 27 '22 16:09

Ben James