During the execution of an Apex Anonymous Block, I observed that Limits.getHeapSize() reached approximately 17274634
, but the execution completed successfully without throwing a System.LimitException: Apex heap size too large exception.
Here is a simplified version of the relevant code:
for (String ruleName : map_name_largeConstraintRule.keySet()) {
List<String> parts = map_name_contentParts.get(ruleName);
if (parts.size() > 1) {
map_name_largeConstraintRule.get(ruleName)
.put('content', String.join(parts, ''));
}
System.debug('after content heap size: ' + Limits.getHeapSize());
parts.clear();
}
if (!map_name_largeConstraintRule.isEmpty()) {
retObjects.addAll(map_name_largeConstraintRule.values());
}
System.debug('heap size--: ' + Limits.getHeapSize());
And here is debug log
13:53:51.962 (1752396828)|USER_DEBUG|[85]|DEBUG|after content heap size: 17274634
13:53:51.962 (1753109595)|USER_DEBUG|[92]|DEBUG|heap size--: 9186520
The execution completed without any heap limit exception.
I would appreciate any clarification about how Apex measures heap usage and enforces heap limits in this scenario.
Thank you!
#Salesforce Developer #Trailhead Challenges #Salesforce Admin #Trailhead