In this popular question about why substring takes O(n) in C#, one of the main answers provided argued that if a large array were allocated and substrings computed by having the new strings just reference a small slice of the array, the garbage collector would not be able to reclaim the array of characters containing the larger string even if the original string were no longer being referenced.
This seems like a perfectly valid answer, but it seems like in theory one could construct a garbage collector for arrays that allowed for most of an array to be garbage collected while leaving behind some small subarray that’s still in use. In other words, if there were a 50,000-element array of which only a small 100-element slice was still in use, the garbage collector could split the array into three pieces – the elements before the 100-element slice, the 100-element slice itself, and the elements after the 100-element slice – and then garbage collect the first and last of these pieces.
My question is whether any language implementations actually use this sort of garbage collector, or whether it exists only in theory. Does anyone know of an example of a language implementation that has an garbage collector like this?
In theory, yes… it is possible. But there is a problem with GCs:
to collect the garbage, it needs to know the layout of the data
being stored in memory, and it must also store data to indicate if
a memory block is in use or not… but the layout information is shared
with the runtime, because the runtime needs to know object types
(i.e. memory layout) in order to do type-casts.
How does GC works?
The GC starts reading the root objects it knows. It gets all the references
and mark them as being in-use. For each of these referenced objects, it gets
the layout and reads more references from theses ones, and marks them
as in-use… and this process continues, until no more references remain.
Notes: I have used type information and layout information, with the same meaning.
Example:
What I described is a very basic GC algorithm.
Take a look at Tri-color marking… that is really awesome!
This is how real modern GC are made.
Garbage collection (computer science) – describes some modern GC methodologies.
But… where is the information about layout stored?
This question is important, because it impacts both the GC and the runtime.
To do fast type casting the type information must be placed near the reference,
or near the allocated memory. We could think to store the type information
in the catalog of allocated memory blocks, but then… type-casts would need
to access the catalog, just like the new operator and the GC when it needs to
delete the object.
If we store the layout information near the reference, then every reference to
the same object would have the same information repeated, along with the pointer
itself.
Example:
If we store the layout information near the allocated memory block, then it is nice!
It is fast, and avoids repeated layout information.
Example:
So far, so nice… but now I want shared memory.
The first thing we notice is that we cannot store the layout information near the
allocated memory anymore.
Imagine an array with shared memory:
Example:
We can still try to place the layout information next to the pointer,
instead. The shared memory array is now possible:
Example:
Remember that this aproach makes us repeat the layout information everywhere…
but the point here is to use less memory isn’t it??? But to share memory, we need
more memory to store the layout-data/pointers. No donuts for us yet. =(
There is only one hope: lets degrade the runtime features!
THIS IS MY ANSWER – How I think it could be possible =>
What about using the memory allocation catalog, to store type informations?
This could be done, but then, dynamic casting would suffer, and also GC would suffer itself.
Remember I told that GC need to access the memory catalog, just to delete objects…
well, now it would need to access the catalog everytime it finds a reference,
not just to delete. OMG!! We are about to kill GC performance with this, and also the runtime performance. Too high cost I think!
<= THIS IS MY ANSWER
But… and if the runtime does not support dynamic casting? if the compiler knows
everything about a type at compile time… then GC would not even exist… it NEEDs the
information about the type, because this information tells it what is the layout of the
memory used by that type.
No easy, smart solution, in sight.
Maybe I am just plain wrong. But I cannot imagine a way to that better than it is already.
Modern GCs are even more complicated than this… I have covered only the basics here,
I think that, modern GCs are optimizing in other ways, that is, other more reliable ways.
Other references:
http://en.wikipedia.org/wiki/Garbage_collection_(computer_science)
http://www.memorymanagement.org/glossary/t.html
http://www.cs.purdue.edu/homes/sbarakat/cs456/gc-2.pdf
Tri-Color Incremental Updating GC: Does it need to scan each stack twice?