View State in Visualforce Page

What is View State ?

View State, Well as the name suggest, it holds the state of your visualforce page. And by state ,I mean all the data which is required and sufficient to recreate or regenerate the VF Page,when it comes back from server.

Did you get it? Don’t worry we will discuss every thing you need to know about view state.

So first thing first, Whenever there is some concept try to understand why is it needed. So,lets go there.

What is the need of View State in VF Page?

So, whenever user requests a web page(VF page in our case),by using URL or by any ways, suppose the page contains form, user fills that form and submits it.
Now if user input is incorrect or there is some validation error, the server responds with error message right? And again user will fill the field values correctly and this time suppose page got submitted successfully.

What is actually happening behind the scenes? Well the browser is issuing the HTTP requests.The VF page is retrieved first time using GET request always. And whenever there is a form submission via save button or so, it is a POST request.
Also in VF page terms it is called POSTBACK why? obviously, because the data on the form is being posted back on same page.What user thinks here is that it is stateful interaction with server, as the page state is changing based on its previous state(previous values) right.

But don’t you thing something is weird here? Guess what? our HTTP is state less protocol right and what we know about it is that it does not maintain the state between transactions.That means the GET requests and the POST request we made are treated as independent requests.

So how to maintain the state now. There must be some mechanism right? There comes the view state for rescue.

What are the things that are stored in view state?

  1. All non-transient and non static data members in the associated controller (either standard or custom) and the controller extensions
  2. The component tree for that page, which represents the page’s component structure and the associated state, which are the values applied to those components.
    For example : apex input tag etc.
  3. Objects that are reachable from a non-transient data member in a controller or controller extension.
  4. Small amount of data needed by visualforce for other housekeeping purposes.

From where can we see the view state of VF Page?

Well view state is in encrypted fomat.View sate can be enabled only if development mode is on.

Go to user’s detail page.Click on Edit and then Check the ‘Show View State in Development Mode’ checkbox.

After that preiew the VF page whose view state you want to check. You can find the View state tab at the bottom where development mode present.

What is maximim size of view state in VF Page?

Earlier it was 135 KB but With Spring’19 release it has been increased to 170 KB.

What are the issues VF page faces when having large view state size?

  1. View State limit exceeded error occurs.
  2. Page load time increases.

How to reduce the view state?

  1. Declare variables as transient or static.
  2. Reduce the Number of fields you are querying in SOQL. Only those fields that are required should be queried and shown on VF Page.
  3. Also, apply refined filter criteria in SOQL to fetch only those records which are required.
  4. Try to minimize the number of apex:form tags and use apex:actionRegion instead.
  5. Even if a single apex:form tag is used try to minimize the content in it.
  6. Decrease the collection items if they are not going to be used in the VF page.
  7. Nullify the variables once they are not in use.