4 ms·
Limit Memory Allocation (if not necessary) Multithreaded programs often do not scale because the heap is a bottleneck. When multiple threads simultaneously al
by egberts1 7y ago
Limit Memory Allocation (if not necessary)
Multithreaded programs often do not scale because the heap is a bottleneck.
When multiple threads simultaneously allocate or deallocate memory from the allocator, the allocator will serialize them. Programs making intensive use of the allocator actually slow down as the number of processors increases.
Malloc (libc) is the worse memory allocation API to use.
Programs should avoid, if possible, allocating/deallocations memory too often and in particular whenever a packet is received.
In the Linux kernel there are available kernel/driver patches for recycling skbuff (kernel memory used to store incoming/outgoing packets).
Using PF_RING (into the driver) for copying packets from the NIC to the circular buffer without any memory allocation increases the capture performance (around 10%) and reduces congestion issues.
Design Evolution
Basic design of malloc() is to dynamically pre-allocate a pool of memory from the OS in which applications can then take smaller pieces from. malloc() is a standard API having a choice of different allocation algorithms and to mitigate the expensive OS system calls (typically done at program initialization time) during allocation of its system memory. The first memory allocation scheme started with a stack-based memory allocation.
Next came the dynamic-based memory allocation scheme where linked-list and bucket-heap mechanism are used to divide the private-heap using size class approach.
Soon, garbage collection algorithm introduced the initial backend of the memory allocation scheme. Frontend covers the usual malloc() API, et. al.
In 2006, a third pool was introduced (after operating system memory pool and library-based memory pool) called the “arena”. Arena is a jemalloc-term and is intended to deal with different memory types such as different-speed memory bank or NUMA-architecture, as well as memory tied to specific to each of the multiple CPU core or even CPU infinity.
Frontend Evolution
Frontend manages the memory being given to the application.
Within the frontend of the memory allocation system, the evolution went in the following order:
- link-list free space
- heap-bucket size classes (eliminating an object header)
- (Process) Owner encoding
- single core local allocation buffers (CLABs)
- Epoch encoding
- Large-size class memory block by direct mmap()
Hazard pointers (safe memory reclamation for lock-free objects) (M.M. Michael, 2004)
- Arena memory pool
- thread-specific local allocation buffers (TLABs)
- constant-time modulo synchronization (early return to OS pool, or FreeBSD madvise call)
Backend Evolution
Backend of the memory allocation system manages the empty, straggling, fragmented or no-longer used memory blocks back to the OS (thereby reducing RSS).
- Pool semantic: Remote f-list encoding, using Treiber stack), (R.K. Treiber,
1986)
- buddy algorithm
- binary buddy algorithm
- BIPOP Table (span-based allocator)(S. Schneider, 2006) aka local free list and
- remote free list
- segment queue (Quasi-linearizability, Y. Afek, 2010)
- multi-core distributed queue (A. Haas, 2013)
- k-FIFO queue (T.A. Henzinger, 2013)
- rsecora 7y ago100% agree. You answer shall be read by every engineer trying to improve performance in intensive memory multithread code.
- scott_s 7y ago> When multiple threads simultaneously allocate or deallocate memory from the allocator, the allocator will serialize them. Memory allocators designed for multithreaded use will not serialize allocations and frees. Such allocators use thread-local data structures and try to avoid touching non-thread-local structures that require synchronization. I am "S. Schneider, 2006" and that was one of the main points of that paper. Modern memory allocators (tcmalloc, jemalloc, even I believe modern glibc) follow similar designs.
- egberts1 7y agoAye, such evolution tracking we must do for the history of malloc; Thanks, Scott. Evolution doesn’t always means most efficient or better. :-)