-4

I'm currently implementing a compiler for a simple language as a side project in C++. I'd like to add a garbage collector. Most (all?) garbage collection algorithms involve stopping the world at some point.

I'm interested in knowing whether there's an efficient way to temporarily pause a set of threads from the parent thread. The only other option I see is periodically checking if the GC wants to run at the top of every function and loop. This strikes me as inefficient, because I'll have to lock and unlock each time, adding considerable overhead. Is there a more efficient way to do this?

  • It has moved on, background collections are a standard feature these days. It is a wheel, nobody likes a square one so don't reinvent one. – Hans Passant Aug 16 '16 at 21:52
  • Why use garbage collection at all? Using standard memory allocations with reference counted objects that free themselves when the last reference is cleared would work much faster and more efficient. For instance, see `std::shared_ptr`. – Remy Lebeau Aug 16 '16 at 21:53
  • @Remy Reference counting is pretty much *the* worst possible implementation of a GC imaginable. The only thing it's semi good at it latency if you accept memory leaks of cycles are involved. – Voo Aug 16 '16 at 22:46
  • 1
    The standard implementation in HotSpot (and the CLR as far as I'm aware) is actually pretty simple and efficient: Let threads access a page. If you want to drop the world make the page non readable and handle everything in the handler for the access violation. The cost is a cacheable read every few thousand instructions which is pretty much free. – Voo Aug 16 '16 at 22:48
  • @Remy - I did consider reference counting, but decided against it both for the added experience and for the reasons mentioned by Voo. – Thomas Redding Aug 17 '16 at 12:53
  • @Hans - correct me if I'm wrong, but doesn't background collection still require stoping threads to scan the stack? – Thomas Redding Aug 17 '16 at 12:53
  • @ThomasRedding correct me if I am wrong, but don't objects still need to be reference counted anyway to know when the last reference is cleared so the object can then be added to the garbage collector's queue of memory to free? It is massively inefficient to stop all threads and then check if every object is still alive or not. Objects tell a garbage collector when they are safe to be freed, even if that freeing is delayed to a background scan. – Remy Lebeau Aug 17 '16 at 15:02
  • Possible duplicate of [How GC suspends/blocks the application threads](http://stackoverflow.com/a/30692272/1362755) – the8472 Aug 17 '16 at 19:40

1 Answers1

-1

There's no decent way around it - you can't expect the C++ Standard Library to magically understand where your VM can be suspended. That logic is something you must write.

Now you can of course implement the rule as you described, or you can make an exception for leaf functions with less that N instructions, and check only every M'th loop iteration, etc. Up to you, really, there's no limit to the amount of brains you can pour into this.

MSalters
  • 173,980
  • 10
  • 155
  • 350