I know that this error is very common when the heap size is more than 12MB for an Asynchronous call, in my case, web service call out. I have referred this documentation: https://help.salesforce.com/articleView?id=000321537&language=en_US&type=1&mode=1 already, which only provides solution while we are having heap size larger at our implementation.

But, in my case, the response I have received via a web service call out is exceeding the heap size limitation and causing the exception. Is there a way/solution for this scenario? My search on the same was no fruitful, so looking for some guidance.

public String rawXMLResponse;
rawXMLResponse = resp.getbody(); //This is where I am getting the heap limit exception. 

1 Answer 1


There are no tricks I know of to help deal with this. Callouts that return too much data just simply can't be handled natively in Apex.

Realistically, the response can't be greater than 3 MB for synchronous transactions or 6 MB for an async transaction (unless you're not using the response body and just looking at the response code, for example) because we need at least 2x the response size in heap space.

  • The response body itself consumes X heap space
  • Storing the response in a new string or processing the response (say, deserializing a JSON response) requires at least X heap space (again)

And that's just to get the data into a workable state. If you want to create or manipulate records based on that data, you'll need even more heap space.

Regex can't really help, because matcher strings are limited to 1 million characters

You can take substrings, but I don't know if any of the other string methods work with strings of that magnitude.

Your best bet here is to take the callout, and introduce a middle man.

  • Apex would make a callout to this intermediary server
  • The intermediary server would forward (or otherwise perform) the callout
  • The intermediary server would capture and process the result
  • The intermediary server would then pass more reasonably sized data back to Salesforce (perhaps in the same transaction, perhaps in separate transactions)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .