File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes how to check memory leaks in java code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to check memory leaks in java code?" Watch "how to check memory leaks in java code?" New topic
Author

how to check memory leaks in java code?

Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 212
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.
Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 212
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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 212
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?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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
Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140
How about using tools like JProbe, JProfiler, JHat, HProf? They can give you the details about memory usage and threading env etc.
Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 212
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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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
Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 212
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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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
Yogesh Gandhi
Ranch Hand

Joined: Dec 05, 2008
Posts: 212
thanks witson for your time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to check memory leaks in java code?