5

We currently have a limit of Maximum Stack Depth of 1000.

I assuming that we could call a method, that calls another method, and then another one 1000 times, so 999 "nested" methods should be fine.

Example:

private Schema.DescribeSObjectResult objectType(Schema.DescribeSObjectResult rq, Integer i){
    Schema.DescribeSObjectResult rs = rq.getSobjectType().getDescribe();
    if(i == 0) return rs;
    else return objectType(rs, --i);
}

Schema.DescribeSObjectResult r = Account.SObjectType.getDescribe();
objectType(r, 998);

If I run this on the Developer Console Execute Anonymous window I get the error:

System.LimitException: Maximum stack depth reached: 1001

But if I change the last bit to

objectType(r, 997)

Then it's a success.

I was under the impression I could it with 999 (initial call + plus 999 recursive calls), but it does not seem to be the case.

So my question is: What are the 2 "hidden" stack?

  • 1
    Initial Call + 997 works , ie total (998). One would code calling Execute anon method. Thats 1. Bringing total to 999. – Pranay Jaiswal Aug 6 at 10:49
  • But then initial call + 998 should work too. 1000 is still allowed, it only fails on 1001. – Fernando Gavinho Aug 6 at 10:54
9

enter image description here

  1. When you say 997, it runs 998 times counting initial call (actually not hidden)

  2. The call to the class constructor

  3. Execute anonymous

This can be tested in Dev console > open log > Debug > View log panels > Execution Overview > Executed Units

  • 1
    hum... from my test it looks like it's not the class constructor but 1. the call to the tollingAPI 2 the call to the execute_anonymous_apex method and then the 998 sobjectType calls. nice! – Fernando Gavinho Aug 6 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.