eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

1. Introduction

In this tutorial, we’ll learn how to configure the heap size when we start a Spring Boot application. We’ll be configuring the -Xms and -Xmx settings, which correspond to starting and maximum heap size.

Then, we’ll use Maven first to configure the heap size when starting the application using mvn on the command-line. We’ll also look at how we can set those values using the Maven plugin. Next, we’ll package our application into a jar file and run it with JVM parameters provided to the java -jar command.

Finally, we’ll create a .conf file that sets JAVA_OPTS and run our application as a service using the Linux System V Init technique.

2. Running from Maven

2.1. Passing JVM Parameters

Let’s start by creating a simple REST controller that returns some basic memory information that we can use for verifying our settings:

@GetMapping("memory-status")
public MemoryStats getMemoryStatistics() {
    MemoryStats stats = new MemoryStats();
    stats.setHeapSize(Runtime.getRuntime().totalMemory());
    stats.setHeapMaxSize(Runtime.getRuntime().maxMemory());
    stats.setHeapFreeSize(Runtime.getRuntime().freeMemory());
    return stats;
}

Let’s run it as-is using mvn spring-boot:run to get a baseline. Once our application starts, we can use curl to call our REST controller:

curl http://localhost:8080/memory-status

Our results will vary depending on our machine, but will look something like this:

{"heapSize":333447168,"heapMaxSize":5316280320,"heapFreeSize":271148080}

For Spring Boot 2.x, we can pass arguments to our application using -Dspring-boot.run.

Let’s pass starting and maximum heap size to our application with -Dspring-boot.run.jvmArguments:

mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Xms2048m -Xmx4096m"

Now, when we hit our endpoint, we should see our specified heap settings:

{"heapSize":2147483648,"heapMaxSize":4294967296,"heapFreeSize":2042379008}

2.2. Using the Maven Plugin

We can avoid having to provide parameters each time we run our application by configuring the spring-boot-maven-plugin in our pom.xml file:

Let’s configure the plugin to set our desired heap sizes:

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
            <execution>
                <configuration>            
                    <mainClass>com.baeldung.heap.HeapSizeDemoApplication</mainClass>
                </configuration>
            </execution>
        </executions>
        <configuration>
            <executable>true</executable>
            <jvmArguments>
                -Xms256m
                -Xmx1g
            </jvmArguments>
        </configuration>
    </plugin>
</plugins>

Now, we can run our application using just mvn spring-boot:run and see our specified JVM arguments in use when we ping our endpoint:

{"heapSize":259588096,"heapMaxSize":1037959168,"heapFreeSize":226205152}

Any JVM arguments we configure in our plugin will take precedence over any supplied when running from Maven using -Dspring-boot.run.jvmArguments.

3. Running with java -jar

If we’re running our application from a jar file, we can provide JVM arguments to the java command.

First, we must specify the packaging as jar in our Maven file:

<packaging>jar</packaging>

Then, we can package our application into a jar file:

mvn clean package

Now that we have our jar file, we can run it with java -jar and override the heap configuration:

java -Xms512m -Xmx1024m -jar target/spring-boot-runtime-2.jar

Let’s curl our endpoint to check the memory values:

{"heapSize":536870912,"heapMaxSize":1073741824,"heapFreeSize":491597032}

4. Using a .conf File

Finally, we’ll learn how to use a .conf file to set our heap size on an application run as a Linux service.

Let’s start by creating a file with the same name as our application jar file and the .conf extension: spring-boot-runtime-2.conf.

We can place this in a folder under resources for now and add our heap configuration to JAVA_OPTS:

JAVA_OPTS="-Xms512m -Xmx1024m"

Next, we’re going to modify our Maven build to copy the spring-boot-runtime-2.conf file into our target folder next to our jar file:

<build>
    <finalName>${project.artifactId}</finalName>
    <resources>
        <resource>
            <directory>src/main/resources/heap</directory>
            <targetPath>${project.build.directory}</targetPath>
            <filtering>true</filtering>
            <includes>
                <include>${project.name}.conf</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <configuration>
                        <mainClass>com.baeldung.heap.HeapSizeDemoApplication</mainClass>
                    </configuration>
                </execution>
            </executions>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>
    </plugins>
</build>

We also need to set executable to true to run our application as a service.

We can package our jar file and copy our .conf file over using Maven:

mvn clean package spring-boot:repackage

Let’s create our init.d service:

sudo ln -s /path/to/spring-boot-runtime-2.jar /etc/init.d/spring-boot-runtime-2

Now, let’s start our application:

sudo /etc/init.d/spring-boot-runtime-2 start

Then, when we hit our endpoint, we should see that our JAVA_OPT values specified in the .conf file are respected:

{"heapSize":538968064,"heapMaxSize":1073741824,"heapFreeSize":445879544}

5. Conclusion

In this short tutorial, we examined how to override the Java heap settings for three common ways of running Spring Boot applications. We started with Maven, both modifying the values at the command line and also by setting them in the Spring Boot Maven plugin.

Next, we ran our application jar file using java -jar and passing in JVM arguments.

Finally, we looked at one possible production level solution by setting a .conf file alongside our fat jar and creating a System V init service for running our application.

There are other solutions for creating services and daemons out of a Spring Boot fat jar, and many provide specific ways of overriding JVM arguments.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)