title: Android ImageView Memory Management
body: <p>I have a mainactivity class, that sets a content view which has a few ImageViews on it.
  On the click of one of the ImageViews a new Intent is created and a new activity started.</p>
  
  <p>The problem is the app runs out of memory when loading the new content view which has an ImageView on it.
  Is there a way of removing the old content view or ImageViews which are no longer being displayed from memory?</p>
  
  <p>Here is my logcat:</p>
  
  <pre><code>01-01 11:08:19.255: D/dalvikvm(416): GC_EXTERNAL_ALLOC freed 15K, 52% free 2587K/5379K, external 9858K/10106K, paused 23ms
  01-01 11:08:19.265: E/dalvikvm-heap(416): 1895348-byte external allocation too large for this process.
  01-01 11:08:19.295: I/dalvikvm-heap(416): Clamp target GC heap from 16.703MB to 16.000MB
  01-01 11:08:19.295: E/GraphicsJNI(416): VM won't let us allocate 1895348 bytes
  01-01 11:08:19.295: D/dalvikvm(416): GC_FOR_MALLOC freed &lt;1K, 52% free 2586K/5379K, external 9858K/10106K, paused 17ms
  01-01 11:08:19.305: D/AndroidRuntime(416): Shutting down VM
  01-01 11:08:19.305: W/dalvikvm(416): threadid=1: thread exiting with uncaught exception (group=0x40015560)
  01-01 11:08:19.315: E/AndroidRuntime(416): FATAL EXCEPTION: main
  01-01 11:08:19.315: E/AndroidRuntime(416): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.turbo/com.example.turbo.ByReferenceBookList}: android.view.InflateException: Binary XML file line #7: Error inflating class &lt;unknown&gt;
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.os.Handler.dispatchMessage(Handler.java:99)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.os.Looper.loop(Looper.java:123)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.app.ActivityThread.main(ActivityThread.java:3683)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at java.lang.reflect.Method.invokeNative(Native Method)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at java.lang.reflect.Method.invoke(Method.java:507)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at dalvik.system.NativeStart.main(Native Method)
  01-01 11:08:19.315: E/AndroidRuntime(416): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class &lt;unknown&gt;
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.view.LayoutInflater.createView(LayoutInflater.java:518)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.app.Activity.setContentView(Activity.java:1657)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at com.example.turbo.ByReferenceBookList.onCreate(ByReferenceBookList.java:31)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
  01-01 11:08:19.315: E/AndroidRuntime(416):  ... 11 more
  01-01 11:08:19.315: E/AndroidRuntime(416): Caused by: java.lang.reflect.InvocationTargetException
  01-01 11:08:19.315: E/AndroidRuntime(416):  at java.lang.reflect.Constructor.constructNative(Native Method)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.view.LayoutInflater.createView(LayoutInflater.java:505)
  01-01 11:08:19.315: E/AndroidRuntime(416):  ... 22 more
  01-01 11:08:19.315: E/AndroidRuntime(416): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.graphics.Bitmap.nativeCreate(Native Method)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.content.res.Resources.loadDrawable(Resources.java:1709)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.widget.ImageView.&lt;init&gt;(ImageView.java:118)
  01-01 11:08:19.315: E/AndroidRuntime(416):  at android.widget.ImageView.&lt;init&gt;(ImageView.java:108)
  01-01 11:08:19.315: E/AndroidRuntime(416):  ... 25 more
  </code></pre>
  
  <p>I am using the following code to start the new activity:</p>
  
  <blockquote>
  <pre><code>Intent byreferencelist = new Intent(getBaseContext(), ByReferenceList.class);
                      startActivity(byreferencelist);
                      finish();
  </code></pre>
  </blockquote>
  
  <p>And then I have:</p>
  
  <blockquote>
  <pre><code>public class ByReferenceList extends Activity {
  
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
  
         setContentView(R.layout.byreferencelist);
  </code></pre>
  </blockquote>
  
  <p>Which displays:
  <a href="http://pastebin.com/ADuE6C4N" rel="nofollow">http://pastebin.com/ADuE6C4N</a>
  (Sorry couldn't post XML for some reason.)</p>