views:

206

answers:

4

I am running the following program trying to figure out how to configure my JVM to get the maximum number of threads my machine can support. For those that might not know, Snow Leopard ships with Java 6.

I tried starting it with defaults, and the following command lines, I always get the Out of Memory Error at Thread 2542 no matter what the JVM options are set to.

java TestThreadStackSizes 100000
java -Xss1024 TestThreadStackSizes 100000
java -Xmx128m -Xss1024 TestThreadStackSizes 100000
java -Xmx2048m -Xss1024 TestThreadStackSizes 100000
java -Xmx2048m -Xms2048m -Xss1024 TestThreadStackSizes 100000

no matter what I pass it, I get the same results, Out of Memory Error at 2542

public class TestThreadStackSizes
{
    public static void main(final String[] args)
    {
        Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            public void uncaughtException(final Thread t, final Throwable e)
            {
                System.err.println(e.getMessage());
                System.exit(1);
            }
        });
        int numThreads = 1000;
        if (args.length == 1)
        {
            numThreads = Integer.parseInt(args[0]);
        }

        for (int i = 0; i < numThreads; i++)
        {
            try
            {
                Thread t = new Thread(new SleeperThread(i));
                t.start();
            }
            catch (final OutOfMemoryError e)
            {
                throw new RuntimeException(String.format("Out of Memory Error on Thread %d", i), e);
            }
        }
    }

    private static class SleeperThread implements Runnable
    {
        private final int i;

        private SleeperThread(final int i)
        {
            this.i = i;
        }

        public void run()
        {
            try
            {
                System.out.format("Thread %d about to sleep\n", this.i);
                Thread.sleep(1000 * 60 * 60);
            }
            catch (final InterruptedException e)
            {
                throw new RuntimeException(e);
            }
        }
    }
}

Any ideas on now I can affect these results?

I wrote this program to figure out what a Windows Server 2003 is capable of, because I am getting these out of memory can't create native threads at very low numbers, like a couple of hundred. I need to see what a particular box was capable of with different -Xss parameters, then I run into this arbitrary limit on OSX.

+1  A: 

You need to find out the maximum number of threads the operating system supports on your system.

On linux you can do something like :

cat /proc/sys/kernel/threads-max

to get the max, and to set it you can do something like :

echo 10000 > /proc/sys/kernel/threads-max

Also try running with :

-XX:-UseBoundThreads

and report back the results.

Amir Afghani
nope not on linux and -XX:-UseBoundThreads doesn't change anything
fuzzy lollipop
see my answer on how to get the max threads for OSX
fuzzy lollipop
+1  A: 

According to the Apple Developer doc the thread stack size should be at least 64K, so your -Xss 1014 is ignored. But even with 64K per thread, the thread stack memory consumption comes only to about 160MB, so this shouldn't be the problem. Threads could also consume memory from a more limited pool, or there could simply be limit on the number of thread you can have per process or user.

FelixM
I set it to 256k and it still fails at the exact same number of threads. java -Xss256k -XX:ThreadStackSize=256 TestThreadStackSizes 10000
fuzzy lollipop
+1  A: 

I figured it out with some ideas from the other answers, 2542 seemed like an arbitrary number. I shut all programs down except the one terminal window I was running my test from and I got to 2545, that told me it was an arbitrary limit.

To get the number of threads for OSX 10.6.3 you do:

sysctl kern.num_threads
kern.num_threads: 2560

and

sysctl kern.num_taskthreads
kern.num_taskthreads: 2560

The 2560 number matches up with the 2542 and 2545 because there are obviously other threads running in the background. Now I just need to figure out how to change these settings.

fuzzy lollipop
At least in Linux sysctl has parameter -w and you can use such construction to change value:sysctl -w kern.num_taskthreads=3000These value is not persistent between restarts.
Lukasz Stelmach
doesn't work on OSX
fuzzy lollipop
A: 

Do you think you will have these much thread concurrently up to 1 hour? I don't think so. I have worked in application which processed hundreds of documents, convert them from and to diff. format, generates proper logs in DB and stores specific info also. Then also it finished in seconds.

The thing you should take care about it, code wisely to avoid making too much threads. Instead use a ThreadPool provided by Java, so that same threads can be utilized when needed. that will provide better performance. Also keep synchronization on minimal blocks to avoid bottle necks in your execution.

thanks.

Paarth