You are here:
ApexGuru Antipattern: Limits.getHeapsize() Methods
The Limits.getHeapSize() method monitors memory consumption during execution. This operation increases resource consumption, especially when used in production environments. When used in debug statements or logs, this method is evaluated even if the logs aren’t printed in production and this causes more performance degradation.
Scope
Detection and recommendation
Detection Example
apex
System.debug('Current Heap Size: ' + Limits.getHeapSize());Recommendation
Avoid using Limits.getHeapSize() in production
environments unless necessary. Use this method with conditions or remove it if monitoring heap
size isn’t required during normal execution.
apex
// Commenting out the Limits.getHeapSize() call in production environments
// System.debug('Current Heap Size: ' + Limits.getHeapSize());
