bcc
bcc copied to clipboard
Bcc tools memleak can not identify memory leaks by new operator under tcmalloc library?
I used tcmalloc as a memory allocator, and then after new memory, it was not reclaimed and was not recognized by the bcc tool memleak.
// leak_lib.cpp
namespace LeakLib {
void slowMemoryLeak() {
int oneMbSize = 262144;
while (true) {
int* p = new int[262144];
for (int i = 0; i < 262144; ++i) {
p[i] = i; // Assign values to occupy physical memory
}
sleep(1); // wait for 1 second
}
}
}
// main.cpp
#include <iostream>
namespace LeakLib {
void slowMemoryLeak();
}
int caller() {
std::cout << "In caller" << std::endl;
LeakLib::slowMemoryLeak();
return 0;
}
int main() {
std::cout << "Starting slow memory leak program..." << std::endl;
caller();
return 0;
}
Makefile:
all: main
main: main.o libLeak.a
g++ -o main main.o -L. -lLeak -ltcmalloc -Wl,-rpath,/usr/local/lib -fno-omit-frame-pointer
main.o: main.cpp
g++ -c main.cpp -fno-omit-frame-pointer -g
libLeak.a: leak_lib.o
ar rcs libLeak.a leak_lib.o
leak_lib.o: leak_lib.cpp
g++ -c leak_lib.cpp -o leak_lib.o -fno-omit-frame-pointer -g
clean:
rm -f *.o *.a main
Here's the output of memleak
$ memleak -p $(pgrep main) --combined-only
Attaching to pid 1118961, Ctrl+C to quit.
[19:28:55] Top 10 stacks with outstanding allocations:
[19:29:00] Top 10 stacks with outstanding allocations:
[19:29:05] Top 10 stacks with outstanding allocations:
[19:29:10] Top 10 stacks with outstanding allocations:
When change to malloc, It behaves normally.
// namespace LeakLib {
// void slowMemoryLeak() {
// int oneMbSize = 262144;
// while (true) {
// // 使用 malloc 分配内存
// int* p = (int*)malloc(oneMbSize * sizeof(int));
// for (int i = 0; i < oneMbSize; ++i) {
// p[i] = i; // Assign values to occupy physical memory
// }
// sleep(1); // wait for 1 second
// // 注意:由于这是一个内存泄露示例,因此我们不调用 free(p);
// }
// }
// }
memleak uses libc as a default target object to attach uprobe.
You can use other library with --obj option.