Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce the number of threads used by the jvm

Tags:

java

jvm

I'm running a single threaded Java app on the following java version:

java version "1.7.0_67"
Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

with the -XX:+UseSerialGC option enabled. Still when I start the application I see multiple threads starting when monitoring the system with htop. I'd like to reduce the number of processes started as much as possible since I have a use case which involves running multiple instances of this application and this will hit the roof of the maximum allowed number of process on the system that I'm running on. Are there any other jvm options other than -XX:+UseSerialGC that I could use to reduce the number of threads starting?

like image 624
Johan Avatar asked Sep 05 '14 09:09

Johan


People also ask

How many threads does JVM use?

Each JVM server can have a maximum of 256 threads to run Java applications. In a CICS region you can have a maximum of 2000 threads. If you have many JVM servers running in the CICS region (for example, more than seven), you cannot set the maximum value for every JVM server.

How can I increase the maximum number of JVM threads?

You can change these values by (temporal) running ulimit command or (permanent) editing /etc/security/limits. conf . This value is the system-global (including non-JVM processes) maximum number of threads. Check cat /proc/sys/kernel/threads-max , and increase if necessary.

What are JVM threads?

What Is a Thread in Java? A Java thread is the execution path in a program. Everything that runs in Java is run in threads. Every application in the JVM world has threads, at least one, even if you don't call it explicitly. It all starts with the main method of your code, which is run in the main application thread.

Is JVM single threaded?

The OS sees JVM as a single process and a single thread. Therefore, any thread created by JVM is supposed to be maintained by it only. Green threads hold all the information related to the thread within the thread object itself.


1 Answers

Apart from -XX:+UseSerialGC which disables Parallel or Concurrent GC, there are the following options to reduce the number of JVM threads:

  • -XX:CICompilerCount=1 leaves only one JIT compiler thread.
  • -XX:+ReduceSignalUsage disables Signal Dispatcher thread. E.g. JVM will not handle SIGQUIT to dump threads.
  • -XX:+DisableAttachMechanism prevents AttachListener thread from starting.

In theory it is possible to disable even more threads (e.g. Service Thread and VM Periodic Task Thread) but this would require patching JVM.

like image 128
apangin Avatar answered Nov 14 '22 21:11

apangin