Let's get started with a Microservice Architecture with Spring Cloud:
Setting the JVM Options for Kafka Tools
Last updated: January 9, 2026
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.















