c中线程的内存分配

时间:2013-04-25 11:00:33

标签: c linux

如何减少C中线程使用的内存?虽然一个线程需要大约8到10 MB的内存,有没有办法减少这个内存?

2 个答案:

答案 0 :(得分:6)

是的,您可以设置线程堆栈的大小。

pthread_attr_t attribute;
pthread_t thread;
pthread_attr_init(&attribute);
pthread_attr_setstacksize(&attribute,size); // size may be defined by u as 1024,2048,etc
pthread_create(&thread,&attribute,fun,0);

...............................................

void *fun(void *arg)
{
      ....
}

答案 1 :(得分:2)

如前所述,您可以在应用程序中使用pthread属性。

但您也可以使用ulimit命令为当前shell中启动的任何应用程序设置限制:

  • ulimit -s:以kiB
  • 显示当前限制
  • ulimit -s 1024:将堆栈限制为1 MiB