Apex Character Limit – What to do?

Apex Character Limit

This post was NOT written by AI.
I only used AI to help refine my grammar, because I am not a native.


What Is the Apex Character Limit?

As you already know, Salesforce uses a multi-tenant architecture. Salesforce enforces limits to ensure that runaway Apex code or processes don’t monopolize shared resources [1]. The Salesforce documentation states that the default Apex code character limit is 6 MB [1]; however, when you go to Setup > Apex Classes, you will see 6,000,000 characters.

apex limit

I couldn’t find any Salesforce resource that confirms it, but it looks like Salesforce loosely assumes that 1 character ≈ 1 byte. For scratch orgs, the limit is 10 MB, which allows for 10,000,000 characters. Other orgs have 6 MB, which is 6,000,000 characters.

Why Is It Important?

When you hit the limit, you won’t be able to deploy any new code. You will get the following error:

The total size of apex code in this application after removing comments
exceeds the maximum character size of 6000000

I experienced this on one of my projects, where many developers didn’t notice that the limit had been reached, and the entire development effort was blocked until Salesforce increased the limit. It is worth keeping an eye on it before it is too late. As we can read in the documentation:

Be aware that the process of increasing the limit will not occur within hours. Salesforce recommends that you start the process at least a week before you expect to hit the limit, and even earlier if remedial steps are required.

How Is It Counted?

What Is Not Counted?

  • Apex code in first-generation (1GP) or second-generation (2GP) managed packages [2].
  • All classes with the @IsTest annotation [2].

How Is It Counted?

Construct Counted? Cost
Code ✅ Yes 1 character = 1
Newline ending a line with code ✅ Yes 1 newline = 1
Indentation (spaces ·) ✅ Yes 1 space = 1
Tab indent (\t) ✅ Yes 1 tab = 1 (not 4)
Trailing whitespace (invisible space at end of a line) ✅ Yes 1 space = 1
// or /* */ inside a string literal ✅ Yes 1 character = 1
Accented / CJK characters (ä ö ü ) ✅ Yes 1 character = 1
Emoji / astral characters (😀) ✅ Yes 1 character = 2
@IsTest annotation text ✅ Yes 8 (7 chars + newline)
Comments: //, /* */, /** */ ❌ No 0 – whole line removed
Blank line ( alone, or spaces only) ❌ No 0 – whole line removed
Extra \r in CRLF (\r\n) vs LF (\n) ❌ No 0 – normalized to LF
Trailing whitespace at end of file ❌ No 0 – trimmed

e.g. ········Integer␣a␣=␣10;↵

Part Table row Counted? Cost
········ (8 spaces) Indentation (spaces) ✅ Yes 1 × 8 = 8
Integer a = 10; Code ✅ Yes 1 × 15 = 15
(newline) Newline ending a line with code ✅ Yes 1
Total 24

What to Do When You Hit It?

Based on my experience from other projects with huge Salesforce orgs, there are a few strategies to mitigate this issue.

Support Case

To increase the character limit from 6,000,000 to 10,000,000, you only need to create a ticket with Salesforce Support. They can increase it quite easily, without any additional approvals. I have done this a few times on my previous projects, and you can also find evidence online that this is possible.

The documentation says that, when you log a case, you need to provide the following information:

  • Test classes are correctly annotated.
  • There are no testing anti-patterns to artificially manipulate code coverage.
  • All Apex classes and triggers compile.
  • The reason for the request.
  • Your org’s current limit.
  • The requested limit. Increases will be reviewed in increments to the nearest multiple of five million.

Support Case with Code Refactoring

I’ve worked on two projects where the limits were even higher than 10,000,000 characters. The org character limit was increased to 15,000,000 characters on one project and to 25,000,000 on another. So it is definitely possible to increase the limit beyond 10 million. However, as we can read in the Salesforce documentation [1]:

Any increases above 10 MB may require additional scrutiny from Support

Increasing the limit above 10,000,000 characters requires more justification and collaboration with Salesforce Support. On the project where we increased the limit to 25 million, we first had to resolve all PMD issues (Salesforce required it). In the documentation we can find that:

As a general rule, regularly compile all Apex code in production and fix compilation failures. You must also ensure that all tests pass, and provide at least the required minimum level of code coverage.

So not only PMD issues, but also compilation problems and code coverage have to be fixed before you get the approval.

It is important to react in advance, before you hit the 10 million character limit. Refactoring can take some time, and Salesforce won’t increase the limit without it. You can end up with a blocked environment.

Just Change Formatting

A third option is to use tabs instead of spaces for code formatting (source). The code looks exactly the same (when tab is 4 spaces), but you save a significant number of characters, because Salesforce counts leading and trailing whitespace. One tab replaces four spaces. To verify this, I created a new scratch org containing only the SOQL.cls class. With spaces, it used 118,794 characters. After converting the indentation to tabs, the usage dropped to 101,268 characters, which is a significant reduction. If we do this across all our classes, I believe we could reduce our total character usage by another 10–15%. The change is significant and the effort is quite low.

apex character limit

apex limit

Version LengthWithoutComments
Original (4-space indent) 118,794
Refactored (tab-converted) 101,268
Difference -17,526 (-14.75%)

It is pretty easy to achieve in Visual Studio Code by selecting the Convert Indentation to Tabs option.

spaces into tabs

Need Help?

If you need help with the Apex character limit or code refactoring, reach out at contact@beyondthecloud.dev or via LinkedIn. You can also message me directly.

Resources

Piotr Gajek
Piotr Gajek
Senior Salesforce Developer
Technical Architect and Full-stack Salesforce Developer. He started his adventure with Salesforce in 2017. Clean code lover and thoughtful solutions enthusiast.

You might also like

Abstract, Virtual, Interface in Apex
August 14, 2022

Abstract, Virtual, Interface in Apex

Find the differences between Abstract, Virtual, and Interface implementation in Apex code. Understand the purpose and make your code better.

Piotr Gajek
Piotr Gajek

Senior Salesforce Developer

Salesforce Flow Considerations
August 29, 2023

Salesforce Flow Considerations

Flows are an amazing automation tool that offers you a lot of possibilities. However it also have a lot of limitations you need to be aware of.

Piotr Gajek
Piotr Gajek

Senior Salesforce Developer