In my program, I found that the memory would continue to increase under multi-threading, and I suspected a memory leak.
In order to verify this problem, I clone this repository and wrote a gtest test case:
#include <vector>
#include <memory>
#include <thread>
void gen_traceid() {
for (int i = 0; i < 10; ++i){
Random::GenerateRandom64();
}
}
TEST(RandomTest, MemoryMultiThreadGenerateRandom64) {
printf("memory leak test");
while (1)
{
std::vector<std::shared_ptr<std::thread>> vcths;
for (int i=0; i< 10; ++i)
{
std::shared_ptr<std::thread> th(new std::thread(gen_traceid));
vcths.push_back(th);
}
for (int i = 0; i < 10; ++i) {
vcths[i]->join();
}
vcths.clear();
}
}
I wrote it in sdk/test/common/random_test.cc, compiled it to generate random_test, and used ./random_test to run it. Then I used the top command to observe that its memory usage will continue to increase.
in the code. When a new thread is created, a new TlsRandomNumberGenerator() instance will be created, and the constructor of this class will execute ::pthread_atfork(prepare , parent, child); Memory continues to increase due to multiple executions of pthread_atfork .
I tried to delete platform::AtFork in the TlsRandomNumberGenerator() constructor function, and the memory usage was normal and would not increase.
In my program, I found that the memory would continue to increase under multi-threading, and I suspected a memory leak.
In order to verify this problem, I clone this repository and wrote a
gtest
test case:I wrote it in
sdk/test/common/random_test.cc
, compiled it to generaterandom_test
, and used./random_test
to run it. Then I used thetop
command to observe that its memory usage will continue to increase.The reason for this problem is that there is
in the code. When a new thread is created, a new
TlsRandomNumberGenerator()
instance will be created, and the constructor of this class will execute::pthread_atfork(prepare , parent, child);
Memory continues to increase due to multiple executions ofpthread_atfork
.I tried to delete
platform::AtFork
in theTlsRandomNumberGenerator()
constructor function, and the memory usage was normal and would not increase.