在现有代码中替换新的内存分配器的最佳解决方案是什么? [英] What is the best solution to replace a new memory allocator in an existing code?

查看:164
本文介绍了在现有代码中替换新的内存分配器的最佳解决方案是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最近几天,我获得了一些关于内存分配器的信息,而不是标准的 malloc()。对于具有多个线程的应用程序,有一些实现似乎比 malloc()好得多。例如, tcmalloc ptmalloc 似乎有更好的性能。



我有一个C ++应用程序在许多地方使用 malloc new 运算符。我认为用 ptmalloc 替换它们可以提高它的性能。但我不知道如何在运行在Linux上的C ++应用程序中使用 new 操作符?是否使用 malloc 或其他标准行为?



c $ c> new 内存分配器与旧的在代码中?是否有任何方式重写行为或 new malloc 或者我需要替换它们的所有调用由 .htmlrel =nofollow> TCMalloc文档


要使用TCmalloc,只需将tcmalloc链接到您的应用程序-ltcmalloc链接器标志。
您可以通过使用LD_PRELOAD在未编译的应用程序中使用tcmalloc:



$ LD_PRELOAD =/ usr / lib / libtcmalloc.so / p>

ptmalloc似乎是类似的(但如果你在Linux上,你可能已经在使用它了因为它是GNU C库的一部分)。



我期望 $ new $ 调用 malloc ,但您可以通过在 malloc ,然后调用 new 。如果你的 new 没有调用 malloc 你可以重新定义它,以便它


During the last few days I've gained some information about memory allocators other than the standard malloc(). There are some implementations that seem to be much better than malloc() for applications with many threads. For example it seems that tcmalloc and ptmalloc have better performance.

I have a C++ application that uses both malloc and new operators in many places. I thought replacing them with something like ptmalloc may improve its performance. But I wonder how does the new operator act when used in C++ application that runs on Linux? Does it use the standard behavior of malloc or something else?

What is the best way to replace the new memory allocator with the old one in the code? Is there any way to override the behavior or new and malloc or do I need to replace all the calls to them one by one?

解决方案

From the TCMalloc documentation:

To use TCmalloc, just link tcmalloc into your application via the "-ltcmalloc" linker flag. You can use tcmalloc in applications you didn't compile yourself, by using LD_PRELOAD:

$ LD_PRELOAD="/usr/lib/libtcmalloc.so"

ptmalloc seems to be similar (but if you're on Linux, you're likely already using it because it's part of the GNU C library).

I would expect operator new to call malloc, but you can easily check for yourself by setting a breakpoint on malloc, then calling new. If your new doesn't call malloc, you can redefine it so that it does.

这篇关于在现有代码中替换新的内存分配器的最佳解决方案是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆