1

I'm creating an android app and have run into OOM problems due to Bitmap images. I certainly need to make the images smaller in terms of memory however I would like to practice correct memory consumption and load them correctly.

Currently my layouts contain the references to the images within the res/drawable-hdpi folder as their backgrounds. I looked at this other stack overflow question: outOfMemoryError with background drawables where the person asking the question had the same problem as I did. I see that the answer states that I should reference the Bitmaps in java and then recycle onPause and set them back up during onResume. Now does this mean that I should not set the backgrounds in xml and then do so within java oncreate and then recycle and set them back up during onResume? Also, I was looking into WeakReferencing but have found myself getting confused by it... Could anyone give me a good explanation of WeakReferencing?

I appreciate all answers,

Cheers,

Jake

Community
  • 1
  • 1
Jake Alsemgeest
  • 692
  • 2
  • 13
  • 25
  • Try this answer by me on SO on a similar problem : http://stackoverflow.com/questions/18255572/android-bitmap-cache-takes-a-lot-of-memory/18255693#18255693 – arshu Aug 21 '13 at 04:13

1 Answers1

0

During the execution of the program, a weak reference will be the first to be garbage collected if there are no soft or strong references binding to it. So if memory is considerably low, or when and if the garbage collector deems appropriate, the weak reference is garbage collected and this is why I have included the else statement within my code to show the occurrence of that situation.
Source: http://www.javacodegeeks.com/2012/01/understanding-java-weak-references.html

When you make something a weak reference you tell the GC that the memory taken up by this guy needs to be freed first. You free the memory by calling System.gc()

Again, there is another answer on SO about this:
Java: difference between strong/soft/weak/phantom reference

And Wikipedia has a much simpler example:
http://en.wikipedia.org/wiki/Weak_reference#Java

In your case, say you are loading a fairly large HD image as a background. Then you need to do some more loading of data into the memory and so on which cause OOM. What you can do is free the memory held by this GD image in onStop() and then in onResume() load it back.
However, I think you should also look into other things in your app that can be made weak references.

Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221
  • Okay I understand the weak reference a bit more now. However, how should I load in images that are currently used as the background for some buttons and layouts? Should I no reference them in the xml and then set the backgrounds in java? I understand the reasoning for the onStop() and onResume() however I do not understand how exactly it would be implemented properly. – Jake Alsemgeest Aug 21 '13 at 23:00
  • A much detailed answer is here: http://developer.android.com/training/displaying-bitmaps/index.html – An SO User Aug 22 '13 at 10:44
  • So what your saying is that I shouldn't set the backgrounds of my views in XML to anything... and I should references them like it says to in the link and then set them using Java? – Jake Alsemgeest Aug 22 '13 at 20:55
  • Yes. Dynamically load them when needed, clean them out when not – An SO User Aug 23 '13 at 01:25