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
6
Question by Jessy · Dec 21, 2009 at 09:19 PM · performanceoptimizationscriptingbasicsmemorynull

Should you use = null to free memory for variables declared in Start()?

If you only use a variable in Start(), does it help with garbage collection if you declare it to be null before the end of Start()?

I suppose this question applies to any function that will be called only once, but I assume Start() is the most common instance of that kind of function.

Comment
Add comment · Show 1
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 dingben · Oct 23, 2010 at 01:26 PM 0
Share

I love the question. Nonetheless, even though Duck's answer is getting the most votes, I believe that there is truth in all the answers given, especially Jashan's. I have not deciphered if any discrepancies exist between the proposed explanations but I sense there are. I hope someone will beat me in condensing all the answers in a solid and doubtless text. (see my comments in Jashan's post)

3 Replies

· Add your reply
  • Sort: 
avatar image
13
Best Answer

Answer by duck · Dec 21, 2009 at 10:29 PM

I believe mono's garbage collection works by monitoring references to objects and identifying isolated items - that is, items or groups of items which can no longer be referenced by the currently running program.

In this case, whether or not you null the reference before Start completes, the fact that the variable falls out of scope when Start() ends, means that it will be picked up promptly by the garbage collector.

Bear in mind that this only applies to non unity objects - Unity has its own way of dealing with objects which derive from its Object class, which seems to be entirely separate from Mono's garbage collection!

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
4
Best Answer

Answer by Peter Alexander · Dec 22, 2009 at 02:26 AM

Duck is right. Mono currently uses the Boehm garbage collector, which is a mark and sweep collector. Once your variable goes out of scope, it will lose the reference to the heap allocated object in exactly the same way that it would if you set it to null. In any case, that object isn't going to be freed until the next time the GC stops the world.

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
6

Answer by jashan · Dec 22, 2009 at 09:42 AM

The important thing here - which is a bit unclear in the question - is where you actually declare the variable. It doesn't matter so much where you use it - what matters is where you declare it. The scope of the variable depends on where it is declared, and what the garbage collector does with it depends on the scope.

So, if you have:

public class MyCoolClass : MonoBehavior {

 private string myMemberVariable; // variable declaration with "instance scope"

 public void Start() {
     string myMethodVariable = "SomeCoolString"; // variable declaration and assignment with "method scope"
     myMemberVariable = "Another cool string!"; // assignment

     /* some funky code that actually makes use of these useless strings, like: */

     myMethodVariable = myMemberVariable; // using myMemberVariable to assign its value to myMethodVariable
 }

}

What happens is that myMethodVariable will be garbage collected at some point in time after Start() was executed. The reason is that it was declared in the method, and so its scope is in the method, the garbage collector will have it marked as "not in use" and drop it from memory (at some point in time).

However, myMemberVariable still keeps the value that was assigned to it in the Start() method. Thus, it will only be garbage collected, when your instance of MyCoolClass is "out of scope" (which happens only when MyCoolClass is destroyed, or when the game object it is attached to is destroyed).

So, in that case, "nulling" myMemberVariable at the end of Start() would make sense in regards to garbage collection. The question, of course, would be why you declare a variable that you only use in one method with instance scope in first place - but you may have your reasons (it seems that in the context of iPhone development, people use "pools of objects" in order to avoid garbage collection - and for such a "pool of object" this exact pattern does seem to make sense to me; and in that case, you probably would "null it" to make sure you don't add even the slightest amount of extra memory) ;-)

Comment
Add comment · Show 3 · 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 dingben · Oct 23, 2010 at 01:23 PM 0
Share

1.When you say 'after Start() WAS executed', you do mean when Start() has exited, completely finished? 2.When you say "not in use" do you mean that the variable's value is still at the memory location the compiler placed it but the pointer to it is now NULL? 3. Is not my$$anonymous$$ethodVariable also garbage collected/destroyed when the class becomes out of scope?

avatar image dingben · Oct 23, 2010 at 01:24 PM 0
Share
  1. ...and I understand squeezing as much memory as possible ahead of garbage collection by setting the value of the variable(s) to NULL, but I have my doubts that setting it to NULL helps as the memory location is more than likely reserved in segments that cannot be reused until freed completely? Does NULL free the mem loc completely or simply sets it to no value thus the segment remains occupied?

avatar image jashan · Oct 24, 2010 at 06:16 PM 1
Share
  1. When it has exited (which is after "my$$anonymous$$ethodVariable = my$$anonymous$$emberVariable". 2. "Not in use" = no longer referenced. 3. my$$anonymous$$ethodVariable is already marked for garbage collection when it Start() is completed. 3. "The class becomes out of scope" doesn't really make sense. Objects of the class could be out of scope, though - but then all is marked for garbage collection, including "my$$anonymous$$emberVariable". 4. Ahead of garbage collection, even setting variables to NULL won't make a difference. $$anonymous$$emory is freed only during garbage collection. "Null" just removed the reference to the object.

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

No one has followed this question yet.

Related Questions

UI-based game memory and performance issue. 1 Answer

Memory management and C# do's and don'ts? 5 Answers

Reasonable heap alloc. per second and total ? 0 Answers

Optimize (Garbage Collection, Memory usage) 1 Answer

Using an individual mesh from a large .fbx file 0 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