2012年tcmalloc学习笔记之二

<style type="text/css"> <!-- @page {margin:2cm} h3 {margin-bottom:0.21cm} h3.western {font-family:"Liberation Sans",sans-serif} h3.cjk {font-family:"文泉驿正黑"; font-style:normal} h3.ctl {font-family:"Lohit Hindi"} h2 {margin-bottom:0.21cm} h2.western {font-family:"Liberation Sans",sans-serif; font-size:14pt; font-style:italic} h2.cjk {font-family:"文泉驿正黑"; font-size:14pt; font-style:italic} h2.ctl {font-family:"Lohit Hindi"; font-size:14pt; font-style:italic} pre.western {font-family:"DejaVu Sans Mono",monospace} pre.cjk {font-family:"文泉驿等宽正黑",monospace} pre.ctl {font-family:"Lohit Hindi",monospace} h1 {margin-bottom:0.21cm} h1.western {font-family:"Liberation Sans",sans-serif; font-size:16pt} h1.cjk {font-family:"文泉驿正黑"; font-size:16pt; font-style:normal; font-weight:bold} h1.ctl {font-family:"Lohit Hindi"; font-size:16pt; font-weight:bold} p {margin-bottom:0.21cm} --> </style>

2012tcmalloc学习笔记之二

一、tcmalloc两种方式对malloc等内存分配和释放函数进行截获并提供服务。

A.环境变量的方式,意即通过环境变量LD_PRELOAD

<style type="text/css"> <!-- @page {margin:2cm} p {margin-bottom:0.21cm} a:link {} --> </style>

环境变量LD_PRELOAD指定程序运行时优先加载的动态连接库,这个动态链接库中的符号优先级是最高的。

标准C的各种函数都是存放在libc.so的文件中,在程序运行时自动链接。使用LD_PRELOAD后,自己编写的malloc的加载顺序高于glibc中的malloc,这样就实现了替换。

1.如何使用,意即指定动态库的路径即可

exportLD_PRELOAD="/usr/local/lib/libtcmalloc.so"

B.直接调用动态链接库

1.在程序中如何使用tcmalloc

 #if defined(USE_TCMALLOC)
 #define malloc(size) tc_malloc(size)
 #define calloc(count,size) tc_calloc(count,size)
 #define realloc(ptr,size) tc_realloc(ptr,size)
 #define free(ptr) tc_free(ptr)
 #endif

2.程序编译说明

编译的时候在Makefile增加编译开关USE_TCMALLOC即可

二、编译出来安装的libtcmalloc.so动态库放在什么地方

/usr/local/lib/libtcmalloc.so

三、如何检测tcmalloc是否正在工作

使用命令:

lsof-n | grep tcmalloc


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值