lllllllllllll
lllllllllllll

Reputation: 9130

How can runtime systems support "GC" on compiled binaries?

So basically I only know some basic concept of GC:(

I am new in functional programming language, and when I am studying the Haskell's runtime system, RTS, I found that RTS support GC for the compiled binary from Haskell.

So I am confused with this part, is there a separate process created by RTS that can do GC stuff towards Haskell binaries?

What's more, if so, is there any GC implementation for C/C++ ? (Suppose programmers only use kinda of "smart pointer" to use memory, and they don't need to care about memory management, GC process will take care of it)...? As far as I know, it seems that .net can work in this way... am I right?

Upvotes: 1

Views: 183

Answers (2)

Mikael Persson
Mikael Persson

Reputation: 18572

is there a separate process created by RTS that can do GC stuff towards Haskell binaries?

Garbage collection does not, in general, require a separate process (e.g., a "background" process), although some virtual-machine garbage collectors might do that (I don't know).

Think of the garbage collector as being merged with the heap allocator. Whenever you ask for some memory to be allocated, you invoke the heap to do so (which may hang for some time before finding a chunk to allocate). With a garbage collector, there is simply an additional step where the garbage collector first checks if it should do some collecting before allocating some memory.

What is central to garbage collection is the instrumentation (or annotation) of the memory to be able to infer which chunks of memory are still referred to / reachable. This kind of instrumentation simply implies additional operations during certain key points, like allocation, deallocation (if there is an explicit mechanism for it), and setting / getting pointer values (which are usually "hidden" under-the-hood in a garbage collected language, as opposed to languages like C/C++ that have "raw" pointers).

What's more, if so, is there any GC implementation for C/C++ ?

Yes, there are all sorts of GC implementations for C/C++. They are not very popular (AFAIK), probably because C is low-level enough that things are usually manageable, and C++ has RAII (and smart pointers) which almost-completely eliminates the need for GC.

But if you really want GC for C/C++, you can certainly get it. There are both non-deterministic GC libraries (similar to JVM or .NET) and deterministic GC libraries (similar to PHP or Python).

As far as I know, it seems that .net can work in this way... am I right?

Yeah, .NET like Java, is garbage collected. From a GC user's perspective, .NET is about the same as the JVM, as far as I know.

Upvotes: 4

user207421
user207421

Reputation: 310980

So I am confused with this part, is there a separate process created by RTS that can do GC stuff towards Haskell binaries?

No. Why would there be? You don't need a separate process to do GC. The Java JVM doesn't need a separate process. You seem to have some confusion somewhere. All you need is some code in the runtime library.

What's more, if so, is there any GC implementation for C/C++ ?

Yes.

As far as I know, it seems that .net can work in this way... am I right?

If you mean that .NET has GC you're correct. If you mean that it's done via smart pointers or a separate process you're wrong.

Upvotes: 3

Related Questions