UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2009-05-12 09:04 AM

jordi.escola
Member
Registered: 2009-05-12
Posts: 4

Re: Thread LEAK

Offline

#2 2009-05-12 11:03 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Thread LEAK

For one thing, "ret" isn't initialized to zero, so that can give unexpected behaviour.

Give Valgrind a try, it's a great memory debugger.

That said, you don't have a memory leak I think. What causes not all memory
to be freed are glibc's malloc implementation and heap fragmentation, but
it can be something else as well. When you mmap a library you'll get high VIRT
but low RES. When you start using that library its pages are read from the
file on-demand and copied to memory, and you end up with higher RES. The
same happens for executables and other mapped files.

If you're on Linux look at /proc/$PID/smaps and maps to see what that
memory area is exactly. I suspect it's glibc's/libpthread's RW mapping for
its own bookkeeping, in this case all the thread stuff. That or it's the heap.

Memory usage is a complicated thing affected by many details, using RES
or VIRT to detect if you've a memory leak or not isn't a good method, it
only works if you have a big leak and when they steadily keep increasing
over time (though that can also be caused a by heap fragmentation). The
numbers you posted aren't out of the ordinary and I wouldn't worry about it.

If you repeat your test again after the first one your memory usage should
end up about the same as it ended up before. But comparing cold cache
with the memory usage afterwards will never work. To hunt down real
memory leaks use Valgrind (or read the code).

Offline

#3 2009-05-12 12:13 PM

jordi.escola
Member
Registered: 2009-05-12
Posts: 4

Re: Thread LEAK

I use valgrind too.. and mtrace..

But my problem is that  the memory is not liberated totally, and with every new thread that is created new memory is accumulated.

TIA

Offline

#4 2009-05-12 03:17 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: Thread LEAK

Offline

#5 2009-05-12 04:21 PM

jordi.escola
Member
Registered: 2009-05-12
Posts: 4

Re: Thread LEAK

Begin program ....  13560   984

End program 109964  1312

If you modify the code and increase to 4000 threads the final memory(report) increase 300kb. and not liberated, virtual memory aprox 100 Mb.

The memory should return to her first (984) value  or not?

My problem is in a big project that i have... and the memory increase up to 1 > 2 GB because we received to much connections every time..

This sample is a little scale in order to find a solution...

Offline

#6 2009-05-12 09:47 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: Thread LEAK

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

static void* cliente(void *datos);

int main()
{
    int ret;
    pthread_t mythread[4];
    char buf[1024];

    sprintf (buf, "ps -p %d u", getpid());
    system (buf);
    printf("BEGIN: press key\r\n");
    fgets(buf,sizeof(buf),stdin);

    for (ret = 0; ret<4; ret++) {
        pthread_create((pthread_t *)&mythread[ret],NULL,cliente,NULL);
        pthread_detach(mythread[ret]);
    }

    sleep (1);
    sprintf (buf, "ps -p %d u", getpid());
    system (buf);
    printf("END: press key\r\n");
    fgets(buf,sizeof(buf),stdin);
    sprintf (buf, "ps -p %d u", getpid());
    system (buf);

    for (ret = 0; ret<4; ret++) {
        pthread_create((pthread_t *)&mythread[ret],NULL,cliente,NULL);
        pthread_detach(mythread[ret]);
    }

    sleep (1);
    sprintf (buf, "ps -p %d u", getpid());
    system (buf);
    printf("END2: press key\r\n");
    fgets(buf,sizeof(buf),stdin);
    sprintf (buf, "ps -p %d u", getpid());
    system (buf);

    return (0);
}

static void* cliente(void *datos)
{
    char *buff=(char *)malloc(100000000);
    sleep(5);
    printf("end thread..\r\n");
    free(buff);
    buff=NULL;
    pthread_exit(NULL);
}

Offline

#7 2009-05-13 09:14 AM

jordi.escola
Member
Registered: 2009-05-12
Posts: 4

Re: Thread LEAK

correct... in your code, the RSS memory increase aprox 200 kb... if you increase the number of threads the  memory used  increasingly and it would not be liberated

Offline

#8 2009-05-13 01:43 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Thread LEAK

Offline

#9 2009-05-13 01:56 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: Thread LEAK

Offline

#10 2009-05-13 09:23 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Thread LEAK

The VSZ is that big because each thread allocates 100MB.

Offline

#11 2009-05-13 10:49 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: Thread LEAK

Offline

#12 2009-05-13 11:38 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Thread LEAK

Offline

Board footer

Powered by FluxBB