Memory Allocation

Problems

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 worst memory allocation API to use.

Programs should avoid, if possible, allocating/deallocating 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 (CPU/core and thread, separately)

- 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)

Competition

There are better ones out there that does not worsen as more threads/processes performs memory allocation system calls; they are listed in best-to-good performance order [seed with source]:

Comparison of malloc design

CAS, Atomic Contention Characteristics

CAS / Atomic Contention characteristics

NUMA, Memory Locality characteristics

NUMA / Memory Locality characteristics

Benchmark-Oriented Practical Performance

Benchmark-Oriented Practical Performance

Allocator Recommendation

Allocator Recommendation

Decision Chart for Malloc Selection

- [http://www.phrack.org/issues.html?issue=57&id=8#article]

- [https://sploitfun.wordpress.com/2015/03/04/heap-overflow-using-malloc-maleficarum/]

- [http://phrack.org/issues/66/10.html]

References

- R. J. Maher, Problems of storage allocation in a multiprocessor multiprogrammed system, Communications of the ACM, 4(10):421-422, October 1961

-

A fast storage allocator, Kenneth C. Knowlton, Communications of the ACM, 8(10):623-625, October 1965.

-

Statistical properties of the buddy system, P.W. Purdom and S. M. Stigler, Journal of the ACM, 17(4):683-697, October 1970

- Statistical investigation of three storage allocation algorithms, P. W. Purdom, S. M. Stigler, and Tat-Ong Cheam, BIT, 11:187-195, 1971.

- A note on an optimal-fit method for dynamic allocation of storage, J. A. Campbell, Computer Journal, 14(1):7-9, February 1971.

- Worst-case analysis of memory allocation algorithms, M. R. Garey, R. L. Graham, and J. D. Ullman, In Fourth Annual ACM Symposium on the Theory of Computing, 1972

- A class of dynamic memory allocation algorithms, D. S. Hirschberg, Communications of the ACM, 16(10):615-618, October 1973

- Dynamic storage allocations of arbitrary sized segments, J. S. Fenton and D. W. Payne, In Proc. IFIPS, pages 344-348, 1974

- Worst-case of Memory Allocation Algorithms, Garey 1972

- A simplified recombination scheme for the Fibonacci buddy system, B. Cranston and R. Thomas, Communications of the ACM, 18(6):331-332, July 1975.

- Buddy systems, J. L. Peterson and T. A. Norman, Communications of the ACM, 20(6):421-431, June 1977.

- Worst case fragmentation of first fit and best fit storage allocation strategies, J. M. Robson, Computer Journal, 20(3):242-244, August 1977.

- Fast-fit: A new hierarchical dynamic storage allocation technique, M. Tadman, Master’s thesis, UC Irvine, Computer Science Dept., 1978.

-

The double buddy-system, David S. Wise, Technical Report 79, Computer Science Department, Indiana University, Bloomington, Indiana, December 1978

-

Memory fragmentation in buddy methods for dynamic storage allocation, A. G. Bromley, Acta Informatica, 14(2):107-117, August 1980.

- Optimal fit of arbitrary sized segments, Ivor P. Page, Computer Journal, 25(1), January 1982.

- Parallelizing the usual buddy algorithm, A. Gottlieb and J. Wilson, Technical Report System Software Note 37, Courant Institute, New York University, 1982.

- Fast fits: New methods for dynamic storage allocation, C. J. Stephenson, In Proceedings of the Ninth Symposium on Operating Systems Principles, pages 30-32, Bretton Woods, New Hampshire, October 1983. ACM Press. Published as Operating Systems Review 17(5), October 1983.

- On the asymptotic optimality of first-fit storage allocation, E. G. Coffman, Jr., T. T. Kadota, and L. A. Shepp, IEEE Transactions on Software Engineering, SE-11(2):235-239, February 1985.

- Efficient implementation of the first-fit strategy for dynamic storage alloca- tion, R. Brent, ACM Transactions on Programming Languages and Systems, July 1989.

- Fast allocation and deallocation of memory based on object lifetimes, David R. Hanson, Software Practice and Experience, 20(1), January 1990.

- Dynamic Storage Allocation: A Survey and Critical Review, very useful chronological order of malloc(), 1995

-

A Memory Allocator, 2000

- Solaris mtmalloc (archived), 2003

-

A History of malloc, 2010

- Heap and allocators, 2015

- Understanding glibc malloc

- The Origins of Malloc, 2017