Memory management in 40 major C projects, a survey

· The Consensus ·

3 min read Original article ↗

You are getting early access to this article as a subscriber. Your support makes articles like this possible. Thank you.

C is well known for requiring manual memory management with malloc and free. And while many major projects do actually manage memory like this, many projects also implement higher-level memory management primitives such as regions (where objects allocated within a region need not be manually freed, freeing a region frees all objects allocated within the region at once), reference counting (manually incrementing and decrementing outstanding references, freeing an object only when its references go to zero), scope-based cleanup using compiler extensions, and a number of major C projects even actually implement garbage collection (fully automatic memory management).

Programmers sometimes assume that projects famous for diligence lean away from purely manual memory management. But SQLite, famous for correctness, allocates 100% manually while cURL and Redis allocate mostly manually (97% and 85%), reference counting only some of their objects. Another surprising finding is that Linux and QEMU in the last few years have been starting to use scope cleanup rules (i.e. compiler extensions) to automatically clean up memory, such as Linux's __free(kfree) and QEMU's g_autofree.

The Consensus tracks and indexes code for over 400 major software infrastructure projects. This article surveys 40 of the C projects within this group, excluding test code, third-party code, and examples.

Majority allocation strategy#

To discover majority allocation we count allocation call sites (which is certainly a limited metric, not accounting for sub-region or size of allocation, etc.). seL4 is an exception to much of this article since it has no heap at all.

8 projects solely allocate with malloc, implementing no other general higher-level strategy for allocating. These projects are: brotli, FreeRTOS, HAProxy, libdeflate, lz4, SQLite, xz-utils, and zlib.

16 projects primarily, but not exclusively, allocate with malloc.

7 projects primarily, if not all exclusively, allocate with a region system. With region based allocators, objects are allocated within a region. Freeing the region frees all objects within the region. Sometimes regions can be nested. And sometimes you might still manually free an object allocated within a region.

And 5 projects primarily allocate with a garbage collector.

3 projects primarily allocate reference-counted objects.

ProjectReference Counting %Manual %Region %
CPython82172
Perl66340
PHP55414

All projects#

Across all surveyed projects, with links to their various allocation strategies by example.

Projectfree/mallocRegionGarbage CollectorReference CountsScope Cleanup
apache-http-serverapr_bucket_alloc, apr_bucket_freeapr_pool_create, apr_pool_clearnonenonenone
brotliBROTLI_ALLOC, BROTLI_FREEnonenonenonenone
citusstrdup, freepalloc0, MemoryContextResetnonenonenone
curlcurlx_calloc, curlx_freenonenoneCurl_creds_unlinknone
dqliteraft_malloc, raft_freenonenonelogReleasenone
emacsxmalloc, xfreenoneFconsnonenone
erlangerts_alloc, erts_freenoneHAllocerts_bin_releasenone
freertospvPortMalloc, vPortFreenonenonenonenone
gitxstrdup, freemem_pool_alloc, mem_pool_initnoneblame_origin_decrefnone
haproxystrdup, freenonenonenonenone
juliamalloc_s, freenonejl_gc_allocnonenone
libdeflatelibdeflate_aligned_malloc, libdeflate_aligned_freenonenonenonenone
linuxkzalloc_obj, kfreedevm_kzallocnonekref_put__free
llama-cpp (ggml C)calloc, freeggml_new_tensor_1d, ggml_freenonenonenone
luarealloc, freenoneluaS_newnonenone
lz4ALLOC, FREEMEMnonenonenonenone
neovimxstrdup, xfreearena_memdupz, arena_mem_freenonetv_dict_unrefnone
nginxngx_alloc, ngx_freengx_create_pool, ngx_destroy_poolnonenonenone
ocamlcaml_stat_alloc, caml_stat_freenonecaml_copy_doublenonenone
perlNewx, SafefreenonenoneSvREFCNT_decsv_2mortal
pgbouncermalloc, freenonenonestrpool_decrefnone
pgvectornonepalloc, MemoryContextDeletenonenonenone
phpemalloc, efreezend_arena_alloc, zend_arena_releasegc_collect_cycleszend_string_releasenone
postgislwalloc, lwfreepalloc, AllocSetContextCreatenoneshared_gserialized_unrefnone
postgresqlpg_strdup, pg_freepalloc, MemoryContextResetnonenonenone
pythonPyMem_Malloc, PyMem_Free_PyArena_New, _PyArena_FreePyObject_GC_NewPy_DECREFnone
qemug_new0, g_freenonenoneobject_unrefg_autofree
rediszmalloc, zfreenonenonedecrRefCountnone
rubyxmalloc, xfreeNEW_LABEL, compile_data_freeTypedData_Make_Structnonenone
sel4nonenonenonenonenone
sqlitesqlite3_mprintf, sqlite3_freenonenonenonenone
timescaledbnonepalloc, AllocSetContextCreatenonenonenone
tmuxxstrdup, freenonenoneserver_client_unrefnone
tree-sitterts_malloc, ts_freenonenonets_subtree_releasenone
universal-ctagseStrdup, eFreenonenonees_object_unrefnone
vimvim_strsave, vim_freenonegarbage_collectdict_unrefnone
xenxzalloc, xfreeGCSPRINTF, GC_INITnonenonenone
xz-utilslzma_alloc, lzma_freenonenonenonenone
zlibZALLOC, ZFREEnonenonenonenone
zstandardmalloc, freeZSTD_cwksp_reserve_aligned64, ZSTD_cwksp_initnonenonenone

Noticed a mistake? Have a question or comment? Write to the editor.