为什么当我增加最大堆大小时,最大线程数会减少?

2022-09-03 06:48:18

此测试显示可以在 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);
        }
    }

当我用默认堆大小(256M)运行它时,我得到

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)

当我将最大堆大小增加到512M时,我得到

Max memory  494M
Max threads 1906
...

当我将最大堆大小增加到1024M时,我得到

Max memory  989M
Max threads 1162
...

也就是说,堆内存越多,线程越少。为什么?


答案 1

每个线程都需要一个堆栈。分配给堆的内存越多,留给堆栈的内存就越少。

如果您使用的是32位JVM,这将特别严重,因为该进程对于所有内容(代码,堆,堆栈等)的地址空间不超过4GB。我无法在我的 64 位框中重现此行为,其中“最大线程数”保持不变,无论我为堆分配多少内存。

值得注意的是,许多操作系统都允许您调整堆栈的大小。在 Unix 上,这是使用 .ulimit -s


答案 2

我猜想增加堆大小(并使用全部堆大小)会降低操作系统可用于创建新线程的内存量,因此它无法像以前那样创建那么多线程。


推荐