0

I want to show the Instruction message along with the Max heap size Exception Message Thats the Error I am getting what just I want to show a custom message along with it I have added message in the catch blog but not working

Heres the code I am using:

stQry = 'SELECT ' + AllFieldNames('VWC_CvID_BodySize_Pairs__c') + ' FROM VWC_CvID_BodySize_Pairs__c';

try {   
    CvBSPairs = Database.query(stQry);
}
catch (QueryException e) {
    ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'Heap limit Exceeded Try to Select Other Filters');
    ApexPages.addMessage(myMsg);            
}
3
  • Is this a preventive measure because heap size is 6 MB for Synchronous Transactions and you want to alert user or you want to show this when the heap size is exceeded(Visualforce throws an error automatically)
    – manjit5190
    Jan 4, 2017 at 9:47
  • @manjit_singh like it exceed the limit and i want to show some instruction to the user or move toward som tab
    – Malik
    Jan 4, 2017 at 10:01
  • Hi Malik, welcome to SFSE. Please take a moment to visit the help center and read How to Ask and How to Answer. Please use the edit link under your post (or in this comment) when you have additional material to add. Answers are for an entirely different purpose.
    – Adrian Larson
    Jan 4, 2017 at 14:03

2 Answers 2

7

When you hit the limit a System.LimitException is thrown with a message:

Apex heap size too large: ...

but that exception cannot be caught by Apex code (by design).

If the problem arises in a loop that you can add code to, the best you can do is check that you are approaching the limit:

Integer heapThreshold = (Limits.getLimitHeapSize() * 3) / 4;
for (...) {
    ...
    if (Limits.getHeapSize() > heapThreshold) {
        throw new MyCustomException('To much heap used, email us at...');
    }
}

and throw and catch a custom exception (or add a page message and abort) to report the problem.

Probably better though to design your code so the heap limit is never approached.

PS

Based on your comment the data returned from the query exceeds the heap size. If you don't need all the fields for your logic cutting it down to just the fields you need might get around the problem. But if the number of records is very large (and growing) then you may have to change the processing to use a batchable (Google that) that can process a group of the overall record set at a time where each group gets its own set of governor limits including the heap one.

PPS

Looks like the heap problem may be caused by querying all the records at once. Apex has a SOQL For Loop that automatically queries in blocks of 200 (AFAIK) records (discarding the previous block) so if each block of 200 records fits within the heap space you may be able to avoid hitting the governor limit.

You need to code your loop like this:

for (VWC_CvID_BodySize_Pairs__c pairs : Database.query(stQry)) {
     // Processing logic
}

rather than holding a list of all the records.

4
  • @Malik See the PS.
    – Keith C
    Jan 4, 2017 at 12:21
  • Your parentheses in your threshold calculation make it more confusing, if anything.
    – Adrian Larson
    Jan 4, 2017 at 14:36
  • @AdrianLarson OK. Just always twitchy about operator precedence and certainly don't want 3 / 4 = 0. Not sure that Integer heapThreshold = Limits.getLimitHeapSize() * 3 / 4 is that much clearer. Think the OP would appreciate a more detailed answer about what to do when its the query results that exceeds the heap limit...
    – Keith C
    Jan 4, 2017 at 14:39
  • Fair enough. I might just use decimal notation in my own code but as long as it works, it's just stylistic.
    – Adrian Larson
    Jan 4, 2017 at 14:40
0

Please use the link and try to optimize .

Enable Developer mode in the user object and try to see the size of the objects and records.

Reduce unused field from SOQL query.

Dont load all data when page load begins.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .