Search code examples
javamultithreadingjava-8jvmjvm-hotspot

Impact of heap space / non heap space memory on number of possible threads for a jvm process


Based on a number of blog posts and stackoverflow questions I've come to the following conclusions:

  • Maximum number of possible threads in a java application is dependent on the OS limit and NOT on stack size (xss). Source
  • Also, according to the many answers to this SO question max possible threads can depend on a number of factors-
    • OS and CPU architecture
    • Heap size

I haven't considered the impact of heap size on the number of threads my application can create like the answers in the SO question suggest.

Is there a straightforward jvm property which allows me to configure the max memory that can be used for the creation of new threads? I don't think I can justify increasing -Xmx or any other memory section not explicitly dedicated to thread creation for increasing the number of threads in my app but I can't seem to find such a jvm property.

JVM in question is openjdk 8 (hotspot) 1.8.


Solution

  • In OpenJDK 8, there isn't a direct JVM property to configure the max. memory specifically for thread creation. The memory used for thread stacks is a part of the process's native memory, and the total native memory that a process can use is managed by the operating system.

    So you were right about the -Xmx param – it controls the maximum heap size but doesn't influence the native memory that can be used for thread creation. The important note is that if your application uses less heap memory, more native memory may be available for thread creation, assuming the total memory of the system is fixed - perhaps that's something you may try.

    So yeah, here isn't a direct JVM property to control the total memory used for thread creation specifically.