1. 程式人生 > >gcc下使用tcmalloc(gperftools)2.4的注意事項

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時就成問題了。雖然實際應用中目前沒有發現任何問題,但還是存在預期風險,所以加上這四個選項是最安全的一種做法。

相關推薦

no