Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How is heap size in salesforce calculated?

Asked by Darsh K May 22, 2023 1.1K views 1 answer
Share

About this question

I'm trying to send a lot of data from Apex to VisualForce Page via JS remoting because I want to create an Excel file , so just for testing purpose I uploaded a text file (4.87 MB, 5110400 Characters) as a static resource I got the file in Apex added it to a list of string.


Problem

I added the body of Static Resource to the list twice, below is my Apex and VisualForce code:

Apex Class:

public with sharing class HeapSize_Controller { @remoteAction public static List generateRandomObject() { List fileData = new List(); StaticResource sr = [ SELECT Id, Body FROM StaticResource WHERE Name = 'RandomFile' LIMIT 1 ]; String body = sr.Body.toString(); fileData.add(body); fileData.add(body); System.debug('========== Heap Size :: ' + Limits.getHeapSize()); return fileData; } }

VisualForce Page:

  [removed] var j$ = jQuery.noConflict(); j$(document).ready(function() { getData(); }); function getData() { Visualforce.remoting.Manager.invokeAction( '{!$RemoteAction.HeapSize_Controller.generateRandomObject}', function(result, event){ if (event.status) { console.log('======= List :: '+ result); } else { console.log('======= Error Message :: '+ event.message); } }, {escape: true,buffer: false} ); } [removed] 
Now the heap size in Apex is 10.22 MB (as per Debug logs)
But, when the page is loaded the remoting throw this error:
Why? Is it failing because the Synchronous Limit for Heap Size is 6MB? Then why does the remote say it's exceeding 15 MB?
And when I do this in my Apex method,
//String body = sr.Body.toString(); fileData.add(sr.Body.toString()); fileData.add(sr.Body.toString()); System.debug('========== Heap Size :: ' + Limits.getHeapSize());
The heap size now is 15.33 MB, even though I only added it twice to the list.
I then went ahead and did this:
//String body = sr.Body.toString(); fileData.add(sr.Body.toString()); fileData.add(sr.Body.toString()); sr = new StaticResource(); System.debug('========== Heap Size :: ' + Limits.getHeapSize());

Which made sense when I saw the debug logs, the heap size now was 10.22 MB again. which leads me to believe that object sr holds about 5 MB? Can someone please help me understand? Doesn't Remoting response handle upto 15 MB data? The Documentation says so.

Your answer

1 Answer

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.