gmhk
gmhk

Reputation: 15960

Memory Leak in a Java based application

There is a memory leak happens in an application when a short lived object holds a long lived object, My question is how can we identify 1) which object lives longer and shorter, any tool which measures life of an object?

2nd Question

I am constantly getting the Out of Memory Space Error and I tried increasing the Heap memory to 2 GB, but still i am getting, please suggest me any open source tool with which i can identify the memory leak issue and fix.

At present I am restarting the server every time as a temporary solution, but Suggest me any thing which i can fix permanently.

Upvotes: 1

Views: 584

Answers (7)

evernat
evernat

Reputation: 1743

As said, jvisualvm have good tools to analyze the heap live.

But you can also use jvisualvm or -XX:+HeapDumpOnOutOfMemoryError to take a heap dump in a file. And then take the file to your destkop, to open it in Eclipse Memory Analyzer. Eclipse MAT is even better to analyze the memory.

Upvotes: 1

Steve B.
Steve B.

Reputation: 57333

A short-lived object holding a reference to a long-lived object will not cause problems. (a good overview , including generational garbage collection).

2GB is an awful lot of objects/references. If you're running out of heap space at 2Gb you're likely holding onto massive amounts of data and/or keeping open resources when you're done with them. You should post at the very least a description of what your application does and how long it takes to die.

You can get some sense of what's happening quickly by watching the garbage collector (e.g. run with "-verbose:gc" which will tell you when the garbage collector is running and how much it collects).

Upvotes: 0

Ivan
Ivan

Reputation: 1256

I've found the Heap Walker in Netbeans very usefull

Upvotes: 1

Victor Sorokin
Victor Sorokin

Reputation: 12006

There are 2 options:

  • It just may be your application doesn't have enough heap allocated. Measure size of your input and give application corresponding heap;

  • There's memory-leak: take profiler, examine your heap, find objects which shouldn't be there or there too much of them ('short-living objects', in your terms), identify which 'long-living' object holds them, fix this. You should know your code to understand which objects must be 'short-living' and which must be 'long-living'.

Upvotes: 1

kukudas
kukudas

Reputation: 4934

I think you can track memory leaks with jsconsole (which comes shipped with JDK6 if i'm not mistaken).

Upvotes: 0

Neil
Neil

Reputation: 5780

Out of Memory occurs on a server because it literally uses up all memory it's allowed to have. Not sure about what application you're using for hosting the server, but for Apache, you need to add the line -Xmx512m where 512 is the maximum amount of megabytes it's allowed to have.

If you leave the application to run long enough, it's going to happen. This isn't because of memory leaks in Java but the server itself which has a tendency to do so. You can't change this behavior, but you can at least increase the default memory of 256 mb. With the heavy loading site that I work on everyday, 256 mb lasts about 30 minutes for me unfortunately. I've found that 1024 mb is reasonable and rarely crashes due to out of memory exceptions.

I'd strike me as very unusual for Java to be incapable of garbage collecting correctly unless the programmer took a hand at overriding typical functionality.

Upvotes: 0

Alois Cochard
Alois Cochard

Reputation: 9862

You can use the VisualVM tool included in the JDK:

Documentation available here:

Upvotes: 5

Related Questions