By Alvin Alexander. Last updated: February 18, 2021
I just got a “java.lang.OutOfMemoryError: Java heap space error” when trying to use the Scala REPL to analyze a large XML dataset, as shown here:
$ scala Welcome to Scala version 2.10.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_37). Type in expressions to have them evaluated. Type :help for more information. scala> import scala.xml._ import scala.xml._ scala> val xml = XML.loadFile("nasa.xml") java.lang.OutOfMemoryError: Java heap space
Solution: Control the REPL memory use
In short, I solved this problem by starting the Scala REPL with this command to control the RAM usage:
$ scala -J-Xms256m -J-Xmx512m
An alternative way to start the REPL is this:
$ env JAVA_OPTS="-Xms256m -Xmx512m" scala
The scala
man page has a little more information on this memory-related commands:
With Java 1.5 (or newer) one may for example configure the memory usage of the JVM as follows: JAVA_OPTS="-Xmx512M -Xms16M -Xss16M" With GNU Java one may configure the memory usage of the GIJ as follows: JAVA_OPTS="--mx512m --ms16m"
In summary, if you are getting a “java.lang.OutOfMemoryError: Java heap space error” when trying to use the Scala REPL, I hope this has been helpful.