#include #include #include #include #include #define ALLOC_BYTE 512*1024 #define COUNT 500 void *alloc_function( void *ptr ); int main(int argc, char *argv[]) { pthread_t thread1, thread2; char *message1 = "Thread 1"; int iret1, iret2; iret1 = pthread_create( &thread1, NULL, alloc_function, (void*) message1); pthread_join( thread1, NULL); printf("Thread 1 returns: %d\n",iret1); exit(0); } void *alloc_function( void *ptr ) { char *message; message = (char *) ptr; void *myblock[COUNT]; int i= 0,j=0; int freed=0; printf("message_alloc \n"); while(1) { memset(myblock,0,sizeof(myblock)); printf("message_alloc %s \n",message); for(i=0;i< COUNT ;i++) { myblock[i] = (void *) malloc(ALLOC_BYTE); if (!myblock[i]) { printf("No memory for allocating: freeing \n"); printf("OOM may kill ME \n"); for(j=0;j < i;j++) { freed = 1; free(myblock[j]); printf("Currently freeing %d * %d Bytes \n",j,ALLOC_BYTE); } break; } memset(myblock[i],1, ALLOC_BYTE); // printf("Currently allocating %d * %d Bytes \n",i,ALLOC_BYTE); } sleep(1); #if 1 if(!freed) { for(i=0;i< COUNT;i++) { free(myblock[i]); // printf("Currently freeing %d * %d Bytes \n",i,ALLOC_BYTE); } } sleep(1); #endif } }