logoalt Hacker News

gwbas1c • yesterday at 3:44 PM • 1 reply • view on HN

> Instead, what if we knew that we would have a period of time in our code, where we're going to allocate a lot of stuff, do something with it, and then free everything? A good example of this is a web request, or a database query.

> A request comes in, fills in some data structures, and then when the request finishes, the memory isn't needed anymore so it's dropped. This sort of "burst" pattern of allocation, where you allocate a lot up front, and then tear things down all at once at the end, is quite common.

FYI: Generational garbage collectors, such as the one used in the dotnet framework since the beginning (~2002), are very good at handling this.

Basically, the 0 generation starts as a large chunk of memory, and before a slow "mark and sweep" the reachable objects that are in the 0 generation are copied to the 1 generation. This implicitly deallocates unreachable objects in the 0 generation.

(Then again, there are a lot of tradeoffs to using a garbage collected language vs. Rust. I find that a program in Rust, vs an equivalent in C#, has a significantly lower RAM footprint.)


Replies

steveklabnik • yesterday at 3:53 PM

I wrote a blog post about this connection a while back: https://steveklabnik.com/writing/borrow-checking-escape-anal...

➕ show 1 reply