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
GaurgkGaurgk 

Apex heap size too large: 39897844

HI All,

I am trying to write a trigger on object1 i.e after insert/ after update to create record on object2. But I am getting error (Apex heap size too large: 39897844) on saving record on object1.

Thanks in advance. 
SwethaSwetha (Salesforce Developers) 
HI Gaurgk ,
Salesforce enforces an Apex Heap Size Limit of 6MB for synchronous transactions and 12MB for asynchronous transactions and this is a governor limit which means it is a hard limit and cannot be modified or relaxed.

The "Apex heap size 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) and current value. 

Best practices for running within the Apex heap size

>Don't use class level variables to store a large amounts of data.
>Utilize SOQL For Loops to iterate and process data from large queries.
>Construct methods and loops that allow variables to go out of scope as soon as they are no longer needed.

Ref: https://help.salesforce.com/articleView?id=Apex-Heap-Size-Best-Practices&language=en_US&type=1

In order to resolve Heap size error issue, you need to revamp the code following the above best practices so that you don't hit these limits.

Please refer to the following link which discusses different strategies to manage the heap size.

>>http://blog.jeffdouglas.com/2010/08/16/managing-the-heap-in-salesforce-com/
>>https://success.salesforce.com/questionDetail?qid=a1X30000000ddWJEAY

Troubleshooting approach to fix heap size error

1) Add the following debug method to the affected class:
void checkHeapSize(String tag) {
system.debug(tag + ': Heap size is ' + limits.getHeapSize() + ' enforced is ' + limits.getLimitHeapSize());
}

2a) Call the method at various points in the code using a "tag" parameter so that you know where it's called in the code. For example: checkHeapSize('constructor1');

2b) In order to check the heap limit, put the check in the loop and then run the check every 1000th iteration. This is done by adding a local variable called "i" (i.e. "integer i;"), increment it for each iteration, and then running the following code: if (math.mod(i, 1000) == 0) } { call the method }.

3) Review the debug logs to find the heap size to see if it's exceeding the limit. For issues with scheduled batches, testing in the Developer Console will run code synchronously and show the limit as 6 MB but the heap size limit for scheduled batches is the asynchronous limit of 12MB.

4) If heap size doesn't exceed the limit, repeat steps 1-3. If the limit is exceeded, the debug logs should point out the problematic section of code that will need to be fixed.

If this information helps, please mark the answer as best.Thank you
Malika Pathak 9Malika Pathak 9
Hi Gaurgk,
    
Greetings!
    
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. 
It is important to review your code and follow best practices to ensure that the heap limit does not exceed the maximum.
Check whether you are querying too much data. 

Please mark it as the best answer if it helps you to fix the issue.

Thank you!

Regards,
Malika Pathak