MessiahAndrw wrote:
Freeing memory may take a while, but as technology has advanced, it may be feseable to do so, and it removes all possibilities of fragmentation.
When freeing memory, the pointers above the freed memory will be rendered invalid, I haven't thought of a solution to this problem yet.
I've worked with several embedded systems that use a similar approach, but couple it with a lock/unlock pointer system.
For example:
MemoryHandle getVideoFrameBuffer();
Would return an opaque pointer. You can't really do anything with it until you lock it:
void * lockHandle(MemoryHandle);
You can then access this memory directory and, when done, unlock it:
unlockHandle(MemoryHandle);
The trick here is that, after unlocking, the pointer returned by lockHandle() can now, no longer, be considered valid. This is because any unlocked handle can be moved anywhere inside memory without the users/clients knowledge.
With a system like this, as long as the user keeps unused handles in the unlocked state, they can be moved in order to prevent fragmentation.
Generally speaking, these systems will only perform memory block movement when they're running low on memory, or when an allocation fails because there isn't a large enough contiguous block, but there is enough free memory available.
And btw, the second garbage collection method mentioned below looks to be "mark & sweep," upon a very quick scan of the description. This is a fairly common GC method.
However, keep in mind that GC in C is more difficult then in Java. In Java everything is accessed via an indirect object handle and the stack is managed by the vm itself. In C, memory is accessed directly through pointers, and the stack can be managed by the application.
Trying to find if an object is referenced in the Java stack often involves iterating through all the active objects and their references. In C, it seems like you'd have to iterate through all the available registers, any buffers pointed to by the registers, and the same for the stack. On top of that, you have to be smart to notice values offset from the original pointer -- if a register contains (ptr + 4), you'll probably want to consider ptr as being referenced and not eligible for GC.
--Jeff