Loading

Troubleshooting Salesforce Visualforce View State Limit Exceeded Error (170KB)

Publish Date: May 30, 2026
Description

Salesforce Visualforce pages use a view state mechanism to persist variable values between HTTP requests. Because HTTP is a stateless protocol, Salesforce serializes all public and private instance variables in a Visualforce controller into a hidden view state field that is sent to the browser and returned with each POST request.
The maximum allowed view state size is 170KB. When this limit is exceeded, internal users see the error:

"Maximum view state size limit (170KB) exceeded. Actual view state size for this page was \KB."

External users (such as Community members or Site Guest Users) may instead see a generic "Error while loading a VF page" message. If this error occurs intermittently for specific records, it is likely a view state size issue. Contact Salesforce Developer Support and request confirmation by reviewing server logs.
 

Resolution

How Visualforce View State Works

When a Visualforce page with a controller loads, Salesforce executes the following sequence:

  1. The Visualforce page is loaded and the controller class is instantiated — the constructor runs.
  2. All public and private instance variables are initialized.
  3. The variable values are serialized and stored in a hidden view state field on the page.
  4. The page is sent to the browser.

When the user clicks a button that triggers a POST request (such as an apex:commandButton):

  1. The browser sends the POST request, including the view state data.
  2. Salesforce deserializes the view state and reinitializes the controller variables — the constructor does not run again.
  3. Salesforce executes the action method.

How to Reduce View State Size

Use the following techniques to reduce view state size:

1. Use the transient Keyword Declare instance variables as transient when their values do not need to persist across requests. Transient variables are recalculated on each page load and are not serialized into the view state. For example, a DateTime variable declared as transient is recalculated on each page refresh, while a non-transient DateTime variable retains its value across requests via the view state.

2. Minimize the Number of Forms Minimize the number of apex:form tags on a page. Use apex:actionRegion to limit the portion of the page submitted in a POST request instead of using multiple forms.

3. Refine SOQL Queries Retrieve only the fields and records needed for the page. Avoid querying large collections of records or unnecessary fields that contribute to controller variable size.

4. Use Transient Variables for Large Collections All public and private instance variables in standard controllers, custom controllers, and controller extensions are included in the view state. Use transient on large collection variables (Lists, Maps, Sets) that can be re-queried on each request.

5. Replace Command Links and Buttons Where Possible Where navigation is the goal rather than a server-side action, use apex:outputLink instead of apex:commandLink or apex:commandButton, as output links do not trigger a full view state submission.

Additional Resources : 

Optimizing the view state best practice : https://help.salesforce.com/s/articleView?id=000386042&type=1

View State Tab https://help.salesforce.com/s/articleView?id=platform.code_dev_console_tab_view_state.htm&type=5

Visualforce Basics https://trailhead.salesforce.com/content/learn/modules/visualforce_fundamentals

Knowledge Article Number

000384480

 
Loading
Salesforce Help | Article