Ok, so just in case, here is a relevant link about the problem, saying basically that there is no fix because it is inherent to the mechanisms of JSF. I resorted to using the parameter step on the number of views, which seems the more clean.
But I also experimented some dirty fixing. I am posting it there for future reference. Use at your own risk, because there is no guarantee.
package com.mycompany.ajaxutils;
import javax.faces.application.StateManager;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.ajax4jsf.application.AjaxStateManager;
import org.ajax4jsf.util.LRUMap;
public class AjaxStateHelper {
private static final AjaxSateManagerUncover asmu = new AjaxSateManagerUncover(null);
public static void cleanAjaxStateManager() {
HttpSession currentSession = (HttpSession) FacesContext.getCurrentInstance().getExternalContext()
.getSession(false);
LRUMap lrumap = asmu.getLRUMap(currentSession.getAttribute(AjaxStateManager.class.getName()
+ ".VIEW_STATES_MAP"));
lrumap.clear();
}
/**
* This inner class is needed because we need to make a cast to a SynchronizedStateHolder
* which is an inner protected class inside AjaxStateManager
*/
protected static class AjaxSateManagerUncover extends AjaxStateManager {
public AjaxSateManagerUncover(StateManager parent) {
super(parent);
}
public LRUMap getLRUMap(Object stateMap) {
SynchronizedStateHolder s = (SynchronizedStateHolder) stateMap;
return s.getStateMap();
}
}
}
Just call AjaxStateHelper.cleanAjaxStateManager();
from wherever you need it in your application and it will free the memory for you. But beware the potential side effects, this is experimental code