function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
dipu2dipu2 

Too many HEAP_ALLOCATE, is it possible to avoid repeated heap allocation

 I have noticed a lot of these Heap Allocate in the debug log. Should I be concerned about heap allocation. Is there a way to allocate a bigger chunk instead of repeated allocation.

 

11:29:24.883 (3883710000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:52
11:29:24.883 (3883723000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:52
11:29:24.883 (3883734000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:52
11:29:24.883 (3883742000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:52
11:29:24.883 (3883750000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:52
11:29:24.883 (3883758000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:52
11:29:24.883 (3883766000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:52
11:29:24.883 (3883775000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:52
11:29:24.883 (3883788000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:52
11:29:24.883 (3883797000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:52
11:29:24.883 (3883808000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:52
11:29:24.883 (3883816000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:52

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You can't avoid this repeated allocation. Unlike a lower level language, like machine code or C++, where you'd allocate a big chuck at once for, say, an array, you just can't do this in Apex Code. The point here is you shouldn't worry about those allocations, as that is the point of automatic garbage collection. Your only concern is if you exceed the governor limits for heap sizes (see the documentation for current limits).