Pthread_create memory leak

I use C and Linux as my programming platform.

In my application, I call pthread_create. Then I check the memory usage in my application using the ps command line utility and adds 4 to the VSZ column.

But the problem is when the pthread_create, 4 function handler, which was added to memory, is called, was not released. Then, when the application calls pthread_create again, the value 4 is added until it becomes larger.

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

Thanks.

+2
source share
3 answers

ps not the right tool for measuring memory leaks. When you free memory, you are not guaranteed to decrease the vsize process, either due to memory fragmentation or to avoid unnecessary system calls.

valgrind is the best tool to use.

+9
source

You must use either pthread_detach or pthread_join (but not both) on every pthread you create. pthread_join is waiting for the thread to finish; pthread_detach indicates that you are not going to wait for it (therefore, the implementation may free up memory associated with the thread when it exits).

The same thing that Artelius said that ps is not the right tool for diagnosing memory leaks.

+6
source

When you say But the problem is that the pthread_create function handler exits

Do you execute explicit pthread_exit (NULL) as part of the output after thread completion? In addition, the pthread_exit () procedure does not close files that you might open in your thread; any files opened inside the stream remain open even after the stream ends.

+3
source

All Articles