I need to optimize memory in my application. When fragment is closed I need to free-up memory used by that fragment.
I am doing following steps to free-up memory
@Override
public void onDestroy() {
super.onDestroy();
txt_legal_1 = null;
txt_legal_2 = null;
progressBar = null;
mHandler = null;
prefs = null;
content = null;
System.runFinalization();
Runtime.getRuntime().gc();
System.gc();
}
But still memory is not free-up.Can any-one help on this ?
Look at your Activity code, I bet that something is referencing Fragment, or one of it's field. Java Garbage Cleaner can't cleanup object from memory if at least one link persists (it can be even some listener, receiver or so on)
Also you can't expect, that running System.gc() will clean up memory, in fact you just hinting system to perform cleanup. You can look if GC will be completed successfully in "Monitor" tab in Android Studio. There is a button "Initiate GC", with it GC will really start. And if you see that memory consumption will not change, that will mean that you did something wrong, and something still keeps Fragment(or it field) link.