線程佔用內存多大?

#include <malloc.h>
#include <unistd.h>
 
int main()
{
//      char *p =(char *) malloc(1024*1024);
 
        while(1)
        {
                sleep(1);
        }
        return 0;
}
主線程main佔用內存大小爲4188K ,大約4M

示例2:

#include <malloc.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
void *Thread1(void *)
{
        while(1)
        {
                sleep(1);
        }
}
 
int main()
{
        int ret = 0;
        pthread_t threadid;
        ret = pthread_create(&threadid, NULL, Thread1, NULL);
        if(ret == -1)
        {
                printf("create thread failed!!!\n");
        }
        while(1)
        {
                sleep(1);
        }
        return 0;
}

main線程+Thread1線程 佔用大小位14684K  Thread1佔用= 14684-4188=10M



示例3:

#include <malloc.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
void *Thread1(void *)
{
        while(1)
        {
                sleep(1);
        }
}
void *Thread2(void *)
{
        while(1)
        {
                sleep(1);
        }
}
int main()
{
        int ret = 0;
        pthread_t threadid1,threadid2;
        ret = pthread_create(&threadid1, NULL, Thread1, NULL);
        if(ret == -1)
        {
                printf("create thread failed!!!\n");
        }
        ret = pthread_create(&threadid2, NULL, Thread2, NULL);
        if(ret == -1)
        {
                printf("create thread failed!!!\n");
        }
        while(1)
        {
                sleep(1);
        }
        return 0;
}

main線程+Thread1線程+ Thread2線程 佔用內存爲22880K Thread2佔用內存=22880-14684=8M


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