Skip to content

Profiling applications in production without putting users at risk

B2Bnetwork team · · 6 min

System performance

In brief

You profile an application in production safely with sampling tools whose overhead is low and measured, such as JDK Flight Recorder for the JVM and dotnet-trace for .NET, for a limited time and usually on one instance. A profile shows which functions consume CPU time, where the application waits and how much memory it allocates, on real traffic and real data. High-impact operations, such as heap dumps or instrumenting every method, belong on an instance taken out of traffic.

How profiling differs from monitoring

Monitoring and observability tell you that something is slow and in which service: metrics show rising response times, traces point to the slow span of a request. Profiling goes one level deeper and answers which function in the code burns CPU, what threads are waiting for and where the objects that load the garbage collector are created. We cover metrics, logs and traces in the article on observability.

Profilers work in two ways:

  • Sampling. Every few milliseconds the profiler records thread call stacks. Overhead is low and predictable, and the result is statistical: a function seen in 30% of samples uses roughly 30% of the time.
  • Instrumentation. The profiler adds a measurement to the entry and exit of every method. It gives exact call counts but can slow the application down many times over, so it does not belong in production.

Why profile in production

A test environment rarely reproduces everything: data distribution, request mix, the behavior of external systems, a process running for days. A problem that appears once a day for one corporate customer with a million transactions usually does not show up in a test. A profile captured in production shows the real hot paths instead of guesses.

Production profiling does not replace performance testing. Tests check that a change meets requirements before deployment, while a production profile suggests which scenarios to add to the tests and which fixes will pay off most.

Overhead and risk: what is safe

ActivityImpact on the applicationWhere to do it
runtime counters (dotnet-counters, GC and thread statistics)very smallall of production, continuously
sampling CPU and allocation profilesmall, measurablea selected instance, for a limited time
JDK Flight Recorder with default settingssmallproduction, even continuously
JDK Flight Recorder with profile settingslargershort recordings, one instance
class histogram, heap dumphigh, pauses the processan instance taken out of traffic
instrumenting all methods, debugger with breakpointsvery hightest environments only

For the JVM the reference point is JEP 328, which brought Flight Recorder to OpenJDK in JDK 11. Among its goals it lists at most 1% performance overhead out of the box on the SPECjbb2015 benchmark and no measurable overhead when recording is disabled. The Oracle documentation describes two templates: default.jfc collects data with low overhead, profile.jfc collects more at a cost to performance. A benchmark figure is not a guarantee for your application, so measure overhead yourself, as described below.

Tools for the JVM and .NET

JVM. Flight Recorder starts with a command-line option or, in a running process, through jcmd:

java -XX:StartFlightRecording=duration=10m,filename=/var/tmp/app.jfr -jar app.jar
jcmd <pid> JFR.start duration=10m filename=/var/tmp/app.jfr
jcmd <pid> JFR.dump filename=/var/tmp/app.jfr

You open the recording in JDK Mission Control, which shows hot methods, locks, garbage collection, I/O and allocations. The jcmd documentation lists JFR.start as a low-impact operation.

.NET. Microsoft provides dotnet-counters for a continuous view of runtime counters and dotnet-trace for collecting traces from a running process. dotnet-trace is built on the .NET runtime's EventPipe, so it needs no native profiler. You open the trace in Visual Studio, PerfView, or a browser in speedscope format.

At the operating system level, Linux profilers such as perf are also useful, because they see native code and the kernel. They require host privileges, though, so in clusters the platform team usually runs them.

Rules for safe profiling

  1. An overhead budget. Agree with the service owner how much slowdown is acceptable and tie it to the error budget; how to calculate one is covered in the article on SLOs and error budgets.
  2. Measure overhead before production. Run the same load test with and without the profiler and compare response time percentiles.
  3. One instance, limited time. Profile one replica or a canary instance, with the duration set in the command rather than stopped by hand.
  4. Disk space. Write recordings to a volume with a quota, not to the disk used by the database or logs.
  5. Data in profiles. Traces and recordings contain class and method names, file paths and sometimes fragments of data. Treat them like production logs: role-based access, retention limits, deletion after analysis.
  6. Keep a record. Who profiled, when and on which instance. During an incident you then know whether the profiler could have played a part.

Heavy memory operations are covered in the article on memory leaks in the JVM and .NET.

How to turn a profile into a fix

Profiles are most often viewed as flame graphs: bar width is the share of samples and height is stack depth. Look for wide bars near the top, meaning functions that themselves consume a lot of time. Typical findings in enterprise systems are JSON serialization of large objects, regular expressions compiled on every call, debug-level logging on a hot path, a lock around a shared resource and excess allocations in loops.

After a fix, capture a second profile under the same conditions and compare it with the first, then check the effect in response time percentiles rather than the average, as we explain in the article on percentiles instead of averages. An example of performance work before a major change in a bank's system is the project on performance testing online banking before a core migration.

Questions and answers

Can JDK Flight Recorder run in production all the time?

Many teams do this, with default settings and an in-memory recording that is written to disk only when a problem occurs. JEP 328 targeted at most 1% overhead out of the box on a benchmark, but your application may behave differently. Measure the overhead with a load test before turning recording on permanently.

What is the difference between a sampling and an instrumenting profiler?

A sampling profiler records thread stacks at intervals and infers from their frequency where time goes. Overhead is small and does not depend on the number of calls. An instrumenting profiler measures every method entry and exit, so it gives exact numbers, but with short, frequently called methods it slows the application down heavily.

How do I profile an application running in Kubernetes?

The simplest way is with runtime tools invoked inside the container: jcmd for the JVM, dotnet-trace for .NET, writing to a volume and copying the file out with kubectl cp. Pick one replica and, if possible, remove it from the load balancer for heavier operations. Node-level profilers need privileges that usually only the platform team has.

Is production profiling compliant with GDPR?

Profiling itself measures code, but recordings and traces can contain personal data from memory or parameters. So the same rules apply as for logs: minimal scope, access control, limited retention and deletion after analysis. Full memory dumps need particular care, because they contain everything the application was processing.

What it looks like in a project

Your system slows down under load? We'll find where it loses time.

Let's talk

Read more: System performance