我想知道:我的系统可以运行50000号吗?是否有并行线程/进程?
为此,我将“ulimit max process”和“/proc/sys/kernel/pid_max”更改为 50000。 但我仍然无法跨越〜33000号。进程/线程数。
数数。我正在使用的系统上的进程/线程数: ps -eL|wc -l 我写了一个java程序来创建那些没有。线程数。
但最后我遇到了这个异常:
Java HotSpot(TM) 64-Bit Server VM warning: Attempt to protect stack guard pages failed.
Java HotSpot(TM) 64-Bit Server VM warning: Attempt to protect stack guard pages failed.
Java HotSpot(TM) 64-Bit Server VM warning: Attempt to protect stack guard pages failed.
Java HotSpot(TM) 64-Bit Server VM warning: Attempt to deallocate stack guard pages failed.
Total thread created #**32515**
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:640)
at HowManyThreads.main(HowManyThreads.java:12)
Java HotSpot(TM) 64-Bit Server VM warning: Attempt to deallocate stack guard pages failed.
Java HotSpot(TM) 64-Bit Server VM warning: Attempt to allocate stack guard pages failed.
^CJava HotSpot(TM) 64-Bit Server VM warning: Exception java.lang.OutOfMemoryError occurred dispatching signal SIGINT to handler- the VM may need to be forcibly terminated
请帮我创建 50000 个号码。进程/线程。
ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 2066250
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 150000
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 40000
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
猫/proc/sys/kernel/pid_max
50000
Java程序
package create.threads;
public class HowManyThreads
{
private static Object s = new Object();
private static int count = 0;
public static void main(String[] argv)
{
try
{
for(;;)
{
new Thread(new Runnable()
{
public void run()
{
synchronized(s)
{
count += 1;
}
for(;;)
{
try
{
Thread.sleep(1000);
} catch (Exception e){
System.out.println(e);
}
}
}
}).start();
}
}
finally
{
System.out.println("Total thread created #"+count);
}
}
}
这是我的系统上的免费命令输出: 当我的程序运行并抛出错误/异常时
free -g
total used free shared buffers cached
Mem: 252 3 248 0 0 0
-/+ buffers/cache: 3 249
Swap: 1 0 1
当我不运行这个程序时
free -g
total used free shared buffers cached
Mem: 252 2 250 0 0 0
-/+ buffers/cache: 1 250
Swap: 1 0 1
你能帮我吗...我遗漏了什么...
最佳答案
线程使用堆上的内存(令人惊讶!)
您可以通过向 Java 程序提供更多堆来解决问题:
java -Xmx1024m <whatever comes for your application>
1gib 内存。
经验法则是一个线程 1mb,因此如果您想要 50k 线程,则需要 50GB 内存。
要降低此值,您可以减小每个线程的堆栈大小
java -Xss512k
对于 512k 的堆栈,对于您的特定示例,您可能只需要 1k 甚至更少。 The default for Linux x64 seems 256k .
但是我宁愿问为什么你需要这么多线程来做事情。您应该使用与处理器核心中的任何内容相匹配的线程池,然后只安排在该池上执行的工作。
关于java - 我的 64 位机器上可以运行多少个线程/进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26033269/