I've been noticing that, in some cases, the heap size I obtain by calling Limits.getHeapSize() and the actual amount used by the transaction differs by a big margin.
The problem is, I have to call a webservice which may return a very large response. Several times I've received "Apex heap size error", so I have checked its usage. In this instance, I try to render a pdf file encoded as base64, which I retrieve from the WS response, and I found this in the log:
[...]
11:50:04.183 (4183052484)|USER_DEBUG|[31]|DEBUG|Heap size = 1482707
11:50:04.183 (4183061532)|STATEMENT_EXECUTE|[38]
11:50:04.183 (4183067055)|METHOD_EXIT|[10]|01p11000000HRBj|CTR_DOC_ConsultaDocumentacion.obtenerRespuesta()
11:50:04.183 (4183086455)|CODE_UNIT_FINISHED|respuesta
11:50:04.183 (4183123971)|CODE_UNIT_FINISHED|CTR_DOC_ConsultaDocumentacion get(respuesta)
11:50:04.183 (4183176550)|CODE_UNIT_STARTED|[EXTERNAL]|01p11000000I5nJ|extension
11:50:04.183 (4183187978)|CODE_UNIT_FINISHED|extension
11:50:04.184 (4184110670)|CODE_UNIT_STARTED|[EXTERNAL]|01p11000000I5nJ|documento
11:50:04.184 (4184124485)|CODE_UNIT_FINISHED|documento
11:50:04.207 (4207964561)|CUMULATIVE_LIMIT_USAGE
11:50:04.207 (4207964561)|LIMIT_USAGE_FOR_NS|(default)|
Number of SOQL queries: 1 out of 100
Number of query rows: 1 out of 50000
Number of SOSL queries: 0 out of 20
Number of DML statements: 0 out of 150
Number of DML rows: 0 out of 10000
Maximum CPU time: 224 out of 10000
Maximum heap size: 5340165 out of 6000000 ******* CLOSE TO LIMIT
Number of callouts: 1 out of 100
Number of Email Invocations: 0 out of 10
Number of future calls: 0 out of 50
Number of queueable jobs added to the queue: 0 out of 50
Number of Mobile Apex push calls: 0 out of 10
As you can see, I get 1482707 as the heap size just before the end of the transaction (checked with Limits.getHeapSize()). However, at the end of the transaction, I get 5340165
Any ideas why this is happening? I've checked that I don't copy unnecessary variables, and there are only getter access to controller properties after the getHeapSize() call ("extension" and "documento" are just properties of object instance "respuesta")
Thanks in advance.
EDIT:
I am not allowed to post the code in all its detail, so here's is what I can show:
public WS_GestionAplicacionesSoloUCM_RP.obtenerDocumentoResponse respuesta {
get {
System.debug('Heap size = ' + Limits.getHeapSize());
WS_GestionAplicacionesSoloUCM_RQ.obtenerDocumentoRequest request = new WS_GestionAplicacionesSoloUCM_RQ.obtenerDocumentoRequest(TX_Id_documento);
WS_GestionAplicacionesSoloUCM_RP.obtenerDocumentoResponse response = WS_GestionAplicacionesSoloUCM_Impl.obtenerDocumento(request);
System.debug('Heap size = ' + Limits.getHeapSize());
return response;
}
}
public static WS_GestionAplicacionesSoloUCM_RP.obtenerDocumentoResponse obtenerDocumento(WS_GestionAplicacionesSoloUCM_RQ.obtenerDocumentoRequest request) {
String body = '<soapenv:Envelope>...</soapenv:Envelope>';
HttpRequest req = new HttpRequest();
req.setEndpoint(endPoint);
req.setMethod('POST');
req.setBody(body);
req.setHeader('Content-Type', 'text/xml;charset=UTF-8');
req.setHeader('Authorization', 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf(username +':' + password)));
Http http = new Http();
HttpResponse result = http.send(req);
return new WS_GestionAplicacionesSoloUCM_RP.obtenerDocumentoResponse(parseResponse(result));
}
In this case, the retrieved response size matches the heap size of ~1482707, so what the debug call shows is expected.
Thanks again for your answers.