- Home /
How important is it to wrap temp variables/objects in a "using" block?
I know that with C# garbage collection happens automatically. But I've read that it only happens when memory is needed. Therefore, your game might still be taking up a larger memory footprint than necessary a lot of the time.
So, I've read that one way to have some control over garbage collection is to wrap some code in a "using" block. Then whatever temporary variable or object is in that "using" statement will have its memory immediately released at the end of the block, rather than "at some time in the future".
I've shipped several games now, but I've never used the "using" keyword in this way. I'm wondering now if I should try to be more diligent about doing that. My instinct says that it doesn't matter all that much and just makes the code look messier. I'm wondering what others think.
Answer by nsejosh · Dec 09, 2012 at 01:46 AM
All "using" does is call Dispose on the included variables when the using block goes out of scope, assuming those objects implement IDisposable. It has nothing to do with triggering garbage collection whatsoever. If you're curious about whether "using" will affect your memory footprint for a certain object, check if it implements IDisposable and thus needs nonmanaged resources released.
Here is a nice clear explanation: http://www.dotnetperls.com/using
If you find any documentation to the contrary from a reliable source ( i.e. Microsoft or Mono ) please post it.
Answer by Owen-Reynolds · Dec 08, 2012 at 04:06 AM
Not an expert, but USING isn't for garbage collection.
It's for "special" things like files, to be absolutely sure the Close statement gets called in every single case. I'd guess that the actual memory used for the file variable isn't even GCed any faster than normal.
Since Unity wants you to use `Resource` to read files, hopefully they have all the correct USING's buried inside there.
hmm i guess i could have made that confusing
no your right using isnt primarily for GC but I have seen documentation explicitly note that GC is handled more efficiently if using using -its definitely one of its benefits
The reply above (and the microsoft page) say `using` is for non-managed resources. You might think "well, I probably have some of those," but read up what they are. You probably have none.
Stack Overflow has a few threads about this (comes up in google just below the microsoft page.) Some tell you to implement IDisposable, but then you read more on that and it just comes back to only mattering for non-managed resources.
from stack overflow on un-managed resources:
Open files
Open network connections
Unmanaged memory
In XNA: vertex buffers, index buffers, textures, etc.
hmmm so if you are making a GA$$anonymous$$E then you probably have a fair few of these, in other words, probably should use it when you can.
but lets think of this more realistically, if its one of those little optimizations that you can throw in as you go without taking much of your time to implement, wouldnt it be better to do so and have it not do anything, then to actively not do it for such a stupid reason as that you dont believe it works. It doesnt cost you anything to put it in and it might help with GC (very important in heavy code games).
now I would still like to retain my original position which was implement it if you can but dont fret over it. Its just one of those little optimizations you can throw in along the way.
Using is for if you open or create those things with a direct C# command, such as declaring a `streamreader`. You have to also write a Close() command, and `using` makes sure it's always quickly called. If you aren't having to write a close(), there's nothing for `using` to do.
But, Unity is taking care of all that for you. For example, when you drag a texture into a `public Texture2D T;`. Sure, running opens that texture file (or resource bundle,) but way below the level of your code. That's why we use Unity -- it takes care of all those details for us.
right. In Unity you usually never Open files manually. If you do you're free to use "using"
If you use networking you probably will use the builtin Network engine which has a fully managed interface. It's completely managed by Unity.
You will almost never need or use unmanaged memory, why would you...
As far as i know we talk about Unity, not XNA ;)
Answer by lil_billy · Dec 07, 2012 at 08:30 PM
well im aware of the benefit of using using lol
however i didnt know that C# handled garbage collection that way
Knowing that bit now I would say yes you could get away with it however there would be cases where it would be lethal not to
cases where you suddenly read in a whole lot of information and run them through a slew of temporary variable declarations
(like reading in a LOT of save data)
or something along those lines
its just one of those added optimizations you can do like StringBuilder
though tbh a person can spend months optimizing the hell out of a script, i believe there is such a thing as "good enough"
Answer by Dave-Carlile · Dec 08, 2012 at 05:06 AM
Any object you create that references limited, external, non-managed resources - files likely being the most common - should be created with using to make sure those resources are released as soon as possible.
http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.100).aspx
It also makes sure that if any sort of exception occurs while using the resource that it is properly released. Think of it as an "acquire-use-release" sort of pattern for resources.
and there would be the documentation i was talking about