Scenario
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<String> generateRandomObject() {
List<String> fileData = new List<String>();
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:
<apex:page showHeader="true" sidebar="true" controller="HeapSize_Controller">
<apex:includeScript value="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"/>
<script type="text/javascript">
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}
);
}
</script>
</apex:page>
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 Synchronous Limit for Heap Size is 6MB? Then why does the remoting say its 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.