Results 1 to 2 of 2
Hi, how do you do?
I have faced a problem with my code (a small tcp server). After the thread returns, the memory not decreases, but when a new connection ...
- 01-01-2011 #1Just Joined!
- Join Date
- Jan 2011
- Posts
- 4
Pthread not release memory
Hi, how do you do?
I have faced a problem with my code (a small tcp server). After the thread returns, the memory not decreases, but when a new connection is made, memory not increases and the new connection initialize a new thread with a same thread id of the previous thread. When two connections are made at same time, two thread ids are created and when the respective threads returns the memory not decreases. The Valgrind indicates that not memory leak occurs, the pointers are released. Indeed, more memory are allocate when new thread id is created.
i used gcc and debian.
my code:
thanks a lot.Code:#include <stdio.h> #include <stdlib.h> #include <strings.h> #include <errno.h> #include <sys/socket.h> #include <arpa/inet.h> #include <pthread.h> #include <signal.h> #include <sys/signal.h> #define QUEUE_SIZE 1000 #define BUF_SIZE 4096 static pthread_key_t tid01_key; static pthread_once_t tid01_once = PTHREAD_ONCE_INIT; void connection_destructor(void *pointer) { int id; id = pthread_self(); printf("Destructor ID thread %i\n", id); pthread_detach(pthread_self()); free(pointer); } void connection_once(void) { pthread_key_create(&tid01_key, connection_destructor); } int *open_connection(void *); int main() { int iSockListen; int *iAccept; pthread_t thread01; struct sockaddr_in listen_addr; struct sockaddr *cliaddr; socklen_t len; iSockListen = socket(AF_INET, SOCK_STREAM, 0); if( iSockListen == -1) { perror("socket:"); exit(-1); } listen_addr.sin_family = AF_INET; listen_addr.sin_port = htons(3010); listen_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); bzero(&(listen_addr.sin_zero), 8); if( bind(iSockListen, (struct sockaddr *)&listen_addr, sizeof(struct sockaddr)) < 0) { perror("bind:"); exit(-1); } if (listen( iSockListen, QUEUE_SIZE) < 0) { perror("listen:"); exit(-1); } for (;;) { len = sizeof(cliaddr); iAccept = (int*)malloc(sizeof(int)); *iAccept = accept(iSockListen, (struct sockaddr *)&cliaddr, &len); pthread_create(&thread01, NULL, &open_connection, (void *) iAccept); } } int *open_connection(void *connection) { pthread_once(&tid01_once, connection_once); pthread_setspecific(tid01_key, connection); char buffer_rec[BUF_SIZE] = "HTTP/1.1 200 OK\n"; int tcpsocket; int thread_id; tcpsocket = *((int *) connection); thread_id = pthread_self(); printf("ID thread %i\n", thread_id); send(tcpsocket, buffer_rec, strlen(buffer_rec), 0); sleep(5); shutdown(tcpsocket, SHUT_RDWR); return((void *)0); }Last edited by MikeTbob; 01-02-2011 at 03:42 PM. Reason: Added Code Tags
- 01-02-2011 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Please enclose your code inside code blocks as I have done for you above. It keeps the web site from converting some constructs into smileys, conserves the indentation, and other stuff like that so that it is much more readable.
Memory that is allocated using the heap with either the C malloc/realloc/calloc functions, or C++ new operators, is associated with a process, not a thread. Once you free such memory using free or delete, it is still associated with the process and not returned to the operating system. The next call to malloc/new et al will try to allocate memory from the process heap before asking the OS for more memory. Tools such as valgrind will not consider such freed heap memory as leaks, yet your process will continue to show that is has that memory in use. FWIW, you can return unused memory to the OS with the system calls brk/sbrk, but that is not generally recommended as you can return memory still in use to the OS without meaning to, resulting in the general destruction of your program (crash). Direct use of brk/sbrk is usually only useful if you are implementing your own malloc/free functions in most cases - not something for any but the most experienced system software engineer IMO. I have been there, and done that when doing research on memory management and garbage collection algorithms, so I speak with some experience in that regard.Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote
