In a sandbox environment, I am writing an inbound email handler. I process the email's attachments asynchronously. I am getting the following error:
System.LimitException: Apex heap size too large: 28966369
on the following line:
String encodedString = EncodingUtil.convertToHex(filedata);
Can someone explain to me why I'm getting this error given the following debug statements?
The code that produced these debugs:
System.debug(Limits.getHeapSize() + '/' + Limits.getLimitHeapSize());
String encodedString = EncodingUtil.convertToHex(filedata);
System.debug(Limits.getHeapSize() + '/' + Limits.getLimitHeapSize());
I seem to always surpass the heap size limit as my code is running and handling the large attachment files. Upon processing a 6MB+ attachment file, I receive the error.
- How is the heap size increasing if my entire code is on a loop with variables that keep overwriting themselves? The debugs suggest that this is in fact happening, that the heap size is getting reset upon each iteration, but then...
- how does converting to hex from 15m bytes result in a limit exception while the same conversion is okay in the previous iterations?
- How can I check beforehand whether I can process (convert to hex) a byte collection if I can't know a reliable threshold value, given the debugs?
- I read here that on production, I will receive errors upon surpassing 12MB in heap size for asynchronous transactions while the same limits apply but aren't enforced on sandbox. Is this true? In that case how should I proceed?