This is more of a theoretical question. If jvm is implemented in go which itself is a garbage collected language, then does that jvm need a separate garbage collector to be implemented for its own operation?
- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
No, it can rely on the host language's garbage collector. Of course it has to be careful not to reference memory that is logically unreachable (unreachable from the guest program and not needed for the VM). But that is not fundamentally different from avoiding "logical memory leaks" (holding onto memory you don't need any more) in any other program written in the host language.
An example is RPython. Although it is unusual in that the RPython garbage collectors are written in (a low-level, manually-memory-managing subset of) RPython, all other RPython code acts almost entirely like an ordinary garbage-collected language. A VM written in RPython represents all guest language objects (and almost all objects needed only for its internal purposes) as garbage-collected RPython objects, rather than implementing a second GC only for the guest language. For example, in PyPy there is the
Yes, it does. The reason is that garbage collection can only reclaim inaccessible resources, but what is accessible depends on the level on which you view the system.
The host VM implements garbage collection to reclaim memory that the guest program used but has now released. In your case, the guest program is another VM, and its guests are application programs. When an application program uses and discards memory, it no longer has a handle to that memory, so it can never use the values stored in it again. That, however, is a condition that only the guest VM can check.
From the point of view of the host VM, all the memory that the guest VM has passed out to the application was first passed out by the host VM to the guest VM, and the guest VM still has a handle to that memory (it has to have a handle to it, otherwise it wouldn't even be possible for the guest VM to implement garbage collection). It follows that if the guest VM doesn't reclaim a block of memory, the host VM can't reclaim that memory either, resources will leak and you may eventually run out. In general, garbage collection or another form of resource reclaiming is necessary for every level of virtualization that involves a change of ownership of physical resources.