Monitoring Tomcat Memory Usage: A Comprehensive Guide

how to monitor tomcat memory usage

Monitoring Tomcat memory usage is crucial for successfully running Java applications in production. Tomcat is a server for Java-based web applications and is one of the most widely used servers for Java applications. Monitoring Tomcat memory usage helps to ensure that your application responds to requests quickly and detects any potential errors or memory leaks in your code. There are several ways to monitor Tomcat memory usage, including using JMX beans, third-party monitoring tools, and built-in features of Tomcat.

Characteristics Values
Memory Usage JVM Heap Memory Elements, JVM Non-Heap Memory Parts, Eden Space, Permanent Generation, Survivor Space, Code Cache, Tenured Generation/PS Old Gen
Garbage Collection The system spends more than 98% of CPU time on garbage collection and recovers less than 2% of the heap
Thread Usage Tomcat provides the ThreadPool MBean to determine the status of in-use threads.
Request Throughput Catalina:type=GlobalRequestProcessor, which has properties like requestCount and errorCount
Number of Sessions The number of concurrent sessions the server can support is determined by counting the number of sessions.
Response Time If your system takes too long to respond to requests, users are likely to quit.
Database Connection Pool Monitoring this can help tune the number of connections in a pool that your application needs.
Error Rates This metric is helpful in identifying any issues in your codebase.
Uptime This is a simple measure that shows how long your server has been running or down.

shundigital

Memory usage

There are several tools available to monitor memory usage on Tomcat servers:

  • JMX beans (Java Management Extensions): These provide a high-level overview of memory usage and other metrics. While JMX beans don't provide granular detail, they can be useful for quickly identifying potential issues.
  • Tomcat Manager: A basic tool that comes with Tomcat, it provides information on server status, deployed applications, and memory leaks. However, it is limited and not recommended for production instances.
  • JavaMelody: A more user-friendly tool that offers graphs and data visualisation for various performance metrics, including memory usage. It can also be used to trigger garbage collection cycles.
  • SolarWinds Server & Application Monitor (SAM): A comprehensive monitoring solution that includes memory usage tracking for Tomcat servers, along with virtual machines, SQL Server, Exchange, and Active Directory. It offers customisable alerts, reports, and dashboards.
  • JConsole: Part of JDK, JConsole allows you to monitor live threads, CPU usage, and memory usage for various memory parts, including JVM Heap memory and Non-Heap memory.
  • HioxIndia Client Login: A service that allows you to check the available memory space in your Tomcat server.

shundigital

Garbage collection

The CollectionCount metric on the JMX MBean server displays the number of collections that have occurred since the service started. Under normal conditions, this amount will gradually rise, but you can use a JMX monitoring tool to determine how many collections occur over a given time.

The "Execute the garbage collection" link in JavaMelody can be used to start a full garbage collection cycle.

To monitor memory usage, you can use JConsole (part of JDK). Append the following line to JAVA_OPTS in ~/appservers/apache-tomcat/bin/catalina.sh and then restart Tomcat:

Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=AFREEPORTNUMBER \ -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false

You can then connect with JConsole by giving the remote address and port:

Jconsole HOSTING_SERVER_IP:JMX_PORT

This will allow you to monitor live threads and loaded class numbers, CPU usage, and memory usage for various memory parts, including:

  • JVM Heap memory elements
  • JVM Non-Heap memory parts
  • Eden Space
  • Permanent Generation
  • Survivor Space
  • Code Cache
  • Tenured Generation/PS Old Gen

shundigital

Thread usage

Each Tomcat connector has a pool of worker threads and one or more acceptor threads. When a connector receives a request, the acceptor thread assigns it to an available worker thread, which then sends the request to the engine for processing. The worker thread becomes available again once the engine returns a response, and the connector sends it back to the client.

To fine-tune Tomcat thread usage, you can adjust the following parameters for a connector's thread pool:

  • MaxThreads: The maximum number of threads allowed to run simultaneously, with a default value of 200.
  • MinSpareThreads: The minimum number of threads (idle or active) that should always be available, with a default of 10.
  • MaxConnections: The total number of concurrent connections the server will accept and process, with default values of 10,000 for NIO/NIO2 mode and 8,192 for APR/native.
  • AcceptCount: The maximum number of TCP requests that can wait in a queue at the OS level when no worker threads are available, with a default value of 100.
  • ConnectionTimeout: The number of milliseconds a connector will wait before closing an idle connection, with a default value of 20,000.

If the maximum number of threads is reached and all threads are busy, the server will only accept a certain number of concurrent connections (as set by maxConnections). Any additional incoming connections will be placed in a queue (acceptCount) until a thread becomes available. If the queue is full, the server will start refusing new requests.

To have more control over thread management and distribution across connectors, you can use an executor, which allows you to configure threads for a single pool instead of adjusting values per connector. The executor includes parameters such as maxThreads, minSpareThreads, and maxQueueSize.

Tomcat provides the ThreadPool MBean to monitor the status of in-use threads. The currentThreadsBusy, currentThreadCount, and maxThreads properties show the number of threads currently busy, in the thread pool, and the maximum number of threads that can be generated, respectively.

By monitoring thread usage and fine-tuning thread parameters, you can ensure that Tomcat efficiently handles request throughput and processing times, providing a better experience for your users.

shundigital

Response time

To monitor response time, you can utilise the MBean Catalina:type=GlobalRequestProcessor, which includes attributes such as requestCount and errorCount, representing the total number of requests made and errors encountered. Additionally, the maxTime attribute indicates the longest time taken to process a single request, while processingTime represents the cumulative time taken to process all requests.

However, viewing this MBean directly has the drawback of including all requests made to the server. To isolate HTTP requests specifically, you can refer to the "HTTP hits per minute" graph available in monitoring tools such as JavaMelody. JavaMelody provides a high-level overview of all requests and their average response time. Nevertheless, if you require more detailed information about each request, you may need to employ additional tools that can track the performance of your application for every individual web request.

Another tool worth considering is Stackify's Prefix, which can provide insights into the performance of your application on a per-request basis. Prefix also allows you to see which application each request belongs to, which is particularly useful if you have multiple applications deployed on the same Tomcat server.

In summary, monitoring response time is essential to ensure a positive user experience and to identify any potential bottlenecks or issues within your Tomcat-based application. By utilising tools like JavaMelody and Prefix, you can gain valuable insights into the responsiveness of your system and take appropriate actions to optimise its performance.

shundigital

Database connection pool

The Tomcat server's connection pooling feature is an important aspect of its performance monitoring. It enables applications to connect to a backend database and execute queries efficiently. By implementing connection pooling, applications can reduce the overhead of opening individual connections for each request, thereby lowering the load on the database server and improving response times for users.

To set up a connection pool in Tomcat, you can follow these steps:

Add a `Resource` element to the `conf/context.xml` file:

Xml

  • Configure the connection pool as a resource in the Tomcat JDBC documentation, specifying the `factory` attribute as `org.apache.tomcat.jdbc.pool.DataSourceFactory`.
  • Set the jmxEnabled flag to `true` to expose the connection pool object as an MBean. This allows you to monitor the connection pool's performance and health.
  • Optionally, you can use tools like Tomcat Manager or JavaMelody to visualize and manage the connection pool's metrics.

By utilizing connection pooling, you can optimize database connections and improve the overall performance of your Tomcat application. It is crucial to monitor the connection pool to ensure that your application has an adequate number of connections available, reducing potential bottlenecks and enhancing the user experience.

Frequently asked questions

You can monitor Tomcat memory usage through the Tomcat Manager App or a third-party tool. The Manager App is password-protected and provides basic information on the server's status and deployed applications. Third-party tools such as SolarWinds SAM, JavaMelody, and Stackify Prefix, ManageEngine Applications Manager, and AppDynamics provide more detailed insights into memory usage and other performance metrics.

In addition to memory usage, it is important to monitor garbage collection, thread usage, request throughput, response time, database connection pool, error rates, and uptime. These metrics can help identify potential issues and ensure optimal performance of your Tomcat-based applications.

You can use JConsole, which is part of JDK, to monitor Java process memory usage in real time. By appending specific lines to JAVA_OPTS in catalina.sh and restarting Tomcat, you can connect to JConsole and monitor memory usage for JVM Heap memory elements and Non-Heap memory parts.

There are several tools available for Tomcat performance monitoring, including SolarWinds SAM, JavaMelody, Stackify Prefix, ManageEngine Applications Manager, and AppDynamics. These tools offer features such as memory usage monitoring, error tracking, performance analytics, and more.

Written by
Reviewed by
Share this post
Print
Did this article help you?

Leave a comment