Passa ai contenuti principali

How to configure Tomcat JVM parameters

How to increase JVM size in Tomcat

In order to change the JVM heap size for Tomcat, we need to pickup the catalina.sh/catalina.bat file and modify the value for the JAVA_OPTS parameter. For example, suppose we want to change the max heap size from 256 MB to 512 MB while setting the Perm Gen = 256 MB.
Here's how to modify the JAVA_OPTS:

JAVA_OPTS="-Xms128m -Xmx512m -XX:MaxPermSize=256m"

The changes in the JVM settings will take effect on the server restart. You can verify the change done in the JVM parameter by running the jmap command that is part of the JDK distribution. This tool requires passing the processId of Tomcat. Example:

C:\Users\tomcat>jmap -heap 3860
Attaching to process ID 3860, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 24.51-b03

using thread-local object allocation.
Parallel GC with 4 thread(s)

Heap Configuration:
   MinHeapFreeRatio = 40
   MaxHeapFreeRatio = 70
   MaxHeapSize      = 2122317824 (2024.0MB)
   NewSize          = 1310720 (1.25MB)
   MaxNewSize       = 17592186044415 MB
   OldSize          = 5439488 (5.1875MB)
   NewRatio         = 2
   SurvivorRatio    = 8
   PermSize         = 21757952 (20.75MB)
   MaxPermSize      = 85983232 (82.0MB)
   G1HeapRegionSize = 0 (0.0MB)

Heap Usage:
PS Young Generation
Eden Space:
   capacity = 68157440 (65.0MB)
   used     = 54483128 (51.95915985107422MB)
   free     = 13674312 (13.040840148925781MB)
   79.93716900165265% used
From Space:
   capacity = 5242880 (5.0MB)
   used     = 5232656 (4.9902496337890625MB)
   free     = 10224 (0.0097503662109375MB)
   99.80499267578125% used
To Space:
   capacity = 5242880 (5.0MB)
   used     = 0 (0.0MB)
   free     = 5242880 (5.0MB)
   0.0% used
PS Old Generation
   capacity = 88080384 (84.0MB)
   used     = 7874520 (7.509727478027344MB)
   free     = 80205864 (76.49027252197266MB)
   8.940151759556361% used
PS Perm Generation
   capacity = 22020096 (21.0MB)
   used     = 17099528 (16.30738067626953MB)
   free     = 4920568 (4.692619323730469MB)
   77.65419369652157% used

11415 interned Strings occupying 1616648 bytes.
The highlighted line of code in the previous code snippet is reflecting the value changed after the recycle in the JVM parameter.

Commenti

Post popolari in questo blog

Tomcat maxThreads configuration

Tomcat maxThreads represents the maximum number of request processing threads to be created by the HTTPConnector. < Connector port= " 8443 " protocol= " org.apache.coyote.http11.Http11Protocol " maxThreads= " 250 " SSLEnabled= " true " scheme= " https " secure= " true " clientAuth= " false " sslProtocol= " TLS " connectiontimeout= " 20000 " /> This determines the maximum number of simultaneous requests that can be handled. If not specified, this attribute is set to the default value of 200. How the process works: At server startup, the HTTP Connector will create a number of processing threads based on the value configured for the minSpareThreads attribute. Each incoming request requires a thread for the duration of that request. If the number of simultaneous requests cannot be handled by the currently available request processing threads, additio...

Tomcat Websocket example

A WebSocket is a full-duplex communication mechanism that allows both textual and binary messages to be sent between clients and servers, without the HTTP request/response life cycle. WebSockets allow either the client or the server to send a message at any time, providing an asynchronous solution for working with data while the user is performing a task. Websockets API are included in Tomcat 7 Web server distribution so you don't have to download any extra library: In this tutorial we will show how to create a WebSocket example using Apache Tomcat and Eclipse. Start by creating on Eclipse a new Dynamic project named websocket-example : We will now create a server side class named WebSocketDemo that is going to echo messages from a Javascript client: package com . sample ; import java . io . IOException ; import java . nio . ByteBuffer ; import javax . websocket . OnMessage ; import javax . websocket . Session ; import javax . websocket . server . ServerEndp...

Tomcat session listener example

As part of Servlet 2.3 specification, we can now make use of session creation and destruction events. Our listener object will be called every time a session is created or destroyed by the server. You can use two interfaces as listener for your Session: HttpSessionListener triggers the listener when a new session is created or destroyed HttpSessionBindingListener triggers the listener when an Object is bound/unbound from the Session HttpSessionListener example: package com.sample; import javax.servlet.http.HttpSessionListener; import javax.servlet.http.HttpSessionEvent; public class SessionCounter implements HttpSessionListener { private static int activeSessions = 0; public void sessionCreated(HttpSessionEvent se) { activeSessions++; } public void sessionDestroyed(HttpSessionEvent se) { if(activeSessions > 0) activeSessions--; } public static int getActiveSessions() { return activeSessions; } } As you can see, all you have to do is implementing two meth...