Hi
This is my example of pthread not releasing memory resources when the thread
function exits.
I have written (copied and modified) a small program that creates 200
threads which exit after 10 seconds.
The main function then sleeps for 20 seconds, allowing me/you to ps aux |
grep <progname> to see the big vsize increase.
You can clearly see (below the source) that even though the functions exit,
memory is not released.
I am supposed to call a certain function to have the memory released?
Thanks,
Isaac Aaron
#include <pthread.h>
void first_function(int *);
void second_function(int *);
int j;
main()
{
int i;
pthread_t thread1, thread2;
for (i=0;i<100; i++) {
pthread_create(&thread1, NULL, (void*) first_function, (void*)&i);
pthread_create(&thread2, NULL, (void*) second_function, (void*)&i);
usleep(100);
}
sleep(20);
}
void first_function(int *num)
{
int x,y,z;
printf("Entered first_function %d\n", *num);
sleep(10);
printf("Exited first_function\n", *num);
}
void second_function(int *num)
{
int x,y,z;
printf("Entered second_function %d\n", *num);
sleep(10);
printf("Exited second_function %d\n", *num);
}
[e-tsik@qbserv e-tsik]$ ps aux | grep pthread
e-tsik 25556 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25557 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25682 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25683 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25684 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25685 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25686 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25687 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25688 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25689 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25690 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25691 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25692 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25693 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25694 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25695 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25696 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25697 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25704 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25705 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25706 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25707 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25708 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25709 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25710 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25711 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25712 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25713 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25714 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25715 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25716 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25717 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25718 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25719 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25720 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25721 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25722 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25723 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25730 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25731 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25732 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25733 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25734 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25735 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25736 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25737 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25738 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25739 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25740 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25741 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25742 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25743 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25744 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25745 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25746 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25747 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25748 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25749 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
[e-tsik@qbserv e-tsik]$ ps aux | grep pthread
e-tsik 25556 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25557 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25769 0.0 0.4 1708 596 pts/7 R 07:34 0:00 grep pthread
This is my example of pthread not releasing memory resources when the thread
function exits.
I have written (copied and modified) a small program that creates 200
threads which exit after 10 seconds.
The main function then sleeps for 20 seconds, allowing me/you to ps aux |
grep <progname> to see the big vsize increase.
You can clearly see (below the source) that even though the functions exit,
memory is not released.
I am supposed to call a certain function to have the memory released?
Thanks,
Isaac Aaron
#include <pthread.h>
void first_function(int *);
void second_function(int *);
int j;
main()
{
int i;
pthread_t thread1, thread2;
for (i=0;i<100; i++) {
pthread_create(&thread1, NULL, (void*) first_function, (void*)&i);
pthread_create(&thread2, NULL, (void*) second_function, (void*)&i);
usleep(100);
}
sleep(20);
}
void first_function(int *num)
{
int x,y,z;
printf("Entered first_function %d\n", *num);
sleep(10);
printf("Exited first_function\n", *num);
}
void second_function(int *num)
{
int x,y,z;
printf("Entered second_function %d\n", *num);
sleep(10);
printf("Exited second_function %d\n", *num);
}
[e-tsik@qbserv e-tsik]$ ps aux | grep pthread
e-tsik 25556 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25557 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25682 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25683 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25684 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25685 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25686 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25687 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25688 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25689 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25690 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25691 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25692 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25693 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25694 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25695 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25696 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25697 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25704 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25705 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25706 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25707 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25708 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25709 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25710 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25711 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25712 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25713 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25714 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25715 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25716 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25717 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25718 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25719 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25720 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25721 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25722 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25723 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25730 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25731 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25732 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25733 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25734 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25735 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25736 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25737 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25738 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25739 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25740 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25741 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25742 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25743 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25744 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25745 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25746 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25747 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25748 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
e-tsik 25749 0.0 0.9 411052 1256 pts/4 S 07:34 0:00 ./pthread
[e-tsik@qbserv e-tsik]$ ps aux | grep pthread
e-tsik 25556 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25557 0.0 0.9 411052 1256 pts/4 S 07:33 0:00 ./pthread
e-tsik 25769 0.0 0.4 1708 596 pts/7 R 07:34 0:00 grep pthread