开发者

pthread_create memory leak

开发者 https://www.devze.com 2023-01-04 21:56 出处:网络
I am using C language and Linux as my progra开发者_Go百科mming 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 c

I am using C language and Linux as my progra开发者_Go百科mming 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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消