Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does max number of threads decrease when I increase max heap size?

This test shows the max number of threads that can be created in Java

    System.out.println("Max memory  " + Runtime.getRuntime().maxMemory() / 1024 / 1024 + "M");
    for (int i = 0;; i++) {
        Thread t = new Thread() {
            public void run() {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                }
            };
        };
        try {
            t.start();
        } catch (Error e) {
            System.out.println("Max threads " + i);
            e.printStackTrace();
            System.exit(1);
        }
    }

When I run it with default heap size (256M) I get

Max memory  247M
Max threads 2247
java.lang.OutOfMemoryError: unable to create new native thread
    at java.lang.Thread.start0(Native Method)
    at java.lang.Thread.start(Thread.java:691)
    at test.Test1.main(Test1.java:19)

when I increase max heap size to 512M I get

Max memory  494M
Max threads 1906
...

when I increase max heap size to 1024M I get

Max memory  989M
Max threads 1162
...

That is, more heap memory fewer threads. Why is that?

like image 208
Evgeniy Dorofeev Avatar asked Apr 21 '13 07:04

Evgeniy Dorofeev


People also ask

Does increasing heap size improve performance?

You can improve performance by increasing your heap size or using a different garbage collector. In general, for long-running server applications, use the J2SE throughput collector on machines with multiple processors (-XX:+AggressiveHeap) and as large a heap as you can fit in the free memory of your machine.

What will happen if heap memory gets full?

When the heap becomes full, garbage is collected. During the garbage collection objects that are no longer used are cleared, thus making space for new objects. Note that the JVM uses more memory than just the heap.

Does heap size affect performance?

A too small heap size may affect performance if your system also does not have enough cores, so that the garbage collectors do compete over the CPU with the business threads. At some point, the CPU spends a significant time on garbage collection.

How many threads JVM can handle?

Any machine with a modern CPU (most recent couple generations of AMD or Intel) and with 1 - 2 Gig of memory (depending on OS) can easily support a JVM with thousands of Threads.


1 Answers

Each thread requires a stack. The more memory you allocate to the heap, the less memory is left for the stacks.

This will be particularly acute if you're using a 32-bit JVM, since the process will have no more than 4GB of address space for everything (the code, the heap, the stacks etc). I cannot reproduce this behaviour on my 64-bit box, where "Max threads" remains the same regardless of how much memory I allocate to the heap.

It is worth noting that many operating systems enable you to tweak the size of the stack. On Unix this is done using ulimit -s.

like image 125
NPE Avatar answered Oct 23 '22 10:10

NPE