Search code examples
androidbitmapandroid-mapviewosmdroid

Android MapView with GoogleTileOverlay not freeing bitmaps


I am having an issue with drawing custom tiles over Google maps.

Using GoogleTileOverlay from OSMDroid, I am drawing custom tiles that are stored locally on the devices SD card.

The problem occurs after I have opened the map and then try to return to other activities. Things start getting weird. Background colors defined in XML aren't showing up and other strange things.

I have searched far and wide for a reason and all I have been able to come up with is that there is not enough memory?

According to Eclipse MAT there is 8.4 MB of bitmaps left over after I have destroyed the activity that holds the MapView and GoogleTileOverlay

Let me know if there is any other information I can provide.


Solution

  • I was dealing with OOM exceptions due to memory leaks coming from the bitmaps. It seems that removing the overlay from MapView's Overlay list before clearing the tiles cache (so that no new tiles are created after the clearance) in onPause of map activity works. I ended up having single instance of LRUMapTileCache no matter how many times the map activity was created / resumed.

    My onPause batch is as follows:

    // .. get custom tiles off the screen first
    mMapView.getOverlays().remove(mCustomTilesOverlay);         
    // .. release all custom tiles' bitmaps
    mCustomTilesOverlay.clearTileCache();
    // .. unregister intent receiver (in order not to leak it)
    mCustomTilesOverlay.detach();
    // .. indicate not-ready state by clearing the reference
    mCustomTilesOverlay = null;
    

    The 8 MB still persist though (see edit), I'm using the android:largeHeap="true" application attribute to compensate.

    edit

    Turned out those 8 MB came from other sources. The cache gets successfuly cleared with this.