Problem with POSIX pthreads and virtual memory


 
Thread Tools Search this Thread
Top Forums Programming Problem with POSIX pthreads and virtual memory
# 1  
Old 08-06-2008
Data Problem with POSIX pthreads and virtual memory

Hi, i have this code... in order to test my problem...
Code:
#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??

Last edited by jim mcnamara; 08-06-2008 at 10:55 AM.. Reason: added code tags
# 2  
Old 08-06-2008
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.
# 3  
Old 08-06-2008
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. !?
# 4  
Old 08-06-2008
Pass threadid not its location

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.

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

# 5  
Old 08-07-2008
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.
# 6  
Old 08-07-2008
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.
# 7  
Old 08-07-2008
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);

}
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Virtual Memory problem on local zone

Hi I have a problem with Virtual Memory on one local zone (myzone1): //from local zone (myzone1) swap -s total: 22888544k bytes allocated + 0k reserved = 22888544k used, 3325856k available swap -l swapfile dev swaplo blocks free /dev/swap ... (0 Replies)
Discussion started by: primo102
0 Replies

2. Programming

POSIX Message Queue Memory Allocation

Hi, I wanted to know whether the POSIX message queues are statically allocated memory by the kernel based on the parameters specified in the open or as and when we send messages, memory are allocated? Does the kernel reserve the specified memory for the message queue irrespective of whether... (1 Reply)
Discussion started by: sumtata
1 Replies

3. Programming

Memory LEAK with pthreads

I have this code... #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 =... (8 Replies)
Discussion started by: JEscola
8 Replies

4. UNIX for Advanced & Expert Users

POSIX thread runs out of memory

i am creating threads in my program using the POSIX interface. when the thread starts executing i run out of memory and get a core dump. i have tried to increase the threads stack size using pthread_attr_setstacksize, but of no use since i guess the dynamic memory is allocated on the heap and... (1 Reply)
Discussion started by: aniketkadu2002
1 Replies

5. UNIX for Dummies Questions & Answers

cpu, memory and virtual memory usage

Hi All, Does anyone know what the best commands in the UNIX command line are for obtaining this info: current CPU usage memory usage virtual memory usage preferably with date and time parameters too? thanks ocelot (4 Replies)
Discussion started by: ocelot
4 Replies

6. AIX

ulimits max locked memory virtual memory

Hi, Would any one be so kind to explain me : are ulimits defined for each user seperately ? When ? Specialy what is the impact of : max locked memory and virtual memory on performance of applications for a user. Many thanks. PS : this is what I can see in MAN : ulimit ] ... (5 Replies)
Discussion started by: big123456
5 Replies

7. UNIX for Advanced & Expert Users

Shared Memory (Posix)

hi I had to create a shared memory segment which allows the creation of 8 child processes each with 1024 bytes and contains a common buffer area of 2096bytes. i was able to create the shared memory with shm_open() followed by ftruncate() and mmap() system calls. but for the shared buffer, i... (3 Replies)
Discussion started by: pretty
3 Replies

8. Programming

POSIX Thread - Memory leak

Hi all! I am implementing an http server in c++ using the posix thread, but i am having a memory leak and i cannot find the reason. I have already commented out the section that initializes the threads and i found out, the problem is when i initialize/run the threads. In the threads i have... (1 Reply)
Discussion started by: laurovalente
1 Replies

9. Programming

Problem with pthreads

hi i have a code: I found that after exiting from child thread memory isn't freed. I commented everything which is "some actions" here, so thread's function contains only two lines. But it doesn't help. What do I do wrong? Thanks a lot (3 Replies)
Discussion started by: sery0ga
3 Replies

10. Programming

how can i avoid virtual memory problem

Hi i was trying to compile one of my project source file which is neary 2MB large. i couldnt compile it, instead getting an error like "insufficient virtual memory" how come i get away with this problem ,please suggest. thanks (1 Reply)
Discussion started by: Raom
1 Replies
Login or Register to Ask a Question