linux下一個線程佔用多少內存

羣裏討論出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>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章