function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rathod BabuRathod Babu 

How to avoid heap size error in apex class

Hi Everyone,

I'm having problem with heap size error
We have a trigger from the trigger we are calling so any classes,if we do any changes in the record it showing heap size 

Please help 
Thanks in advance 

Regards
Rathod
Raj R.Raj R.
It is hard to judge what the issue is without example or the actual code, but have you doubled checked you are following best practices? Please provide a little more context. 
  • do you have nested for loops?
  • are you doing a query outside of for loops and using things like Maps to parse through the data?
  • are you trying to perform asynchronous or synchronous operations?

https://help.salesforce.com/articleView?id=000004186&type=1 
Akshay_DhimanAkshay_Dhiman
Hey Rathod,
Here is the solution to your problem. Hope this will help you.

Salesforce enforces an apex heap size limit of 6MB for synchronous transactions and 12MB for asynchronous transactions, and if you exceed the limit, you will receive the error message.
The apex heap size is too large error occurs when too much data is being stored in memory during processing. The limit depends on the type of execution(e.g. synchronous vs asynchronous calls)
The resolution for this error is using best practices for running within the heap size.
Here is an example of apex code that will exceed heap size limit.

public class HeapCheckIssue{

   public static void myTest(){
        String tStr = 'aaaaa bbbbb ccccc ddddd eeeeee fffff ggggg 11111 22222 33333 44444';
        List<String> baseList = tStr.split(' ');
        List<String> bigList = baseList;
        Map<integer, List<String>> SampleMap = new Map<integer, List<String>>();
        SampleMap.put(1, bigList);     
        for (integer i=0; i<50; i++) {
            List<String> tempList = new List<String>();
            tempList = Samplemap.get(1);
            bigList.addAll(tempList);
        }
        system.debug('FINAL LIST SIZE IS '+bigList.size());
   
   }


}
This example shows an incorrect use of collections. 
The List baseList, the value of SampleMap and the List tempList are pointing to the same memory address. As a result, the heap size doubles with each iteration of the loop.

Resolution: This example shows a similar algorithm that will not exceed the heap size:

public class HeapCheckIssue{

   public static void myTest(){
      String tStr = 'aaaaa bbbbb ccccc ddddd eeeeee fffff ggggg 11111 22222 33333 44444';
    List<String> baseList = tStr.split(' ');
    Map<integer, List<String>> SampleMap = new Map<integer, List<String>>();
    List<String> bigList = baseList;
    SampleMap.put(1, bigList);
    List<string> myList = new list<string>(); //Declare a new list
    for (integer i=0; i<50; i++) {
      List<String> tempList = new List<String>();
      tempList = SampleMap.get(1);
      system.debug('templist: ' + tempList.size());
      system.debug(' bigList: ' + bigList.size());
      myList.addall(tempList); //original code is  bigList.addall(tempList);
    }
    system.debug('FINAL LIST SIZE OF bigList IS '+bigList.size());
    system.debug('myList IS '+mylist.size());
   
   }


}

If you find it helpful, mark as a Best Answer so that other's also get help from this.

Thanks
Akshay