Forget borrow checkers: C3 solved memory lifetimes with scopes

https://news.ycombinator.com/rss Hits: 19
Summary

Modern languages offer a variety of techniques to help with dynamic memory management, each one a different tradeoff in terms of performance, control and complexity. In this post we’ll look at an old idea, memory allocation regions or arenas, implemented via the C3 Temp allocator, which is the new default for C3. The Temp allocator combines the ease of use of garbage collection with C3’s unique features to give a simple and (semi)-automated solution within a manual memory management language. The Temp allocator helps you avoid memory leaks, improve performance, and simplify code compared to traditional approaches. Memory allocations come in two broad types stack allocations which are compact, efficient and automatic and heap allocations which are much larger and have customisable organisation. Custom organisation allows both innovation and footguns in equal measure, let’s explore those. Memory Leaks When we dynamically allocate memory, with say malloc() typically we need to free() it afterwards, otherwise we can’t use that memory until the OS process exits. If that memory isn’t being used and it has not been freed this is called a memory leak and can lead to restricting or running out of available system memory. Avoiding Memory Leaks Common solutions are RAII, reference counting or garbage collection which automatically free those variables. Each method has different tradeoffs. RAII needs classes or structs to manage their own memory cleanup with a lot of extra code. Reference counting counts how many users each memory allocation has, when this hits zero the memory is freed. Reference counting is expensive with multiple CPU cores as we need to synchronise these counts and share information between cores. Garbage collection competes with the program for both memory and CPU time, reducing program performance and increasing memory usage. Memory Allocation Regions Memory allocation regions, go by various names: arenas, pools, contexts; The idea dates back to the 1960’s ...

First seen: 2025-07-13 14:55

Last seen: 2025-07-14 12:59