You are not logged in.
Pages: 1
Offline
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
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
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
#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
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
Pages: 1