linux下一个线程占用多少内存

来源:互联网 发布:iphone自定义铃声软件 编辑:程序博客网 时间:2024/04/28 01:28

群里讨论出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_tstacksize)
{
#ifdef FLOATING_STACKS
 
  if (__pthread_max_stacksize == 0)
   __pthread_init_max_stacksize ();

  if (stacksize >__pthread_max_stacksize)
    returnEINVAL;
#else
 
  if (stacksize >STACK_SIZE)
    returnEINVAL;
#endif

 
  if (stacksize __stacksize = stacksize;
  return 0;
}


对于java ,Solaris 默认  256k for 32-bit intel, 512Kfor 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>
#include <pthread>

 


void *thread_routine (void *arg)
{
    printf ("Thethread is heren");
 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" );
    returnNULL;
}

int main (int argc, char *argv[])
{
    pthread_tthread_id;
   pthread_attr_t thread_attr;
    size_tstack_size;
    intstatus;

    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 %un",
       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 %un",
       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 ("Createthread");
 }

   getchar();
    printf("Main exitingn");
    pthread_exit(NULL);
    return0;
}


 

看看执行过程:

gcc -pthread -g -DDEBUG -lrt  -o thread_attrthread_attr.c
./thread_attr
Default stack size is 8388608; minimum is16384        //默认的栈大小为8M
Default stack size is 16777216; minimum is16384     //设置后的结果为16M
The thread is here
The thread is here
The thread is here
The thread is here
The thread is here
Get 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!!!

Mainexiting</pthread></pthread>


原创粉丝点击