Forums Register Login

how to check memory leaks in java code?

+Pie Number of slices to send: Send
Dear friends,

I have an ear file which is deployed on a websphere application server. After 2 days of deployment the ear starts giving outofmemoryerror in logs or a heap dump problem.

My code has a thread which has an infinite loop. Might or might not contain problematic statements leading to the error said above.

Can you guys please suggest me some techniques or tools to tackle such problems or quickly find out the problematic pain points in the code.

Regards
Yogesh Gandhi.
+Pie Number of slices to send: Send
things i have already checked in the code i have added to ear are

1)
all database connections are properly released.therefore i do not think there should be any connection leakage.
+Pie Number of slices to send: Send
 

Yogesh Gandhi wrote:My code has a thread which has an infinite loop. Might or might not contain problematic statements leading to the error said above.


Well that would probably be the place to start.

Can you guys please suggest me some techniques or tools to tackle such problems or quickly find out the problematic pain points in the code.


Beyond profiling, not too many.

But for future reference, you might want to get a good book on defensive programming techniques. An ounce of prevention is worth a pound of cure.

Winston
+Pie Number of slices to send: Send
i agree but there must exist a checklist for good programming like

you should not create new variables inside a loop if it can also be done outside loop.

any other suggestions you might add, that you might want me to check in the code.

but even if i create new variables in the loop, doesn't the garbage collector takes care of that?
+Pie Number of slices to send: Send
 

Yogesh Gandhi wrote:i agree but there must exist a checklist for good programming like
you should not create new variables inside a loop if it can also be done outside loop...


It would take too long. There are plenty of good books and articles out there on the subject; you need to SearchFirst (←click).

As for profiling; there are better people than me to advise you. Hopefully one of them will reply.

Winston
+Pie Number of slices to send: Send
How about using tools like JProbe, JProfiler, JHat, HProf? They can give you the details about memory usage and threading env etc.
+Pie Number of slices to send: Send
I have read on internet that memory leak will occur only if my code holds a reference of memory that is increasing constantly with time and the reference to that object is not released. hence garbage collection cannt collect it.

in other words this will never give out of memory:

while(true)
{
String s = new String("yogesh");
}

so if all my variables are local there is less chances of memory leak.

Guys, please add your knowledge about the topic to this thread.
+Pie Number of slices to send: Send
 

Yogesh Gandhi wrote:so if all my variables are local there is less chances of memory leak.


Which is fine if they can be local. A lot of the time, they can't.

Guys, please add your knowledge about the topic to this thread.


I honestly don't know what to tell you. There could be a gazillion reasons that your code is producing a memory leak, and you haven't given us anything to go on except "tell me what it might be" (paraphrased).

How about a snippet (and I do mean a snippet; please don't mind-dump all your code) of the code that you think might be causing the problem? Either that or an SSCCE.

And before you do, please read the UseCodeTags page. Thoroughly.

Winston
+Pie Number of slices to send: Send
dear wintson,

Apologies for not using the code inside tags.I posted the reply from mobile thats why i was not able to put my code inside tags, because i was not able to recall the tags.

i just wanted to know what are the possible scenarios where memory is leaked?
if i will know the scenarios i will be able to track the pain area in my code and post it here and seek further help from experts in this forum.

i read an article about memory leaks here : http://olex.openlogic.com/wazi/2009/how-to-fix-memory-leaks-in-java/
it has also given a possible snippet of code causing memory leaks. After reading this my perception is that if all my variables are local to my method then there are less chances of the problem with my code.

You said that there can be millions of reasons causing a memory leak. I would like to request you, if you can share one or two scenarios that can cause a memory leak.
+Pie Number of slices to send: Send
 

Yogesh Gandhi wrote: i just wanted to know what are the possible scenarios where memory is leaked?


The basic reason is that something, somewhere is holding on to an object reference (and in your case, probably many thousands of them) long after it needs to.

if i will know the scenarios i will be able to track the pain area in my code and post it here and seek further help from experts in this forum.


And that's exactly the problem: there are literally millions of reason why this might be happening and you haven't provided us with any code.

i read an article about memory leaks here : http://olex.openlogic.com/wazi/2009/how-to-fix-memory-leaks-in-java/
it has also given a possible snippet of code causing memory leaks. After reading this my perception is that if all my variables are local to my method then there are less chances of the problem with my code.


Well if you can write an application of any any size that uses entirely local variables, then you're a better programmer than I.

You said that there can be millions of reasons causing a memory leak. I would like to request you, if you can share one or two scenarios that can cause a memory leak.


And, as I said: No.

I don't want to seem unhelpful, but it really could be any number of things and, after half a dozen posts, I'm nowhere nearer being able to help than I was at the start.

Either post some real code from your app, or wait for someone else.

Winston
+Pie Number of slices to send: Send
thanks witson for your time.
Forget Steve. Look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1591 times.
Similar Threads
How to mavenize a project
Multiple EAR files on WAS4 or 5
dynamic WEB-INF/lib
RAD 7.0 Version -- Please Help
How is IoC useful over conventional object instantiation ?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 09:04:30.