# How can developers use the Limits class to inspect asynchronous governor limits at runtime?

**Topic:** Apex · **Difficulty:** Medium · **Tags:** Apex, Asynchronous Apex, Governor Limits

Read it on the site: https://www.forcechallenges.com/interview-questions/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:**
1. Call `Limits.getX()` for current consumption.
2. Call `Limits.getLimitX()` for the ceiling.
3. 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:**
```apex
public static void enqueueSafely(Queueable job) {
    Integer remaining = Limits.getLimitQueueableJobs() - Limits.getQueueableJobs();
    if (remaining > 0 && !System.isBatch() && !System.isFuture()) {
        System.enqueueJob(job);
    } else {
        // fallback: mark records for a scheduled sweep instead
        System.debug(LoggingLevel.WARN, 'No async headroom, deferring');
    }
}
```

**Hidden gotchas & must-remember points:**
- Limit values change by context: queueable limit is 50 in sync and 1 in async.
- `Limits` does not show daily org-wide async limits.
- LimitException cannot be caught; checking beforehand is the only protection.
- Calling Limits methods is cheap but logging inside loops adds CPU.
- Not every limit has an obvious getter name; confirm method names in the Limits class reference rather than guessing.

**Best practice:** Check headroom before enqueueing in code paths that can run from triggers or bulk loads, and degrade gracefully.

---
Source: ForceChallenges · last updated 2026-11-02
