ptmalloc 是glibc的内存分配管理
tcmalloc 是google的内存分配管理模块
jemalloc 是BSD的提供的内存分配管理
三者的性能对比参考从网上的一个图如下:
自己测试了一下:
代码如下:
1: #include
2: #include
3:
4: using namespace std;
5: int main(int argc, char**argv) {
6: cout << "Hello world" << endl;
7: std::map int_map;
8: for (int i = 0; i < 10000000; ++i) {
9: int_map.insert(std::map::value_type(i,i));
10: }
11: return 0;
12: }
都是执行一个map insert 100W次操作。
为了测试方便,我们生成了3个binary,分别链接使用jemalloc和tcmalloc 和ptmalloc的库做对比:
ldd jemalloc_test
linux-vdso.so.1 => (0x00007fffc6fc3000)
libjemalloc.so.1 => /usr/local/lib/libjemalloc.so.1 (0x00007f45181aa000) libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f4517ea4000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f4517c1e000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f4517a08000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f4517673000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f4517454000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f4517250000)
/lib64/ld-linux-x86-64.so.2 (0x00007f45183f2000)
ldd tcmalloc_test
linux-vdso.so.1 => (0x00007fff94160000)
libtcmalloc.so.0 => /usr/lib/libtcmalloc.so.0 (0x00007f20107dc000) libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f20104d6000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f2010250000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f201003a000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f200fca5000)
libunwind.so.7 => /usr/lib/libunwind.so.7 (0x00007f200fa8b000)
/lib64/ld-linux-x86-64.so.2 (0x00007f2010a68000)
ldd ptmalloc_test
linux-vdso.so.1 => (0x00007fff08501000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f7e6c156000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7e6bed1000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f7e6bcba000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7e6b925000)
/lib64/ld-linux-x86-64.so.2 (0x00007f7e6c483000)
然后分别执行各程序,使用time统计时间如下:
time./jemalloc_test
Hello world
real 0m9.927s
user 0m9.650s
sys 0m0.250s
time ./tcmalloc_test
Hello world
real 0m9.836s
user 0m9.410s
sys 0m0.410s
time ./ptmalloc_test
Hello world
real 0m11.890s
user 0m11.520s
sys 0m0.360s
jemalloc和tcmalloc的性能不分伯仲,而ptmalloc则要低一些。
这里简单介绍下性能。后面抽时间再介绍下各自的实现原理。