My friend and I are studying for platform developer I, and one of the questions we use for studying asks what are the best practices to avoid heap limit errors in triggers.

The full question reads:

"A developer encounters APEX heap limit errors in a trigger. Which two methods should the developer use to avoid this error? Choose 2 answers."

We agree on one of the answers, which is using SOQL for loops, but we're not sure about what the other answer should be.

Our two options are:

  • Use the "transient" keyword when declaring variables
  • Remove or set a collection to null after use

My friend says on the internet he finds that the first one is marked as the answer, but I'm a bit skeptical, since my understanding was that Transient variables mainly affected the viewstate, and I'm 80% sure that triggers aren't serializable.

I'm not that used to Apex' garbage collection specifically, but I'd assume that setting a collection to null should mark it as collectable, and would reduce the heap size. That said, if the question requires knowledge of that, it is way more difficult than the other questions we have, and that makes me skeptical as well.

So, my question is: Which of the two options is correct? Or are both of them incorrect?

The other option is:

  • "Query and store fields from related object in a collection when updating related objects".
  • Again, please include the exact wording of the question. – Adrian Larson Feb 14 at 20:52

I will try to answer to the best of my knowledge

1)Use the "transient" keyword when declaring variables

Yes you were spot on with transient. Declaring a variable as transient wont' make it part of view state. Also declaring a transient variable will make sure its not serialized(JSON.deserialize). But it will still be part of heap. Transient variable has nothing to do with heap, Thus it won't be a correct option.

2) Remove or set a collection to null after use

There is garbage collection and heap deallocation. It happens when the block where you have declared the variable ends. If you wanna free up heap before the end of block and you don't have any use of that variable later then it makes sense to make it null and free up some heap. It's correct and good practice to do so if playing with a huge chunk of records

Example 1:

public static void myMethod(){
    List<Account> aacList = [My SOQL];

}//Heap will be deallocated here

Example 2:

public static void myMethod(){
    List<Account> aacList = [My SOQL];
    aacList = null; //Heap will be deallocated here 
    //More logic

3)Query and store fields from a related object in a collection when updating related objects

This feels illogical and overhead to store fields of related object in a collection, and is wrong in my opinion.

  • I hope I never write code that requires me to set collections to null within a method. Using lots of methods (blocks) seems like a better solution :-) – cropredy Feb 15 at 2:16

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.