title: JSF 2.2 Memory Consumption: Why does Mojarra keep the ViewScoped Beans of the last 25 Views in Memory?
body: <p><strong>Memory per Session grows</strong></p>
  <p>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 data in our ViewScoped Beans is quite high (sometimes more than 1MB). Anyway - when navigating 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.</p>
  <p><strong>Solution 1 - Changing Context Params (not working)</strong></p>
  <p>Now - we played around with the official context parameter from Mojarra which are set to 15 by default:</p>
  <pre><code>com.sun.faces.numberOfLogicalViews
  com.sun.faces.numberOfViewsInSession
  </code></pre>
  <p>Changing those parameters to a lower value did not have any impact on the memory consumption in our load tests.</p>
  <p><strong>Solution 2 - Changing activeViewMapsSize (working)</strong></p>
  <p>We were debugging Mojarra and found the following Code in <code>ViewScopeManager</code>:</p>
  <pre><code>Integer size = (Integer) sessionMap.get(ACTIVE_VIEW_MAPS_SIZE);
  if (size == null) {
      size = 25;
  }
  </code></pre>
  <p>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:</p>
  <pre><code>public class SetActiveViewMapsSizeSessionListener implements HttpSessionListener {
      @Override
      public void sessionCreated(HttpSessionEvent event) {
          event.getSession().setAttribute(ViewScopeManager.ACTIVE_VIEW_MAPS_SIZE, 1);
      }
  }
  </code></pre>
  <p>That obviously worked. The memory stopped growing since only 1 view is kept.</p>
  <p><strong>So why 25 Views in Memory ?</strong></p>
  <p>So Mojarra keeps a history of 25 Views in Memory in case of not a different value is defined in Session. I can't find any documentation about this. Can someone explain what this is 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.</p>
  <p>Is Solution 2 a valid approach? Could someone explain the drawbacks of this approach?</p>
  <p><strong>Update 1</strong></p>
  <p>After various comments and a deeper debugging, it turned out that:</p>
  <ul>
  <li><code>com.sun.faces.numberOfLogicalViews</code> defines the logicalViewMap size, which stores only(!) the state of the ui component tree</li>
  <li><code>com.sun.faces.application.view.activeViewMapsSize</code> defines the size of the activeViewMap, which holds the ViewScoped beans</li>
  </ul>
  <p>When changing <code>numberOfLogicalViews</code> 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 - <code>numberOfLogicalViews</code> to 15 and <code>activeViewMapsSize</code> 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 <code>activeViewMapsSize</code> higher than the <code>numberOfLogicalViews</code> and not the same since we want to tune our memory consumption without getting an unpredictable behavior.</p>
  <p><strong>Update 2</strong></p>
  <p>We created an issue at Mojarra: <a href="https://github.com/eclipse-ee4j/mojarra/issues/4019" rel="nofollow noreferrer">JAVASERVERFACES-4015</a>.</p>