17 ms·
So a few questions on C and C++ memory allocation for the experts here. Will typical C++ new (and C malloc) give thread contention issues? Specifically I thin
by CyberDildonics 12y ago
So a few questions on C and C++ memory allocation for the experts here.
Will typical C++ new (and C malloc) give thread contention issues?
Specifically I think I have seen multi threading performance die when multiple tasks are finishing and doing lots of freeing at one time.
-Is it possible that is true, and if so, is that the sort of thing tcmalloc (and jemalloc?) fix?
-If they fix it, what is the best way to get that into C++ programs that use new instead of malloc (the vast majority through STL structures)?
- robmccoll 12y agoYes, simpler designs for malloc share free pool tracking structures among threads and use locks to protect against creating races. Other malloc implementations use thread local free pool caching structures to lessen the frequency of synchronization in the allocator in the general case. TC actually stands for 'thread caching'. Under the hood, the default std allocator being used to power new is typically calling malloc, so simply linking in a different implementation should work.
- rayiner 12y agoYes (to both--new usually calls malloc), but the degree to which depends on the platform. In the old days, there was a single lock around the allocator serializing malloc/free. These days, all the major platforms have a memory allocator that tolerates some degree of concurrency, and you can also link in tcmalloc or jemalloc. Even then, a lot will depend on your allocation patterns. For example, many threaded malloc implementations depend on thread-local caches to avoid having to take a lock on the fast path. This works great for many workloads, but can also fall flat on others. Consider what happens when a producer thread allocates memory, passes a pointer to a consumer thread which frees it. The thread-local cache in the producer thread will never be replenished by free() calls, and the thread-local cache in the consumer thread will never be used by malloc() calls. Thus performance can become very dependent on the mechanism the allocator uses to transfer memory to/from the local caches and the global heap.