Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Best Way to Learn about Memory and Garbage Collection , for Android app?

Discussion in 'Scripting' started by Insightful_Designer, Feb 15, 2023.

  1. Insightful_Designer

    Insightful_Designer

    Joined:
    Jul 28, 2022
    Posts:
    19
    I develop an AR App in Unity for Android.
    I want to ask about memory and garbage collection.
    In one C# script I have 6 AssetBundles as fields and 6 GameObjects as fields.
    From inside each AssetBundle, I load an asset, aka a GameObject.
    ( The App is such that I am obliged to have one GameObject per AssetBundle.)
    Also, I have 20 anchors as fields, a List Of GameObjects,
    All those are fields.

    My script has the following form
    public class MyScript : MonoBehaviour
    {
    // my fields are
    AssetBundle1, AssetBundle2, ..., AssetBundle6;
    GameObject1, GameObject2, ..., GameObject6;
    Anchor1, Anchor2, ..., Anchor6
    List<string>
    List<bool>
    and some other fields.

    void Function1()
    { ....
    }
    void Function2()
    { ....
    }
    void Function3()
    { ....
    }
    void Function4()
    { ....
    }

    }

    And my Question.

    Given the fact that Functions 1 through 4, they ALL use the fields above(AssetBundles, GameObjects, etc),
    should I, from a Memory and Garbage Collection and Performance stand point,
    make all those fields local variables that each function will pass to another or not.
    I made them all fields, of MyScript, because ALL my functions use them.
    Where can I read about that memory optimization thing? I read a little about heap and stack, not a lot though.
    So, where to read? And, should I make anything, that can be done, a Local variable?
     
  2. atr0phy

    atr0phy

    Joined:
    Nov 5, 2014
    Posts:
    43
    "Premature optimization is the root of all evil." -Tony Hoare

    Why are you trying to optimize your code?

    Is it feature complete but running too slowly? And do you have some measurement that indicates optimization will improve it? Because unless both of those things are true, then you're preoptimizing.

    The absolute most important characteristic of your code is that it properly conveys its intent, to you, the programmer. So if you're intentionally making your code less readable for the sake of optimizations that the code may never even need, then you're preoptimizing.

    The more time you spend preoptimizing, the less time you have to expand your feature set and the greater chances of introducing unexpected bugs.

    There are obvious optimizations that you'll naturally make as you develop your skills as a programmer and learn the nuances of variable scope, but that shouldn't be your primary concern right now. Your primary concern should be writing code that works and that you can easily understand, the rest will come naturally as you grow and expand your knowledge.

    Once your program is ready for user consumption, once you've had time to test it yourself to identify possible performance issues, and once you have a userbase that can test it to report any performance issues they may be having, only then should you be worrying about optimization.
     
    Bunny83, mopthrow and Kurt-Dekker like this.
  3. Insightful_Designer

    Insightful_Designer

    Joined:
    Jul 28, 2022
    Posts:
    19
    One:
    Thanks for the lesson. I really got what you said.

    Two:
    The code runs. It's just that I was thinking about what if I use the AssetBundles and GameObjects that take memory the wrong way.
    Truth is the code runs pretty good.
     
  4. kdchabuk

    kdchabuk

    Joined:
    Feb 7, 2019
    Posts:
    47
    Bunny83 likes this.