Pthreads-高内存使用率 [英] Pthreads - High memory usage

查看:91
本文介绍了Pthreads-高内存使用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C编程,在256Mb系统上的Linux中创建了很多Pthread.我通常有+ 200Mb的免费空间.

I am programming something in C that creates a lot of Pthreads in Linux on a 256Mb system. I usually have +200Mb free.

当我以较少的线程数量运行程序时,它可以工作,但是一旦创建了大约100个线程,由于系统内存不足,它会报错.我做了几次测试,每个线程几乎消耗了2Mb.线程的堆栈大小设置为16Kb.

When I run the program with a low amount of threads it works, but once I make it create around 100 threads it gives errors because the system runs out of memory. I did several tests and each threads use almost 2Mb. The stack size of the threads is set to 16Kb.

我用来创建每个线程的代码:

The code I use to create each thread:

pthread_attr_t attr;
pthread_attr_init(&attr);
size_t stacksize;
stacksize = (double) 16*1024;
int res = pthread_attr_setstacksize (&attr, stacksize);
int res2 = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if (res != 0 || res2 != 0) {
    logs << "pthread_attr_XX: error "+int2string(res);
    exit(-1);
}
pthread_t id;
pthread_create(&id, &attr, &Class::thread_callback, &some_var);

是正常现象还是我缺少什么?谢谢.

Is it normal or am I missing something? Thanks.

推荐答案

不确定是否有帮助,但是在创建第一个线程之前,尝试使用RLIMIT_STACK调用setrlimit将堆栈大小限制为16k.

Not sure it will help, but try calling setrlimit with RLIMIT_STACK to limit the stack size to 16k before creating your first thread.

这篇关于Pthreads-高内存使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆