Why does Valgrind show increasing stack usage with boost::thread?
This isn't boost::threads, it happens with just plain pthreads too. I grabbed the sample program from here (Pthread Creation and Termination), upped the thread count to 1000 and compiled as plain C, and I see the same behavior when processing it with massif. So either it's something in pthreads or something valgrind/massif is doing.
EDIT: used program (Pthread Joining) as well. See second graph.
Creation and Termination:
KB547.6^ # | @@@# | @@@@@@@# | @@@@@@@@@@# | @@@@@@@@@@@@@# | ::::@@@@@@@@@@@@@# | ::::: ::@@@@@@@@@@@@@# | @@@::::: ::@@@@@@@@@@@@@# | @@@@@@@::::: ::@@@@@@@@@@@@@# | @@@@@@@@@@::::: ::@@@@@@@@@@@@@# | @@@@@@@@@@@@@::::: ::@@@@@@@@@@@@@# | @@@@@@@ @@@@@@@@@@::::: ::@@@@@@@@@@@@@# | @@@@@ @@ @@ @@@@@@@@@@::::: ::@@@@@@@@@@@@@# | @@@@@ @@ @@ @@ @@@@@@@@@@::::: ::@@@@@@@@@@@@@# | ::@@@@@@@ @@ @@ @@ @@@@@@@@@@::::: ::@@@@@@@@@@@@@# | @@@::@ @@@@@ @@ @@ @@ @@@@@@@@@@::::: ::@@@@@@@@@@@@@# | @@@@ @::@ @@@@@ @@ @@ @@ @@@@@@@@@@::::: ::@@@@@@@@@@@@@# | @@@@@@ @ @::@ @@@@@ @@ @@ @@ @@@@@@@@@@::::: ::@@@@@@@@@@@@@# | :::::@@@ @@ @ @::@ @@@@@ @@ @@ @@ @@@@@@@@@@::::: ::@@@@@@@@@@@@@# | :@@: :: @@@ @@ @ @::@ @@@@@ @@ @@ @@ @@@@@@@@@@::::: ::@@@@@@@@@@@@@# 0 +----------------------------------------------------------------------->Mi 0 13.22
Pthread joining, minus the math busy work:
KB548.8^ # | @@#:: | :::@@#:: | ::::::@@#::: | ::::: :::@@#::::: | @@@@::::: :::@@#::::: | @@@@@ ::::: :::@@#::::::: | :@@:@@@@@ ::::: :::@@#:::::::@ | @@@:@ :@@@@@ ::::: :::@@#:::::::@ | :::@ @:@ :@@@@@ ::::: :::@@#:::::::@:: | @@@:: @ @:@ :@@@@@ ::::: :::@@#:::::::@:: | @@:@ @:: @ @:@ :@@@@@ ::::: :::@@#:::::::@:: | @:::@@:@ @:: @ @:@ :@@@@@ ::::: :::@@#:::::::@:: | ::@@:: @@:@ @:: @ @:@ :@@@@@ ::::: :::@@#:::::::@:: | ::@: @@:: @@:@ @:: @ @:@ :@@@@@ ::::: :::@@#:::::::@:: | :@@::@: @@:: @@:@ @:: @ @:@ :@@@@@ ::::: :::@@#:::::::@:: | @@@:@ ::@: @@:: @@:@ @:: @ @:@ :@@@@@ ::::: :::@@#:::::::@:: | @@@@@ :@ ::@: @@:: @@:@ @:: @ @:@ :@@@@@ ::::: :::@@#:::::::@:: | @@@:@@@@@ :@ ::@: @@:: @@:@ @:: @ @:@ :@@@@@ ::::: :::@@#:::::::@:: | @@@@ :@@@@@ :@ ::@: @@:: @@:@ @:: @ @:@ :@@@@@ ::::: :::@@#:::::::@:: 0 +----------------------------------------------------------------------->Mi 0 19.14
It looks like joining should eventually bring the stack size back down, even under valgrind.
Your code doesn't give the cleanups a chance to happen. When you call join
on a thread, it waits until the thread signals completion, not the actual release of all of its resources. If you create the threads more slowly or put a delay or yield in your loop, the "leak" will go away.