4

I have list view of data, and each item opens up a view that has a map embedded in it. The purpose of this map is to show a single marker. The code works fine, but if I have a lot of list items (50+), cycling back and forth is causing the app to throw OutOfMemory exceptions.

Can someone help me understand what I am doing wrong here.. I am guessing that there is something wrong in my Map initialization logic that is causing this.. and I am trying to figure that out.

public class DetailView extends Fragment implements OnInfoWindowClickListener {

    private MapView mMapView;
    private GoogleMap googleMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {

            mMapView = (MapView) v.findViewById(R.id.mapBusinessLocation);
            mMapView.onCreate(savedInstanceState);
            mMapView.onResume();//needed to get the map to display immediately

            MapsInitializer.initialize(this.getActivity() );
            //MapController myMapController = mMapView.getController();
            googleMap = mMapView.getMap();
            googleMap.setMyLocationEnabled(true);
            CameraUpdate center=
                    CameraUpdateFactory.newLatLng(new LatLng(-20.269927,57.672729)); 
             CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);

            googleMap.moveCamera(center);
            googleMap.animateCamera(zoom); 
    }

       @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

        placeMarker("test");

        }

        private Marker placeMarker(String title) {
            //Get the hue
            String pinColor = "#f29217";    
            int c = Color.parseColor(pinColor);
            float[] pixelHSV = new float[3];
            Color.colorToHSV(c, pixelHSV);
            return googleMap.addMarker(new MarkerOptions()
             .position(new LatLng(-20.269927,57.672729 ))
             .title(title)
             .snippet("")
             .icon(BitmapDescriptorFactory.defaultMarker(pixelHSV[0])));
        }
           @Override
        public void onResume() {
            super.onResume();

                if(mMapView != null) {
                mMapView.onResume();
                }
        }

        @Override
        public void onPause() {
            super.onPause();

                if(mMapView != null) {
                mMapView.onPause();
                }
        }

        @Override
        public void onDestroy() {
            super.onDestroy();

                if(mMapView != null) {
                mMapView.onDestroy();
                }

        }

        @Override
        public void onLowMemory() {
            super.onLowMemory();

                if(mMapView != null) {
                mMapView.onLowMemory();
                }
        }

}

The stack trace when the app crashes is given below..

    PP_VERSION_CODE=53
    ANDROID_VERSION=4.3
    PHONE_MODEL=GT-I9505
    CUSTOM_DATA=
    STACK_TRACE=java.lang.OutOfMemoryError
    at android.graphics.Bitmap.nativeCreate(Native Method)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:726)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:703)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:670)
    at maps.al.k.a(Unknown Source)
    at maps.as.b.a(Unknown Source)
    at maps.as.b.a(Unknown Source)
    at maps.as.b.b(Unknown Source)
    at maps.au.al.a(Unknown Source)
    at maps.au.at.a(Unknown Source)
    at maps.ay.ap.a(Unknown Source)
    at maps.ap.f.a(Unknown Source)
    at maps.ap.f.b(Unknown Source)
    at maps.aj.y.l(Unknown Source)
    at maps.aj.y.run(Unknown Source)

    LOGCAT=03-03 16:26:47.204 E/dalvikvm-heap(15112): Out of memory on a 4194320-byte allocation.
    03-03 16:26:47.204 I/dalvikvm(15112): "GLThread 911" prio=5 tid=29 RUNNABLE
    03-03 16:26:47.204 I/dalvikvm(15112):   | group="main" sCount=0 dsCount=0 obj=0x478125e0 self=0x79d89680
    03-03 16:26:47.204 I/dalvikvm(15112):   | sysTid=15717 nice=1 sched=0/0 cgrp=apps handle=1982454736
    03-03 16:26:47.204 I/dalvikvm(15112):   | state=R schedstat=( 0 0 0 ) utm=47 stm=8 core=1
    03-03 16:26:47.204 I/dalvikvm(15112):   at android.graphics.Bitmap.nativeCreate(Native Method)
    03-03 16:26:47.204 I/dalvikvm(15112):   at android.graphics.Bitmap.createBitmap(Bitmap.java:726)
    03-03 16:26:47.204 I/dalvikvm(15112):   at android.graphics.Bitmap.createBitmap(Bitmap.java:703)
    03-03 16:26:47.204 I/dalvikvm(15112):   at android.graphics.Bitmap.createBitmap(Bitmap.java:670)
    03-03 16:26:47.204 I/dalvikvm(15112):   at maps.al.k.a((null):-1)
    03-03 16:26:47.204 I/dalvikvm(15112):   at maps.as.b.a((null):-1)
    03-03 16:26:47.204 I/dalvikvm(15112):   at maps.as.b.a((null):-1)
    03-03 16:26:47.204 I/dalvikvm(15112):   at maps.as.b.b((null):-1)
    03-03 16:26:47.204 I/dalvikvm(15112):   at maps.au.al.a((null):-1)
    03-03 16:26:47.204 I/dalvikvm(15112):   at maps.au.at.a((null):-1)
    03-03 16:26:47.204 I/dalvikvm(15112):   at maps.ay.ap.a((null):-1)
    03-03 16:26:47.204 I/dalvikvm(15112):   at maps.ap.f.a((null):-1)
    03-03 16:26:47.204 I/dalvikvm(15112):   at maps.ap.f.b((null):-1)
    03-03 16:26:47.204 I/dalvikvm(15112):   at maps.aj.y.l((null):-1)

03-03 16:26:47.204 I/dalvikvm(15112):   at maps.aj.y.run((null):-1)
Sharath
  • 969
  • 9
  • 30

1 Answers1

0

What Android version are you working on? Because on earlier (2.3-) Android versions Bitmap's are not automatically cleaned by the garbage collector. Also check out this answer: Bitmap, Bitmap.recycle(), WeakReferences, and Garbage Collection

Community
  • 1
  • 1
Alex van den Hoogen
  • 744
  • 1
  • 5
  • 22
  • SDK 10 is Android 2.3.3. So bitmaps are treated different than on 3.0 and higher. Please read the link or lookup "Bitmap clean Android" on Google for appropiate answers. – Alex van den Hoogen Mar 04 '14 at 11:03
  • thanks..this worked.. I guess its safe to bump up minSdkVersion to ICS.. only 20% of ppl are on GingerBread, and those phones can't do maps well anyway.. :) – Sharath Mar 13 '14 at 05:48