Android API Google Maps V2 OutOfMemory Exception

I have a list view of the data, and each element opens a view in which there is a map built into it. The purpose of this map is to show one marker. The code works fine, but if I have many list items (50+), looping back and forth causes the application to throw OutOfMemory exceptions.

Can someone help me understand what I'm doing wrong here. I suppose there is something wrong with my card initialization logic, and I'm trying to figure it 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 application crashes is shown 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)
+4
source share
1 answer
0

Source: https://habr.com/ru/post/1529985/


All Articles