Memory per Session grows
We are experiencing high memory consumption using JSF 2.2 (2.2.12) with Mojarra. After investigating our load tests it turned out that the size of the data in our ViewScoped Beans is quite high (sometimes more than 1MB). Anyway - when navigation from view to view, the session memory size grows and grows. We can't decrease the size of the beans on short-term, so this behavior has quite some impact.
Solution 1 - Changing Context Params (not working)
Now - we played around with the offical context parameter from Mojarra which are set to 15 by default:
com.sun.faces.numberOfLogicalViews
com.sun.faces.numberOfViewsInSession
Changing those parameters to a lower value did not have any impact on the memory consumption in our load tests.
Solution 2 - Changing activeViewMapsSize (working)
We were debugging Mojarra and found the following Code in ViewScopeManager
:
Integer size = (Integer) sessionMap.get(ACTIVE_VIEW_MAPS_SIZE);
if (size == null) {
size = 25;
}
The default size for keeping the last visited views seems to be 25. Seeing this, we implemented a Session Listener which sets this value to 1:
public class SetActiveViewMapsSizeSessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent event) {
event.getSession().setAttribute(ViewScopeManager.ACTIVE_VIEW_MAPS_SIZE, 1);
}
}
That obviously worked. The memory stopped growing since only 1 view is kept.
So why 25 Views in Memory ?
So Mojarra keeps a history of 25 Views in Memory if not a different value is defined in Session. I can't find any documentation about this. Can someone explain what is this for? Is it for Browser Back? We have caching disabled on our JSF pages. So browser back will always create a new view. This shouldn't be an issue for us.
Is Solution 2 a valid approach? Could someone explain the drawbacks of this approach?
Update 1
After various comments and a deeper debugging, it turned out that:
com.sun.faces.numberOfLogicalViews
defines the logicalViewMap size, which stores only(!) the state of the ui component treecom.sun.faces.application.view.activeViewMapsSize
defines the size of the activeViewMap, which holds the ViewScoped beans
When changing numberOfLogicalViews
to 1, mojarra will still keep track of all view scoped beans of the last 25 views. When you configure it the other way around - numberOfLogicalViews
to 15 and activeViewMapsSize
to 1 - the view cannot be correctly initialized due to missing data I guess. We didn't even get an exception. I would like to understand, why mojarra chose to set the activeViewMapsSize
higher than the numberOfLogicalViews
and not the same since we want to tune our memory consumption without getting an unpredictable behavior.
Update 2
We created an issue at Mojarra: JAVASERVERFACES-4015.