Bonnie Eisenman bio photo

Bonnie Eisenman

Software engineer, author, knitter, Esperantist. Member of NYC Resistor and author of Learning React Native.

🐦 Twitter šŸ¤– Github šŸ”¶ RSS Feed

This is a really small programming ā€œstyleā€ best practice that came up at work last week.

TLDR; don’t pre-concatenate strings in Java log calls!

I’m working in Java now after a long stint in Scala-land. I used Java in university and at my first few internships but haven’t worked in it since then. So, there are a lot of coding conventions and best practices that I’m having to re-learn…

One of them is about logs!

Fun fact, my very first internship project at Google was about calculating the memory usage of log strings in Gmail. Log strings can be expensive. Let’s look at some code.

// Version 1 - boo
log.info("Data processed for user " + user.getId());

// Version 2 - yay
log.info("Data processed for user {}", user.getId());

Though they look very similar, Version 1 is more expensive than the Version 2.

Let’s say that you have your logging level configured so that only ERROR level logs are actually logged. However, the concatenation in version 1 will happen before the invocation of log.info. That means that you’re doing extra work on every request to concatenate those strings…and then throw them away! Yuck! If you are processing lots of traffic, all of those extra (useless) string allocations can add up and really impact your newgen memory churn. And worst of all, those strings are unique per user (or message ID, or whatever other variable you’re logging).

In version 2, the user’s ID is passed as an argument to the logger. The logger will only build the string if it actually needs it. Yay! Much better.

It feels like a small style nit but these kinds of allocations can have a real impact. Also, logs are a common place to find this kind of wasted work, but the same principle applies generally. ā€œDon’t pre-build unique strings unless you have toā€ is a good rule of thumb.

Thanks to Sean Wu for discussing this with me in Recurse Center chat!

Related links: