无法在 4GB iMac OSX 10.6.3 Snow Leopard(32 位)上使用 Java 超过 2542 个线程 [英] Can't get past 2542 Threads in Java on 4GB iMac OSX 10.6.3 Snow Leopard (32bit)

查看:17
本文介绍了无法在 4GB iMac OSX 10.6.3 Snow Leopard(32 位)上使用 Java 超过 2542 个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行以下程序,试图弄清楚如何配置我的 JVM 以获得我的机器可以支持的最大线程数.对于那些可能不知道的人,Snow Leopard 附带 Java 6.

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.

我尝试使用默认值和以下命令行启动它,无论 JVM 选项设置为什么,我总是在线程 2542 处出现内存不足错误.

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

无论我通过什么,我都得到相同的结果,2542 出现内存不足错误

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
", this.i);
                Thread.sleep(1000 * 60 * 60);
            }
            catch (final InterruptedException e)
            {
                throw new RuntimeException(e);
            }
        }
    }
}

关于我如何影响这些结果的任何想法?

Any ideas on how I can affect these results?

我编写这个程序是为了弄清楚 Windows Server 2003 的能力,因为我得到这些内存不足无法创建本机线程的数量非常少,比如几百个.我需要看看一个特定的盒子能用不同的 -Xss 参数做什么,然后我在 OSX 上遇到了这个任意限制.

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.

推荐答案

2542 似乎是一个任意数字:

我关闭了所有程序,除了我运行测试的一个终端窗口,然后我得到了 2545,这告诉我这是一个任意限制.

2542 seems 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.

要获取 OSX 10.6.3 的线程数,请执行以下操作:

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

> sysctl kern.num_threads
kern.num_threads: 2560

> sysctl kern.num_taskthreads
kern.num_taskthreads: 2560

2560 数字与 25422545 匹配,因为显然有其他线程在后台运行.根据官方文档 kern.num_taskthreads 无法在 OSX 桌面版中进行调整.

The 2560 number matches up with the 2542 and 2545 because there are obviously other threads running in the background. According to the official documentation kern.num_taskthreads can not be adjusted in the desktop version of OSX.

这篇关于无法在 4GB iMac OSX 10.6.3 Snow Leopard(32 位)上使用 Java 超过 2542 个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆