21

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?)

1 Answer 1

18

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.

3
  • So if I understand it right, what you are saying is it's my program which is leaking the memory. Commented Feb 24, 2017 at 11:24
  • Most likely - all SBT metaspace issues I found in the past were some sort of memory leaks in a particular program - it might be, your part, it might be your libraries, or some config. You might try to attach some profiler to SBT and look what is not collected in between reloads. Commented Feb 24, 2017 at 11:40
  • 1
    @MateuszKubuszok I'm also getting this error in simple example apps. I'm wondering if it's a general problem or just with my settings. I opened an issue here Commented Oct 12, 2017 at 4:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.