Governor Limits in Apex

Governor Limits in Apex

Introduction

Apex runs on a multi-tenant platform, meaning multiple organizations (tenants) share the same computing resources. To make sure no single code execution takes up all the resources and affects others, Salesforce enforces Governor Limits. These are runtime limits that every Apex execution must follow — such as limits on SOQL queries, DML operations, CPU time, and heap size.

If your code exceeds these limits, it throws a runtime exception, and the transaction fails.

Real-Life Analogy

Think of Salesforce as a public library with limited seating. To ensure fairness, there’s a time limit for how long each person can use a seat (just like a CPU time limit in Apex), and you can borrow only a certain number of books (similar to SOQL or DML limits). 
If someone tries to break the rule and take 50 books at once, the librarian (Salesforce Governor) stops them immediately — the same way Apex Governor Limits prevent misuse of platform resources. 

Common Apex Governor Limits

Limit Type Value (per transaction)
SOQL Queries 100 queries
Records Retrieved via SOQL 50,000 records
DML Statements 150 statements
Records Processed via DML 10,000 records
CPU Time 10,000 ms (synchronous)
Heap Size 6 MB (synchronous)

If you need to process more records than the limit (e.g. >10,000), you should use Batch Apex.

1: Track SOQL Usage

List<Account> accList = [SELECT Name FROM Account LIMIT 10];
System.debug(‘SOQL Queries Used: ‘ + Limits.getQueries());

Output:

SOQL Queries Used: 1

Code Explanation:

• We queried Account once. 
• Limits.getQueries() tells us how many SOQL queries have been used in this transaction. 

2: Track DML Usage

List<Contact> contactList = new List<Contact>();

for (Integer i = 0; i < 3; i++) {
contactList.add(new Contact(FirstName=’Test’, LastName=’User’ + i));
}

insert contactList;

System.debug(‘DML Statements Used: ‘ + Limits.getDmlStatements());

Output:

DML Statements Used : 1

Code Explanation :

• We created and inserted 3 contacts using a single insert statement (bulk insert).

• Only 1 DML statement was used, keeping us within limits.

Best Practices to Stay Within Limits

Bulkify your code: Avoid DML or SOQL inside for loops. 

 Use Collections: Insert/update records in bulk. 

Query only required fields: Don’t use SELECT
Use Limits methods to monitor usage (Limits.getQueries(), Limits.getDmlStatements() etc). 

Tasks For Practice

Task 1: Count SOQL Queries Used

Run two SOQL queries and print the total queries used using Limits.getQueries(). 

Task 2: Check DML Statements

Insert 5 Lead records in one DML operation and print the number of DML statements used with Limits.getDmlStatements().

Task 3: Avoid DML in Loops

Write a loop that creates 10 Contact records, but insert them after the loop using a collection (List).
Check how many DML statements were used.

Task 4: Track CPU Time Usage

Write code that performs a long loop operation and use: 

System.debug(‘CPU Time Used: ‘ + Limits.getCpuTime());

to see how much CPU time was consumed.