Some strategies on how to write Apex scripts that run within heap limits.
|Knowledge Article Number||000170956|
|Description||Some strategies on how to write Apex scripts that run within heap limits.|
|Resolution||1. Look for heap size in debug logs. If heap size is approaching the limit, investigate it and refactor the code.
2. Use the 'Transient' keyword with variables. It is used to declare instance variables that cannot be saved, and shouldn’t be transmitted as part of the view state for a Visualforce page.
3. Use Limit methods. Use heap limits methods in your Apex code to monitor/manage the heap during execution.
// check the heap size at runtime
4. Reduce heap size during runtime by removing items from the collection as you iterate over it.