I have a huge memory leak with my map overlays. I have debugged and tested out my threads to make sure they are closing ad nausium so I am going to start by supposing that the memory leak is with my Map Overlay and work my way from there.
I am loading overlays onto my map and when the map is panned I am removing all the overlays and loading new ones. The data to create the overlays is retrieved in a thread. The application works, it just leaks over a meg of memory every reload.
What is the appropriate way of getting rid of overlays so the garbage collector cleans it up? What am I doing wrong?
This is what I am doing (not the full code).
// getting my overlays list
private List<Overlay> overlays;
overlays = mapView.getOverlays();
// trying to get rid of old overlays
for (Overlay i : overlays)
if (i instanceof ParcelOverlay)
// and loading new overlays
int count = 0;
while (count < parcelData.length)
overlays.add(new ParcelOverlay(MapFrontEnd.this, parcelData[count]));
The actual overlays use the MapActivity as a context. The overlay draws some rectangles, has a text paint where it loads a font and can load up to 3 bitmaps depending on the data.
// font loading:
face = Typeface.createFromAsset(context.getAssets(), "fonts/sd_led_screen.ttf");
//example of image load
buildingBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.playerstore);
//and this is what I have added to attempt to clean up loose ends. this is called before overlays.clear().
public void cleanUp()
context = null;
if (buildingBitmap != null)
buildingBitmap = null;
if (ghostBitmap != null)
ghostBitmap = null;
if (presentBitmap != null)
presentBitmap = null;