views:

155

answers:

6

I have a problem with a Java memory leak, which for some reason does not show up in my profiler (Yourkit). When I run my Java application (A server with some threads for listening, sending and processing data) it seems that every time I get a new connection and this connection is removed some memory is not cleaned up. At least, this is what windows (and Linux) say.

When I run my application with my profiler, it simply shows the memory the way it is supposed to be, when a thread is closed all the memory is cleaned up. However, in reality, after a while Java simply crashed because it uses too much memory, so regardless of what my profiler says, I tend to believe windows and Linux that I do use it. Running garbage collection does not clean up the memory either.

So what could this be? I've tried everything I know, close the threads, set all the objects to null, delete the content of every array, etc. I am pretty sure the threads are closed because both eclipse and the compiler and the printouts seem to confirm this.

Does anyone have a clue?

A: 

It sounds like you are profiling something the wrong way, you might be missing something.

It is uncommon for a profiler to miss out on a leak, and one which is clearly bad enough to crash your server. Have you checked that the reason you are crashing in indeed memory-related?

Can you post the suspected part of your code and some profiling results?

Yuval A
A: 

You could try browsing the heap - use jmap to create a binary heap dump , jhat will start a small local server that will allow you to browse the heap contents. Eclipse also has a graphical plugin to allow you to browse heaps.

jmap -dump:format=b,file=FILENAME PID
jhat HEAPFILENAME

This may not be all that helpful,but it's a start- e.g. if you've got enormous maps of strings a heap full of hundreds of thousands of strings doesn't tell you which maps and what parts of the code are the offenders.

if you're creating lots of threads you could also try running jstack, which will tell you the thread status of all the threads currently running in your jvm.

Steve B.
+2  A: 

You could ask your VM to generate a heap dump when an OutOfMemory-Error occurs, and then analyse this heapdump.

For Sun's HotSpot VM, you can request heap dump generation as described here.

To analyze the dump, you could use jVisualVM or jhat. (There may be better tools, I haven't needed them so far, though.)

meriton
A: 

There is another answer, one that doesn't involve a memory leak, that can cause a java process to crash. Stack Overflow. Do any of your threads do any recursion? Is there a way your code is creating a huge call stack? You need to post the crash message so we can weed out various possibilities.

Kelly French
+1  A: 

Attach with jvisualvm (in the Java 6 JDK) and see if it provides information (including profiling) that YourKit does not.

Thorbjørn Ravn Andersen
A: 

Perhaps, there is a native memory leak. You said "connection". What kind connection: socket connection, connection to SQL database?

Serge