tags:

views:

85

answers:

3

Hi,

I am using C language and Linux as my programming platform.

In my app, I call pthread_create. Then I check the memory usage of my app using ps commandline tool and it adds 4 in the VSZ column.

But the problem is when the pthread_create function handler exits, the 4 that was added in the memory was not release. Then when the app call the pthread_create again, a 4 value was added again until it gets bigger.

I tried pthread_join and it seems the memory still getting bigger.

Thanks.

+8  A: 

ps is not the right tool to measure memory leaks. When you free memory, you are not guaranteed to decrease the process' vsize, both due to memory fragmentation and to avoid unnecessary system calls.

valgrind is a better tool to use.

Artelius
it seems i can't use valgrind since my app is running in embedded device.
sasayins
I think you should be able to use the tool Purify to do the same for Embedded code.
IntelliChick
Also read http://www.linuxjournal.com/article/6059
Artelius
+2  A: 

When you say
*But the problem is when the pthread_create function handler exit*

Are you doing an explicit pthread_exit(NULL) as part of the exiting after completion of the thread execution? Also, the pthread_exit() routine does not close any files that you might have opened in your thread; any files opened inside the thread will remain open even after the thread is terminated.

IntelliChick
yeah, I call pthread_exit(NULL) at the end of the function. and my pthread function handler is just a simple printf in the screen.
sasayins
+4  A: 

You should use either pthread_detach or pthread_join (but not both) on every pthread you create. pthread_join waits for the thread to finish; pthread_detach indicates that you don't intend to wait for it (hence the implementation is free to reclaim the storage associated with the thread when it terminates).

ditto what Artelius said about ps not being the right tool for diagnosing memory leaks.

David Gelhar