I am wondering how to decide between heap size and processing time. This probably rather trivial but I have been researching for about an hour. The two images I used system.debug(Limits.getHeapSize());
They both are going over the same exact record in Salesforce.
The first image I did a soql for loop
for(variableType variableName : [soql query])
The second image I made a list with the soql query and then looped over the list.
List<sobject> myList = [soql query];
for(sobject variableName : myList)
From everything I have read online the first way I ran it is slower than the second, which I confirmed by using Limits.getCpuTime();. I am now wondering why in the first way I ran it, it shows a higher peak in heap size than the second way I ran it? From the Salesforce docs I read that the first way I ran it was supposed to be better for memory management and not hitting the heap size limit. Can anyone shed some light as to why the “better for not hitting heap size limit” way is causing my peak heap size to be greater? Am I understanding something wrong about when Salesforce checks heap size limits? Does heap size not matter during a soql for loop and only after the execution of it?
Thank you for any help, I am new to Apex and programming in general. Still trying to figure it all out and how to make these sort of design/architecture decisions.