I like to know some basic stuffs
I am developing an android application and thus it lead me to memory management issues. Recently watched a video related to memory management by google I found that using a static variable in an activity causes memory leaks even in orientation change of activity as it keeps references to other objects in the activity,The man in video said that using static variable with the help of a static method will solve the issue, I like to know how a static method will solve the issue ?
I like to know the very best way to use static variable in my application for example I have a global class in which I stores some static variables as its name indicates these values will assessed and modified by different activities, As an example I have a static variable WIDTH and currently update it like this
Global.WIDTH = 12
or get it like int width = Global.WIDTH
from different activites, is this is a correct method ,or do i have to use a static method to get the width like int width = Global.getWidth();
,then what's the difference between both of these , what is the best way to do this,
what are other important things we have to keep in mind when dealing with static variables ?
Thank you all and sorry for the long description ...
Static variables can cause memory leaks because they'll never go out of scope, but if you only have a few primitives such as int or long then you won't have a problem.
You need to be careful if you start referring to objects in static variables, especially collections. If a collection never goes out of scope, neither do any of the objects contained within it, and neither do any of the objects they refer to, so there's a chance that the collection will continue to grow and use up more and memory.
The variable in your question, presumably an int, will be OK though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With