Optimize the View State

To maintain a Visualforce page’s view state, Lightning Platform stores the state of components, field values, and the controller as an encrypted string in a hidden form element. The view state has a limit of 170 KB. A large view state requires longer processing times for each request, including serialization and deserialization time, and encryption and decryption time. If you reduce the view state size, then your page can load faster and stall less often.

To examine a Visualforce page’s view state, set the Development Mode and the Show View State in Development Mode user permissions. The View State tab in the development mode footer displays the distribution of the view state. Make sure you know the view state size of each page, and test with large data volumes to prevent issues that can occur after deployment.

To reduce the view state:

  • Use filters and pagination to reduce data that requires state.
  • Declare an instance variable with a transient keyword if the variable is only useful for the current request. A transient variable isn’t included in the view state.
  • Refine your SOQL calls to return only data that's relevant to the Visualforce page.
  • Reduce the number of components that your page depends on.
  • Make data read-only. Use the <apex:outputText> component instead of the <apex:inputField> component.
  • Use JavaScript remoting. Unlike the <apex:actionFunction> component, JavaScript remoting doesn’t require an <apex:form> component. JavaScript remoting doesn’t reduce the overall view state of a page, but your page generally performs better without the need to transmit, serialize, and deserialize the view state. The tradeoffs are the loss of the reRender attribute and the necessity of additional JavaScript code to handle callbacks.