gcc下使用tcmalloc(gperftools)2.4的注意事项

前几天在折腾项目代码的编译问题,打算使用tcmalloc内存池来管理内存分配。无意中在gperftools的说明文档README中看到了这段话:

NOTE: When compiling with programs with gcc, that you plan to link
with libtcmalloc, it’s safest to pass in the flags

-fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free

when compiling. gcc makes some optimizations assuming it is using its
own, built-in malloc; that assumption obviously isn’t true with
tcmalloc. In practice, we haven’t seen any problems with this, but
the expected risk is highest for users who register their own malloc
hooks with tcmalloc (using gperftools/malloc_hook.h). The risk is
lowest for folks who use tcmalloc_minimal (or, of course, who pass in
the above flags :-) ).

说得很明白,当使用gcc来编译项目时,建议加上-fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free这四个编译选项。
看编译选项的名字就知道,这是阻止编译器优化时使用内置版本的malloc,calloc,realloc,free函数。
gcc在优化的时候,假设是使用自己的内置(built-in)的内存管理函数,这种假设在使用tcmalloc时就成问题了。虽然实际应用中目前没有发现任何问题,但还是存在预期风险,所以加上这四个选项是最安全的一种做法。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值