Quick revision
The short answer
Java evaluates method arguments before the call, so String.format and toJson run on every request and their strings are thrown away inside log.debug when DEBUG is off. Switching to parameterised logging removes the formatting cost, but toJson still runs because it is an argument, so it must be guarded by isDebugEnabled() or passed lazily as a Supplier. We then confirm the drop with a new allocation profile and add a review or lint rule so it does not come back.
What to remember
In this answer, we saw that a disabled debug line still allocated heavily because Java evaluates arguments before the call, so String.format and toJson ran for every order. Parameterised logging removed the formatting but not the expensive toJson argument, which needed a guard or a lazy Supplier. We verified the result with a second allocation profile and closed the loop with a lint rule and a logging guideline.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Java evaluates method arguments before the call, so String.format and toJson run on every request and their strings are thrown away inside log.debug when DEBUG is off. Switching to parameterised logging removes the formatting cost, but toJson still runs because it is an argument, so it must be guarded by isDebugEnabled() or passed lazily as a Supplier. We then confirm the drop with a new allocation profile and add a review or lint rule so it does not come back.
Why this question is asked
Logging statements look harmless, and that is exactly why they cause so many allocation problems. A line that never prints anything in production can still build large strings on every request. Interviewers like this scenario because it tests a basic language rule, argument evaluation order, and whether we know how logging libraries are designed to work around it. In this answer, we will see why the disabled log line still allocates, try the first fix and see why it is not enough, apply the full fix, and then look at how to verify it and keep it fixed.
1Why a Disabled Log Line Still Does Work
In Java, all arguments of a method call are evaluated before the method body runs. The logging library cannot help with that, because by the time log.debug starts executing, its argument is already a finished String.
Look at the original line in that light. First order.id() is called. Then order.items() is called and passed to toJson, which walks the whole list and builds a JSON string. Then String.format parses its format pattern, converts both values and builds another string. Only then is log.debug called. Inside it, the very first thing the library does is check whether DEBUG is enabled for this logger, see that it is not, and return. All the work, and all the strings it produced, become garbage immediately.
7 messages in order. Play them, or step through them one at a time.
The expensive work happens before the logger has a chance to say no.
For an order with 50 items, the JSON alone can easily be tens of kilobytes, and String.format copies it again. At a few thousand orders per second, that is how we arrive at gigabytes per second of allocation for a line that prints nothing.
2Fix 1: Parameterised Logging
SLF4J and Log4j 2 accept a message pattern with {} placeholders and the arguments separately. The library checks the level first and only formats the message when the level is enabled. So the formatting work disappears for disabled levels.
This is a real improvement and should be the default style everywhere. But notice the catch: toJson is still an argument expression, so it still runs before log.debug is called. The pattern formatting was the cheap part. The JSON serialisation, which walks the items and builds the big string, is still paid on every request.
3Fix 2: Make the Expensive Argument Lazy
We need the expensive argument to be evaluated only when the level is on. There are two common ways. The classic one is an explicit guard. The newer one is to pass a Supplier, a small function the library calls only if it decides to log.
With either form, when DEBUG is off the cost is one level check and, for the lambda versions, possibly a tiny lambda object that escape analysis often removes. The guard is the most explicit and the easiest to read in reviews. The Supplier forms keep the call on one statement. Both are correct; teams should pick one style and use it consistently.
4Verifying the Fix
A fix for a performance problem is not done until we have measured it. We take the same JFR recording as before, under the same load, and compare the allocation rate and the top allocating stacks.
We should also look at the knock-on effects described by the GC log: young collections should now run far less often, and CPU time on GC threads should fall. If the allocation rate barely moved, the profile tells us the next culprit.
5Keeping It Fixed
One fixed line does not stop the next developer from writing the same thing. The pattern is easy to detect mechanically, so it belongs in tooling, not only in memory. Static analysis tools such as SonarQube and PMD have rules that flag string concatenation or formatting inside log calls and unguarded expensive arguments (check the exact rule names in your version). Adding such a rule to the build, plus a short note in the team's logging guidelines that shows the guard and the Supplier form, catches most cases before review.
It is also worth asking whether the line is needed at all. Logging a full JSON dump of every order at DEBUG is rarely useful in production. Logging the order id and the item count is often enough, and it costs almost nothing even when enabled.
6Follow-up questions
Is string concatenation with + any better than String.format here?
No. log.debug("order " + id + " items " + json) also builds the string before the call. Concatenation is cheaper than String.format, but it is still eager. Only parameterised messages, guards or Suppliers avoid the work.
Does parameterised logging allocate anything when the level is off?
Very little. With one or two arguments, SLF4J has overloads that avoid creating an array. With three or more arguments the varargs array is created before the call, and primitive arguments are boxed. That is usually negligible, but in an extremely hot loop a guard avoids even that.
Could the JIT remove the dead work by itself?
Not reliably. To skip toJson, the JIT would have to prove that the result is unused and that toJson has no side effects, across a call into the logging library. In practice it does not, which is why the allocation shows up in JFR.
7Conclusion
In this answer, we saw that a disabled debug line still allocated heavily because Java evaluates arguments before the call, so String.format and toJson ran for every order. Parameterised logging removed the formatting but not the expensive toJson argument, which needed a guard or a lazy Supplier. We verified the result with a second allocation profile and closed the loop with a lint rule and a logging guideline.
Open with
The cause is plain Java evaluation order. Arguments are evaluated before a method is called, so toJson walks the items and String.format builds the message on every request, and then log.debug checks the level, sees DEBUG is off and throws both strings away.
Then, if they want more
With large orders and thousands of requests per second, that easily adds up to gigabytes per second. The first fix is parameterised logging, log.debug with a pattern and placeholders, so the library only formats the message when the level is enabled. But that alone is not enough here, because toJson is still an argument and still runs eagerly.
So I either wrap the call in if log.isDebugEnabled, or pass the JSON as a Supplier, using the SLF4J 2 fluent API with addArgument and a lambda, or Log4j's Supplier overloads, so the serialisation only happens when DEBUG is actually on.
Then I verify: same load, new JFR recording, and I check that the allocation rate dropped and toJson disappeared from the top allocation sites, and that young GC frequency went down.
Finally, I add a static analysis rule against eager formatting in log calls and a short guideline, and I would question whether a full JSON dump at DEBUG is needed at all.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "JFR shows a service allocating about 2 GB per second, and most of it comes from debug...": https://scrivio-interview-library.netlify.app/java/java-performance/c403baf542/