How is HEAP_ALLOCATE related to actual heap size

heaplimitslog

Finest level log of a heap size limit exception log contains HEAP_ALLOCATE lines. Aggregating HEAP_ALLOCATE Byte values yields 2371721 bytes (2.37 MB).

Right after that, the log is showing a FATAL_ERROR, stating the heap is 6385056 bytes (6.38 MB).

What is the relationship between HEAP_ALLOCATE log lines and the actual heap size?

Best Answer

The relationship is 1:1. Unfortunately, it's hard to be completely accurate about how much heap you're using. For example, if Apex performs a DML operation, the trigger's heap space is shared for static variables but separate for local variables. This makes it possible to use nearly 100 MB of heap without hitting the actual heap governor limit (e.g. 15 recursive DML operations). It's also possible to exceed 40 MB of memory in synchronous code--but only for a few milliseconds, until the next heap limit check executes. In addition, FINEST produces a lot of log data, but you only get 20 MB of log, so data in the middle is truncated. If you want to keep closer track of your usage, it's usually better to sprinkle some System.debug(LogLevel.ERROR, Limits.getHeapSize()) and turn down the logging to the ERROR level. This will help you read how much space you're using.

Related Topic