In my android app I often instantiate the following objects.
mWidget = new ComponentName(mContext, Widget.class);
mAppWidgetManager = AppWidgetManager.getInstance(mContext);
mLayoutInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
The objects are used very often. What is the best practice. Instantiating them once as a private member in my class or just instantiate them each time I need them?
Holding the reference might consume more memory but if they are used often it might be prefered rather instantiating them 10 times in a minute?
Thanks for your help 🙂
Holding the reference might consume more memory but if they are used often it might be prefered rather instantiating them 10 times in a minute?
I don’t think this statement is correct. Holding reference doesn’t increase memory consumption, memory consumption will be always same, but memory is not eligible for garbage collection and reallocation for other objects.
Object creating is always costly, if you are not messing up with references (in other words making object ineligible for GC), I think having single object is good choice.