Print this page

Error "View State limit(135) exception root cause"

Knowledge Article Number 000221049
Description 1. Facing an error of Maximum view state size limit (135KB) exceeded. Actual view state size for this page was <Above_135KB>KB, internally.
2. Sometimes external user(community, site guest user) can see only exception page with "Error while loading a VF page" error 
  - If it is occurring intermittently for few records, it might be possible that it is "view state size limit" exceeded error, to confirm please log a case with developer support, and ask them to confirm it by checking server logs.

 
Resolution General: In general HTTP is a stateless protocol. Therefore there is no native way to implement a "state" in HTTP.

In Salesforce it is necessary to be aware of a specific state of a variable. Such a state could be the value of a property in the controller.

Salesforce Example: Let's imagine you implement a simple form in visual force:
-VF page-
<apex:page controller="myController">
    <apex:form>
        <apex:commandButton value="do" action="{!do}"/>
    </apex:form>
</apex:page>
The corresponding controller looks like this:

-Controller-
public class myController
{
    public String abc;
    public myController()
    {
    abc = 'def';
    }

    void do()
    {

    }
}

Salesforce will execute the page as follows (simplified):

- Load visualforce page and detect the tag controller
- Your apex Class is called and the constructor is executed
- The variable abc is initialized by the constructor
- The value of your variable is stored in a "view state" field on your visual force page
- The page is sent to the client

When the user press the button "Do" calling the do():

- The browser starts a POST Request and sends data to salesforce
- Part of the request is your view state
- Salesforce "reads" your view state and reinitializes the variable abc without calling the constructor again.
- Salesforce executes the method "do"

In order to reduce the view state, make use of the following:

A. "transient" keyword - to declare instance variables that can't be saved, and shouldn't be transmitted as part of the view state for a VF page. For example :

---
transient DateTime theDate;
DateTime theOldDate;
---

Here , theDatecauses the transient date to be updated because it is being recreated each time the page is refreshed, whereas theOldDatecontinues to have its original value, which has been de-serialized from the view state, so it remains the same.

B. Minimize the number of forms i.e <apex:form> on a page. Make use of <apex:actionRegion> instead of using 2 or more forms.

C. Refine your SOQL to retrieve only the data needed.

D. All public and private date members present in standard, custom and controller extensions are saved as a part of view state. so use transient variables which are not stored in state of page.

E. Instead of using <apex:commandlink> or <apex:commandbutton>, go for <apex:outputlink> 

This is also mentioned in public article : https://help.salesforce.com/apex/HTViewSolution?urlname=Best-practice-for-optimizing-the-view-state&language=en_US  , where you can find what are the general cause and how to avoid it.

Easy way to see the view state on VF page : https://help.salesforce.com/apex/HTViewHelpDoc?id=pages_edit.htm&language=th , or this : https://help.salesforce.com/apex/HTViewHelpDoc?id=code_dev_console_tab_view_state.htm&language=th 

We have to follow this to avoid view state limit, I am 100% sure if you follow this you will never face the View State Limit issue. For a quick solution you can find the variable using "View state" tab which is taking too much state, and then you can use "transient" keyword in the controller with variable as mentioned above.




promote demote