4 ms·
Ask HN: When should I worry about implementing a garbage collector?
I am a novice programmer who is trying to implement a programming language as an effort to learn more about how things work below the hood. So, what I am confused about is when should I implement a GC? How do I even determine whether I need a GC or not? Since I am using rust, do I even need one? Rust has the ownership model, so do I need to worry about explicit memory management for my language or will that be an implementation detail that will be handled by the underlying rust code?
Also, as a side note, what are some good, free resources to learn more about compiler implementations?
- PaulHoule 9y agoAlthough C usually has manual memory allocation and deallocation (malloc and free), some C programs embed https://en.wikipedia.org/wiki/Boehm_garbage_collector https://en.wikipedia.org/wiki/Boehm_garbage_collector
- bradleyjg 9y agoI assume you mean you are using rust to implement your language rather than that you are implementing rust. If that's the case whether or not you need a garbage collector comes down to deciding whether or not you want users of your language to have to allocate memory manually or not. As for as when goes, it can come relatively late in the development of the language. Not having a garbage collector or any other way of freeing memory will make it likely that programs written in your language will run out of memory, but with so much memory on modern computers you'll be able to write more than trivial programs without running into that problem. A simple stop-the-world mark and sweep GC without any generations should be relatively easy to implement. https://blogs.msdn.microsoft.com/abhinaba/2009/01/30/back-to-basics-mark-and-sweep-garbage-collection/ https://blogs.msdn.microsoft.com/abhinaba/2009/01/30/back-to...
- rurban 9y agoNo, please don't make the mistake of adding another slow and bad mark & sweep GC. Do it properly with a simply copying collector. It can be much shorter than a mark & sweep also.
- gus_massa 9y agoHow similar is your language to rust? Does it has ownership like rust? does it have explicit allocation and dealocation like C/C++? Does it have automatic memory management like Java/Python/Javascipt? Good garbage collectors are difficult to implement and it's probably too much for an initial project. You can probably use Rc or Arc and hope nobody makes too many cycles before there is an official package in rust for a garbage collector. https://www.rust-lang.org/en-US/faq.html#is-rust-garbage-collected https://www.rust-lang.org/en-US/faq.html#is-rust-garbage-col... As the other comment says, in C you can use the Boehm garbage collector. I don't know if there is something similar for rust.
- deleted 9y ago[deleted]