Bug 19735 - Google Maps in Google Play Services (JellyBean) v16 leaks memory like crazy
Summary: Google Maps in Google Play Services (JellyBean) v16 leaks memory like crazy
Status: RESOLVED FEATURE
Alias: None
Product: Android
Classification: Xamarin
Component: General (show other bugs)
Version: 4.12.4
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2014-05-13 14:06 UTC by Miha Markic
Modified: 2014-05-14 02:50 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Project withough Google Play Services component (otherwise it is too big) (13.72 KB, application/octet-stream)
2014-05-13 14:08 UTC, Miha Markic
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FEATURE

Description Miha Markic 2014-05-13 14:06:32 UTC
Insert your Google Maps API Key in manifest and run the attached project (it requires Android 4.2 or higher). Change device orientation by rotating it. The activity will get recreated each time orientation change and so will memory leak increase (looking at Heap's allocation through DDMS)

I am using Google Play Services (JellyBean) 16.0, Xamarin 4.12.4. The application really doesn't do anything, just inflates the view where map fragment is defined.
Tested on Genymotion emulator, Galaxy Nexus 7 2013.

Main.axml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/MyButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/Hello" />
    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

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

    SetContentView(Resource.Layout.Main);
}
Comment 1 Miha Markic 2014-05-13 14:08:42 UTC
Created attachment 6795 [details]
Project withough Google Play Services component (otherwise it is too big)
Comment 2 Jonathan Pryor 2014-05-13 15:40:57 UTC
I suspect the problem is that the GC needs to be run. Mono's GC gets a skewed view of the world, because each Java.Lang.Object instance is only ~20 bytes in size, no matter how much Java memory it may be holding onto:

http://docs.xamarin.com/guides/android/advanced_topics/garbage_collection/#Disposing_of_Peer_instances

Since Mono GC's are "on-demand", this means that a great deal of Java-side garbage can collect before Mono's GC will kick in, because it's invisible.

In such scenarios, you may need to help things out, by e.g. overriding Activity.OnDestroy() and calling GC.Collect().

Does that reduce your memory use?
Comment 3 Miha Markic 2014-05-13 16:14:04 UTC
Yep, makes total sense. And indeed, quick test shows that it seems to be the cause. Arrgh, I should have thought of that :)
I'll do more tests tomorrow. Thanks!
Comment 4 Miha Markic 2014-05-14 02:50:07 UTC
Looks that's was it. GC.Collect() FTW.