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:
Looking at these:
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.
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.
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.
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.
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.
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?
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.
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.
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.
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 ...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With