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".