开发者

Effect of Scala class definitions on perm gen space

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 开发者_JS百科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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜