本文介绍了具有jemalloc的C ++ STL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在jemalloc(或任何其他malloc实现)中使用C ++ STL容器?

How is it possible to use C++ STL containers with jemalloc (or any other malloc implementation)?

像include jemalloc/jemalloc.h一样简单吗?还是我应该为他们写一个分配器?

Is it as simple as include jemalloc/jemalloc.h? Or should I write an allocator for them?

编辑:我正在使用的应用程序会在其生命周期内分配和释放相对较小的对象.我希望替换默认的分配器,因为基准测试表明该应用程序不能扩展到2个以上的内核.分析表明它正在等待内存分配,这就是导致扩展问题的原因.据我了解,jemalloc将对此有所帮助.

Edit: The application I'm working on allocates and frees relatively small objects over its lifetime. I want the replace the default allocator, because benchmarks showed that the application doesn't scale beyond 2 cores. Profiling showed that it was waiting for memory allocation, that's what caused the scaling issues. As I understand, jemalloc will help with that.

我想看到一个解决方案,该解决方案与平台无关,因为该应用程序必须在Linux和Windows上均可工作. (在Linux上,与其他实现的链接很容易,但据我所知,在Windows上很难.)

I'd like to see a solution, that's platform-neutral as the application has to work on both Linux and Windows. (Linking against a different implementation is easy under Linux, but it's very hard on Windows as far as I know.)

推荐答案

C ++允许您替换 operator new.如果此替换operator new调用je_malloc,则std::allocator将间接调用je_malloc,然后所有标准容器都将调用.

C++ allows you to replace operator new. If this replacement operator new calls je_malloc, then std::allocator will indirectly call je_malloc, and in turn all standard containers will.

这是迄今为止最简单的方法.编写自定义分配器需要编写整个类.替换malloc可能还不够(不能保证未替换的operator new会调用malloc),并且存在艾德里安·麦卡锡(Adrian McCarthy)先前指出的风险

This is by far the simplest approach. Writing a custom allocator requires writing an entire class. Replacing malloc may not be sufficient (there's no guarantee that the non-replaced operator new calls malloc), and it has the risks noted earlier by Adrian McCarthy

这篇关于具有jemalloc的C ++ STL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 07:43