You need to sign in to do that
Don't have an account?

Reduce heap size for SOQL Query
Am keep on hitting "Apex heap size too large" exception.
This is my code,
SOQL query returns 166 rows(we cannot assume this),
Debug log says, after querying it gets exceeded the total heap size
So it is hitting heap size limit exception here. What can I do to reduce the heap size in the query.
Note : All the fields in the query is mandatory to process the record. If I remove any of the field I'll get "SObject was retrieved without querying the requested field" error.
Any help is appreciable
This is my code,
public class assetAfterUpdateFutureCall(){ @future public static void futureMethodName(List<ID> lisOfAssetIDs){ System.debug('++++ Start of the method - Heap Size+++++ '+Limits.getHeapSize()); for(Asset assetToProcess : [Select id,Install_Street1__c,Install_Street2__c, Install_City__c, Install_State_Province__c, Install_Zip_Code__c, Install_Country__c from Asset Where ID = :lisOfAssetIDs]){ System.debug('++++ After Querying in loop - Heap Size+++++ '+Limits.getHeapSize()); } } }
SOQL query returns 166 rows(we cannot assume this),
Debug log says, after querying it gets exceeded the total heap size
++++ Start of the method - Heap Size+++++ 47549 ++++ After Querying in loop - Heap Size+++++ 20736749 ++++ After Querying in loop - Heap Size+++++ 20736759 ++++ After Querying in loop - Heap Size+++++ 20736769 ++++ After Querying in loop - Heap Size+++++ 20736779 . . . . .if we see debug log. When start of the method the heap size is just 47549 but after querying in the loop it gets increased to 20736749
So it is hitting heap size limit exception here. What can I do to reduce the heap size in the query.
Note : All the fields in the query is mandatory to process the record. If I remove any of the field I'll get "SObject was retrieved without querying the requested field" error.
Any help is appreciable
Optimize your query as much as possible. Add some filters with where conditions which helps to reduce the heap size.
Thanks
Brahma
https://developer.salesforce.com/page/Apex_Code_Best_Practices
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_VLSQ.htm
Nested for loop method will be helpful when executing DML statements inside for loop. However, I tried this and it is not reducing the heap size.
what are the types of the columns?
Thanks for your suggestions. I've moved this method from future to batch apex then the issue is solved
Good news (SOLVED). You have used a querylocator and a scope.
Alain