7

I need some fast, thread safe memory pooling library. I've googled a lot, but fast solutions don't have thread safety, while others are really big.

Any suggestions?

Daniel
  • 4,272
  • 8
  • 35
  • 48
  • Why not just malloc()? It is too slow? Did you try some per-thread pool? It is faster, but waste some memory. – J-16 SDiZ Jan 25 '11 at 04:15
  • i have hundrends of malloc/free per second with small chunks (<500b), so yes, just free/malloc are really slow. i also would like to get some thread safe solution so i won't have headache about pool management in threads. – Daniel Jan 25 '11 at 04:36
  • Also consider arena memory management. If it is suitable for you it will be many times faster. – Joshua Jan 25 '11 at 18:37

4 Answers4

8

Both nedmalloc and ptmalloc are C based thread caching memory managers, based around doug lea's malloc(the core of most linux allocators). They are both under good licences as well, unlike hoard, which requires payment for commercial use, last I looked. Googles tcmalloc also has C bindings iirc, and is built from the ground up as a thread caching allocator, as well as some built in heap and cpu profiling tools, it is however build for massive memory usage(the example they give is 300mb+ per thread), and as such many not work as well as expected for smaller scale apps

Necrolis
  • 25,836
  • 3
  • 63
  • 101
1

You're supposed to use one memory pool per thread.

Joshua
  • 40,822
  • 8
  • 72
  • 132
  • 2
    Note that while this can help, there are still threading issues that need attention - while you can ensure that an allocation from a particular thread can be directed to a particular pool, you have to be prepared to free a block for any pool from any thread. – Michael Burr Jan 25 '11 at 04:28
  • why am i supposed to use separate memory pools for each thread? – Daniel Jan 25 '11 at 04:37
  • This can also lead to one thread arena reserving hundreds of megs of memory in its free store, starving other thread arena who actually need to allocate the memory – Necrolis Jan 25 '11 at 04:52
  • 1
    This permits high speed by not requiring a semaphore. In order to do this you need a whole-program optimization to ensure that anybody using the memory pool never deallocates via the wrong pool. – Joshua Jan 25 '11 at 16:11
  • This sounds like a great idea, but apparently there is no support for it in the C standard library. Do you know of any good pool allocator libraries for C? I was interested in talloc, but I would like something that does not add the overhead of dynamically calling destructors. – isekaijin Feb 19 '14 at 23:19
  • For some reason I assumed Windows. See CreateHeap, HeapAlloc, etc. – Joshua Feb 20 '14 at 03:29
0

The Apache Portable Runtime works well and shouldn't be all that big.

Raph Levien
  • 5,088
  • 25
  • 24
  • i've tried apr as first approach and it is really big + has too much api i won't use at all. – Daniel Jan 25 '11 at 04:36
0

Have you tried Hoard?

See also these two articles from Intel.com

J-16 SDiZ
  • 26,473
  • 4
  • 65
  • 84
  • i've seen it, but it seems to be more like a hack that bolts on existing application (LD_PRELOAD), while i need to have some control over pool (size, max size, current usage, etc...) – Daniel Jan 25 '11 at 04:39