Types of Data Saved in View State:
Following data are saved in Viewstate
1. All public and private data members present in
Standard, Custom and Controller extensions.
2. Objects that are reachable from data members
in Custom and Controller extensions.
3. The component tree for that page that
represents the page’s component structure and the associated state which are
the values applied to those components.
4. Small amount of data needed by visualforce for
other general purposes.
Transient Variable and
Objects:
As Viewstate is transferred over HTTP with every
response, it is very necessary to control the size of View State. There are
lots of situations where data is not needed after the postback or not needed on
the Visualforce page then in that case we can make variable or object
transient. The transient variables are not passed to view state and therefore
not stored in View State.
Reducing View State Size:
As
we know that total size of ViewState can be 135KB (On date of
writing this article), There maybe chances that you will hit maximum allowed
limit or improve performance of Visualforce page by reducing Viewstate size:
1. Declare variables as Transient if possible. All public and
private data members present in standard, custom, and controller extensions are
saved as a part of the view state.
2. Declare variable as Static, as it is not saved
in View State.
3. Minimize the number of forms i.e
<apex:form> on a page. Make use of <apex:actionRegion> instead of
using 2 or more forms.
4. Refine your SOQL to retrieve only the data
needed.
5. Instead of using
<apex:commandlink> or <apex:commandbutton>, go for
<apex:outputlink>
6. Decide if some data can be read-only and use the <apex:outputText> component
instead of <apex:inputField>.
7. If you want to manage your own state, instead
of using <apex:form> use HTML <form> tag instead.
8. Recreate state instead of saving in Viewstate.
Means if you can use SOQL instead of saving in some object or List, use it.
9. You can also use Web service call, Ajax
Remoting to recreate state instead of saving in Object.
10. Use JavaScript remoting. Unlike the <apex:actionFunction> component, JavaScript Remoting does not require a form component. This technique doesn’t reduce the overall view state of a page, but your page generally performs better without the need to transmit