Apex Heap Size too large Error in Salesforce

Apex heap size too large error in Salesforce

Salesforce is a cloud based multitenant platform. That means resources are shared among all the tenants who are using it.  Once our Salesforce application is customized to some level, we may see this error on some user action. 

Salesforce has defined some governor limits. These are threshold limits; one cannot go beyond these by any means.

 Below is the link for Salesforce defined Apex governor limits.

https://developer.salesforce.com/docs/atlas.en-us.salesforce_app_limits_cheatsheet.meta/salesforce_app_limits_cheatsheet/salesforce_app_limits_platform_apexgov.htm

 Out of all these limits, we have limitation regarding Heap Size.

Description

Synchronous Limit

Asynchronous Limit

Total heap size4

6 MB

12 MB

 

What is Heap

Heap is the memory allocated to the program. Heap size is the total memory consumed by the transaction. If there are a lot of things happening in one transaction, then heap size uses will keep increasing.

Suppose we have reached to the limit of the Heap size uses, and we still require some memory to be used for our ongoing transaction, then system will give error and it will not allow to go beyond the defined limit (6 MB for synchronous and 12 MB for Asynchronous transaction.).

 Root Cause

We get this error as we have consumed all the heap permitted to us in the single transaction.

If we do not follow the best practices while doing the development, our code will consume more memory than the normal scenario.

 There are some common reasons due to which we may encounter this issue.

1.     Unnecessary variable Declaration:

As a developer, we tend to declare various variable in apex programming.

We normally do not care if we are using it or not. Also, we might not even need that if we follow best practice.

Each variable we declare takes up space in memory and it gets added until we release that from memory. We can release that space once we nullify it.

See below example:

Here we can see that we have created Lists of Contact Object and in the end, we have not released this from memory as well.

We can do some modifications to this code.

a)     Do not use the List object variable at all which we are not using further.

b)    Use SQOL directly in the for syntax.


 

2.     Unnecessary fields in SOQL

Many times, developer queries unwanted fields in the SOQL, while those are not even used later.

Remember, a greater number of fields means more storage required in the memory for the same.

See below example:




In this code we are querying unnecessary fields in the query like (Email, LastName). We can reduce the memory required by not using those fields in the select statement.

Corrected Code:

 



3.     Querying unnecessary records in the SOQL:

Many times, we do not necessarily check how many numbers of rows we are getting in the SOQL query output.

Remember every record we fetch, requires heap memory to store those data.

 See sample code below

 


We can see that we are getting all records in the SOQL and then checking if the record has specific email id, then we are performing some action. Instead of getting all the records here, we can simply put a filter in the SOQL itself. It will save heap size.

See the corrected code below:


 

   Some more troubleshooting ideas.

1.     Check the Heap size uses in the debug logs:

Raise the debug log for the user and see the heap uses and refactor the code at the place where it is taking more heap size. 

2.     Developer can also check the heap size uses in the code:

Limits.getHeapSize():  using this developer can get the amount of heap size being used.

Limits.getLimitHeapSize(): Using this developer can get to know about the total limit of heap size.

Below are the uses of both in below code snippet.



Output in the logs for the same:


We can clearly see that at line 9 it shows how much memory it is consuming, end at line 10 it shows total memory available (which is 6 MB).

At line 12, we can see that after we make the list variable null, it clears the memory and now it is reduced to 1180 bytes from 5474 bytes.

 

3.     Use transient keyword for variables:

This will create instance variable that cannot be saved and that will save heap requirement.

 

I will keep adding more into this. Please let me know in the comments if you have some more ideas or have any doubt.



                  

                                                         


No comments:

Post a Comment

Async/Await Concept in Javascript/LWC

  Concept of async and await in JavaScript async and await are used in asynchronous programming in JavaScript. They help us write clean...