tcmalloc vs. jemalloc

from stackoverflow:

Q:

I have an application which allocates lots of memory and I am considering using a better memory allocation mechanism than malloc.

My main options are: jemalloc and tcmalloc. Is there any benefits in using any of them over the other?

There is a good comparison between some mechanisms (including the author's proprietary mechanism -- lockless) in http://locklessinc.com/benchmarks.shtml and it mentions some pros and cons of each of them.

Given that both of the mechanisms are active and constantly improving. Does anyone have any insight or experience about the relative performance of these two?

A:

If I remember correctly, the main difference was with multi-threaded projects.

Both libraries try to de-contention memory acquire by having threads pick the memory from different caches, but they have different strategies:

  • jemalloc (used by Facebook) maintains a cache per thread
  • tcmalloc (from Google) maintains a pool of caches, and threads develop a "natural" affinity for a cache, but may change

This led, once again if I remember correctly, to an important difference in term of thread management.

  • jemalloc is faster if threads are static, for example using pools
  • tcmalloc is faster when threads are created/destructed

There is also the problem that since jemalloc spin new caches to accommodate new thread ids, having a sudden spike of threads will leave you with (mostly) empty caches in the subsequent calm phase.

As a result, I would recommend tcmalloc in the general case, and reserve jemalloc for very specific usages (low variation on the number of threads during the lifetime of the application).


A:

I have recently considered tcmalloc for a project at work. This is what I observed:

  • Greatly improved performance for heavy usage of malloc in a multithreaded setting. I used it with a tool at work and the performance improved almost twofold. The reason is that in this tool there were a few threads performing allocations of small objects in a critical loop. Using glibc, the performance suffers because of, I think, lock contentions between malloc/free calls in different threads.

  • Unfortunately, tcmalloc increases the memory footprint. The tool I mentioned above would consume two or three times more memory (as measured by the maximum resident set size). The increased footprint is a no go for us since we are actually looking for ways to reduce memory footprint.

In the end I have decided not to use tcmalloc and instead optimize the application code directly: this means removing the allocations from the inner loops to avoid the malloc/free lock contentions. (For the curious, using a form of compression rather than using memory pools.)

The lesson for you would be that you should carefully measure your application with typical workloads. If you can afford the additional memory usage, tcmalloc could be great for you. If not, tcmalloc is still useful to see what you would gain by avoiding the frequent calls to memory allocation across threads.


### 回答1: 这是一个编译错误,提示找不到jemalloc库的头文件。可能是因为你的系统没有安装jemalloc库,或者编译时没有正确指定jemalloc库的路径。你需要安装jemalloc库,并确保编译时正确指定了jemalloc库的路径。 ### 回答2: 这个错误信息表明在编译过程中,系统找不到jemalloc的头文件jemalloc.h,导致编译失败。jemalloc是一个专门用于内存管理的库,一般用于替换操作系统的默认malloc库,从而提高程序的性能和稳定性。 解决这个错误,需要检查以下几个方面: 1. 是否安装了jemalloc库:如果没有安装jemalloc库,可以通过以下命令安装库: sudo apt-get install libjemalloc-dev 2. 是否设置了正确的头文件路径:在编译命令中需要指定jemalloc库的头文件路径,例如: gcc -o test test.c -I/usr/include/jemalloc 3. 是否引入了正确的头文件:在程序中需要引入正确的头文件,例如: #include <jemalloc/jemalloc.h> 如果上述三方面都已经检查了,仍然出现找不到头文件的错误,可以尝试卸载并重新安装jemalloc库,或者升级gcc等开发工具。 ### 回答3: 这个错误提示表明编译器在编译时无法找到 jemalloc 库的头文件。jemalloc 是一种内存分配器,用于管理大型的虚拟内存空间。在使用 zmalloc 库时,如果缺少 jemalloc 库,则会出现这个错误。 要解决这个问题,可以尝试以下几种方法: 1. 安装 jemalloc 库。在 Linux 中,可以使用以下命令安装: ``` $ sudo apt-get install libjemalloc-dev ``` 在 Mac OS X 中,可以使用以下命令安装: ``` $ brew install jemalloc ``` 如果已经安装了 jemalloc 库,可以尝试将其安装路径添加到编译器的搜索路径中,例如: ``` $ export CFLAGS="-I/usr/local/include" $ export LDFLAGS="-L/usr/local/lib" ``` 2. 使用其他的内存分配器。除了 jemalloc,还有其他一些内存分配器可供选择,包括 tcmallocmalloc。可以尝试更换内存分配器,看是否能够解决问题。 3. 查看 zmalloc.h 源代码。有时候,头文件中的路径可能有误,导致编译器无法找到正确的库。可以打开 zmalloc.h 文件,检查头文件路径是否正确。 综上所述,解决 zmalloc.h 缺少 jemalloc/jemalloc.h 错误的方法有多种,可以尝试安装 jemalloc 库,更换其他内存分配器,或者检查 zmalloc.h 头文件路径是否正确。不同的方法可能适用于不同的操作系统和编译环境,需要根据具体情况进行选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值