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.

Partner – LambdaTest – NPI EA (cat=Testing)
announcement - icon

Regression testing is an important step in the release process, to ensure that new code doesn't break the existing functionality. As the codebase evolves, we want to run these tests frequently to help catch any issues early on.

The best way to ensure these tests run frequently on an automated basis is, of course, to include them in the CI/CD pipeline. This way, the regression tests will execute automatically whenever we commit code to the repository.

In this tutorial, we'll see how to create regression tests using Selenium, and then include them in our pipeline using GitHub Actions:, to be run on the LambdaTest cloud grid:

>> How to Run Selenium Regression Tests With GitHub Actions

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. Overview

Kafka is an open-source distributed message streaming middleware used to collect, store, and process data in real-time. Several command-line tools are included with Kafka. They’re useful for administration, testing, and debugging.

These tools are ultimately shell scripts that internally launch Java classes using the java command. Therefore, we may need to adjust the JVM (Java Virtual Machine) settings for several reasons, including memory management, garbage collection performance, and stability.

In this tutorial, we’ll discuss how to set the JVM options for the command-line Kafka tools. The version of Kafka we use in the examples is 4.1.1.

2. Using KAFKA_HEAP_OPTS

The KAFKA_HEAP_OPTS environment variable is used to control the JVM heap size of the Kafka tools, including the Kafka server. We’ll discuss its usage for them separately.

2.1. Kafka Tools

We must export KAFKA_HEAP_OPTS before starting the tool using the export command. For example, let’s set the minimum and maximum heap size of the JVM to 1 GB for kafka-console-producer.sh, one of the Kafka Tools that produces messages:

$ export KAFKA_HEAP_OPTS="-Xms1G -Xmx1G"
$ kafka-console-producer.sh --bootstrap-server localhost:9092 --topic test-topic
>

The arrowhead symbol > shows that the tool is running and ready to send messages. Let’s check the value of the minimum and maximum heap size using jcmd:

$ jcmd $(pgrep -f ConsoleProducer) VM.flags
5090:
-XX:CICompilerCount=3 ... -XX:MaxHeapSize=1073741824 ... -XX:MinHeapSize=1073741824 ... -XX:+UseG1GC

We abbreviated the output since there are many VM flags listed. We get the PID (Process ID) of the running process using $(pgrep -f ConsoleProducer) and pass it to jcmd. Its VM.flags command prints the JVM flag options and their current values. ConsoleProducer is the name of the Java class the script launches.

As is apparent from the output, the values of the minimum and maximum heap sizes, -XX:MinHeapSize and -XX:MaxHeapSize, are both 1073741824 bytes, i.e., 1 GB.

2.2. Kafka Server

The method in the previous subsection is also valid while starting the Kafka server from the command line. However, if the Kafka server is running as a systemd service in Linux, then we need to define the environment variable in the service’s unit file. Here is an example unit file:

$ cat kafka_server.service 
[Unit]
Description=Kafka Server

[Service]
Environment="KAFKA_HEAP_OPTS=-Xms2g -Xmx2g"
ExecStart=/home/baeldung/work/kafka-vm-options/start-server.sh

[Install]
WantedBy=multi-user.target

The Environment option in the Service section, which is of interest to us, sets the KAFKA_HEAP_OPTS environment variable to “-Xms2g -Xmx2g”, i.e., the minimum and maximum JVM heap sizes are 2 GB. This time, let’s check the values using the systemctl status command:

$ sudo systemctl status kafka_server
● kafka_server.service - Kafka Server
     Loaded: loaded (/etc/systemd/system/kafka_server.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2025-12-23 07:03:46 EST; 3s ago
   Main PID: 9218 (start-server.sh)
      Tasks: 6 (limit: 4597)
     Memory: 4.0M
        CPU: 4.437s
     CGroup: /system.slice/kafka_server.service 
             ├─9218 /bin/bash /home/baeldung/work/kafka-vm-options/start-server.sh
             └─9943 java -Xms2g -Xmx2g -server -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 ...

Obviously, the JVM parameters are “-Xms2g -Xmx2g”, as indicated by the output above.

3. Using KAFKA_OPTS

We can use the environment variable KAFKA_OPTS to set JVM system properties and any JVM options not related to the heap size. Firstly, let’s discuss its usage for Kafka tools other than the Kafka server, and then for the Kafka server itself.

3.1. Kafka Tools

We can set KAFKA_OPTS from the command line, similar to KAFKA_HEAP_OPTS, and change the JVM options or system properties of the Kafka tools. For example, let’s change the default Kafka logging directory for kafka-console-producer.sh:

$ export KAFKA_OPTS="-Dkafka.logs.dir=/tmp/kafka"
$ kafka-console-producer.sh --bootstrap-server localhost:9092 --topic test-topic
>

Having set KAFKA_OPTS and started kafka-console-producer.sh, let’s check whether the setting is successful using jcmd. However, we use the VM.system_properties command of jcmd this time:

$ jcmd $(pgrep -f ConsoleProducer) VM.system_properties | grep kafka.logs.dir
kafka.logs.dir=/tmp/kafka

The Kafka logging directory is /tmp/kafka, as expected.

3.2. Kafka Server

If we start the Kafka server from the command line, we can use the method in the previous subsection. If the server is running as a systemd service, we need to define it in the service’s unit file, just like KAFKA_HEAP_OPTS.

For example, we can use the -Dkafka.logs.dir flag to change the default Kafka logging directory as follows:

[Service]
Environment="KAFKA_OPTS=-Dkafka.logs.dir=/tmp/kafka"
ExecStart=/home/baeldung/work/kafka-vm-options/start-server.sh

We showed only the Service section of the unit file. The logs are stored in the /tmp/kafka directory. Once we apply this change and restart the server, we can check it using jcmd:

$ sudo jcmd $(pgrep -f kafka.Kafka) VM.system_properties | grep kafka.logs.dir
kafka.logs.dir=/tmp/kafka

We filtered the output of jcmd using grep, as the output is long. The result is as expected.

4. Using KAFKA_JVM_PERFORMANCE_OPTS

The environment variable, KAFKA_JVM_PERFORMANCE_OPTS, is typically used to tune the GC (Garbage Collector) and low-level performance settings of the JVM for the Kafka server. Let’s discuss it separately for Kafka tools other than the Kafka server, and then for the Kafka server itself.

4.1. Kafka Tools

We need to set the KAFKA_JVM_PERFORMANCE_OPTS environment variable from the command line before running the Kafka tool, as in the previous examples. For example, let’s change the GC used by the JVM for kafka-console-producer.sh:

$ export KAFKA_JVM_PERFORMANCE_OPTS="-XX:+UseZGC -XX:+AlwaysPreTouch"
$ kafka-console-producer.sh --bootstrap-server localhost:9092 --topic test-topic
>

In this example, we pass the command-line options -XX:+UseZGC and -XX:+AlwaysPreTouch to the server using the KAFKA_JVM_PERFORMANCE_OPTS environment variable. We instruct the server to use the ZGC (Z Garbage Collector) instead of the default G1 Garbage Collector. Besides, we utilize the -XX:+AlwaysPreTouch command-line option to avoid latency by pre-touching the Java heap during JVM initialization.

Having started the producer, let’s check the value using jcmd:

$ jcmd $(pgrep -f ConsoleProducer) VM.flags
4877:
-XX:+AlwaysPreTouch ... -XX:+UseZGC

Obviously, the settings are as expected.

4.2. Kafka Server

If we start the Kafka Server from the command line, we can use the method in the previous subsection. However, if we run the server as a systemd service, we can set KAFKA_JVM_PERFORMANCE_OPTS in the Kafka server’s unit file just like setting KAFKA_HEAP_OPTS:

[Service]
Environment="KAFKA_JVM_PERFORMANCE_OPTS=-XX:+UseZGC -XX:+AlwaysPreTouch"
ExecStart=/home/baeldung/work/kafka-vm-options/start-server.sh

Let’s check the status of the service when we start it with this configuration:

$ sudo systemctl status kafka_server
● kafka_server.service - Kafka Server
     Loaded: loaded (/etc/systemd/system/kafka_server.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2025-12-23 09:12:56 EST; 1h 56min ago
   Main PID: 29923 (start-server.sh)
      Tasks: 108 (limit: 4597)
     Memory: 1.1G
        CPU: 3min 24.313s
     CGroup: /system.slice/kafka_server.service 
             ├─29923 /bin/bash /home/baeldung/work/kafka-vm-options/start-server.sh
             └─30651 java -Xmx1G -Xms1G -XX:+UseZGC -XX:+AlwaysPreTouch ...

As is apparent from the output, we’re successful in passing the command-line options to the server using the KAFKA_JVM_PERFORMANCE_OPTS environment variable.

5. Conclusion

In this article, we discussed how to set the JVM options for the command-line Kafka tools, including the Kafka server. We saw that we can use the KAFKA_HEAP_OPTS, KAFKA_OPTS, and KAFKA_JVM_PERFORMANCE_OPTS environment variables to set the JVM heap size, JVM system properties, and GC settings, respectively. We need to export them from the command line before running the tools.

Additionally, we learned that if the Kafka server is managed by systemd, we need to define these environment variables in the server’s unit file.

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)
guest
0 Comments
Oldest
Newest
Inline Feedbacks
View all comments