What is the cause of this strange Scala memory leak? -
this question has answer here:
even 7g of heap space, 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 = { (i <- 0 100000000) { val f = new foo() } } }
what causing problem, , how can avoided? problem seems call bar
in finalize method, don't understand why leak memory. know typical classes don't need override finalize, necessary in real version of code.
as said in comments, not @ scala-specific problem. finalize
comes straight java.lang.object
. problem described this answer, though hesitate question exact duplicate.
the gist of finalize
needs called something. when you're creating 100 billion objects in rapid succession, getting created faster can finalized. thread needs available call finalize
, isn't because it's busy creating more objects.
how can fix it? start not creating 100 billion instances of same object in rapid succession. know toy example, in real world you'll have try avoid allocating many of these @ same time. , given not actual code in question, it's harder give better advice.
Comments
Post a Comment