While uploading a file, we send base64 and urlencoded blob(3 MB size) to Apex. In apex method, while using EncodingUtil.urlDecode method, we are hitting the heap size limit.Strange thing is, same code is working fine in one of the other sandbox even the variable size is doubled while URL Decode, So one org i am hitting Heap limit and other one not giving Heap error even thoush it holds same sized variable. Below is my code

    public static String saveTheFileAttachment(Id parentId, String fileName, String base64Data, String contentType) {
        String fileId = '';
            system.debug('Heap before' + Limits.getHeapSize());
            base64Data = EncodingUtil.urlDecode(base64Data, 'UTF-8');          
            system.debug('Heap after 2' + Limits.getHeapSize());            
            ContentVersion conVer = new ContentVersion();
            conVer.ContentLocation = 'S'; 
            conVer.Case_Id__c = parentId;
            conVer.PathOnClient = fileName; 
            conVer.Title = fileName; 
            conVer.VersionData = EncodingUtil.base64Decode(base64Data); 
                insert conVer;
                fileId = conVer.Id;
                throw new AuraHandledException('User does not has access to create file. Contact with your Administrator');
        }catch(Exception e){
            System.debug('Exception Line:'+e.getLineNumber());
            throw new AuraHandledException(e.getMessage());
         return fileId;        

1 Answer 1


Heap limits are flexible. That is, you can briefly exceed this limit when system resources are available. Here's a simple example that may or may not work in your org:

Account[] a = new Account[0];
String[] k = new String[] { 
for(String s: k) {
    a.add(new Account(Name=s.repeat(3000000)));
// System.AssertException: Assertion Failed: Assertion failed: 51001418

Note that I use a failed assertion just to make it easier to find the result without having to read the debug logs.

Here, I'm getting away with using 51MB of heap. And this isn't asynchronous code, or an email handler, it's Execute Anonymous. In my org, going to 'r' results in a limit exception:

System.LimitException: Apex heap size too large: 54001435

I've managed to get as high as 138MB of heap usage without an exception using a now-patched bug that I discovered some years ago.

Just be warned that breaking Limits.getLimitHeapSize() by even 1 byte may trigger a LimitException. You should design your code to avoid hitting this limit as much as possible.

In your case, lightning-file-upload supports up to 2GB files, and requires no Apex. This may be a suitable alternative.

  • Thanks for the response, Problem with "lightning-file-upload", upload event will not fire in standalone apps or lightning out. Sorry i din t mentioned earlier, I am using this file upload functionality in custom application being accessed through site URL. Any Idea why it's always giving Heap error in ORG and working fine in other ORG with same code and same file. Jul 10, 2023 at 5:47

You must log in to answer this question.

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