0

Please check this code:

TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Locale loc = null;
WeakReference<Locale> locWeak = new WeakReference<Locale>(loc);
String country = locWeak.get().getDisplayCountry(new Locale("", tm.getSimCountryIso()));
Log.i(TAG,"Country is: "+country);

The WeakReference is a bit useless in here as I make a strong reference in "new Locale".

Can anyone please suggest a better approach to WeakReference for something like this:

Locale loc = new Locale("",tm.getSimCountryIso());

The reason I use WeakReference, is because I don't need to hang on to this Object as it become useless as the app progresses. Secondly, I need to keep a clean memory as I will need to do some heavy tasks later on.

Thanks

Steven Byle
  • 13,149
  • 4
  • 45
  • 57
Mr H
  • 5,254
  • 3
  • 38
  • 43

1 Answers1

0

In case anyone checking this:

After my research on the similar subject, I came up with the conclusion that using WeakReference at this function is more expensive in memory than using StrongReference.

Check this link: https://stackoverflow.com/a/16975197/415038 it is another question of mine on this matter. Good complete answers are there.

Community
  • 1
  • 1
Mr H
  • 5,254
  • 3
  • 38
  • 43