Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by casimps1 · Dec 07, 2012 at 08:05 PM · memorygarbage-collectionusingtemporary

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-1

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lil_billy · Dec 08, 2012 at 04:10 AM 0
Share

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

avatar image Owen-Reynolds · Dec 08, 2012 at 03:40 PM 0
Share

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.

avatar image lil_billy · Dec 08, 2012 at 06:24 PM 1
Share

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.

avatar image Owen-Reynolds · Dec 08, 2012 at 08:33 PM 1
Share

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.

avatar image Bunny83 · Dec 09, 2012 at 02:11 AM 0
Share

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 ;)

avatar image
0

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"

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lil_billy · Dec 08, 2012 at 05:25 AM 0
Share

and there would be the documentation i was talking about

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity IOS(IPad2) auto exit app after loaded too many images 1 Answer

Reasonable heap alloc. per second and total ? 0 Answers

calling www to get audio.clip multiple times causes memory problems 1 Answer

How do you use a script in another script? 1 Answer

Mono profiler hooking on Unity 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges