A Funny Java Flavoured Look at the World

Tuesday, June 13, 2006

OutOfMemoryException and investigation

We were investigating an out of memory exception today, these problems are the worst kind of problems to find, well not so difficult to find but can be tricky to solve them.

It's so hard to find out where the problem is and then when you do fixing them is really difficult. I think it's one of the major weakness of many Java programmers is that sometimes we have a bit of a lazy style when it comes to memory management because we usually trust the JVM to come and help us out and of a jam and Garbage collect all the objects we have left dangling.

We have done some sniffing about checking the Tomcat server logs and seeing how much memory is being consumed. It also brings me onto the subject of load testing, I haven't ever really done any load testing, except for one occasion where we got 20 people from the company to all go through a test script and log times between events/pages etc.

It's funny that we leave this testing until last because after that it is going to be difficult to change it. When you look at code and think how can I optimise the memory usage here, the main things you can come up with are minor improvements. To have a real effect on the memory usage I think you are really going to need a bit of redesigning of the objects used and left dangling.

It's always interesting to look at your code in a heuristic way and it does help to give you a good understanding of the code and look at parts you don't often look at. Plus any improvement/optimization and refactoring of the code can only be a good thing, as long as a bug doesn't slip in.

We were looking at the Struts actions today and we believe that any instance variables in an action is shared between any call that calls that action. This means that firstly these variables are hanging around and secondly two separate instances of the same action could be sharing variables in an unplanned way. This gives us something to investigate and probably create a best practice not to use instance variables in base actions and only use static finals and to pass in the other variables.

I did google OutOfMemory Exception in struts but nothing came up so if anyone has any knowledge on the subject or links to articles please add them in on the comments. I have discussed Java and Memory management on a blog entry before http://hoskinator.blogspot.com/2006/04/is-memory-management-in-java-garbage.html


  • Suggest reading section 4.4.1 in the struts user guide, building controller classes.

    The most important principle that aids in thread-safe coding is to use only local variables, not instance variables, in your Action class.

    By Blogger Billy Bob Bain, at Tue Jun 13, 07:22:00 pm 2006  

  • This may be helpful


    By Anonymous Anonymous, at Wed Jun 14, 01:33:00 am 2006  

  • Search the Struts User Mailing List archives (http://www.mail-archive.com)

    This link may be of use:

    By Anonymous Anonymous, at Wed Jun 14, 08:41:00 am 2006  

  • Thanks for the comments and links to resources they have been really useful.

    The Struts user guide was very useful and I feel a bit of a chump for not actually looking their first, this is a problem with googling problems sometimes it sends you off down useless paths but if I had thought about it I might gone to a more useful website.

    Thanks again for the useful posts

    By Blogger Hosky, at Wed Jun 14, 10:55:00 am 2006  

Post a Comment

<< Home