Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
Hi So I am trying to understand that what is heap and how it is managed in Salesforce.

What I did is created a VF page and a Http callout class, I have stored a large XML on remote server that I am getting back from my Http callout. Also I have made a custom logging method to capture the max heap that is being used through the Http and XML parsing(by passing Limits.getHeapSize()) which works fine.

My confusion is the number do not match, for instance even for a file as big as 5MB my debug shows Maximum heap size: 0 out of 6000000. and my custom logger shows MAX Allowed : 6000000 | Highest Used Heap : 11929380.

Note: I know that I have used a custom login that capture heap using "Limits.getHeapSize()", so if I see a number example "11929380", shouldn't the code fail as I donot have any try catch.

Code details: I am using XmlStreamReader to parse the XML. I am using Transient variable to display the result on screen.

Custom :

 

public class HttpCallout{

public static innerResult callout(String endpoint, String type){

innerResult rs = new innerResult();

captureMaxResource(Limits.getHeapSize() , Limits.getCpuTime() , 'before http call' , rs);

Http h = new Http();

HttpRequest req = new HttpRequest();

req.setEndpoint(endpoint); // PDF hosted xml

req.setMethod(type);

HttpResponse res = h.send(req);

captureMaxResource(Limits.getHeapSize() , Limits.getCpuTime() , 'after http call but before parsing' , rs);

rs.result = parserXML( res.getBody() , rs);

captureMaxResource(Limits.getHeapSize() , Limits.getCpuTime() , 'after parsing' , rs);

return rs;

}

public static string parserXML(String xmlStr , innerResult rs){

//parsing of XML

captureMaxResource(Limits.getHeapSize() , Limits.getCpuTime() , 'after xml parsing' , rs);

return null;

}

public static void captureMaxResource(integer heap , integer cpu , string logindex , innerResult rs){

if(rs.maxHeap != null && rs.maxHeap < heap){

rs.maxHeap = heap;

rs.indexmaxHeap = logindex;

}

else{

rs.maxHeap = heap;

}

if(rs.maxCPU != null && rs.maxCPU < cpu){

rs.maxCPU = cpu ;

}

else{

rs.maxCPU = cpu ;

}

}

public class innerResult{

public string indexmaxHeap{get;set;}

public integer maxHeap{get;set;}

public integer maxCPU{get;set;}

public string result{get;set;}

}

}

 
1 answer
0/9000