1

I'm trying to reduce the heap size limit while a program is running - I'm doing it by taking the data (a list for example) and after I'm using it, I'm assigning the instance to a new List :

Example:

List<Account> **newList** = new List<Account>([SELECT Id FROM Account]);
System.debug('========== Heap Size 1 :: ' + Limits.getHeapSize());

**newList** = **new List<Account>();**
 System.debug('========== Heap Size 2:: ' + Limits.getHeapSize());

Output :

enter image description here

you can see in the above screenshot - that this action reduced the heap size..

So it seems it works, But what happens with the data while the program running? This data is really free by the system?

On big programs - this clear of the heap size could come with a cost of CPU time for example?

Thanks!

3

So it seems it works, But what happens with the data in the while the program running?

From a technical stand point, it remains in the heap waiting to be deallocated.

This data is really free by the system?

It's free space in terms that you're allowed to use more heap, and will no longer appear in the object's memory graph, but will still be floating around in the heap somewhere until a HEAP_DEALLOCATION event.

On big programs - this clear of the heap size could come with a cost of CPU time for example?

Yes, the CPU cost does exist, but it's a very small amount compared to the time used by everything else in a typical program. While it's theoretically possible that too many allocations could cause CPU governor problems, more likely it will be something much larger that you can fix.


Side note: do not make a list from a query of records; it's already a list. Since we're on the topic of heap allocation:

account[] x = new list<account>([select id from account limit 1]);

Results in 20 HEAP_ALLOCATION events in my org, but:

account[] x = [select id from account limit 1];

Results in 15 HEAP_ALLOCATION events in my org.

Likewise, if you don't plan on using the list again, you can null it:

account[] x = [select id from account limit 1];
...
x = null;

This eliminates the associated HEAP_ALLOCATION events necessary for creating a new list and is subsequently faster.

Also, if you do need it again, you can clear the list:

account[] x = [select id from account limit 1];
...
x.clear();

This is 2 HEAP_ALLOCATION events cheaper than:

x = new account[0];
| improve this answer | |
0

If you want to perform with query results always use a SOQL for loops. SOQL for loops queries all records and perform in multiple batches.

Try something like:

for( List<Account> accounts : [SELECT Id FROM Account]){
    //perform query results
}

Always put a WHERE condition to limit records when handling with large records.

Build better Apex scripts to manage heap limits give you key idea to manage heap limits.

| improve this answer | |
  • Hi @Reshma, I'm sorry maybe I didn't write the question right. I updated it I hope it more understandable now. – Salvation Dec 5 '18 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.