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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With