Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Google Maps leaking?

$
0
0

Can somebody double check my sample that shows Google Maps leaking like a boss? The sample is as simple as possible, using 4.2 as minimum Android version to avoid using support libraries.

It creates a button and a MapFragment and then places a large bitmap on map (to speed up memory leak). Upon pause the MapFragment is removed. When activity is recreated (destroyed and then created) the memory usage goes up and keeps going up until it crashes eventually. I tested it by rotating the emulator/device but it all boils down to activity recreation.

Perhaps I am doing something obviously wrong? Note that even without placing a huge bitmap on it it'd eventually leak (by smaller steps). I've also tried some variations but all leaked. I was using DDMS's HEAP to see that both allocated memory and #objects are growing.

To run the sample you'd need to use your Google Maps key and place it into manifest and allow com.rthand.android.testmapleak package to access the service.

here is the code as well:

`protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        InitMap();
    }

    private MapFragment mapFragment;
    private void InitMap()
    {
        GoogleMapOptions mapOptions = new GoogleMapOptions()
                .InvokeMapType(GoogleMap.MapTypeNormal)
                .InvokeZoomControlsEnabled(false)
                .InvokeCompassEnabled(true);
        FragmentTransaction fragTx = FragmentManager.BeginTransaction();
        mapFragment = MapFragment.NewInstance(mapOptions);
        fragTx.Replace(Resource.Id.map, mapFragment);
        fragTx.Commit();
    }

    private GoogleMap map;
    private Marker marker;
    protected override void OnResume()
    {
        base.OnResume();
        map = mapFragment.Map;
        SetBitmaps();
    }

    protected override void OnPause()
    {
        base.OnPause();
        if (marker != null)
        {
            marker.Remove();
        }
        map.Clear();
        FragmentTransaction fragTx = FragmentManager.BeginTransaction();
        fragTx.Remove(mapFragment);
        mapFragment.Dispose();
        fragTx.Commit();
    }

    private void SetBitmaps()
    {
        Stream iso = Assets.Open("image.jpg");
        Bitmap bitmap = BitmapFactory.DecodeStream(iso);
        MarkerOptions poiOptions = new MarkerOptions()
                .SetPosition(new LatLng(46.055875, 14.350028))
                .InvokeIcon(BitmapDescriptorFactory.FromBitmap(bitmap));
        marker = map.AddMarker(poiOptions);
        bitmap.Recycle();
        bitmap.Dispose();
    }`

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>