I'm wondering if it makes sense to use lazy val
to prevent unnecessary heap allocations, thereby making the GC's job a little easier. For instance, I am in the habit of writing code like so:
lazy val requiredParameterObject = new Foo {
val a = new ...
val b = new ...
// etc
}
for (a <- collection) a.someFunction(requiredParameterObject)
The idea here is that by making requiredParameterObject
lazy, I am saving a heap allocation in the case that collection
is empty. But I am wondering: do the internal details of lazy
make this not an effective performance win?