2

I made an android application with googlemap api, and draw some 16x16 png (about 200 count) on overlay. When I move or zoom on/in mapview, "out of memory" error occurs very often.

I also used the googlemap appication in my htc itself. Seams that it uses about 14+MB memmory, and never happens "out of memory".

How to save memmory usage in a googlemap api, or how to enlarge android api memmory limit.

Thanks a lot!

Sandeep R
  • 2,284
  • 3
  • 25
  • 51
Xiaofeng
  • 41
  • 3
  • See my answer to another question on the same issue: [android MapView always causes an OutOfMemoryError in nested elements](http://stackoverflow.com/questions/5460650/android-mapview-always-causes-an-outofmemoryerror-in-nested-elements/6134375#6134375) [Google Groups: Issue 2181: Memory leak in system when using MapView](http://code.google.com/p/android/issues/detail?id=2181) – Muzikant May 29 '11 at 04:48

2 Answers2

2

My own solution: Catching OutOfMemoryError when zoomin/zoomout, will prevent api to be killed by VM. Because it dies usually when doing map zoom after a translation.

    mapView.setBuiltInZoomControls(true);  
    ZoomButtonsController zoomctrl = mapView.getZoomButtonsController(); 
    zoomctrl.setAutoDismissed(false);//自动隐藏关闭
    zoomctrl.setVisible(true);
    zoomctrl.setOnZoomListener(new ZoomButtonsController.OnZoomListener() {

        public void onZoom(boolean zoomIn) {
            // TODO Auto-generated method stub
            try{
                Log.i(TAG, "OnZoomListener");
                System.gc();
                if(zoomIn)
                {                       
                    mc.zoomIn();
                }
                else
                {
                    mc.zoomOut();
                }
                System.gc();
            }
            catch(OutOfMemoryError e)
            {
                e.printStackTrace();
                Log.e(TAG, e.toString());
                Toast.makeText(GoogleMap.this, e.toString(), Toast.LENGTH_LONG);
            }
            catch (Exception e)
            {
                Log.w(TAG, e.toString());
                Toast.makeText(GoogleMap.this, e.toString(), Toast.LENGTH_LONG);
            }               
        }

        public void onVisibilityChanged(boolean visible) {
            // TODO Auto-generated method stub

        }
    });

    private boolean myDoubleTouch(float x, float y, MapView mapView)
    {
    Log.i(mParent.TAG, "myDoubleTouch: " + x +","+y);
    try
    {
        mapView.getController().zoomInFixing((int)x, (int)y);
    }
    catch(OutOfMemoryError e)
    {
        System.gc();
        e.printStackTrace();
        Log.e(mParent.TAG, e.toString());
        Toast.makeText(m_mapview.getContext(), e.toString(), Toast.LENGTH_LONG);
    }
    catch (Exception e)
    {
        Log.w(mParent.TAG, e.toString());
        Toast.makeText(m_mapview.getContext(), e.toString(), Toast.LENGTH_LONG);
    }           

    return true;
}
Xiaofeng
  • 41
  • 3
0
public class CustomMapView extends MapView {
    // private Context context;
    public CustomMapView(Context context, String apiKey) {

    super(context, apiKey);
    // this.context = context;
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    try {
        return super.dispatchTouchEvent(ev);

    } catch (Exception ex) {

            //     Log.e("CustomMapView", "Caught the exception");
        int zoomLevel = getZoomLevel();
        getController().setZoom(zoomLevel-1);
        super.setVisibility(View.GONE);
        super.setVisibility(View.VISIBLE);
    }

    return true;
}

@Override
public void draw(Canvas canvas) {
    try {
        super.draw(canvas);
    } catch (Exception e) {

             //     Log.e("CustomMapView", "Caught the exception");
        int zoomLevel = getZoomLevel();
        getController().setZoom(zoomLevel-1);
        super.setVisibility(View.GONE);
        super.setVisibility(View.VISIBLE);
    }
}

public CustomMapView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // this.context = context;
}

public CustomMapView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // this.context = context;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    System.gc();
    return super.onTouchEvent(ev);
}

}

Along with onZoomListener, I had added my own CustomMapView which extends MapView. This solved the memory exception in my application.

gary
  • 4,227
  • 3
  • 31
  • 58
Maggi
  • 155
  • 7