Socket memory

Hi,

I am using Linux to run TCP server.

Whenever i accept a TCP connection from a client, i am able to around 10MB increase in prcoess(Using pmap).

How can i free this when i close the connection ?

I tried close(fd), but that is not freeing up this memory chunk.

Please help to achieve this.

Thanks
Kumaran R

Does it increase repeatedly with successive connections? As in, open connection, close it, and allow another, it still increases another chunk? If it reuses the same chunk, this may just be buffers. If not, I suspect this is your own memory leak, what's your code like?

Hi,

This is my code.

 
#include "../hdr/isamhdr.h" 
#include <mcheck.h>
 
int main(int argc, char *argv[]) 
{
    struct addrinfo hints, *res, *p;
    struct sockaddr *accepted_conc;
    int status, socket_fd,accept_fd,sockaddr_size=sizeof(accepted_conc),curr_conc=0,max_conc;
    pthread_t *threads;

    if(argc != 3)
    {
        printf("ITF_ERR: Usage %s <port numebr> <max_connections>\n",argv[0]);
        return 2;
    }

    max_conc=atoi(argv[2]);
    threads=calloc(max_conc,sizeof(pthread_t));
    accepted_conc=calloc(max_conc,sizeof(struct sockaddr));

    memset(&hints, 0, sizeof hints);

    hints.ai_family = AF_UNSPEC; 
    hints.ai_socktype = SOCK_STREAM;    // open tcp connection
    hints.ai_flags = AI_PASSIVE;                // server always runs on localhost


    if ((status = getaddrinfo(NULL,argv[1], &hints, &res)) != 0) 
    {
        printf("ITF_ERR: getaddrinfo():%s\n", gai_strerror(status));
        return 2;
    }
    // socket()
    if((socket_fd = socket(res->ai_family,res->ai_socktype,res->ai_protocol)) <0 )
    {
        printf("ITF_ERR: socket() %s\n",strerror(errno));
        return 2;
    }
    else
        printf("socket() call sucess\n");
    
    // bind()
    if((status = bind(socket_fd,res->ai_addr,res->ai_addrlen)) < 0)
    {
        printf("ITF_ERR: bind() %s\n",strerror(errno));
        return 2;
    }
    else
        printf("bind() call sucess\n");

    // listen()
    if((status = listen(socket_fd,10)) < 0)
    {
        printf("ITF_ERR: listen() %s\n",strerror(errno));
        return 2;
    }
    else
        printf("listen() call success\n");

    while(curr_conc < max_conc)
    {
        printf("waiting for new connections ...\n");
        // accept()
        if((accept_fd = accept(socket_fd,accepted_conc+curr_conc,&sockaddr_size)) <0 )
        {
            printf("ITF_ERR: accept() %s\n",strerror(errno));
            return 2;
        }
        else
        {
            curr_conc++;
            printf("%d -accept() call success\n",curr_conc);
        }
        status = pthread_create(&threads[curr_conc], NULL,_server_thread, (void *)accept_fd);
        if(status)
        {
            printf("ITF_ERR; return code from pthread_create() is %d\n", status);
            return 2;
        }
    }
    close(socket_fd);
    printf("no room for further threads\n");
    
    return 0; 
} 
 
void *_server_thread(void *sock_fd)
{ 
    void *lib_hdl; 
    USR_qry_ptr qry; 
    USR_rslt_set_ptr rslt; 
    unsigned char qry_str[LINE_MAX];
    int fd=(int)sock_fd,sts;

    if((lib_hdl=dlopen(ISAM_LIB,RTLD_NOW)) == NULL) 
    { 
        printf("%s\n",dlerror()); 
        return NULL; 
    } 
 
    dl_qry_parser=dlsym(lib_hdl,"qry_parser"); 
     
    if(dl_qry_parser == NULL) 
    { 
        printf("%s\n",dlerror()); 
        return NULL; 
    } 
    dl_execute_qry=dlsym(lib_hdl,"execute_qry"); 
     
    if(dl_execute_qry == NULL) 
    { 
        printf("%s\n",dlerror()); 
        return NULL; 
    } 
    dl_print_rslt_set=dlsym(lib_hdl,"_print_rslt_set"); 
     
    if(dl_print_rslt_set == NULL) 
    { 
        printf("%s\n",dlerror()); 
        return NULL; 
    }
    dl_free_rslt_set=dlsym(lib_hdl,"_free_rslt_set"); 
     
    if(dl_free_rslt_set == NULL) 
    { 
        printf("%s\n",dlerror()); 
        return NULL; 
    }
    
    dl_free_qry=dlsym(lib_hdl,"_free_qry"); 
     
    if(dl_free_qry == NULL) 
    { 
        printf("%s\n",dlerror()); 
        return NULL; 
    }
    
    
    while(1)
    {
        sts=recv(fd,qry_str,LINE_MAX,0);
        if(sts < 0)
        {
            printf("SRV_ERR: recv() %s\n",strerror(errno));
            close(fd);
            pthread_exit(NULL);
        }            
        if(!strcmp(qry_str,"quit"))
        {
            printf("SRV_ERR: Thread exiting by quit\n");
            close(fd);
            pthread_exit(NULL);
        }
    
        qry=(*dl_qry_parser)((unsigned char *)qry_str);        
        rslt=(*dl_execute_qry)(qry);

        (*dl_print_rslt_set)(rslt);

        // do clean up        
        (*dl_free_rslt_set)(rslt);
        (*dl_free_qry)(qry);
    }

    return NULL;
}
    

I have checked all my functions using mtrace() for memory leak. It is under control.

When i give any number of inputs keeping the number of connections constant, then there is no memory increase.

But as and when i start new a connection, i am able to see 10 MB increase in private memory under anonymous using pmap.

Could this be a socket memory and that should be freed using some system call,

Please advice.

Thanks
Kumaran

You are creating new threads every time you receive a connection! Threads of course take memory. You're not properly letting the threads finish, either. To free up their memory when they quit, you should do pthread_join in main().

You can also reduce the stack size before you create the thread with pthread_attr_setstacksize. (there's also a nice example under the pthread_create manpage.) The stack size should be at least 16384 bytes and, on most systems, a multiple of 4096 bytes. It should also be as large as it needs to be to contain all the thread's local variables and function calls, or you may start getting mysterious segmentation faults when calls nest too deep.

Thanks a mill for your suggestions.

I am not sure how to use thread_join(). Because so for i have heard that it will be used to sync the main and sub-threads.

Let me try using that.

What if i use the detached threads. Will that free the memory as and when i exit from the thread.

Thanks
Kumaran R

Yes, detached threads automatically return their resources upon finish. This means you can't check their return status, though.

Thanks.

That worked fine with detached state.

I need to find a work around to get the return status, though right now it is not required.