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.


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.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.

  • welcome to the community. to get better answer please post your code as well. – Himanshu Oct 21 '15 at 10:14
  • There is the potential that the maximum was reached before (or after) you write out the limits value. You are checking at a point in time but the max could have occurred and some garbage collection carried out and so the heap is reduced when you check. – Richard Durrant Oct 21 '15 at 13:34

This is an expected behavior.

Date returned as response to the web-service count against the heap-size. You are probably getting base64 string so it would increase the heap-size.

Also, note that it is not necessary that the base64 encoding of a 1 MB file will always have same size.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.