Reputation: 16029
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.
Upvotes: 2
Views: 2557
Reputation: 27900
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.
Upvotes: 6
Reputation: 626
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.
Upvotes: 3
Reputation: 49134
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.
Upvotes: 9