Java 的最大线程数非常有限?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3180988/
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-10-30 00:43:04  来源:igfitidea点击:

Java very limited on max number of threads?

javamultithreadingmemorymemory-managementprofile

提问by erotsppa

We have a small text box with 512Mb of ram. We wanted to see how many threads we can create in Java in this box. To our surprise, we can't create many. Essentially the minimum stack size you can set with -Xss is 64k. Simple math will tell you that 64*7000 will consume 430Mb so we were only able to get it up to around 7000 threads or so and then we encountered this error:

我们有一个带有 512Mb ram 的小文本框。我们想看看在这个框中我们可以在 Java 中创建多少线程。令我们惊讶的是,我们不能创造很多。本质上,您可以使用 -Xss 设置的最小堆栈大小是 64k。简单的数学计算会告诉你 64*7000 将消耗 430Mb,所以我们只能让它达到大约 7000 个线程,然后我们遇到了这个错误:

java.lang.OutOfMemoryError: unable to create new native thread. 

Is this the true limit with Java? Per 512Mb of ram we can only squeeze in 7k number of threads or so?

这是 Java 的真正限制吗?每 512Mb 的 ram 我们只能挤入 7k 左右的线程?

回答by Tobias P.

Use asynchronous IO (java nio) and you'll don't need 7k threads to support 7k clients, a few threads for handling io (5?) will be enough.
Take a look at Netty;)

使用异步 IO (java nio) 并且您不需要 7k 线程来支持 7k 客户端,处理 io (5?) 的几个线程就足够了。
看看Netty;)

One thread for each client is a really bad design.

每个客户端一个线程是一个非常糟糕的设计。

回答by Noel M

Once you create your 7k threads, you're not going to have any memory to do anything useful. Perhaps you should have a rethink about the design of your application?

一旦您创建了 7k 线程,您将没有任何内存来做任何有用的事情。也许您应该重新考虑应用程序的设计?

Anyway, isn't 512Mb quite small? Perhaps you could provide a bit more information about your application or perhaps the domain?

无论如何,512Mb 是不是很小?也许您可以提供有关您的应用程序或域的更多信息?

回答by Gnoupi

It's not the programming language, it's on the operating system level.

这不是编程语言,而是操作系统级别。

More reading about it, for Windows:

更多关于它的阅读,对于 Windows:

回答by Justin Ardini

Keep in mind that you will never be able to dedicate 100% of the RAM to running Java threads. Some RAM is used by the OS and other running applications, meaning you will never have the full 512 Mb available.

请记住,您永远无法将 100% 的 RAM 专用于运行 Java 线程。操作系统和其他正在运行的应用程序会使用一些 RAM,这意味着您将永远无法使用完整的 512 Mb。

回答by richj

You don't necessarily need one thread per client session. If you look at the way that a J2EE (or JavaEE) server handles multiple connections it uses a mixture of strategies including concurrency, queuing and swapping. Usually you can configure the maximum number of live concurrent instances and idle time-out values at deployment time to tune the performance of your application.

每个客户端会话不一定需要一个线程。如果您查看 J2EE(或 JavaEE)服务器处理多个连接的方式,它会使用混合策略,包括并发、排队和交换。通常,您可以在部署时配置实时并发实例的最大数量和空闲超时值,以调整应用程序的性能。

回答by Andreas

Try setting the maximum memory allowed -Xmx to a lower value and see whether the thread count can be increased. In a project at work I could allocate around 2,5k threads with -Xmx512m and about 4k threads with -Xmx96m.

尝试将允许的最大内存 -Xmx 设置为较低的值,看看是否可以增加线程数。在工作中的项目中,我可以使用 -Xmx512m 分配大约 2.5k 个线程,使用 -Xmx96m 分配大约 4k 个线程。

The bigger your heap the smaller your thread stack space (at least to my experience).

堆越大,线程堆栈空间越小(至少根据我的经验)。