不知怎的,我无法在MacBook上打开超过10223个文件,而sysctl,launchd和ulimit的打开文件限制设置为65535,我可以从C程序打开62282个文件。
你知道Java是否有一些额外的限制,以及我如何改变它?
我用来测试的Java代码是:
import java.io.*;
import java.util.*;
class OpenFilesTest {
private static List<InputStream> streams = new ArrayList<InputStream>();
public static void main(String[] args) {
for (int i = 0; true; i++) {
FileInputStream f = null;
try {
f = new FileInputStream("/dev/null");
} catch (Throwable e) {
System.err.println(e.getMessage());
e.printStackTrace();
System.exit(1);
}
streams.add(f);
System.out.println("We have " + (i + 1) + " InputStream's for /dev/null");
}
}
}
此输出:
We have 1 InputStream's for /dev/null
We have 2 InputStream's for /dev/null
We have 3 InputStream's for /dev/null
...
We have 10221 InputStream's for /dev/null
We have 10222 InputStream's for /dev/null
We have 10223 InputStream's for /dev/null
/dev/null (Too many open files)
java.io.FileNotFoundException: /dev/null (Too many open files)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:97)
at OpenFilesTest.main(test.java:11)
我用来测试的C代码:
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
int main(void)
{
for (int i = 0; 1; i++)
{
int fd = open("/dev/null", O_RDONLY);
if (fd == -1)
{
perror("Couldn't open /dev/null one more time");
exit(EXIT_FAILURE);
} else {
printf("Opened /dev/null %d times\n", i + 1);
}
}
}
及其输出:
Opened /dev/null 1 times
Opened /dev/null 2 times
Opened /dev/null 3 times
...
Opened /dev/null 62287 times
Opened /dev/null 62288 times
Opened /dev/null 62289 times
Couldn't open /dev/null one more time: Too many open files in system
有关该系统的更多相关信息:
$ uname -a
Darwin hostname.local 13.1.0 Darwin Kernel Version 13.1.0: Thu Jan 16 19:40:37 PST 2014; root:xnu-2422.90.20~2/RELEASE_X86_64 x86_64
$ java -version
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
我也在Linux上试过这个(Debian Wheezy 7.4),在那里我也可以使用这个Java程序打开接近65535个文件句柄。
这么多打开文件的基础用例是Scala中的服务器负载测试应用程序。
答案 0 :(得分:5)
查看options specific for the OS X JVM。特别适用于MaxFDLimit:
-XX: - MaxFDLimit
指示VM避免将文件描述符限制设置为默认最大值。默认行为是将限制设置为OPEN_MAX指定的值,即10240.通常,这是进程可能打开的最大文件数。但是,可以使用sysctl实用程序将此限制增加到用户指定的值。在这种情况下,您可能希望传递-XX:-MaxFDLimit以阻止Java VM将打开文件的数量限制为10240.