How do I set the Java options for Kafka?

Modifying a script in the bin directory is highly unrecommended. When upgrading Kafka to the next version, extracting the new binaries would override the changes made in the script.

The preferred way should be to set the environment variable KAFKA_HEAP_OPTS outside the script.

export KAFKA_HEAP_OPTS="-Xmx1G -Xms1G"

If the var is set before starting Kafka via the script it will use the var instead of the default values defined in /bin/kafka-server-start.sh


Looking at kafka-run-classh.sh - kafka uses the following variables:

  • $KAFKA_HEAP_OPTS
  • $KAFKA_JVM_PERFORMANCE_OPTS
  • $KAFKA_GC_LOG_OPTS
  • $KAFKA_JMX_OPTS
  • $KAFKA_LOG4J_OPTS

You can run it via:

export KAFKA_HEAP_OPTS="-Xmx1G -Xms1G" 
export KAFKA_JMX_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=12346 -Dcom.sun.management.jmxremote.rmi.port=12346 -Dcom.sun.management.jmxremote.local.only=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false" 
bin/kafka-server-start.sh -daemon config/server.properties

Another way to do this is by modifying information written in /bin/kafka-server-start.sh:

export KAFKA_HEAP_OPTS="-Xmx1G -Xms1G"

or in /bin/kafka-run-class.sh:

KAFKA_JVM_PERFORMANCE_OPTS="-server -XX:+UseCompressedOops -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:+CMSScavengeBeforeRemark -XX:+DisableExplicitGC -Djava.awt.headless=true"

You can also set this parameters via your service definition as shown below.

[Unit]
Requires=zookeeper.service
After=zookeeper.service

[Service]
Type=simple
User=kafka
ExecStart=/bin/sh -c '/home/kafka/kafka/bin/kafka-server-start.sh /home/kafka/kafka/config/server.properties > /home/kafka/kafka.log 2>&1'
ExecStop=/home/kafka/kafka/bin/kafka-server-stop.sh
Restart=on-abnormal
Environment="KAFKA_HEAP_OPTS=-Xmx4G"
Environment="KAFKA_JMX_OPTS=-Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.net.preferIPv4Stack=true"
Environment="JMX_PORT=9999"

[Install]
WantedBy=multi-user.target