Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properties containing references good/bad

Tags:

android

I am sort of new in Android/Java (Normally work with PHP and JavaScript). I have read a few articles about memory leaking problems with apps when references are used the wrong way, so I have a question about something I have often seen in other peoples work.

A lot of people, when they need to access things like Views and such in multiple methods, keeps a reference of this in a property which are assigned during the creation of the Activity. From what I have read (or at least understood of what I have read), this is one of the courses for memory leaks?

Is it better to assign an ID to the objects and then search for them in each method? If so, what about dynamic created objects?

like image 438
Daniel B Avatar asked Dec 04 '12 12:12

Daniel B


People also ask

What are property accessors?

A get property accessor is used to return the property value, and a set property accessor is used to assign a new value. In C# 9 and later, an init property accessor is used to assign a new value only during object construction. These accessors can have different access levels.

Can landlords give a bad reference UK?

If the information your landlord provides is truthful, then yes they can give you a bad reference. This doesn't mean that you'll need to be your landlord's best pal in order to get a good reference - generally, you'll just need to tick the boxes. Did you pay rent on time? Tick.

How long does reference check take for renting UK?

How long does tenant referencing take? It can vary depending on which tenant referencing service you use. However, most companies aim to complete a check within 48 hours. Checks can be completed quickly if tenants have all their information and documentation ready at the start of the process.


1 Answers

Good if you use them correctly, bad if you don't.

You only leak if you pass something outside the activity to another class and the life time of that class is greater than the life time of the activity. Android might destroy your activity once it is no longer the foreground activity and if something outside the activity is holding a reference to it, then the garbage collector cannot release the memory back to the heap.

Be especially careful with activity contexts, statics and singletons.

Simply keeping a reference to a view inside an activity is absolutely normal.

Here's an example of bad (pseudo code);

public class MyApplication extends Application{
    public static ImageView activityBackgroundImageView;
}

public class MyActivity extends Activity{

    ImageView iv = findViewById(R.id.myImageView);
    myApplication.activityBackgroundImageView= iv; // <==== LEAK

}

Actually, the leak is not there, it only leaks when (if) myActivity is finished() or destroyed.

Each object has a reference count - how many objects hold a reference to it. After you set a reference to the ImageView in your activity, the reference count is one. You then copy that reference to the Application class. NB. Everything in Java is passed by value, so you pass the value of the reference, precisely a copy of the value - i.e. a new reference to the same object. The reference count on the ImageView is now two.

Sometime later, you finish() your activity and the reference count is decremented. It is now one. The garbage collector cannot release that ImageView object since is has a non-zero reference count.

Of course, you could fix it by nulling the reference in the Application but you now have spaghetti code.

like image 178
Simon Avatar answered Oct 02 '22 22:10

Simon