How to reduce the number of objects created in Scala?

后端 未结 5 1643
余生分开走
余生分开走 2021-02-08 03:24

I\'m programming a computer graphics application in Scala which uses a RGB class to return the color at a point in the image. As you can imagine, the function which return the c

5条回答
  •  孤独总比滥情好
    2021-02-08 03:53

    Supposedly, and they are, short lived objects and I have read that the garbage collector should re-claim them quickly. However I'm still worried about it. How does the GC know that I'm throwing it away quickly? So confusing.

    It doesn't know it. It assumes it. This is called the generational hypothesis on which all generational garbage collectors are built:

    • almost all objects die young
    • almost no old objects contain references to new objects

    Objects which satisfy this hypothesis are very cheap (even cheaper, in fact, than malloc and free in languages like C), only objects which violate one or both assumptions are expensive.

提交回复
热议问题