error

Maximum view state size limit (170KB) exceeded

What does this error mean?

Visualforce pages store component state and controller variables in something called the view state. This allows Salesforce to maintain page state between requests.

Salesforce enforces a limit of 170 KB for view state size. If your page stores too much data, Salesforce throws the View State Too Large error.

Common Causes

1. Large collections stored in controller

Storing large lists of records in controller variables increases view state size.

2. Unnecessary variables in controller

Public properties automatically become part of view state unless marked otherwise.

3. Nested components and large page structures

Complex Visualforce pages with multiple components increase the stored state.

4. Storing entire sObject lists

Controllers that store large lists of sObjects can exceed the view state limit quickly.

How to Fix It

Solution 1 — Use transient variables

Apex

public transient List accounts;

Mark variables as transient to prevent them from being stored in view state.

Solution 2 — Reduce stored collections

Apex

List accounts = [
    SELECT Id, Name
    FROM Account
    LIMIT 50
];

Only retrieve the records you actually need.

Solution 3 — Move logic to JavaScript or Apex services

Use AJAX, JavaScript remoting, or Lightning components to reduce view state dependency.

lightbulb

Pro Tip: Use the View State Inspector in the Visualforce developer tools to see which variables contribute the most to view state size.