Every Apex transaction runs on a budget. The platform allows a fixed number of
SOQL queries, DML statements, records, CPU milliseconds and megabytes of memory,
and when you exceed one the transaction is stopped with a System.LimitException.
For a standard synchronous transaction, the documented limits are:
Batch and future transactions have some larger limits: 200 SOQL queries,
60,000 ms CPU and 25 MB heap in Winter '27 (12 MB previously). The DML statement
and row limits remain 150 and 10,000. Scheduled Apex uses synchronous limits.
Use Limits.getLimitHeapSize() to check the actual heap limit in your context;
release and compatibility settings can matter. Do not infer platform limits
from a restricted runner or a failed call to an invented method.
Sources, checked November 17, 2026: Salesforce governor limits
and Limits class reference.
Apex exposes both usage and the total limit for the current transaction.
For example, Limits.getQueries() returns queries used, while
Limits.getLimitQueries() returns the total allowed. The difference is the
remaining query budget. Other pairs include getDMLStatements() /
getLimitDMLStatements() and getHeapSize() / getLimitHeapSize().
These methods take no arguments; there is no generic getLimitUsage(String).
Read usage counters like this:
Integer queries = Limits.getQueries(); Integer queryRows = Limits.getQueryRows(); Integer statements = Limits.getDmlStatements(); Integer dmlRows = Limits.getDmlRows(); Integer cpuTime = Limits.getCpuTime(); Integer heapSize = Limits.getHeapSize();
The counters are cumulative for the whole transaction, so the difference between a
reading taken before a piece of code and a reading taken after it is what that
piece of code spent:
Integer before = Limits.getQueries(); List<Account> rows = [SELECT Id FROM Account WHERE Name IN :names]; Integer queriesUsed = Limits.getQueries() - before;
The most common way to run out of budget is a query inside a loop. Three names in
a list is three queries. Three thousand names is three thousand queries, and the
transaction dies long before it finishes.
Bind the whole list into one IN clause instead. The list is still a list of
names, but the platform sees a single query:
List<Account> rows = [SELECT Id FROM Account WHERE Name IN :names];
One query, whatever the size of the list. The records that come back still count
against the query row limit, so a very large list is a problem for a different
reason, but the query count stays at one.
A null list bound into an IN clause throws a NullPointerException. An empty
list is legal, but it still spends a query. Check the list first and return
early, so a method that has nothing to look up costs nothing at all.
The first mistake is querying inside a loop, which spends one query per item.
The second is querying before checking the list, which throws on a null list.
The third is guessing at how much a piece of code spends instead of measuring it
with the Limits counters.