Tomcat and JVM Optimization: Configuration and Parameter Tuning Guide
This article explains how to optimize Tomcat by adjusting connector settings, switching between BIO, NIO, and AIO modes, and fine‑tuning JVM options such as heap size, garbage‑collector flags, and thread parameters to improve server performance and stability.
Recently a junior developer asked about Tomcat tuning and JVM optimization during an interview, prompting a detailed walkthrough of both topics.
Tomcat
Tomcat is a lightweight web application server commonly used for small‑to‑medium systems and for developing JSP applications. While many developers know how to start Tomcat, they often lack knowledge of how to adjust its configuration for better performance.
Configuration File Modification
Tomcat is typically installed in a directory like D:\develop\tomcat\apache-tomcat-7.0.88 , with the main configuration file conf/server.xml . The most important section to tune is the <Connector> element, which handles incoming client requests.
Typical connector settings include:
<Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />Key attributes that can be tuned are:
URIEncoding : character encoding, usually UTF‑8.
executor : the thread pool used by the connector.
connectionTimeout : timeout in milliseconds.
maxThreads : maximum number of request processing threads.
minSpareThreads : number of threads kept idle.
maxSpareThreads : maximum idle threads.
minProcessors / maxProcessors : minimum and maximum processing threads for the server.
enableLookups : whether DNS lookups are performed (often disabled for performance).
redirectPort : SSL redirect port.
acceptCount : maximum queue length for incoming connections.
After tuning, a typical optimized connector looks like:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="443" maxThreads="3000" minSpareThreads="20" acceptCount="1000" enableLookups="false" server="None" URIEncoding="UTF-8" />NIO, BIO and NIO2
Tomcat supports different I/O models:
BIO : synchronous blocking I/O.
NIO : synchronous non‑blocking I/O (also supports asynchronous blocking I/O).
AIO : asynchronous non‑blocking I/O, available from JDK 7 onward.
To switch to NIO mode, modify the connector protocol attribute:
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" />
<Connector connectionTimeout="20000" port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443" />Although Tomcat 8+ defaults to NIO, it may still require explicit configuration.
JVM
Optimizing the JVM that runs Tomcat can dramatically improve performance. The JVM options are set via the JAVA_OPTS variable in catalina.bat (Windows) or the corresponding .sh script (Linux). Important flags include:
-Xms : initial heap size (≈ 1/64 of physical memory).
-Xmx : maximum heap size (≈ 1/4 of physical memory).
-Xmn : size of the young generation.
-XX:NewSize / -XX:MaxNewSize : young generation size for older JVM versions.
-XX:PermSize / -XX:MaxPermSize : permanent generation size.
-Xss : thread stack size.
-XX:NewRatio : ratio of young to old generation (excluding perm).
-XX:+AggressiveOpts : enable aggressive optimizations.
-XX:+UseBiasedLocking : improve lock performance.
-XX:+DisableExplicitGC : ignore explicit System.gc() calls.
-XX:+UseParNewGC : parallel young‑generation collector.
-XX:+UseConcMarkSweepGC : concurrent mark‑sweep collector for the old generation.
-XX:MaxTenuringThreshold : maximum age before promotion.
-XX:+UseCMSCompactAtFullCollection : compact old generation after full GC.
-XX:LargePageSizeInBytes : size of memory pages (affects perm size).
-XX:+UseFastAccessorMethods : optimize accessor methods for primitive types.
-XX:+UseCMSInitiatingOccupancyOnly : start CMS based on a manual occupancy threshold.
These options should be selected carefully based on the application’s workload; indiscriminate copying can degrade performance.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.