3

suppose I have an activity, and it contains a TextView. I can initialize the TextView either to a member variable or to a local variable. is there any memory wise difference between these to initialization ?

example : Activity with local view reference:

 public class MainActivity extends Activity{

    @OVerride
    public void onCreate(Bundle b){
       TextView textView = (TextView)findViewById(R.id.my_text_view_id);
    }
}

Activity with member view reference:

 public class MainActivity extends Activity{
    TextView mTextView;

    @OVerride
    public void onCreate(Bundle b){
       mTextView = (TextView)findViewById(R.id.my_text_view_id);
    }
}
droidev
  • 7,352
  • 11
  • 62
  • 94
  • It will available globally inside the activity and apart from that i don't think there is a difference in terms of memory. – Ritt Feb 03 '16 at 08:07

3 Answers3

5

You should always use the minimal scope. So when you declare a variable you should ask yourself:

"Will I need this variable later in a different function?"

Yes -> Use a member variable

No -> Use a local variable

Edit:

What also to consider is the cost of object creation:

If a function does get called repeatedly it is a good practice to instanatiate an object only once, store it as a member variable and reuse it instead of creating a new instance every time the function gets called.

So the 2nd important question is:

"Will this function get called a lot and do I really need a new instance of the object stored in the variable?"

Yes, often, and no, I can reuse the same object over -> use a member variable. This way the same memory is used and no garbage gets piled up. Use only for large Arrays or objects, it is not needed for simple int vars in loops.

Björn Kechel
  • 7,933
  • 3
  • 54
  • 57
  • actually I'm well clear about this, and I didn't practise creating un necessary member variable, I always creats local variable, but my question is about memory – droidev Feb 03 '16 at 09:13
  • for a textview that is in the view there should not be a difference, because the variable only points to where in memory the view is stored. It will be different for objects that get created in a function and are not needed after the function ends anymore. – Björn Kechel Feb 03 '16 at 09:40
  • From your edit if i call the function repeatedly then will the reference be new all the time ? – droidev Feb 03 '16 at 11:37
  • If you are only creating a reference to an existing object, no problem here as the overhead is small. But if you create a whole new object, not only a reference, you should store it as a member. – Björn Kechel Feb 03 '16 at 11:46
1

Generally, try to avoid using 'global' variables unless you have good reason to do .

You can use Local or Global Variable depends upon your Requirement .

Mainly, local variables are that they work within a limited scope means they are declared when a function is called and after ending it the memory taken up by the variable is released.

Finally usage of global variables leads to wastage of memory .

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
1

Memory wise global variables are much more prone to memory leaks. The scope any variable depends on its scope. For local variable, the scope is closing braces of the respected method, and variable is automatically garbage collected after the execution of closing braces. Where as global variable will reside in memory until the any object of that class is in memory.

MrWaqasAhmed
  • 1,479
  • 12
  • 12
  • local variables are cleared from memory automatically after the scope ends I accepts that, but here, local variable is just a reference, and it points to the object which is of type view. and the object will persist through out the activity life cycle – droidev Feb 03 '16 at 09:09