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 Karsnen_2 · Mar 18, 2013 at 07:03 PM · androidiosmobiledestroygarbage-collection

Is this a good practice for mobile devices?

Hello,

I am targeting my game for both Android and iOS and I was wondering whether this would be a good practice. As my target is primarily mobile and to help Unity to collect garbage - was wondering to assign null to all the variables when you destroy a gameobject or that particular script.

Let's say a GameObject A has a Script S. Then when you destroy the gameobject or Load another scene - reference all the variables to a null value.

     Vector3 PlayerPosition;
     float Fl;
     int In;
     Transform ThePlayer;
     
     
     void Start ()
     {
         ThePlayer = GameObject.FindWithTag("Player").GetComponent<Transform>();
         PlayerPosition = ThePlayer.position;
         Fl = 5.0f;
         In = 3;
     }
     
     
     void OnDestroy ()
     {
         ThePlayer = null;
         PlayerPosition = null;
         Fl = null;
         In = null;
     }


Thank you,

regards,

Karsnen.

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

2 Replies

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

Answer by numberkruncher · Mar 18, 2013 at 07:31 PM

In your question you make reference to two different kinds of variables:

  • Value Types - Like float, int, Vector3, etc.

  • Reference Types - Like MonoBehaviour, GameObject, Transform, etc.

There is no benefit to clearing the values of value types, in fact this shouldn't even compile:

 Vector3 PlayerPosition;
 float Fl;
 int In;
 
 void OnDestroy ()
 {
    PlayerPosition = null;
    Fl = null;
    In = null;
 }

With regards to UnityEngine.Object instances you should note, managed objects are simply wrappers around unmanaged objects that are allocated. The unmanaged object counterparts are destroyed using the provided API Object.Destroy (runtime scripts) or Object.DestroyImmediate (editor scripts):

 Destroy(ThePlayer.gameObject);

Though this does not clean up the managed object instance which should be explicitly cleared so that GC can cleanup the managed wrapper object:

 ThePlayer = null;

Though in your example there seems very little point to doing the above in your OnDestroy message handler. You should only worry about nullifying your reference variables in scripts that will continue to exist. Likewise there is little point to nullifying variables within local scopes:

 void SomeFunction(GameObject go) {
     Transform t = go.transform;

     // Do your stuff...

     // Absolutely no point:
     //   #1 - the transform object will still exist.
     //   #2 - the variable is going out of scope, so not GC issue
     t = null;
 }

The way in which you do this will obviously vary with regards to what you are doing, but hopefully the above will help you to understand the general idea.

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 murkantilism · Mar 18, 2013 at 07:09 PM

I'm not a guru on Unity garbage collection by any means, hopefully someone more knowledgeable can confirm:

I don't think setting variables to null with help with garbage collection. It won't entirely free up the memory space taken up by that variable, because it will still store it's value as null.

If the value used to be a large array or a vector, setting it to null will lower the amount of memory taken up by the variable, but it won't lower it to 0 bytes.

Check out these pages for more info: One, Two, Three.

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

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

12 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

Related Questions

Is PhysX being used on mobile 1 Answer

Is Gyroscope supported on all mobile Android iOS devices with a gyroscope? 3 Answers

Mobile Keyboards -- Highlighting all text by default when keyboard is opened (iOS vs. Android) 0 Answers

Does AssetBundle work on mobile? 0 Answers

Receiving UI/touch events while mobile soft keyboard is open 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