windows 无法在 4GB iMac OSX 10.6.3 Snow Leopard(32 位)上使用 Java 超过 2542 个线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2860889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:28:53  来源:igfitidea点击:

Can't get past 2542 Threads in Java on 4GB iMac OSX 10.6.3 Snow Leopard (32bit)

javawindowsmultithreadingmacosmemory

提问by

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 以获得我的机器可以支持的最大线程数。对于那些可能不知道的人,Snow Leopard 附带了 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.

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

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

无论我通过什么,我都会得到相同的结果,内存不足错误为 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 how 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 threadsat very low numbers, like a couple of hundred. I need to see what a particular box was capable of with different -Xssparameters, then I run into this arbitrary limit on OSX.

我编写这个程序是为了弄清楚 Windows Server 2003 的能力,因为我得到的out of memory can't create native threads数量非常少,比如几百个。我需要看看一个特定的盒子能用不同的-Xss参数做什么,然后我在 OSX 上遇到了这个任意限制。

回答by

2542 seems like an arbitrary number:

2542 似乎是一个任意数字:

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.

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

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

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

> sysctl kern.num_threads
kern.num_threads: 2560

and

> sysctl kern.num_taskthreads
kern.num_taskthreads: 2560

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

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

回答by FelixM

According to the Apple Developer docthe 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.

根据Apple Developer doc,线程堆栈大小应至少为 64K,因此您的 -Xss 1014 将被忽略。但是即使每个线程 64K,线程堆栈内存消耗也只有 160MB 左右,所以这应该不是问题。线程也可以从更有限的池中消耗内存,或者可以简单地限制每个进程或用户可以拥有的线程数。

回答by Amir Afghani

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

您需要找出操作系统在您的系统上支持的最大线程数。

On linux you can do something like :

在 linux 上,您可以执行以下操作:

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.

并汇报结果。

回答by Parth

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.

你认为你会同时拥有这么多线程长达 1 小时吗?我不这么认为。我曾在处理数百个文档的应用程序中工作,将它们从差异转换为差异。格式,在数据库中生成正确的日志并存储特定信息。然后它也在几秒钟内完成。

The thing you should take care about it, code wisely to avoid making too much threads. Instead use a ThreadPoolprovided 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.

你应该注意的事情,明智地编码以避免产生过多的线程。而是使用ThreadPoolJava 提供的 ,以便在需要时可以使用相同的线程。这将提供更好的性能。还要保持最小块的同步以避免执行中的瓶颈。

thanks.

谢谢。