Governor Limits
Learning Content
Last updated: July 21, 2026
Governor Limits in Apex
One of the unique characteristics of Salesforce Apex is its Governor Limits. These are platform-enforced limits that control how much CPU time, memory, database operations, and other resources a single Apex transaction can consume.
Since Salesforce follows a multitenant architecture, thousands of organizations share the same platform infrastructure. Governor Limits ensure that no single organization or application consumes excessive resources and negatively impacts other customers.
💡 LearnFrenzy Insight
Imagine a public library with only 100 computers.
If one person occupied 50 computers all day, other visitors wouldn't be able to use them.
To ensure fair usage, the library limits how many computers each person can use.
Salesforce follows a similar approach.
Governor Limits ensure that every organization gets a fair share of the platform's computing resources.
Why Do We Need Governor Limits?
Salesforce hosts applications for thousands of customers on shared infrastructure. Without resource limits, poorly written Apex code from one organization could slow down or affect the performance of other organizations.
Governor Limits help maintain platform stability, improve performance, and encourage developers to write efficient and scalable Apex code.
| Benefit | Description |
|---|---|
| Fair Resource Usage | Prevents one organization from monopolizing platform resources. |
| Improved Performance | Maintains consistent response times. |
| Better Scalability | Supports millions of users efficiently. |
| Encourages Best Practices | Promotes bulkified and optimized Apex code. |
Common Governor Limits
| Limit | Description |
|---|---|
| SOQL Queries | Maximum number of SOQL queries allowed in a transaction. |
| DML Statements | Maximum number of insert, update, delete, and undelete operations. |
| CPU Time | Maximum execution time for Apex code. |
| Heap Size | Maximum memory available during execution. |
| Callouts | Maximum number of HTTP or web service callouts. |
The exact values of these limits vary depending on the execution context, such as synchronous or asynchronous Apex. You'll learn these limits in detail in the dedicated Governor Limits chapter.
Simple Example
The following code executes a single SOQL query to retrieve Account records.
// Execute Anonymous Window
List<Account> accounts =
[SELECT Id, Name FROM Account LIMIT 5];
System.debug(accounts);
Debug Log Output
USER_DEBUG [4]|DEBUG|(
(Account:{Id=001..., Name=LearnFrenzy}),
(Account:{Id=001..., Name=ABC Technologies})
)
A single SOQL query is efficient and well within the Governor Limits for a transaction.
Real Business Scenario
🏢 Business Example
Suppose a sales manager updates 500 Opportunity records.
If the Apex code performs a separate SOQL query for each record, the transaction can quickly exceed the allowed query limit and fail.
Instead, developers retrieve all required records in a single query and process them together.
This technique is called Bulkification and is one of the most important Apex development practices.
Common Causes of Governor Limit Exceptions
| Poor Practice | Recommended Practice |
|---|---|
| SOQL inside loops | Query records once outside the loop. |
| DML inside loops | Perform bulk DML operations. |
| Processing one record at a time | Process collections of records. |
| Repeated database calls | Reuse queried data whenever possible. |
✅ Best Practice
Always write bulkified Apex code. Use Collections, perform SOQL queries outside loops, minimize DML operations, and process records in batches whenever possible.
⚠️ Common Mistake
One of the most common beginner mistakes is writing SOQL queries or DML statements inside loops. This can quickly exceed Governor Limits and cause runtime exceptions.
🎯 Interview Tip
Question: Why does Salesforce enforce Governor Limits?
Answer: Salesforce uses a multitenant architecture where multiple organizations share the same infrastructure. Governor Limits ensure fair resource usage, maintain platform performance, and prevent one organization's code from negatively affecting others.
📚 Learn More
This section introduces Governor Limits as one of Apex's most important platform features.
In the dedicated Governor Limits chapter, you'll learn:
- Complete Governor Limit Reference
- Synchronous vs Asynchronous Limits
- Bulkification Techniques
- CPU Time Optimization
- Heap Size Management
- Best Practices and Design Patterns
- Real-World Optimization Scenarios
- 50+ Practical Examples
Practice What You've Learned
Test your understanding with these practice exercises