Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is java.lang.OutOfMemoryError: Metaspace normal in sbt REPL?

Tags:

I am new to Scala, sbt and its REPL. One thing I really love is the ~ option to loop a run or compile when editing files. So I end up running ~run quite often.

But it leaks memory. As after 20-30 runs the whole sbt interactive shell crashes with:

[info] Compiling 1 Scala source to /home/[redacted]
sbt appears to be exiting abnormally.
The log file for this session is at /tmp/sbt853875123365456892.log
java.lang.OutOfMemoryError: Metaspace
Error during sbt execution: java.lang.OutOfMemoryError: Metaspace

Honestly it's not a big deal, but while I understand what an OOME is, the question I am trying to figure out - is it really what it's supposed to be? The answer is "no", but...

  • is it a sbt bug? (should I try to report it?)
  • is something wrong with my program? (memory leak?)
like image 481
Grzegorz Oledzki Avatar asked Feb 23 '17 21:02

Grzegorz Oledzki


People also ask

What is Java Lang OutOfMemoryError Metaspace?

OutOfMemoryError exception with a detail MetaSpace is thrown. The amount of metaspace that can be used for class metadata is limited by the parameter MaxMetaSpaceSize , which is specified on the command line. When the amount of native memory needed for a class metadata exceeds MaxMetaSpaceSize , a java.

Can Metaspace run out of memory?

When an application cannot allocate an object in the Java Metaspace because the Metaspace has run out of memory, Pega Platform™ generates a java. lang. OutOfMemoryError exception.


1 Answers

Basically the SBT, when compiles and runs your program, uses only one JVM - the one it was run with. As such if there is anything in metaspace that cannot be garbage collected you might eventually run out of memory - for instance if you create some connection (I saw examples of MongoDB or ElasticSearch) and forget to close it on shutdown, it will stay open preventing you from collecting a few objects - after several recompile-reload cycles, that few objects might use up your metaspace.

So it is a memory leak, that usually wouldn't affect your program, but SBT makes it visible as exception you saw.

In a long run you should identify the leak and remove it, for a workaround you can use something like SBT revolver, which allows you to start the program in another JVM - thus memory leaks won't sum up, as you will be just closing and running new JVM instead of running everything in the same JVM as SBT.

like image 130
Mateusz Kubuszok Avatar answered Oct 25 '22 00:10

Mateusz Kubuszok