Over the years, I have been involved in some projects involving web development in J2ee. JAVA memory usage is an issue that’s common among all.
Following are some of the questions that come across to a developer regarding the JAVA memory:
- Memory Usage Statistics.
- Trending of Memory statistics.
- Memory Leak.
- Performance optimization in case memory leaks occur.
When it comes to answering the above, the most common suggestion is to enable heap dumps and analyze it using a heap analyzer tool. However, there are times and projects where these options are not approved off and developer is always asked to review code again and again. This is again a frustrating option for all and specially for someone who has just joined a maintenance project and reading through code is not a feasible option. It has happened to me and I did the following to solve some of my problems and eventually all.
Instead of analyzing heap dumps, I decided to do the following:
- Add a request filter to my J2EE application. This is a standard approach where in you can define a filter for your application by configuring it in web.xml. You can do a internet search for details on this.
- Add following log statements in the filter:
- URL fired.
- Gather data from daily app usage and build some trending statistics.
Not only were we able to decide an optimum memory setting for our server, we were able to detect leaks as well. However, I agree detecting leaks wasn’t as simple as it’s with other tools considering the debugging effort that’s involved.It is not a conventional approach but comes in handy when projects don’t want to involve costs and maintain equilibrium at production systems as well.