easlern

Hi guys- simple question. As I understand it, in XNA (C# in general ) you can Dispose() of objects or just deref them and let the GC clean them up. Which one is generally better for programming on 360 I would guess cleaning up objects when I'm sure I'm done with them would be better practice, but I don't know much about managed memory/GC scheme in compact framework, so I'd appreciate educated opinions from folks. Thank you!

Edit: by deref, I mean letting objects go out of scope or setting them to null (not "deref"erencing a pointer). Sorry for any confusion!



Re: XNA Game Studio Express 360: better to Dispose() or let garbage collector clean up?

AndyL

IDisposable and the associated method Dispose are part of a pattern in C# designed to deal with non-managed resources (file handles, GUI items, textures etc etc).

For instance, you have allocated a few 1024x1024 textures, weighing in at 3 Mb each. You would want to release the non-managed memory immediately, not when the system got around to finalising those objects (which by the way is not guaranteed to actually happen).

Whenever you see an object that implements IDisposable you want to be thinking about when you will dispose of the object yourself. For short term objects you might also want to look the 'using' keyword up, as you can use the following to ensure you dispose of the object when as soon as it goes out of scope:

using (MyObject ob = new MyObject())
{
// code that uses ob
}





Re: XNA Game Studio Express 360: better to Dispose() or let garbage collector clean up?

easlern

So would I be correct in assuming that if I were to dispose of objects I don't need anymore (before the GC finds them and cleans them up) I could reduce collection times by giving the GC less to do Or would that not convey an advantage (Long collection times are annoying so I'd rather free objects over time than have the GC free them all at once.)



Re: XNA Game Studio Express 360: better to Dispose() or let garbage collector clean up?

AndyL

No, it doesn't quite work like that. When unmanaged resources are used, the unmanaged part is wrapped in a managed object, which is the one you interact with. Dispose tells the managed object to release the unmanaged resources, but the managed object will be garbage collected normally.

Given the question, it sounds to me like you really need a better understanding of how a managed heap and collector works before going any further - look up Rico Mariani's blog and read all he has to say about managed memory and how the generational and non-generational (i.e. XBox) collectors work.





Re: XNA Game Studio Express 360: better to Dispose() or let garbage collector clean up?

mpswaim

If you have an object that implements IDisposable, then it has to be disposed before the garbage collector can clean it up. In the regular framework (not sure about the compact framework), this is done by a low priority thread. So, assuming that your application is busy doing stuff, the low priority thread may not run very often, and the disposable objects will stick around a lot longer than they should, creating more work for the garbage collector, and increasing your app's memory usage. So, you should dispose of objects yourself, when possible.

One option you should look at is the C# using keyword. The syntax is using (foo bar = ...){/*your code goes here*/} This will automatically dispose of bar when it goes out of scope, so you don't have to worry about code paths or disposing of it in case of an exception.