Thursday 7 February 2013

How to deal with the error: "Maximum View State size limit (135KB) exceeded" ?

Whenever I have worked on some Web Services requirement, I have generally come across this error message : "Maximum View State size limit (135KB) exceeded".
This time I encountered this known error when I was working on Account Online Passwords enhancement for February Release 2013
So today I am taking time to jot down the way to minimize the probability of getting this error.

Few points about view state here:

The view state of a web page is composed of all the data that's necessary to maintain the state of the controller during server requests (like sending or receiving data). Since the view state contributes to the overall size of your page, performance of a page can depend on efficiently managing the view state.

  1. Salesforce allows Visualforce pages to have a maximum view state size of 135KB. The View State tab shows you which elements on your page are taking up that space. A smaller view state size generally means quicker load times.
  2. To minimize your pages' view state, you can optimize your Apex controller code and remove any superfluous Visualforce components used.
  3. For example:
    1. If you notice that a large percentage of your view state comes from objects used in controllers or controller extensions, consider refining your SOQL calls to return only data that's relevant to the Visualforce page.
    2. If your view state is affected by a large component tree, try reducing the number of components your page depends on.
    3. Another important point here is the use of Transient keyword. The Transient variable can not be saved, and shouldn't be transmitted as part of the view state for visual force page.

Point to be noted down is that:

  • Any apex component under tag are counted against View State size. So this could be reduced by using HTML/CSS/JavaScript components because they are not counted against apex view state limit.
  • Try using JavaScript Remoting instead of using apex action tags. In addition to decreasing view state size, you could get data near to real time.

I could show you a scenario where  how I reduced the View State size from around 136KB to around 85KB just by replacing with HTML tags 
in conjuction with tag.

Initial piece of code:

Refined piece of code

Note: However, here we need to use CSS for formatting Table header values.

So we can see how significantly the view state size is reduced just by replacing one apex tag with HTML tag. 

Supporting Links:

No comments:

Post a Comment

Thank you for visiting. Your comments are highly appreciated.