Java – JSF Memory Usage Issue when using using session beans

jakarta-eejavajsf

I am working on an application that has a navigation tree which is a session bean.
Every time I invoke the page that contain this bean, my memory usage will increase. However, after few hours of inactivity, the memory still isn't freed. Are there any ideas why this happens, or workarounds?

Best Solution

You can explicitly set a session timeout in the deployment descriptor, or do it programmatically (though you probably don't want to do that in a JSF application).

Ultimately, this is in the control of the container - the server manages when to release resources regardless of the logical expiry settings.

You can help diagnose what is going on using listeners. For example, you could have your bean class implement HttpSessionBindingListener. It will be notified when it is added or removed from a session. Alternatively, you could watch all session events using a HttpSessionAttributeListener (JSF can use the session to manage view state, so expect some entries you didn't define yourself). The HttpSessionAttributeListener is defined in the web.xml:

<listener>
    <display-name>MyListener</display-name>
    <listener-class>
        somepackage.MySessionDiagnosticListenerImpl
    </listener-class>
</listener>

If you're just leaving the server inactive, it may just be that it relies other session requests to trigger expired session cleanup and you're observing an implementation detail. Or you may be leaking memory because you've set a reference to the object in some unmanaged class.

Related Question