How To Resolve When Salesforce Error Apex Heap Size Too Large
Salesforce upholds an Apex Heap Size Limit of 6MB for synchronous execution and 12MB for Asynchronous execution. The "Pinnacle stack size excessively huge" mistake happens when a lot of information is being put away in memory during preparation.
As far as possible is determined at runtime and varies on how your code is summoned:
- Triggers - 300,000 bytes
- Mysterious Blocks, Visualforce Controllers, or WSDL Methods - 3,000,000 bytes
- Tests - 1,500,000 bytes
This cutoff points additionally scale with trigger bunch sizes:
- For 1-40 records, as far as possible apply
- For 41-80 records, multiple times as far as possible apply
- For 81-120 records, multiple times as far as possible apply
- For 121-160 records, multiple times as far as possible apply
- For at least 161 records, multiple times as far as possible apply
Solution of The Error
Avoid the use of class-level variables for large amounts of data for those Just those variables which are utilized on the Visualforce page ought to be public rest all variables ought to be private, if not utilized by some other class.
Don't forget to check out: Apex Replay Debugger - Salesforce Developer Guide
Put Your Objects on a Tight routine
On the off chance that the articles in your collection contain related objects (i.e., Account objects with various related Contacts for each) ensure the objects that are related, just contain the fields which are really required by your content.
Use the Transient Keyword
Take a stab at utilizing the "Transient" watchword with factors in your regulators and expansions. The transient watchword is utilized to announce occasion factors that can't be saved, and shouldn't be sent as a component of the view state for a Visualforce page.
Use SOQL For Loops
SOQL "for" Loops vary from standard SOQL articulations due to the methods they use to recover sObjects. To evade load size limits, developers ought to consistently utilize a SOQL "for" loops to handle query results that return multiple records. SOQL "for" loops recover all sObjects in a query and loops different bunches of records using inside calls to query and queryMore.
Write an Optimize SOQL For Loops which iterate and proceed records over large queries and there is the most ideal approach to compose a query in for Loop to abstain from occupying space of pile by making a list like below:
Map accountMap = new Map(); for(Account tempAcc : [Select Id, Name From Account Limit 10000]){ accountMap.put(tempAcc.Id , tempAcc.Name); }
Note: There used to be a methodology for managing stack escalated contents by moving them asynchronous @Future Methods. Be that as it may, since as far as possible were expanded in Summer '10 this is not, at this point, motivation to just utilize @Future methods as the limit points are something very similar.
Check out another amazing blog by Aditya here: Top Things You Need to Know About Salesforce Communities
Set the variable as Null when we use it out of the scope as soon as it's required for a longer time. The example is given as follows:
List accLst = [Select Id, Name From Account Limit 10000]; Map accountMap = new Map(); for(Account tempAcc : accLst){ accountMap.put(tempAcc.Id , tempAcc.Name); } //To reduce heap size accLst = null;
Focus on Heap
At the point when your contents run you can see the stack size in the troubleshooting logs. In the event that you notice your stack moving toward the breaking point, you should research why and attempt to refactor your code in a similar manner.
Responses