Android memory management for activity in onStop (or off-screen)

What I'm trying to achieve:

  • Reduce the use of memory that is no longer displayed on the screen, i.e. another action was launched.
  • The possibility for this action is still on the navigation stack, so I assume that I will have to rebuild what was destroyed in onStop into onStart, but I'm not sure how to do it when all the views / buttons were built using layout. xml.

Situation:

I have an Android app that is very heavy, but these images are static in many layouts, with the same backgrounds, button images, navigator headers, etc. This led me to easily create layouts without touching too much code, indicating all the images, their attributes and src positions in the layout.xml files. And it worked great, it was easy to get up and work, but now there are many reports of power shutdown due to excessive memory usage.

So, trying to clean up and allow gc to remove images and views that are not on the screen, I came across an article (see the bottom of the question) suggesting inside the onDestroy method, capturing the trick of your root element within the layout and recursively navigating through the tree, deleting representations and detaching them. However, this only works and is not guaranteed in accordance with the documents when pressing the return buttons.

So onDestroy does not help me when I push a new activity on the stack and do not clear what just left the screen. But the plus side of using the onDestroy method is that the starting point starts with onCreate, so all views are created correctly. When I use this method in onStop, the memory is cleared beautifully when I start a new action, but because I published all the views and they were built using layout.xml, I don’t understand how or what I need to rebuild on onStart. if I destroy everything in onStop, especially considering that I never created any views in the code, since they were all configured due to layout.xml files.

Key questions: How to clear memory when starting a new action? If the contexts are handled correctly, is this consistent with the fact that gc will still clear all image views that are off-screen and rebuild them automatically?

Is there any way to use this inside onStop?

@Override protected void onDestroy() { super.onDestroy(); unbindDrawables(findViewById(R.id.RootView)); System.gc(); } private void unbindDrawables(View view) { if (view.getBackground() != null) { view.getBackground().setCallback(null); } if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { unbindDrawables(((ViewGroup) view).getChildAt(i)); } ((ViewGroup) view).removeAllViews(); } } 
+3
source share
2 answers

A) Using onCreate () / onDestroy () will allocate when activity hits the stack, and unallocate when activity is called from the stack.

C) Using onStart () / onStop () will highlight when activity becomes visible, and not distribute when activity is not visible.

Use either A) or B). Do not mix them, or you will too often isolate or release.

In your case, I would move everything from onCreate () to onStart () and everything from onDestroy () to onStop ().

Also consider using this.setContentView (null) or this.setContentView (new view (this)) in onStop () to make sure old views can be garbage collected:

  @Override protected void onStop() { super.onStop(); View root = findViewById(R.id.RootView); setContentView(new View(this)) unbindDrawables(root); System.gc(); } 
+2
source

I don't think any of the activity callbacks are the perfect place for this. Your activity will inevitably be very slow.

I believe that you would prefer to save images if you can, but want to refuse if the system needs resources. If this (caching) is what you are looking at, WeakReference is your person. See a great article to help you decide / understand.

Also, I don’t think that the Android developer should take care when the views created from the layout files are gc'd. Until you keep strong links to opinions, you should be fine.

+1
source

Source: https://habr.com/ru/post/1398554/


All Articles