Skip to content
javahacks.dev
JavaServer Faces

JSF View State and Why It Grows in Memory and Pages

Understand JSF view state: server side versus client side saving, session memory per view, the number of views setting and how to keep it small.

5 min read
Summarize in AI
Flat illustration of a browser page built from server-side components, with the OpenJDK, CSS, Vaadin, HTML5 and Spring logos.

Every JSF page you visit leaves something behind on the server, or inside the page itself. That thing is the view state, and it explains slow pages and memory growth in long-running sessions. The cycle that uses it is in the JSF lifecycle.

What the state is

On the first request Faces builds a tree of components for the page. On a postback it must restore that tree to apply the submitted values to the right components. Rather than rebuild everything from scratch each time, it saves the parts that differ from the freshly built tree, such as a component's changed value or a dynamically added row. That saved data is the view state.

A page with a form contains a hidden field:

HTML
<input type="hidden" name="jakarta.faces.ViewState" value="-2873445029327312394:5562771213443392314" />

Server-side saving

This is the default:

XML
<context-param>
    <param-name>jakarta.faces.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
</context-param>

The field holds a key, and the state lives in the HTTP session. Pages stay small. The cost is session memory: each view you open adds an entry, and opening several tabs, or a page with a large data table, adds more. Two settings in web.xml limit growth:

XML
<context-param>
    <param-name>org.apache.myfaces.NUMBER_OF_VIEWS_IN_SESSION</param-name>
    <param-value>10</param-value>
</context-param>

Mojarra names its equivalents com.sun.faces.numberOfViewsInSession and com.sun.faces.numberOfLogicalViews. When the limit is exceeded, the oldest view is dropped. A user who goes back to it gets ViewExpiredException.

Client-side saving

XML
<param-name>jakarta.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>

The state is serialized, compressed and, in a safe configuration, encrypted into the hidden field. Memory use on the server drops and sessions can be replicated more cheaply. In exchange every page and every AJAX request carries that field, so large views are slow on the wire. Keep the encryption on and set a stable secret, or a restart invalidates every open page, and an unprotected state is a serialization attack vector, a risk class covered in insecure deserialization.

What makes state grow

  • A big component tree. Every component, including every cell in a table, contributes. Paginate large tables.
  • Dynamic components. Components added in code are recorded.
  • Data inside view scoped beans. A @ViewScoped bean lives as long as the view and is serialized with the session when the application server replicates it. A bean holding a 50,000 row list multiplies by the number of views.
  • Open tabs and bots. Each tab counts as a view, and a crawler hitting forms creates views quickly.

Reduce it

  1. Prefer stateless views for pages without postbacks: <f:view transient="true"> skips saving state.
  2. Load data lazily and page it, see lazy DataTable, so beans hold one page.
  3. Choose the narrowest scope that works.
  4. Cap the number of stored views.
  5. Measure with a heap dump, with the approach in the heap space article.

The settings that bound the server-side state

With server-side saving, each view a user opens adds an entry to the session, and the runtime keeps only a limited number of them. Mojarra reads two context parameters, and both default to 15:

XML
<context-param>
    <param-name>com.sun.faces.numberOfViewsInSession</param-name>
    <param-value>10</param-value>
</context-param>
<context-param>
    <param-name>com.sun.faces.numberOfLogicalViews</param-name>
    <param-value>10</param-value>
</context-param>

The first caps the saved views per session, and the second caps the logical views, which are the pages a user has open in tabs. When the cap is reached, the oldest view is dropped, and a user who clicks Back and submits that old page gets a ViewExpiredException. A lower number saves memory and shortens the Back-button history that works. MyFaces uses its own parameter names, org.apache.myfaces.NUMBER_OF_VIEWS_IN_SESSION among them, so check the implementation in use.

Estimate the memory instead of guessing

The cost is the size of one saved view times the cap times the number of concurrent sessions. A view of 300 KB, 15 views per session and 2,000 active sessions comes to 9 GB, which no heap holds. Measure the size of one view with a heap dump of a test instance, or find the instances that dominate:

Shell
jcmd <pid> GC.class_histogram | grep -iE 'faces|UIComponent|UIViewRoot' | head

If the component classes top the list, the tree is the problem. A table that renders 500 rows, each with five input components, creates thousands of component objects per view. Paging the table to 25 rows divides that by twenty, and no tuning of the cap reaches the same effect.

Pages that need no state

A page that only displays data, such as a report or a landing page, has no use for a saved view. Mark it transient and Faces skips saving the tree:

XML
<f:view transient="true">
    <h:outputText value="#{reportBean.summary}" />
</f:view>

A transient view cannot take a postback, since there is nothing to restore, so use it only for pages whose links are plain GET requests. The same goes for forms posted with <h:form>: if a page never needs JSF's postback, consider a plain HTML form or a REST call.

Client-side saving and its limits

Client-side saving stores the state in a hidden field, so it uses no session memory and makes every page larger. The size of the field is the cost, and it travels with each postback, in both directions. Always keep encryption on for the field: the default is on, and a configured password makes it survive a restart and work across a cluster. Without a password, a restart invalidates every open form, so users see ViewExpiredException after a deploy. Client-side state suits a stateless cluster with small pages, and server-side state suits large pages with few concurrent users.

ChoiceMemory costNetwork costBehaviour on restart
Server sidePer session, bounded by the capSmallViews lost unless sessions are persisted
Client sideNoneHidden field in each requestWorks if the key is fixed
Transient viewNoneNoneNot applicable, no postback

Partial state saving

Faces stores only the changes to the tree since it was built, and rebuilds the rest from the template on restore. It is on by default and it is the reason state is small at all. Turning it off with jakarta.faces.PARTIAL_STATE_SAVING set to false is occasionally suggested to fix an odd bug, and it multiplies the state size. Fix the component or the dynamic tree manipulation that misbehaves, and leave the setting alone.

Where this does not apply

An application with only GET pages and h:link navigation has almost no view state. If the symptom is memory growth over days, a leak in application-scoped caches is equally likely. The reference is the Jakarta Faces specification, and the Mojarra project documents its context parameters.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.