Problem with POSIX pthreads and virtual memory

Hi, i have this code... in order to test my problem...

#include <stdio.h>
#include <iostream>
#include <pthread.h>
static void* cliente(void *datos);

int main()
{
        pthread_attr_t tattr;
        int ret;
        size_t size = PTHREAD_STACK_MIN + 0x0100;
        ret = pthread_attr_init(&tattr);
        ret = pthread_attr_setstacksize(&tattr, size);
        ret= pthread_attr_setdetachstate(&tattr,PTHREAD_CREATE_JOINABLE);
        ret=0;
        pthread_t mythread[401];
        while(ret<400)
        {
                                int error=pthread_create((pthread_t *)&mythread[ret],&tattr,cliente,NULL);
                                if(error==0)pthread_detach((pthread_t)mythread);

                ret++;
        }

        while(true)
        {}
}
static void* cliente(void *datos)
{
        char *buff=(char *)malloc(100000);
        sleep(15);
        free(buff);
        buff=NULL;
        pthread_exit(NULL);

}

this code uses aprox. 370 Mb of virtual memory, But when the threads finish it does not liberate the above mentioned memory.

Any body can help me??

That is because the kernel allocated 370MB of memory to the process - once a process gets memory it keeps that memory allocated until the process ends. This is because calls to brk() to allocate memory are very expensive, and brk() would be called to 'give back' the memory as well as to allocate it.

You can try man brk to see if you want to try memory management yourself. However what you see is expected behavior.

yes this is correct.. but i was waiting that using free before calling to pthread_exit all allocated memory was liberated, and is liberated aprox 30 Mb. (allocated by malloc) but not the rest. !?

Pass the thread id of every thread created to the pthread_detach() call instead of the starting location in memory where they are all stored.

if(error==0) pthread_detach(mythread[t]);

Yes, i try to put pthread_detach(pthread_self()); before the call pthread_exit() but the result is the same... allways the virtual memory is over 200 Mb.... only is liberated aprox 30 Mb.

In case you misunderstood me I was talking about calling pthread_detach() with the threadid as its argument in the function main() instead of in function cliente().
If calling pthead_self() inside the function main() then its argument would be the calling id of the initial thread instead of each new thread that is created.

yes, detach is inside client procedure..

static void* cliente(void *datos)
{
int id=pthread_self();
char *buff=(char *)malloc(100000);
sleep(15);
free(buff);
buff=NULL;
int error=pthread_detach(id);
printf("End Thread (%d)\r\n",error);
pthread_exit(NULL);

}

Well can you change it so that pthread_detach() is called from inside the function main().

the same results....
You can try to execute the code??...
If you do it, you will be able to see as the virtual memory(report) it is reserved and is not liberated when they finish all the threads