Thursday, 24 September 2020

View State Error

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

Wednesday, 23 September 2020

Heap Size Too Large

Salesforce enforces an Apex Heap Size Limit of 6MB for synchronous transactions and 12MB for asynchronous transactions. The "Apex heap size too large" error occurs when too much data is being stored in memory during processing.


Some strategies on how to write Apex scripts that run within heap limits.

 

1. Use soql for loop. This avoid heap size as we are not storing the data in any variable but directly processing it in the loop.

2. Store the required amount of data only. While doing a query usually we retrieve all fields which increases the heap size. One should only query those fields which they will work on. Reduce heap size during runtime by removing items from the collection as you iterate over it.

3. Use local variables instead of class level variable to store large amount of data.   storing data in local variable will go out of scope as soon as the method gets over. This helps in cleaning heap size after use.

4. Use of Transient keyword to declare instance variable that can not be saved, and shouldn't be transmitted as part of the view state for the Visualforce page.

e.g: Transient Integer tempVar ; Some apex objects are automatically considered transient, i.e their value does not get saved as part of the page's view state. These objects are SavePoints, PageReference, XMLStream Classes, etc. Static variables also don't get transmitted through the view state.

5. Use Limit methods. Use heap limits methods in your Apex code to monitor/manage the heap during execution.

Limits.getHeapSize() – Returns the approximate amount of memory (in bytes) that has been used for the heap in the current context.

Limits.getLimitHeapSize() – Returns the total amount of memory (in bytes) that can be used for the heap in the current context.

// check the heap size at runtime
if (Limits.getHeapSize > 275000) {
     // implement logic to reduce
}