Socket memory


 
Thread Tools Search this Thread
Top Forums Programming Socket memory
# 1  
Old 08-06-2010
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

Last edited by kumaran_5555; 08-06-2010 at 08:46 AM..
# 2  
Old 08-06-2010
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?
# 3  
Old 08-07-2010
Hi,

This is my code.

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
# 4  
Old 08-07-2010
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.

Last edited by Corona688; 08-07-2010 at 12:51 PM..
# 5  
Old 08-07-2010
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
# 6  
Old 08-08-2010
Yes, detached threads automatically return their resources upon finish. This means you can't check their return status, though.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 08-09-2010
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

[DOUBT] Memory high in idle process on Solaris 10 (Memory Utilization > 90%)

Hi Experts, Our servers running Solaris 10 with SAP Application. The memory utilization always >90%, but the process on SAP is too less even nothing. Why memory utilization on solaris always looks high? I have statement about memory on solaris, is this true: Memory in solaris is used for... (4 Replies)
Discussion started by: edydsuranta
4 Replies

2. IP Networking

Clarification - Setting socket options at the same time when socket is listening

I need clarification on whether it is okay to set socket options on a listening socket simultaneously when it is being used in an accept() call? Following is the scenario:- -- Task 1 - is executing in a loop - polling a listen socket, lets call it 'fd', (whose file descriptor is global)... (2 Replies)
Discussion started by: jake24
2 Replies

3. Programming

Error with socket operation on non-socket

Dear Experts, i am compiling my code in suse 4.1 which is compiling fine, but at runtime it is showing me for socket programming error no 88 as i searched in errno.h it is telling me socket operation on non socket, what is the meaning of this , how to deal with this error , please... (1 Reply)
Discussion started by: vin_pll
1 Replies

4. Programming

socket function to read a webpage (socket.h)

Why does this socket function only read the first 1440 chars of the stream. Why not the whole stream ? I checked it with gdm and valgrind and everything seems correct... #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include... (3 Replies)
Discussion started by: cyler
3 Replies

5. Solaris

restrcit physical memory with zone.max-locked-memory

Is it possible to restrict physical memory in solaris zone with zone.max-locked-memory just like we can do with rcapd ? I do not want to used rcapd (1 Reply)
Discussion started by: fugitive
1 Replies

6. Programming

How to deal with lots of data in memory in order not to run out of memory

Hi, I'm trying to learn how to manage memory when I have to deal with lots of data. Basically I'm indexing a huge file (5GB, but it can be bigger), by creating tables that holds offset <-> startOfSomeData information. Currently I'm mapping the whole file at once (yep!) but of course the... (1 Reply)
Discussion started by: emitrax
1 Replies

7. Programming

which socket should socket option on be set

Hi all, On the server side, one socket is used for listening, the others are used for communicating with the client. My question is: if i want to set option for socket, which socket should be set on? If either can be set, what's the different? Again, what's the different if set option... (1 Reply)
Discussion started by: blademan100
1 Replies

8. UNIX for Advanced & Expert Users

connect problem for sctp socket (ipv6 socket) - Runtime fail Invalid Arguments

Hi, I was porting ipv4 application to ipv6; i was done with TCP transports. Now i am facing problem with SCTp transport at runtime. To test SCTP transport I am using following server and client socket programs. Server program runs fine, but client program fails giving Invalid Arguments for... (0 Replies)
Discussion started by: chandrutiptur
0 Replies

9. Solaris

How to find Total and Free Physical Memory and Logical Memory in SOLARIS 9

Hi, Im working on Solaris 9 on SPARC-32 bit running on an Ultra-80, and I have to find out the following:- 1. Total Physical Memory in the system(total RAM). 2. Available Physical Memory(i.e. RAM Usage) 3. Total (Logical) Memory in the system 4. Available (Logical) Memory. I know... (4 Replies)
Discussion started by: 0ktalmagik
4 Replies

10. Programming

Socket Programming socket

Hello, I actually try to make client-server program. I'm using SCO OpenServer Release 5.0.0 and when I try to compile my code (by TELNET) I've got this error : I'm just using this simple code : and I get the same error if I use : If someone can help me, Thanks (2 Replies)
Discussion started by: soshell
2 Replies
Login or Register to Ask a Question