Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between (and reasons to choose) tcmalloc/jemalloc and memory pools?

tcmalloc/jemalloc are improved memory allocators, and memory pool is also introduced for better memory allocation. So what are the differences between them and how to choose them in my application?

like image 947
Mickey Shine Avatar asked Mar 26 '12 03:03

Mickey Shine


People also ask

Why is jemalloc better?

The main benefit is scalability in multi-processor and multi-threaded systems achieved, in part, by using multiple arenas (the chunks of raw memory from which allocations are made). jemalloc really helped Aerospike take advantage of modern multithreaded, multi-CPU, multi-core computer architectures.

Why TCMalloc?

Specifically, TCMalloc provides the following benefits: Performance scales with highly parallel applications. Optimizations brought about with recent C++14 and C++17 standard enhancements, and by diverging slightly from the standard where performance benefits warrant. (These are noted within the TCMalloc Reference.)


1 Answers

It depends upon requirement of your program. If your program has more dynamic memory allocations, then you need to choose a memory allocator, from available allocators, which would generate most optimal performance out of your program.

For good memory management you need to meet the following requirements at minimum:

  1. Check if your system has enough memory to process data.
  2. Are you albe to allocate from the available memory ?
  3. Returning the used memory / deallocated memory to the pool (program or operating system)

The ability of a good memory manager can be tested on basis of (at the bare minimum) its efficiency in retriving / allocating and returning / dellaocating memory. (There are many more conditions like cache locality, managing overhead, VM environments, small or large environments, threaded environment etc..)

With respect to tcmalloc and jemalloc there are many people who have done comparisions. With reference to one of the comparisions:

http://ithare.com/testing-memory-allocators-ptmalloc2-tcmalloc-hoard-jemalloc-while-trying-to-simulate-real-world-loads/

tcmalloc scores over all other in terms of CPU cycles per allocation if the number of threads are less. jemalloc is very close to tcmalloc but better than ptmalloc (std glibc implementation).

In terms of memory overhead jemalloc is the best, seconded by ptmalloc, followed by tcmalloc.

Overall it can be said that jemalloc scores over others. You can also read more about jemalloc here:

https://www.facebook.com/notes/facebook-engineering/scalable-memory-allocation-using-jemalloc/480222803919

I have just quoted from tests done and published by other people and have not tested it myself. I hope this could be a good starting point for you and use it to test and select the most optimal for your application.

like image 189
ritesh sangani Avatar answered Sep 20 '22 19:09

ritesh sangani