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

4 Comments:

Post a Comment

<< Home