#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