Skip to content
GitHub Repository Forum RSS-Newsfeed

Garbage Collector

Juan Wajnerman

Finally Crystal will start giving some memory back to the operating system! Today we managed to fit the Boehm-Demers-Weiser conservative garbage collector into the language.

Although we plan to implement a more appropriate and custom garbage collector in the future, it’s a really good starting point to make the language more robust and usable.

In order to make this collector work with Crystal we had to make sure all the allocated block pointers were properly aligned in memory. Unions and type hierarchies were using packed structs and that made some pointers “invisible” to the GC and thus many blocks still in use were being deallocated and consecuently making everything crash quite easily.

Some quick tests reflect the obvious benefits of freeing some memory. For example, samples/ used to require around 13MB of memory to run. Once the GC is enabled it uses just under 1MB.

There is still a long path to travel, but now with a working memory manager we might consider start dogfooding Crystal for some non production or critical tools in our everyday work.