Load size is how much memory expected to store the different articles, factors, and condition of the Summit exchange in memory. This size is covered at 6MB for simultaneous activities and is multiplied to 12 MB for offbeat cycles. In the event that the size is expanded past this covered size, we get the mistake "Apex size excessively huge". This post will drill down ways of dealing with Heap size for Apex code advancement.
1. Bring just required records
In the above model, we are returning the total request list utilizing SOQL which probably won't be needed. Utilize some channel models so that main required records will be gotten. in the event that we have less records, it will take lesser memory.
We can utilize customerid as a channel standards which will channel records and will diminish heap size. Look at our other post Use Channel in SOQL for the advantages of sifting records.
2. Get just required fields
In the above model, we have brought such countless fields yet we are not utilizing a large portion of them in the code. That multitude of gotten fields will find opportunity to recover from the data set and will hold memory also when saved in any factor like requests in the above model.
We ought to just get the necessary fields from the data set. It will lessen the possibilities of Heap size mistakes in addition to the code will work quicker too.
3. Use for circle for SOQL
In the above model, we have performed gotten information from the data set and saved total information in orders variable. This variable is utilized exclusively in for circle and there could be no other use. We can stay away from such sort of SOQL and on second thought of this, we can straightforwardly utilize it in for circle.
List<string> orderNumbers=new List<string>();
for(AcctSeedERP__Sales_Order__c ord:[Select id,AcctSeedERP__Customer__r.Name from AcctSeedERP__Sales_Order__c])
{
orderNumbers.add(ord.Name);
}
system.debug( 'Stack Status ' + (limits.getHeapSize()>limits.getLimitHeapSize()));
system.debug( ': Stack size is ' + limits.getHeapSize() + ' upheld is ' + limits.getLimitHeapSize());
view rawHeapIssueFix.apxc facilitated with ❤ by GitHub
One more significant advantage of involving SOQL in for circle is it will be getting just 200 records in one go and this manner SOQL limit exemptions won't be tossed. So utilize this idea any place you can apply it in zenith code. I have eliminated all unused fields in the above model too, this will likewise diminish memory size.
4. Stay away from system.debug
Stay away from the System.Debug in code. It can make issues underway. Checkout our other post for Enhance Code by Impairing Troubleshoot Mode
5. Stay away from class-level factors and utilize neighborhood variable
Keep away from class-level factors as they will be put away in memory until the total exchange for that class isn't done. It will be smarter to utilize a nearby factor that will consequently deliver after that block execution.
6. Rather than a major strategy utilize more modest techniques
According to best practice, we ought to make more modest techniques around 20 lines. We ought to keep away from greater techniques as they will make intricacy and reliance in code. A variable made in the strategy will be alive till the technique execution so in the event that we have more modest strategies, the variable will be cleaned early soon after strategy execution so our store size will have more space to hold different items.
On account of a greater technique, the variable will be in the load until the total strategy isn't executed, it will take some space and may be a justification for the store size special case. So better will make more modest strategies.
7. Free memory when not being used
Any place we are utilizing objects to store information, attempt to eliminate them from memory following use. We shouldn't pass on everything for the framework to clean. Make a training to clean it right away and free some memory from the store. This will help in putting away different information for usefulness. In the above model, we have an orders variable that can be cleaned after for circle. Dole out invalid to clean object memory.
8. Utilize Asynchronous Techniques
Some of the time we really want to do a few weighty tasks and it could have to utilize a lot of memory, all things considered, we can utilize nonconcurrent strategies so the store size will be expanded to 12MB and we can finish our pinnacle exchange. We can utilize Future callouts, queueable strategies, or bunch occupations in light of the prerequisite. We can utilize this methodology for callout, document dealing with, and so on.
No comments:
Post a Comment