views:

97

answers:

6

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.

+4  A: 

You can use the VisualVM tool included in the JDK:

Documentation available here:

Alois Cochard
A: 

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.

Neil
Does static string Constants take lot of memory?
harigm
Any static variables get put on the heap, not the stack, meaning they persist. However when the jar unloads, most (decent) jvms remove it from the heap. Though one thing's for sure: that's not going to cause an out of memory exception by itself.
Neil
A: 

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

kukudas
+1  A: 

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'.

Victor Sorokin
I have almost allocated 2 GB of Space, and earlier only 15 users were using and now the user count has increased to more than 50, since then I am getting this error.
harigm
+1  A: 

I've found the Heap Walker in Netbeans very usefull

Ivan
A: 

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

Steve B.
@Steve, Is that Because of creating all the constants as Static name value pair? For example Public static final String ENGG_SOUTH_DIVISION = "South";
harigm
Anything you've typed is not going to be significant enough to have anything to do with the amount of memory you're seeing. To prove this to yourself, write a loop that puts a million or so different strings in a set or hashmap - you should easily be able to fit that in much, much less than 2GB. If you're filling up 2GB, the constants you've typed aren't even a rounding error.
Steve B.