2

Is there any way to force to clear all the used resources in an application? I thought in cleaning the cache in the onDestroy function but I don't really know if it is enough.

I'm having troubles freeing resources after closing the application, as I'm opening a lot of images, sounds, and so on in the application. If I open quickly the application for the second time, there are a lot of times in which it can't open certain resource because it is still opened...

Any tips please? Thanks

3
  • Need a lot more detail. How and where are you referencing these references? Do you know when onDestroy() is called and when it's not? What do you mean "closing the application"? Do you understand scope and visibility and the difference between the Activity life cycle and the Application life cycle? Commented Nov 5, 2013 at 19:49
  • I didn't specify anything because I'm looking for a generic solution. One suitable in any cases even if it is with brute force, like forcing someway the garbage collector to delete everything of the application or something like this. Commented Nov 5, 2013 at 20:07
  • What do you mean generic solution? I've written many Android apps and apart from shutting down MediaPlayer, I've never had to free resources explicitly. I close streams, files etc as soon as I've finished with. GC is not relevant since it will collect freed objects anyway. As I say, we need a lot more detail of what you are doing to propose answers since you are clearly doing something unusual. You also haven't answered what you mean by "closing the application". This is a very ambiguous term in Android and I suspect that it's real meaning may point to what you are trying to do. Commented Nov 5, 2013 at 20:52

2 Answers 2

1

Bitmaps:

  • if you are loading them through BitmapFactory, they are cached in the memory and should not prevent you from opening it again. However, call recycle() on the Bitmap objects that you created.
  • if you are opening them with another Activity, you shouldn't need to worry about anything.

Sounds & Videos:

  • if you are opening them with MediaPlayer, call stop() and release() on the MediaPlayer when you are done with it.
  • if you are opening them with another Activity, you shouldn't need to worry about anything.

These are just my (limited) opinion, look some other answers too before proceeding.

Sign up to request clarification or add additional context in comments.

Comments

0

I have run into the exact same problem with freeing memory once an activity/an application is finish. So I use a clean up method!
In every activity's onDestroy the method clean up is called.

 protected void onDestroy(){ cleanUp(findViewById(android.R.id.content)); } public void Cleanup(View rootView) { unbindDrawables(rootView); System.gc(); } private void unbindDrawables(View view) { if (view.getBackground() != null) view.getBackground().setCallback(null); if (view instanceof ImageView) { ImageView imageView = (ImageView) view; imageView.setImageBitmap(null); } else if (view instanceof ViewGroup) { ViewGroup viewGroup = (ViewGroup) view; for (int i = 0; i < viewGroup.getChildCount(); i++) unbindDrawables(viewGroup.getChildAt(i)); if (!(view instanceof AdapterView)) viewGroup.removeAllViews(); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.