Introduction
I was trying to solve an issue in our code where the@ViewScope
beans were not being garbage collected. I spoke a number of times with Manfred Riem at Oracle about the weirdness of this issue. The issue simply put was that we were facing a memory leak where the instances of @ViewScope
objects were not being removed from the view map. As a result, the pages were being kept in memory. The view map is limited to 32 views which helped to hide the issue. In most cases, it would not appear to normal users of our application. The issue was suddenly evident when the view contained tens of thousands of objects. 32 x 10k is REALLY BIG! It really never made it to 32, the system would stall and crash at about 6 instances.
The Culprit
We had implemented our own customNavigationHandler
. This was working quite well on JSF 2.0.x, but a couple of things happened. The JSF implementation was changed to handle another view scope issue, and our implementation of the NavigationHandler
was changed from my original code. The new handler did not handle cleaning up the @ViewScope
object view map which is stored in the session. Oh, yeah, the view map in the session was the change to the API too.
The Solution
The solution turned out to be something simple, re-implement the same mechanism in the defaultNavigationHandler
to clear the @ViewScope
objects from the view map in the session.
Interesting Observations
I was trying to come up with a mechanism to clear the view map data from the session, and came up with aSystemEventListener
to test out some ideas. I thought I would share the code for people to see how the map is cleared. This is an approach to the issue, but as I noted, it was actually something missed in our NavigationHandler
. I thought I should post the code for anyone who was looking for ideas on how to manipulate the map, or clear data in it. So without further hesitation. Here is the code.
ViewMapSystemEventListener.java
To implement the listener, you need to add an entry to the faces-config.xml file as shown below.
2 comments :
Hi.
This was a really interesting post. Actually I need to destroy my viewscope beans
Whenever I click on a navigation menu. This is a problem
Because the navigation menu is a link to another page, but its action is
Not handled by the viewscope (is just a link) so navigating away from my viewscope
Won't trigger the destroy method.
I need to clear muy viewscope whenever there is a click on the html menu.
I am not sure, but in your ViewMapSystemEventListener, you define the viewId as global
Property, that gets updated with the Las ID visited, but if the EventListener
Is a singleton, shared among all users of the app, this viewId would be changed
In any event triggered by any user, so it can't work.
The event Listener should be a session event listener.
Thank you very much.
i.
This was a really interesting post. Actually I need to destroy my viewscope beans
Whenever I click on a navigation menu. This is a problem
Because the navigation menu is a link to another page, but its action is
Not handled by the viewscope (is just a link) so navigating away from my viewscope
Won't trigger the destroy method.
I need to clear muy viewscope whenever there is a click on the html menu.
I am not sure, but in your ViewMapSystemEventListener, you define the viewId as global
Property, that gets updated with the Las ID visited, but if the EventListener
Is a singleton, shared among all users of the app, this viewId would be changed
In any event triggered by any user, so it can't work.
The event Listener should be a session event listener.
Thank you very much.
Post a Comment