Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory leak in Scala and processes

I have a system in Scala, with a lot of simultaneous threads and system calls. This system has some problem, because memory usage is increasing over time.

The image bellow shows the memory usage for one day. When it gets to the limit, the process shuts down and I put a watch-dog to recover it again. inserir a descrição da imagem aqui

I periodically run the command

jcmd <pid> GC.run

And this makes the memory to increase slowly, but the leak still happens.

I analysed with jvisualvm, comparing to distinct moments in time, with 40 minutes delta. The image bellow shows the comparison between these two moments in time. Notice that there is an increase for instances of some classes like ConcurrentHashMap$HashEntry, SNode, WeakReference, char[] and String and many classes in the package scala.collection.concurrent.

memory leaked ojects

What can be causing the memory leak?

Edit 1: Investigating JVisualVM, I noticed object of CNode and INode classes that are in TriedMap, that is instanced inside sbt.TrapExit$App class. Here is the object hierarchy figure:

object hierarchy

like image 562
Daniel Cukier Avatar asked Sep 14 '14 15:09

Daniel Cukier


People also ask

What is a process memory leak?

DEFINITION A memory leak is the gradual deterioration of system performance that occurs over time as the result of the fragmentation of a computer's RAM due to poorly designed or programmed applications that fail to free up memory segments when they are no longer needed.

What is the main cause of memory leaks?

Programming issues Memory leaks are a common error in programming, especially when using languages that have no built in automatic garbage collection, such as C and C++. Typically, a memory leak occurs because dynamically allocated memory has become unreachable.

How do you check if there are memory leaks?

One way to check for memory leak is to press and hold down your Windows key and tap the Pause/Break key to bring up System Properties. Click on the Performance tab and check System Resources for the percentage of free or available RAM.


1 Answers

First capture a heap dump when your application crashes due to an out of memory issue. Add the following flags when starting the jvm

-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/path/to/dump

Next you need to analyze the heap dump to figure out the source of the memory leak. I recommend using Eclipse MAT. The Leak Suspects report should give you a sense of what objects are actually causing the leak.

like image 178
rrevo Avatar answered Sep 18 '22 14:09

rrevo