title: Large ListView containing images in Android
body: <p>For various Android applications, I need large <code>ListView</code>s, i.e. such views with 100-300 entries.</p>
  
  <p>All entries must be loaded in bulk when the application is started, as some sorting and processing is necessary and the application cannot know which items to display first, otherwise.</p>
  
  <p>So far, I've been loading the images for all items in bulk as well, which are then saved in an <code>ArrayList&lt;CustomType&gt;</code> together with the rest of the data for each entry.</p>
  
  <p>But of course, this is not a good practice, as you're very likely to have an <code>OutOfMemoryException</code> then: The references to all images in the <code>ArrayList</code> prevent the garbage collector from working.</p>
  
  <p>So the best solution is, obviously, to load only the text data in bulk whereas the images are then loaded as needed, right? The Google Play application does this, for example: You can see that images are loaded as you scroll to them, i.e. they are probably loaded in the adapter's <code>getView()</code> method. But with Google Play, this is a different problem, anyway, as the images must be loaded from the Internet, which is not the case for me. My problem is not that loading the images takes too long, but storing them requires too much memory.</p>
  
  <p>So what should I do with the images? Load in <code>getView()</code>, when they are really needed? Would make scrolling sluggish. So calling an <code>AsyncTask</code> then? Or just a normal <code>Thread</code>? Parametrize it?</p>
  
  <p>I could save the images that are already loaded into a <code>HashMap&lt;String,Bitmap&gt;</code>, so that they don't need to be loaded again in <code>getView()</code>. But if this is done, you have the memory problem again: The <code>HashMap</code> stores references to all images, so in the end, you could have the <code>OutOfMemoryException</code> again.</p>
  
  <p>I know that there are already lots of questions here that discuss "Lazy loading" of images. But they mainly cover the problem of slow loading, not too much memory consumption.</p>
  
  <p><strong>Edit:</strong> I've now decided to <a href="http://developer.android.com/training/improving-layouts/smooth-scrolling.html" rel="noreferrer">start AsyncTasks in getView() which load the image into the ListView in the background</a>. But this causes my application to run into an RejectedExecutionException. What should I do now?</p>