本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告

戒除浮躁,读好书,交益友


我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:邢红瑞的blog
日志总数:523
评论数量:1142
留言数量:0
访问次数:9693207
建立时间:2004年12月20日




[linux kernel]linux下一个线程占用多少内存
原创空间,  文章收藏,  网上资源,  软件技术,  电脑与网络

邢红瑞 发表于 2006/7/22 17:21:23

群里讨论出mysql的问题,因为mysql是一个连接建立一个线程的,这就涉及到mysql可以建立多少个线程。无论是windwos 还是linux ,每个线程都有自己独立的stack,每个stack 都占用一定的空间。windwos 默认的是1M,这个在exe中可以看到,也可以编译时指定。linux 默认使用pthread.h中的PTHREAD_STACK_SIZE,这和glibc的编译有关系,这样说明linux 可以使用更多的线程。int __pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize){#ifdef FLOATING_STACKS  /* We have to check against the maximum allowed stack size.  This is no     problem if the manager is already started and we determined it.  If     this hasn't happened, we have to find the limit outself.  */  if (__pthread_max_stacksize == 0)    __pthread_init_max_stacksize ();   if (stacksize > __pthread_max_stacksize)    return EINVAL;#else  /* We have a fixed size limit.  */  if (stacksize > STACK_SIZE)    return EINVAL;#endif   /* We don't accept value smaller than PTHREAD_STACK_MIN.  */  if (stacksize < PTHREAD_STACK_MIN)    return EINVAL;   attr->__stacksize = stacksize;  return 0;} 对于java ,Solaris 默认  256k for 32-bit intel, 512K for 32-bit sparc, 1M for 64-bit sparc           linux 可以使用-Xss,默认使用ulimit -s的值。  先来讲说线程内存相关的东西,主要有下面几条:进程中的所有的线程共享相同的地址空间。任何声明为static/extern的变量或者堆变量可以被进程内所有的线程读写。一个线程真正拥有的唯一私有储存是处理器寄存器。线程栈可以通过暴露栈地址的方式与其它线程进行共享。下面的网上的代码     有大数据量处理的应用中,有时我们有必要在栈空间分配一个大的内存块或者要分配很多小的内存块,但是线程的栈空间的最大值在线程创建的时候就已经定下来了,如果栈的大小超过个了个值,系统将访问未授权的内存块,毫无疑问,再来的肯定是一个段错误。可是没办法,你还是不得不分配这些内存,于是你开会为分配一个整数值而动用malloc这种超级耗时的操作。当然,在你的需求可以评估的情况下,你的需求还是可以通过修改线程的栈空间的大小来改变的。 下面的我们用pthread_attr_getstacksize和pthread_attr_setstacksize的方法来查看和设置线程的栈空间。注意:      下面的测试代码在我自己的机子上(ubuntu6.06,ubuntu6.10,redhat 9, gentoo)通过了测试,但是很奇怪的是在我同事的机子上,无论是改变环境,还是想其它方法都不能正常的运行 。在网上查了一下,很多人也存在同样的问题,至今不知道为何。 linux线程的实现方式决定了对进程的限制同样加在了线程身上:)所以,有问题,请参见<pthread之线程栈空间(2)(进行栈) 直接看代码吧,只有一个C文件(thread_attr.c)#include <limits.h>#include <pthread.h>   void *thread_routine (void *arg){    printf ("The thread is here\n"); char p[1024*1024*15]; int i=1024*1024*15;  while(i--) {  p[i] = 3;  }  printf( "Get 15M Memory!!!\n" );   char p2[ 1024 * 1020 + 256 ]; memset( p2, 0, sizeof( char ) * ( 1024 * 1020 + 256 ) ); printf( "Get More Memory!!!\n" );    return NULL;} int main (int argc, char *argv[]){    pthread_t thread_id;    pthread_attr_t thread_attr;    size_t stack_size;    int status;     status = pthread_attr_init (&thread_attr);    if (status != 0)        printf ("Create attr");     status = pthread_attr_setdetachstate (        &thread_attr, PTHREAD_CREATE_DETACHED);     if (status != 0)        printf ( "Set detach"); #ifdef _POSIX_THREAD_ATTR_STACKSIZE   status = pthread_attr_getstacksize (&thread_attr, &stack_size);    if (status != 0)        printf ( "Get stack size");    printf ("Default stack size is %u; minimum is %u\n",        stack_size, PTHREAD_STACK_MIN);     status = pthread_attr_setstacksize (        &thread_attr, PTHREAD_STACK_MIN*1024);    if (status != 0)        printf ("Set stack size");    status = pthread_attr_getstacksize (&thread_attr, &stack_size);    if (status != 0)        printf( "Get stack size");    printf ("Default stack size is %u; minimum is %u\n",        stack_size, PTHREAD_STACK_MIN);#endif int i = 5; while(i--) {  status = pthread_create (   &thread_id, &thread_attr, thread_routine, NULL);  if (status != 0)   printf ("Create thread"); }     getchar();    printf ("Main exiting\n");    pthread_exit (NULL);    return 0;}   看看执行过程:gcc -pthread -g -DDEBUG -lrt  -o thread_attr thread_attr.c./thread_attrDefault stack size is 8388608; minimum is 16384         //默认的栈大小为8MDefault stack size is 16777216; minimum is 16384      //设置后的结果为16MThe thread is hereThe thread is hereThe thread is hereThe thread is hereThe thread is hereGet 15M Memory!!!Get More Memory!!!Get 15M Memory!!!Get More Memory!!!Get 15M Memory!!!Get 15M Memory!!!Get More Memory!!!Get More Memory!!!Get 15M Memory!!!Get More Memory!!! Main exiting


阅读全文(14233) | 回复(1) | 编辑 | 精华
 


回复:linux下一个线程占用多少内存
原创空间,  文章收藏,  网上资源,  软件技术,  电脑与网络

菜鸟(游客)发表评论于2012/2/13 15:49:40

赞邢牛,正好解决了我的问题。复制代码~~


个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
 


回复:linux下一个线程占用多少内存
原创空间,  文章收藏,  网上资源,  软件技术,  电脑与网络

jason(游客)发表评论于2010/3/28 8:48:27

但是很奇怪的是在我同事的机子上, 原因在于系统的limit ulimit -a 可以显示stack的limit

个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
 


回复:linux下一个线程占用多少内存
原创空间,  文章收藏,  网上资源,  软件技术,  电脑与网络

jason(游客)发表评论于2010/3/28 8:47:28

good

个人主页 | 引用回复 | 主人回复 | 返回 | 编辑 | 删除
 


» 1 »

发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.063 second(s), page refreshed 144755610 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号