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
Caleb_SidelCaleb_Sidel 

How to control heap size?

I'm getting a heap size exception. I know I can call @future and I know I can check the limit with Limits.getHeapSize() and Limits.getLimitHeapSize(). But I'd like to avoid @future and I'd like to avoid simply stopping my operation before the limit is reached.

 

Ideally I'd like to be able tofigure out what is taking up memory and clean up my memory!

 

I'm doing a query which returns 432 records and then I'm looping over those records as follows:

 

         for(Record__c child:children)
        {
            totalPages += child.Pages__c != null ? child.Pages__c : 0;
            pagesTriaged += child.Pages_Triaged_Sum__c != null ? child.Pages_Triaged_Sum__c : 0;
            pagesToTriage += child.Pages_to_Triage__c != null ? child.Pages_to_Triage__c : 0;
            pagesMined += child.Pages_Mined_Sum__c != null ? child.Pages_Mined_Sum__c : 0;
            pagesToMine += child.Pages_to_Mine__c != null ? child.Pages_to_Mine__c : 0;
            pagesReviewed += child.Pages_Reviewed_Sum__c != null ? child.Pages_Reviewed_Sum__c : 0;
            pagesToReview += child.Pages_to_Review__c != null ? child.Pages_to_Review__c : 0;
        }

 

The heapsize exception occurs during this loop. I'm not storing a lot in memory but I'm thinking that somehow when I do variable += 1; the heap is making a copy of the variable and the heap isn't cleaning itself up?

 

So I've changed my code to as below and I'm trying to deploy it to test it, but while I'm waiting for it to deploy I thought I'd ask the community...any other ideas? I will post back with results once deployed and/or other things I try until it is either solved or I revert to using @future.

 

        while(children.size() > 0)
        {
            Record__c child = children.remove(i);
            totalPages += child.Pages__c != null ? child.Pages__c : 0;
            pagesTriaged += child.Pages_Triaged_Sum__c != null ? child.Pages_Triaged_Sum__c : 0;
            pagesToTriage += child.Pages_to_Triage__c != null ? child.Pages_to_Triage__c : 0;
            pagesMined += child.Pages_Mined_Sum__c != null ? child.Pages_Mined_Sum__c : 0;
            pagesToMine += child.Pages_to_Mine__c != null ? child.Pages_to_Mine__c : 0;
            pagesReviewed += child.Pages_Reviewed_Sum__c != null ? child.Pages_Reviewed_Sum__c : 0;
            pagesToReview += child.Pages_to_Review__c != null ? child.Pages_to_Review__c : 0;
            child = null;
            i++;           
        }

 

Thank you,

Caleb

Caleb_SidelCaleb_Sidel

So everyone knows, this works quite well. Removing items from the collection quickly reduces heap size. However one thing that shocked me was that when I did a system.debug after my query (5 fields, 483 records) it was roughtly 99,000 bytes (limit on a trigger for 1 record is 100,000 bytes).

 

So I changed my code - it now is close to the script statement limit, but it's far from the heap size limit. Instead of doing a single query (I know I'll never have more than 1,000 children so I *could* do 1 query) I do a query loop. This way there are only ever 200 records in memory. The heap size is now around 40,000 bytes after the first query of 200 records, goes down as the collection items are removed and then goes back up at the next loop iteration.

 

 

The new code is:

 for(List<Record__c> children : [select <fields> from Record__c where parent__c =: <parent_Id>] {

    while(children.size() > 0)
        {
            Record__c child = children.remove(i);
            totalPages += child.Pages__c != null ? child.Pages__c : 0;
            pagesTriaged += child.Pages_Triaged_Sum__c != null ? child.Pages_Triaged_Sum__c : 0;
            pagesToTriage += child.Pages_to_Triage__c != null ? child.Pages_to_Triage__c : 0;
            pagesMined += child.Pages_Mined_Sum__c != null ? child.Pages_Mined_Sum__c : 0;
            pagesToMine += child.Pages_to_Mine__c != null ? child.Pages_to_Mine__c : 0;
            pagesReviewed += child.Pages_Reviewed_Sum__c != null ? child.Pages_Reviewed_Sum__c : 0;
            pagesToReview += child.Pages_to_Review__c != null ? child.Pages_to_Review__c : 0;
            child = null;
            i++;           
        } 

}