PDA

View Full Version : Reference-counting. Do you use it?



chronozphere
19-02-2011, 05:31 PM
Hey everyone,

I was just wondering if you guys often use reference-counting in your code instead of freeing things by hand. For those who are unfamiliar with it, it's a technique to keep track of the number of other objects that are using an object. As soon as that number becomes zero, the object will free itsself.

Interfaces use this technique aswell. If you create an object and pass an interface-reference to other parts of your program, the reference-count will be incremented. Once these references are "nilled", the reference-count will decrease again.

I'm deciding whether to implement reference-counted resources in my engine.Let me know what your thoughts about this are.

wagenheimer
19-02-2011, 05:38 PM
Hummm... I don't use this yet, but it will be very usefully! Do you have some simple example of how to implement something like this?

Murmandamus
19-02-2011, 05:40 PM
I rarely use it, and only in specific situations. It is primarily useful in cases where you have many-to-one relationships in your class/object hierarchy, and it does incur a small bit of overhead, so I avoid it in most cases where it wouldn't help anyway, sticking with explicit freeing instead.

If you are planning to implement it throughout your game engine, I'd recommend doing a fairly thorough cost/benefit analysis to see if it would be a good idea or not.

JSoftware
19-02-2011, 07:27 PM
Using interfaces can sometimes give strange bugs of some instances not getting freed, and you'll spend ages debugging it. So I'm usually very careful about how I manage them

But I do use ansistrings and dynamic arrays all the time. Those are delicious :)

WILL
19-02-2011, 08:07 PM
I don't think I've followed this technique to it's design, but in Garland's quest I've created 'Kill' flags in each of my game objects and had an external procedure cycle through all of my objects looking for it. Should it find it, it'll free up the object and any other of it's allocated 'innards'.

chronozphere
19-02-2011, 08:33 PM
I don't think I've followed this technique to it's design, but in Garland's quest I've created 'Kill' flags in each of my game objects and had an external procedure cycle through all of my objects looking for it. Should it find it, it'll free up the object and any other of it's allocated 'innards'.

That's not real reference counting. Looks more like a home-made garbage collection system to me (I use it too to clean-up all kinds of objects in my engine that were deleted during the "frame update" in question). :)



If you are planning to implement it throughout your game engine, I'd recommend doing a fairly thorough cost/benefit analysis to see if it would be a good idea or not.


Are you now referring to performance now? or is it something else?

I'm considering to use this for my resource-management (textures, shaders, models etc). I still have some doubts on whether I will actually make use of this feature (I'll use a boolean to enable ref-counting for a specific resource). For example, it would be annoying if a texture was needed somewhere, after it has been freed by the system because there were no refs left. Just loading everything at startup and breaking things down in the end seems as the easiest sollution. On the other hand, this would give overhead as the game might not use all the resources it potentially "could" use. ???

Andru
19-02-2011, 08:52 PM
I'm prefer full control, so I free memory myself(except Strings, of course :)). And as WILL I use kill-flag, but only for some objects(e.g. sprites in sprite engine).

Murmandamus
19-02-2011, 09:05 PM
Well, not just performance, but complexity and maintainability, too. Reference counting is a single method, usually part of a set of methods in garbage collection, so you'll likely be adding significant more complexity to the engine overall (for example, the caching issue you mentioned.. loading/freeing a constantly-used resource can exact a serious performance hit).

Making it optional is good feature-wise, but that will incur a small bit of overhead, too. Ultimately, it boils down to one question: how much of the garbage collection / caching work do you want your engine to do versus how much you want the programmer using your engine to do? Also, who is your target audience with your game engine? Advanced programmers are going to want more control and performance; beginners like not having to do a lot of "unnecessary stuff". If you can find a way to make both happy with it, then I'd do it; I'm just not sure doing both is feasible. Your mileage may vary, though. :)

User137
19-02-2011, 10:01 PM
I have usually a very clear master class or something that holds the objects and takes care of their freeing. It sounds a little odd concept to have X object floating somewhere in memory space used be other random objects.