All about them curves…

Tzhe’ela Trooper
3 min readJun 1, 2020

Salesforce Apex Heap Limits.

Heaps, who needs them?! Pffff…. Am I right?!
Calm down all you infrastructure engineers, just kidding.

Oh, this is a good time to mention that if you just googled: Why my query returned only 200 records, even though I didn’t set a limit? or, Why even with a @ readOnly annotation I’m still not able to loop through more than a few thousand records - even though Salesforce told me I can return 1m query rows? or How to avoid Apex heap limits when processing shit load of data? Feel free to jump to the end.

Where were we? Ah Hips, sorry - Heaps are storage spaces. For anything that is running some type of logic, we are talking about memory storage a.k.a the ability to keep track. And like any type of storage, it has its limits.

I bet it happens to you as well - you are engaged in a meeting, a discussion, a thinking process, whatever, and suddenly your mind is empty. Completely blank. Who am I? What was I doing? What was I saying? Why am I here again? Oh yeah, I was talking about heaps. That’s an example of your brain heap maxing out and your brain rebooting to some extent. Rebooting, you should know, is the ultimate time to call that loyal garbage collector of ours and .Dispose(of some mess) . After the reboot, you quickly check your cache, and based on what you were able to nicely catalog, a new processing thread starts, supported by the newly available memory space. Sometimes our brains need more than a few seconds or minutes to tidy up and free space again, so we call it a day and keep this complex thinking exercise to a different time.

Sure, as you are the infrastructure engineer of your body, you can run routine maintenance operations to make sure you keep growing your heap size, pushing back the threshold for zoning out. Wow this was a long sentence. Where you able to keep track? Did you find yourself reading it more than once? Let’s continue. Assuming you are in control of the machine you are working with, you can take the same approach and buy new and improved hardware from time to time. More often than not however, you are stuck with the limits given to you by others (pointing the finger at you Salesforce 😏).

For now, Apex heap limits stand at 6MB for synchronous processing, 12MB for asynchronous processing, and 36MB for Email services.

Of course we also have other limits such as CPU, SOQL, SOSL, DML, query depth, triggers stack depth, callouts, timeout for callouts … you get the point, but did you know there is also a JSON string heap limit?! Because there isn’t! You can still get an error mentioning that Class.System.JSON.Serialize has reached its heap limits, but this is apparently just Salesforce way of saying - Hmmm… Hello there, can you please serialize fewer records? It’s getting hard to keep track on the records in their SObject structure AND as a string… Kindly modify your code and try again.

So how do you still get your crazy amount of records and loop through them? Regardless of the readOnly annotation, even within your regular 50k query rows, the below iteration structure is recommended:

If you didn’t already, try returning 20k records or some other stupid number in a for loop, and confirm for yourself that only 200 are being processed. However, if you use the above structure, and assuming you chose the magic number that allows Salesforce to keep track of the records, the loop, and the thing you are doing, it will work.

Don’t simply take my word for it, check out this Salesforce KB article for reference.

Ta-Da!

Also - Check out this FQA page for other cool facts for optimizing queries.

--

--