Search This Blog

Friday, April 8, 2011

Memory Management

This is an open reply to Patrick's comment, which was important enough that it really needs to be addressed sooner rather than later.

The architecture I'm putting together for the Fourier optics library allows a mesh of contexts to form, including cycles of contexts.  Patrick pointed out that if you try automatic memory management (garbage collection, reference counting, etc.) you can get into trouble when you have a cycle.  It can get MUCH worse if you include the fact that there may be multiple threads of control, multiple processes, and (worse) a distributed network of machines that have slow, unreliable connections to one another.  In short, we're talking about 'the cloud'... and all the problems that entails.  This is many, many PhD. dissertations in itself.  It is not a problem I intend to try to solve myself!

Instead, I'm going to require that users of the library be aware of the objects that they are allocating, and know when they should all be live, and when they are dead.  In short, I'm passing the buck to you, the end user.

The bit of good news is that every object in the framework has its own UUID; you can query any object for its UUID, and you will know that if two objects have the same UUID, then you're really talking about the same object.  In addition, since UUIDs are universally unique, it means that distributed garbage collection (garbage collection across a network of machines) becomes a real possibility.  I am not going to implement this, but if you have the urge to do so, go ahead.

A note about copying objects

Copying implies different objects.  That means that even if the objects are equivalent, they are two separate chunks of memory.  For this reason, I require that they have separate UUIDs.  This can become extremely confusing if anyone tries to implement proxy objects; after all, the proxy is a stand-in for the real object, so what does it mean to copy the proxy, or to delete it?  While this isn't a big deal if you only have one process, it becomes a much bigger problem if you want something like distributed objects that are shared across the network.  I have no idea how to handle this well.  Again, it's up to you, the end user to cook up something intelligent. 

Let me know if anyone has a good solution that works across multiple platforms; I'm not touching any of that!

1 comment:

  1. I am completely ok with this. Your focus should be on the CUDA implementation, not these issues. Wait until your library is famous for that. :)