What governor limits impact Apex Describe methods?
As mentioned, describe limits have been removed entirely. Just for completeness, there are a few remaining limits that are affected by these describe calls, but consumption is negligible. These limits are Heap Space
and CPU Time
. The numbers below are for my org, so mileage will vary. I doubt the numbers would ever get high enough to be a concern.
Methodology
You need separate approaches (though one execution is sufficient) to derive the measures that follow. All of this was run in Execute Anonymous
with logging levels set to NONE
except for Apex
, which was at DEBUG
.
Long start = Datetime.now().getTime();
for (Integer i = 0; i < 100; i++)
Map<String, SObjectType> describe = Schema.getGlobalDescribe();
system.debug(Datetime.now().getTime() - start);
// divide above number by 100 (or whatever number of iterations you use) to get ms elapsed
Integer heapBefore = Limits.getHeapSize();
Map<String, SObjectType> describe = Schema.getGlobalDescribe();
system.debug(Limits.getHeapSize() - heapBefore);
// divide the above by Limits.getLimitHeapSize(); for percentage of heap used
Consumption
Schema.getGlobalDescribe()
CPU Time
: ~7ms
Heap Space
: ~0.3%Schema.SObjectType.getDescribe()
CPU Time
: ~0.002ms
Heap Space
: ~0.001%Schema.SObjectField.getDescribe()
CPU Time
: ~0.38ms
Heap Space
: ~0.001%Schema.DescribeSObjectResult.getRecordTypeInfosByName()
CPU Time
: ~0.2ms
Heap Space
: ~0.0001%
Analysis
If you are getting the global describe more than once, cache it. It should be quite simple to cache, and the heap consumption is likely worth the tradeoff even if the number of calls is <50.
For object describes, every 1000 calls (in a single transaction) consume only 2ms (out of 10s), so you don't need to pay much attention unless you are expecting >100K calls. Caching 1000 fields will only consume 1% of your heap space, so that's not a huge concern either. I recommend you use whatever makes your code cleanest.
For field describes, you only get three calls per millisecond, so the calculus is a little different. Now 1000 calls consume an amount of time that is perceptible by humans. However, the cache rate is the same. If you are describing the same field many times, consider caching.
For record type describes, the CPU situation is much the same as with fields. You get five calls per millisecond, so you don't necessarily want to call it thousands of times. But caching it is very inexpensive. You should be able to cache the describes for every single RecordType
without consuming 1% of your heap.
There is no limit for Describe Call in Salesforce.
As per the below document, There were few limits before Summer'14 release and it was remove post Summer'14 release.
For more information:- https://developer.salesforce.com/releases/release/Summer14/New+Apex+Enhancements