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
Mohammed AzarudeenMohammed Azarudeen 

Reduce heap size for SOQL Query

Am keep on hitting "Apex heap size too large" exception.

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
 
brahmaji tammanabrahmaji tammana
Hi Azaruddin,

Optimize your query as much as possible. Add some filters with where conditions which helps to reduce the heap size.

Thanks
Brahma
Alain CabonAlain Cabon
Blob among the data types ?
public class assetAfterUpdateFutureCall(){
      @future
       public static void futureMethodName(List<ID> lisOfAssetIDs){
           System.debug('++++ Start of the method - Heap Size+++++ '+Limits.getHeapSize());
          for(List<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 IN :lisOfAssetIDs]){
               for(Asset assetUniq:assetToProcess) {
                    
               }

           }
       }
}

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
Mohammed AzarudeenMohammed Azarudeen
@Alain Cabon

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.
Alain CabonAlain Cabon
There are too many objects (assetToProcess.size() should be huge) or Blob. You have to filter or optimize your query.
what are the types of the columns?
Mohammed AzarudeenMohammed Azarudeen
@Alain Cabon,

Thanks for your suggestions. I've moved this method from future to batch apex then the issue is solved
Alain CabonAlain Cabon
@Mohammed Azarudeen

Good news (SOLVED). You have used a querylocator and a scope.

Alain