Back to Articles
Apex & Architecture
June 2, 202618 min read

Salesforce Governor Limits Explained with Practical Examples (2026 Guide)

Understand Salesforce governor limits with real Apex examples. Learn optimization strategies for SOQL, DML, CPU time, and heap size limits in multi-tenant environments.

A
Alex RiveraPrincipal Salesforce Architect

In a cloud-based multi-tenant architecture like Salesforce, multiple customers (tenants) share the same physical server resources. To prevent one organization's poorly optimized code from crashing the shared processor, memory, or database, Salesforce enforces strict execution boundaries known as Governor Limits. Understanding these limits is critical for writing robust code and clearing developer assessments. Let's look at the core execution limits, practical examples, and optimization techniques. For an interactive visual breakdown, check our main Governor Limits page.

Transaction Boundaries: The Scope of Limits

Governor Limits are measured per transaction. An Apex transaction begins when a trigger fires, a controller executes, a queueable runs, or a web callout starts, and it ends when the database commit is finalized. Limits reset completely at the start of a new transaction boundary (such as moving from a trigger context to an asynchronous Apex queueable).

Core Salesforce Governor Limits with Apex Examples

1. SOQL Limits (100 Sync / 200 Async Queries)

You are allowed a maximum of 100 synchronous SOQL queries per transaction. Exceeding this triggers a System.LimitException: Too many SOQL queries: 101.

apex
// Bad: SOQL inside a loop will hit the 100 query limit quickly for (Account acc : Trigger.new) { List<Contact> conList = [SELECT Id FROM Contact WHERE AccountId = :acc.Id]; } // Good: Move query outside the loop and bind using a Set of IDs Set<Id> accIds = new Set<Id>(); for (Account acc : Trigger.new) { accIds.add(acc.Id); } List<Contact> contacts = [SELECT Id, AccountId FROM Contact WHERE AccountId IN :accIds];

2. DML Limits (150 Statements per Transaction)

You are limited to 150 DML statements (insert, update, delete, upsert) per transaction. Attempting 151 triggers System.LimitException: Too many DML statements: 151.

apex
// Bad: Single DML inside loop (Hits 150 limit) for (Opportunity opp : oppList) { opp.StageName = 'Closed Won'; update opp; } // Good: Accumulate records in a List and perform a single DML statement List<Opportunity> toUpdate = new List<Opportunity>(); for (Opportunity opp : oppList) { opp.StageName = 'Closed Won'; toUpdate.add(opp); } update toUpdate;

3. CPU Time Limits (10,000 ms Sync / 60,000 ms Async)

The transaction thread cannot exceed 10 seconds (10,000 milliseconds) of CPU processing time. In bulk triggers, CPU time is consumed by nested loops and heavy iterations. To optimize, use Maps for fast matching and move CPU-heavy operations to asynchronous contexts.

4. Heap Size Limits (6 MB Sync / 12 MB Async)

The heap limit governs the memory occupied by your variables and objects in a transaction. When retrieving large data volumes (LDV), loading everything into memory bloats the heap. Use SOQL for-loops to process records in 200-count chunks instead of loading them all into a single collection:

apex
// Good: Processing records in batches of 200 to keep the Heap size low for (List<Account> batch : [SELECT Id, Name FROM Account WHERE CreatedDate = TODAY]) { // Do processing here }

5. Async Apex Limits (Queueable, Future, Batch)

Asynchronous executions have double the limits of synchronous transactions (e.g., 200 SOQL queries, 12 MB heap size, and 60,000 ms CPU time). Additionally, organizations are limited to a daily rolling allocation of async executions (usually 250,000 or based on user license count).

6. Flow Governor Limits

Declarative automations built with Salesforce Flow run inside the same transaction boundary as triggers. Flows are subject to the same 100 SOQL limit. For instance, executing a "Get Records" element inside a Loop element in Flow acts like a query inside a loop, resulting in runtime limit failures.

Bulkification Strategies: Designing Scale-Safe Apex

To ensure your application scales, apply these bulkification strategies: never write SOQL or DML statements inside loops, write trigger handler code that expects and processes collections of records, utilize Apex Maps to structure queries for quick key-value matches, and validate logic against trigger volume tests of 200+ records in test methods.

Common Interview Questions & Answers

Question 1: How do you handle a System.LimitException programmatically?

You cannot catch a System.LimitException using a standard try-catch block. Once a governor limit is reached, the transaction is immediately rolled back and terminated. To handle this, developers must write proactive guards using the Limits class (e.g., Limits.getQueries() < Limits.getLimitQueries()) to split execution boundaries before hitting the limit.

Question 2: What is a mixed DML exception and how do you prevent it?

A mixed DML exception occurs when you attempt to insert or update setup objects (such as User or Group) and non-setup objects (such as Account or Contact) in the same transaction. To resolve this, run the DML of one of the categories inside an asynchronous block (such as @future or System.enqueueJob()) to execute it in a separate transaction context.

FAQ Section (Salesforce Governor Limits FAQs)

  • Can async Apex query limits be exceeded? -> Yes. Asynchronous execution increases the limit from 100 to 200 SOQL queries per transaction, but exceeding 200 will still fail with a LimitException.
  • Does querying custom metadata count against governor limits? -> Custom metadata queries do count toward the 100 SOQL query limit, but they do not count against query row limits, and their metadata access is cached.
  • Does System.runAs() bypass governor limits? -> No. System.runAs() is used in test classes to verify user sharing contexts and execution profiles. It does not bypass governor limits.

Practice Real-World Limits Scenarios

Want to test your ability to explain CPU time bounds, heap limits, and trigger architectures? Test yourself using our Mock Interview page. Select Strict mode to simulate recruiter scrutiny, or read more guides like the Apex Interview page and our Trigger Interview page.

Technical Interview Hub

Prepare comprehensively for Salesforce hiring loops by exploring our specialized interactive study modules.

Evaluate Your Readiness

Done reading? Put your theory to the test. Launch our AI-powered voice recruiter simulation to benchmark your performance.

Launch Mock Interview