Saturday, 25 November 2017

Heap Size In Salesforce

What is heap size?
Many times developers have faced this problem. It is very important to understand what is heap size in salesforce.
Heap size is the memory allocated to the objects created in apex to store data in it during run time.
In apex code whenever an object is created, certain memory is allocated to it dynamically during the runtime of the program.
This memory is used to store data in it.

Watch for heap size:-
1. use limit methods to get heapsize in at any instance of time during runtime.
   Limits.getHeapSize() --> Returns the amount of memory already used.
   Limits.getLimitHeapSize() --> Returns the total memory size allocated in the current context.

2. You can watch out for heap size in the debug log as shown below :-
   

How to avoid heap size errors :- 
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.
    example :- for(Account ac:[select id, name from account]){ // insert logic here}

2. Store required amount of data only. While doing a query ususaly we retrieve all fields which increases the heap size. One should only query those fields which they will work on.

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.

Create record in lightning

With the advancement of lightning, we are getting introduced to new features of lightning that helps in creating a better functionality in ...