How to Slash Spring Boot Startup Time from 10s to Sub‑Second: Proven Techniques

This article explores comprehensive strategies for dramatically reducing Spring Boot application startup latency, covering real‑world case studies, dependency slimming, JVM and CDS tuning, lazy and asynchronous bean initialization, GraalVM native image compilation, and advanced runtime techniques such as CRaC, with detailed metrics and step‑by‑step guidance.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
How to Slash Spring Boot Startup Time from 10s to Sub‑Second: Proven Techniques

Introduction: Why Startup Speed Is Critical

During the "618" traffic surge on an e‑commerce platform, a traditional JVM‑based order service took three minutes to restart a single instance, causing the system to miss over a hundred thousand transactions. Similar delays were observed in an advertising platform where a single machine needed 400‑500 seconds to start, plus image download time, leading to a ten‑minute deployment window and lost ad revenue. These cases demonstrate that application startup time is a foundational factor for system elasticity, especially in cloud‑native and micro‑service architectures.

Multi‑Dimensional Value of Startup Optimization

From a technical perspective, startup speed directly impacts three core areas:

System elasticity and resource efficiency : In Serverless and FaaS scenarios, fast cold starts are essential; a traditional JVM can take tens of seconds, while native images can start in sub‑second.

Development and operations efficiency : Slow startups prolong integration testing cycles; reducing startup time can double developer productivity.

Cost control : In 2025 cloud‑native environments, memory usage of native images is up to 60 % lower than traditional JVMs, and startup time reductions from 3 s to 100 ms can triple container utilization.

Technical Evolution and Optimization Space

Traditional JVMs use interpretation plus JIT compilation, leading to 10‑30 s startup for large Spring Boot apps. GraalVM native images perform ahead‑of‑time (AOT) compilation, shrinking startup to 2‑3 s. CRaC (Coordinated Restore at Checkpoint) captures a process snapshot, allowing restoration in milliseconds.

Toolchain for Startup Bottleneck Analysis

The following tools form a three‑step analysis chain:

Spring Boot Actuator : Enable /actuator/startup (Spring Boot 3.4+) to obtain a JSON timeline of each startup phase.

Spring Startup Analyzer : An agent‑based tool that generates an interactive HTML report with flame graphs and bean initialization rankings.

JProfiler / Java Flight Recorder / VisualVM : JVM‑level profilers for class‑loading, thread contention, and GC pauses.

Actuator Configuration Example

management:
  endpoint:
    startup:
      enabled: true
  endpoints:
    web:
      exposure:
        include: startup

Access the timeline via curl http://localhost:8080/actuator/startup. The context refreshed phase often dominates (e.g., 72 % of total time).

Dependency and Auto‑Configuration Slimming

Redundant dependencies and auto‑configuration classes increase class‑path scanning and memory consumption. Apply a three‑step slimming process:

Dependency analysis : Run mvn dependency:analyze to identify unused declared dependencies and undeclared used ones; remove the former.

Auto‑configuration exclusion : Use

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, RedisAutoConfiguration.class, HibernateJpaAutoConfiguration.class})

or the property

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration

to skip unnecessary starters.

Starter component reduction : Replace spring-boot-starter-web (Tomcat) with spring-boot-starter-webflux for reactive workloads, or use spring-boot-starter-webclient when only an HTTP client is needed.

Typical results: dependency count reduced from 45 to 28 (‑38 %), JAR size from 120 MB to 84 MB (‑30 %), and overall startup time from 10 s to 7.7 s (‑23 %).

JVM Parameter Tuning

Key tuning areas include memory configuration, startup acceleration flags, and GC selection.

Memory : Set fixed heap size (e.g., -Xms2g -Xmx2g) and metaspace ( -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m) to avoid dynamic resizing.

Startup acceleration : Disable byte‑code verification ( -Xverify:none) and limit JIT tier ( -XX:TieredStopAtLevel=1) for a 20 % speedup in development.

GC : Use G1GC for ≤4 GB heaps ( -XX:+UseG1GC -XX:MaxGCPauseMillis=100) or ZGC for larger heaps ( -XX:+UseZGC).

Example command:

java -Xms2g -Xmx2g -XX:+UseG1GC -XX:MaxGCPauseMillis=100 -XX:+ParallelRefProcEnabled -XX:+UseCompressedOops -XX:+AlwaysPreTouch -jar app.jar

Class Data Sharing (CDS)

CDS creates a shared archive ( .jsa) of class metadata, allowing multiple JVM instances to reuse it. Generate the archive with:

java -Xshare:dump -XX:SharedArchiveFile=app-cds.jsa -jar app.jar

Start the application with -XX:SharedArchiveFile=app-cds.jsa. In Spring Boot 3.3+, CDS is automatically generated when -Dspring.context.exit=onRefresh is used, reducing class‑loading time by 30‑50 %.

Coordinated Restore at Checkpoint (CRaC)

CRaC snapshots the entire JVM state after warm‑up. Add the experimental spring-crac dependency, start the app, then create a checkpoint with jcmd <pid> JFR.checkpoint. Subsequent starts restore from the checkpoint:

java -XX:CRaCRestoreFrom=checkpoint-file -jar app.jar

Benchmarks show cold‑start reductions from 3.5 s to 0.8 s (‑77 %).

GraalVM Native Image (AOT Compilation)

Building a native image removes the JVM entirely, yielding sub‑second startup and lower memory footprints.

Prerequisites: JDK 17+, GraalVM 22.3+, native-maven-plugin or native-gradle-plugin.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-aot</artifactId>
</dependency>
<plugin>
  <groupId>org.graalvm.buildtools</groupId>
  <artifactId>native-maven-plugin</artifactId>
</plugin>

Compile with: ./mvnw -Pnative native:compile -DskipTests Handle reflection by adding @RegisterReflectionForBinding(MyClass.class) or a reflect-config.json file. After compilation, startup drops from 3 s to 0.8 s and memory from 220 MB to 75 MB.

Bean Initialization Optimization: Lazy & Asynchronous Loading

Spring Boot 2.2+ supports global lazy initialization via spring.main.lazy-initialization=true, deferring non‑critical beans until first use. Critical beans (data sources, transaction managers) should stay eager ( @Lazy(false)).

For beans that must initialize at startup but are slow, apply @Lazy together with @Async and a custom thread pool:

@Component
@Lazy
public class HeavyResourceLoader {
    @Async
    @PostConstruct
    public void init() {
        loadHugeData();
    }
}

This pattern moved a 3.2 s initialization down to 200 ms and reduced peak memory by 420 MB in a real case.

Comprehensive Optimization Path (Case Study)

A medium‑size microservice originally started in 10 s. Applying the following steps achieved a final startup of 0.8 s:

Dependency & auto‑configuration slimming : Removed unused libraries and excluded 8 auto‑config classes, cutting startup to 7.7 s.

JVM & CDS tuning : G1GC parameters and CDS archive reduced class‑loading time to 1.8 s, overall startup to 4.5 s.

Bean lazy & async loading : Global lazy init plus async for three heavy beans lowered startup to 3.0 s.

GraalVM native image : AOT compilation produced a native binary that started in 0.8 s with 75 MB memory usage.

Overall improvements: 92 % reduction in startup time, 85 % reduction in memory, and a three‑fold increase in container density.

Verification Framework (Three‑Dimensional Validation)

To prove the effectiveness of optimizations, combine:

Metric monitoring : Expose Actuator metrics ( /actuator/metrics, /actuator/startup) and scrape them with Prometheus; visualize with Grafana.

Log analysis : Enable JVM logs ( -Xlog:gc*:file=gc.log:time,uptime,level,tags) and use Spring Startup Analyzer to generate flame graphs and bean‑timing reports.

Load testing : Simulate real traffic with wrk -t4 -c100 -d30s http://localhost:8080/api or JMeter/Gatling; compare latency, throughput, and error rates before and after changes.

Long‑term monitoring ensures that performance gains persist in production.

Key Takeaways

Traditional JVM tuning and dependency reduction can achieve 30‑50 % startup improvements. GraalVM native images and CRaC provide order‑of‑magnitude gains but require careful handling of reflection and stateful resources. Combining CDS, lazy bean loading, and asynchronous initialization yields a balanced, low‑risk path, while native images are ideal for Serverless or ultra‑fast microservices.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

startup optimizationSpring BootgraalvmBean InitializationCDSCRaC
Su San Talks Tech
Written by

Su San Talks Tech

Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.