Scrivio Interview library
ScenarioAdvanced7 min read · updated 12 November 2026

Startup time of a large Spring application is dominated by many static initializers that build caches and parse resources. What fleet-wide approach do you take?

First we measure, with JFR, -Xlog:class+init and Spring's startup tracking, to find the few initializers that really cost time. Then we make expensive ones lazy with the holder idiom or memoized suppliers, or move the work to build time, and we use Class Data Sharing or the JDK's AOT cache to save class loading and linking. Finally we set a fleet-wide standard that libraries do no I/O in static initializers, because static state also makes tests and native-image builds harder.

Why this question is asked

Startup time matters more than it used to. Services scale out by starting new instances under load, rolling deployments wait for each instance to become ready, and slow starts make autoscaling lag behind traffic. When static initializers dominate startup, the cost is spread across hundreds of classes and dozens of libraries, so no single team sees it as their problem. Interviewers ask this to see whether we can approach it as an engineering programme: measure, fix the biggest offenders, use the platform's features, and change the standard so it stays fixed. In this answer, we will recap why static initializers hurt startup, measure them, look at the fixes in order of value, see what CDS and AOT caches do and do not save, and finish with the fleet-wide standard.

1Why Static Initializers Show Up in Startup

Each class has one static initializer, made of its static field initializers and static blocks, and the JVM runs it the first time the class is used. In a Spring application, startup touches thousands of classes: component scanning, bean creation and auto-configuration load and initialize them all. If many of those classes build caches, parse XML or JSON resources, compile regular expressions or scan the classpath in static code, all of that work runs before the application can accept traffic.

Two details make it worse. Class initialization runs on whichever thread first touches the class, usually the single main thread during startup, so this work rarely runs in parallel. And the work is often wasted: a library may build a large table that this particular service never uses.

2Measuring Before Changing Anything

We start with numbers, on one representative service. Spring Boot can record startup steps with an ApplicationStartup implementation, and the actuator startup endpoint then shows which beans took longest to create. That tells us where time goes at the bean level.

To see static initialization itself, we record the startup with Java Flight Recorder and look at the hot methods: static initializers appear as frames named clinit in the stack traces, along with whatever they call. Unified logging can list classes as they are initialized; the class+init tag is a starting point, and some recent JDKs add more detailed initialization timing, so check your version for the exact options.

Java
# record the first 60 seconds of startup
java -XX:StartFlightRecording=duration=60s,filename=startup.jfr -jar app.jar
# list class initialization events in the log (exact tags and detail vary by JDK version)
java -Xlog:class+init=info:file=init.log -jar app.jar

Typically the result is a long tail with a few large items. In one service, we might find that four initializers take 3.1 seconds of a 9-second start: a classpath scan for plugins, a large dictionary parsed from a resource, a regex table and a vendor client warming its connection pool. Those four are where we start.

3Fix 1: Make the Expensive Work Lazy

Work that is not needed for every request, or not needed during startup, should run on first use. The holder idiom does this for static values with no locking code of our own: the value lives in a nested class, and the JVM initializes that class only when it is first touched.

Java
final class Dictionary {
private Dictionary() {}
private static final class Holder {
static final Map<String, Entry> WORDS = parse("/dictionary.json"); // runs on first lookup, not at startup
}
static Entry lookup(String word) { return Holder.WORDS.get(word); }
}
// Startup: Dictionary is loaded, Holder is not. First lookup: Holder initializes once (thread-safe).

For values that may need refreshing or a fallback, a memoized Supplier, or a Spring bean marked @Lazy, gives the same effect with more control. But there is a catch: lazy work moves the cost to the first request. For latency-sensitive services, we may prefer to start the work on a background thread after the application is ready, so the first users are not the ones who pay.

4Fix 2: Move the Work to Build Time

Some work produces the same result every time, such as parsing a fixed resource or scanning the classpath for plugins. That work can run once at build time. A build step can parse the JSON dictionary into a compact binary file, or generate a Java class with the table already in it, and a plugin scan can be replaced by a generated index file or a ServiceLoader file listing the implementations.

5Fix 3: Use CDS and the AOT Cache for Loading and Linking

The JVM itself can save part of startup. Class Data Sharing stores parsed and verified classes in an archive that later runs map into memory, so class loading gets cheaper. Spring Boot 3.3 and later can prepare an application for CDS with a training run. JDK 24 added the AOT cache (JEP 483), which also stores classes in a loaded and linked state, and Java 25 made creating it a single step and added method profiles from the training run.

Text
# Java 25: a training run writes the cache, later runs use it
java -XX:AOTCacheOutput=app.aot -jar app.jar # exercise the app, then stop it
java -XX:AOTCache=app.aot -jar app.jar
What each technique saves
TechniqueSavesDoes not save
Holder idiom, @Lazy, Supplierwork this run never useswork moved to the first request
Build-time generationrepeated parsing and scanningnothing, but needs a build step
CDS archiveclass parsing and verificationour static initializers
AOT cache (JDK 24+)loading and linking, plus profiles in 25our static initializers
Native imagealmost all startup workflexibility: needs init configuration

Notice what this does and does not save. It saves loading, verifying and linking classes, which is often a large part of startup, and reported improvements of 30 to 40 percent are common for Spring applications. It does not, in general, run our own static initializers ahead of time, so a clinit that parses a 20 MB file still parses it on every start. CDS and the AOT cache complement fixes 1 and 2; they do not replace them.

6Native Images and Tests Raise the Same Issue

Static state makes two other things harder. Tests cannot run the same initializer twice with different inputs, because the class initializes once per JVM. And GraalVM native images must decide for each class whether to run its static initializer at build time or at run time. Since GraalVM moved to run-time initialization by default, a class whose static code reads the environment works, but loses the startup benefit, and forcing build-time initialization of such a class can bake build-machine values into the binary. Either way it needs configuration that someone has to maintain.

7The Fleet-Wide Standard

The fixes above are one-off. To keep startup from creeping back, we set a standard that every team and every shared library follows: static initializers may build only constants and small pure tables, and do no I/O, no network calls, no classpath scanning and no configuration reads. Large data is loaded lazily or generated at build time. Shared libraries get this rule first, since one library fixes dozens of services.

We back it with a startup-time metric for every service, recorded in CI on a fixed machine size, and a budget, for example no more than a 10 percent regression per release without an explanation. The base Docker image and service template enable the AOT cache or CDS by default, so every team gets the platform saving without having to learn it.

8Follow-up questions

How do you decide between lazy initialization and doing the work at startup on purpose?

We look at who pays. If most instances never use the data, lazy loading is pure saving. If every request needs it, lazy loading only moves the cost to the first users, so we either keep it at startup and include it in the readiness check, or start it on a background thread right after startup and serve a fallback until it is ready.

What would you check first if the AOT cache gave no improvement?

That the training run really exercised startup and was made with the same JDK, the same classpath and the same JVM options as production, since a mismatch makes the JVM ignore the cache. The JVM logs a warning when it cannot use the cache; we would check those logs and then re-measure with JFR to see whether the remaining time is in our own static initializers.

9Conclusion

In this answer, we treated slow static initializers as a fleet problem. We measured with JFR, unified logging and Spring's startup tracking to find the few initializers that matter, made them lazy with the holder idiom or suppliers, or moved fixed work to build time, and used CDS and the AOT cache to save class loading and linking, while noting that these do not run our own static initializers. We saw that static state also complicates tests and native images, and closed with a standard of no I/O in static initializers, backed by a startup budget per service.

What to say in the interviewAbout 90 seconds aloud

Open with

I would treat it as a measurement-first programme. On a representative service I record startup with JFR, where static initializers show up as clinit frames, plus class+init logging and Spring Boot's startup tracking for bean timings.

Then, if they want more

Usually a handful of initializers, like classpath scans, big resource parsing or client warm-ups, account for most of the time.

For those, I make the work lazy with the holder idiom, a memoized supplier or a lazy bean, keeping in mind that this moves the cost to the first request, or I move fixed work to build time, for example by generating the table or an index file.

Then I take the platform savings: CDS, which Spring Boot supports with a training run, or on JDK 24 and 25 the AOT cache, which saves class loading and linking and in Java 25 also keeps method profiles. I would be clear that these do not run our own static initializers ahead of time, so they complement the code fixes.

Static state also makes testing and native images harder, because native images must decide build-time or run-time initialization per class. So the lasting step is a standard, starting with shared libraries, that static initializers do no I/O, plus a startup-time budget per service in CI and the AOT cache enabled in the base image.

About this answer

Last updated
12 November 2026
Versions it names
Java 25, Java 24, Spring Boot 3.3
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 "Startup time of a large Spring application is dominated by many static initializers that...": https://scrivio-interview-library.netlify.app/java/classes-and-objects/8484df269b/