0

What, if anything, is the overhead of leaving System.debug() statements in Apex code that's deployed to production?

Obviously, if Debug Logging is started for a User who is executing the code there must be some overhead to execute the debug() statements and log the output, but in the normal circumstances where Debug Logging is NOT enabled, is there any overhead at all?

1 Answer 1

4

Unlike some languages, where debug statements are stripped from code in production mode, Apex does not do this. That means that System.debug will use CPU time, because the parameters are serialized every time. Every debug statement you leave in your code will increase the CPU usage of your code. Conceptually, heavy use of debugging can lead to CPU timeout errors.

3
  • Thanks! So, have I generally been right in considering it a 'best practice' to remove System.debug() statement from Apex code before it's deployed to production? The only time I usually make an exception to this rule is in pieces of code which are intended for error handling and, therefore, shouldn't be executed in normal circumstances.
    – John Lewis
    Commented Aug 30, 2023 at 11:07
  • 1
    @JohnLewis Correct. "If a tree falls in a forest, does it still make a sound?" logic applies here. I personally do not allow debug statements in production code if I have control over the code. If one needs to debug in production, they can use Checkpoints, which allows one to inspect heap memory on demand without debug statements.
    – sfdcfox
    Commented Aug 30, 2023 at 11:09
  • 1
    Another reason not to permit debug statements in production revolves around PII and other sensitive information; if this were to be included in debug statements it could be leaked out of the org when downloading and sharing debug logs.
    – Phil W
    Commented Aug 30, 2023 at 12:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .