Maulik Soneji Maulik Soneji - 12 days ago 5
Scala Question

Memory Analysis of Scala

I understand that scala supports immutability and so when we if we have two integer vals and we do val1 + val2, so it will create a new integer which will have the value as val1 + val2.

My question is that because scala creates new instances of objects to not to mutate the previous object, what are the performance and memory drawbacks of immutability?

Edit: I want details specific to scala about how it deals with the performance and memory overheads.

Answer

Scala code is compiled to JVM bytecode so basically memory management does not differ compared to Java. Chapter 6 of Programming in Scala, First Edition mentions potential trade-offs of using immutable memory. Potentially you may create many instances of objects but Scala libraries usually create short-lived objects so the garbage collector should deal with them quickly.

Comments