Reputation: 158
Is there a good way to clear memory in android? Because if I do this the memory is still increasing while I switch between activities.
public class Resurces extends AppCompatActivity {
private TextView wikipedia1,wikipedia2,googlemaps,googlePhotos;
private void clearMemory(){
wikipedia1.setOnClickListener(null);
wikipedia2.setOnClickListener(null);
googlemaps.setOnClickListener(null);
googlePhotos.setOnClickListener(null);
wikipedia2 = null;
wikipedia1 = null;
googlePhotos = null;
googlemaps = null;
Runtime.getRuntime().gc();
}
@Override
protected void onDestroy() {
super.onDestroy();
try{
clearMemory();
Runtime.getRuntime().gc();
finish();
}catch (Exception e){
e.printStackTrace();
}
}
}
Upvotes: 0
Views: 1445
Reputation: 317342
It's not necessary to try to force garbage collection. In fact, it's kind of a waste of effort. The GC is highly optimized on modern versions of Android and will do the right thing so that you don't run out of memory, unless of course your app is actually using too much memory. Don't worry about the increasing memory - that is natural as an app encounters new code. It will come back down once it reaches a threshold determined by Android.
See also:
Upvotes: 1