Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it make sense to implement a Garbage Collector in a Language with Garbage Collection

I want to implement a garbage collector (GC) in Java or Haskell, but does this make sense?

Will I be able to control when my own implementation of the GC kicks in and not the GC of the implementation language?

I guess that there can be three kinds of answers for this question:

  1. Set a flag when running the virtual machine (VM) that disables the GC of the implementation language.
  2. Use a special construct that lets me manage my own memory, like using continuation-passing style (CPS) to manage my own evaluation strategy.
  3. No, use a language with no GC.

Looking at these:

  1. I prefer the second option since I will be able to use that construct in all languages.
  2. I don't like the first option because I'll have to manage the memory of my interpreter as well.
  3. (And I don't prefer the third option neither, but I can't control that)

This is not a duplicate of I build a interpreter on a language with a garbage collector. I need a garbage collector for the interpreter? as I do not want to bootstrap my interpreter with the underlying GC.

like image 982
Little Helper Avatar asked May 04 '17 17:05

Little Helper


People also ask

Which language has best garbage collection?

Lisp is especially notable as both the first functional programming language and the first language to introduce garbage collection. Other dynamic languages, such as Ruby and Julia (but not Perl 5 or PHP before version 5.3, which both use reference counting), JavaScript and ECMAScript also tend to use GC.

Is garbage collection good in programming?

Any programmer that has had to manually manage memory knows the value of automatic memory management, also known as garbage collection. Garbage collection prevents several potential bugs that occur in manually managed memory, such as dangling pointers, double free errors, and several types of memory leaks.

In which language is garbage collected automatically?

Languages like Java and SML provide automatic garbage collection : the system automatically identifies blocks of memory that can never be used again by the program, and reclaims their space for use by later allocations.

What language has no garbage collection?

This is one of many reasons why languages like Java and C# are slower than C and C++ by design. And it is also the reason why C and C++ don't have and never will have a garbage collector, since those languages prioritize execution speed.

What is garbage collector in Java?

Garbage collector destroys these objects. The main objective of Garbage Collector is to free heap memory by destroying unreachable objects. The garbage collector is the best example of the Daemon thread as it is always running in the background. How Does Garbage Collection in Java works?

What is the simplest way to implement a garbage collector?

The simplest way to implement a garbage collector is: Make sure you can collate the global roots. These are the local and global variables that contain references into the heap. Make sure you can traverse the heap, e.g. every value in the heap is an object that implements a Visit method that returns all of the references from that object.

Is garbage collection a standard feature?

In the world of new languages it seems like garbage collection is standard feature. A way for the runtime to locate unused bits of memory and release them. It has become so common that some people can’t even imagine using a language without it.

When to use garbage collection in C++?

Thus any portion of your code which frees up large blocks of memory is a good candidate for running manual garbage collection. Invoking the garbage collector manually during the execution of a program can be a good idea on how to handle memory being consumed by reference cycles.


2 Answers

As so often in IT, the answer is: it depends.

When your interpreter keeps control of all "objects" within your context; then of course, your interpreter will probably keep a list of all "its" objects. So even when those objects would somehow be visible to the JVM, they would all be reachable, thus alive; so they could not be subject to JVM gc.

But when you somehow "embed" the objects that your interpreter deals with into the surrounding java context - then the JVM gc could be responsible for them.

Coming from there, the answer would go into the direction: yes, this could be possible. But beyond that; it really depends on your goal and driving requirements. Are you implementing this for pure education purposes; or are you interested in creating some sort of "real" product that is of "real" value to other people?

If the later is the case, then you probably want to embed your interpreter very tightly with the JVM - in order to benefit from the massive investment that turned the JVM into the great platform it is today. You see, almost 20 years of research went into current JVM JIT compiler and GC technology. Do you want to take advantage of that in order to "do your own thing" ...

So, as outlined: it very much depends on your "real" goals.

Finally: you might find this SE-Radio podcast on JRuby and the JVM platform helpful in the context of your question. The JRuby folks were simply not happy with the performance of the standard Ruby engine; and they choose the JVM as platform to build a better Ruby engine ...

like image 104
GhostCat Avatar answered Oct 21 '22 18:10

GhostCat


Makes sense for what? If you want to implement your own GC just to learn the algorithms involved, it doesn't matter. A GC basically just manages resources that reference each other, until they don't. That's perfectly doable in Java and other GCed languages. Instead of freeing the memory, you'd just call another method on the object and remove it from a list of 'living' objects.

For a shipping product? Not sure. You can take advantage of existing mechanisms in the implementation language, but a GC in particular usually needs control over when memory should be relinquished. And if the implementation language prevents that, that's a problem.

like image 25
uliwitness Avatar answered Oct 21 '22 19:10

uliwitness