How can developers use the Limits class to inspect asynchronous governor limits at runtime?
Answer
Basics: Governor limits are per transaction. Limits.getQueueableJobs() returns how many queueable jobs you have enqueued so far; Limits.getLimitQueueableJobs() returns the cap (50 in a synchronous transaction, 1 inside an async job). The same pattern exists for future calls, queries, DML, callouts, heap and CPU.
How it works:
- Call
Limits.getX()for current consumption. - Call
Limits.getLimitX()for the ceiling. - Compare and decide whether to proceed, defer, or fall back to a different async mechanism.
Advanced / deep dive: Useful async-related methods include getFutureCalls()/getLimitFutureCalls(), getQueueableJobs()/getLimitQueueableJobs(), getCallouts()/getLimitCallouts(), getHeapSize()/getLimitHeapSize() (6 MB sync, 12 MB async), and getCpuTime()/getLimitCpuTime() (10,000 ms sync, 60,000 ms async). Context checks: System.isBatch() and System.isFuture() tell you where you are running, which matters because @future cannot be called from batch or future. The Limits class only covers the current transaction; for org-wide async consumption (such as the daily async executions) use System.OrgLimits.getMap() (verify the exact key names such as DailyAsyncApexExecutions in current docs) or the REST /limits resource.
Code / config example:
public {
Limits.getLimitQueueableJobs() - Limits.getQueueableJobs();
(remaining > && !System.isBatch() && !System.isFuture()) {
System.enqueueJob(job);
} {
System.debug(LoggingLevel.WARN, );
}
}