Chapter 16: Garbage Collection

Memory management is one of the most important aspects of modern programming, and almost any language that you invent should provide automatic memory management via garbage collection. This chapter presents a couple of methods with which you can implement garbage collection in your language. The first method, called reference counting, is easy to implement and has the advantage of freeing memory as you go. However, reference counting has a fatal flaw. The second method, called mark-and-sweep collection, is a more robust mechanism that is much more challenging to implement, and it has the downside that execution pauses periodically for however long the garbage collection process takes. These are two of many possible ...

Get Build Your Own Programming Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.