I thought I'd completed my first J2ee app, but with every page refresh, a little bit more memory is gobbled up until its all gone and I get a java.lang.outofmemory error.
My app is similar to the SUN Duke's bank tutorial but without any database writes. It has a dispatcher servlet that calls JSP pages. The JSP gets data for display by using a getDetails method on a session bean. The session bean gets the data from various entity beans.
Am I right in assuming that when the page is first displayed the entity beans will be created and will just be re-used when the page is refreshed? (Or will new entity beans be created each time?)
Each time the page refreshes I see the following info from print statements I've put into the entity bean class:
Any ideas for where to look would be greatly appreciated.
P.S (I'm using Apache-Tomcat/JBoss - I don't have a debugger so I don't know how many beans are being created)
Increasing memory usage isn't necessarily bad; the JVM will not really garbage collect unless it needs to, so it will eventually use up all the memory in the heap with which it was allocated at startup (-Xm option on java commandline I think).
OutOfMemoryError is pretty serious, tho.
I would start by looking at the memory parameters you are using on startup. I believe they default to 32M or 64M, meaning that the JVM can never have more memory than that.
Put in some debugging on available memory (see the java.lang.System and java.lang.Runtime classes). Try calling the garbage collector and see if that buys you any more time.
You shouldn't have to call the GC yourself, and the JVM should call it if it's getting low on memory, but if you have lots of large objects, and lots of new sessions, you may need to increase the heap size of the JVM.
In terms of your code, obviously it is more difficult to write memory leaks in Java than it would be in C/C++, but if you are creating lots of objects, and then holding onto them for a long time (e.g. creating lots of large HttpSessions with a sessiontimeout of an hour or never), memory could get used up that the GC doesn't think can be freed. You could try limiting what you put in the HttpSession, or set your sessiontimeout to be very small (5 minutes or something). Of course, that may not jive with your requirements.