Explicit Memory Reclamation in Scala
The JVM raw interpreter doesn't perform liveness analysis, so references sometimes persist much too long. Concretely in Scala, this leads to high memory consumption by lazy lists when a parent frame retains a reference to the root of the list. We added a mechanism to the Scala compiler to erase these references.
The following Scala code (Scala 3.8.0-RC1-bin-20250731-fe6b7eb-NIGHTLY, OpenJDK 21) - used in the EPFL CS-214 course - is a simple program that indefinitely plays a sound represented by a sine wave at 440Hz.
It first creates the sine as a LazyList, so that it is only evaluated when asked for. It can then play it. sine is effectively infinite: until it's stopped, the program will forever play the tune!
@main def main = val sine: LazyList[Sample] = loop(sinePulse(440)) play(sine)
However, this program will quickly run into an issue and throw a runtime error... Can you see why?