Bug 13425 - Garbage collector not triggered at GREF threshold
Summary: Garbage collector not triggered at GREF threshold
Status: CONFIRMED
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler (show other bugs)
Version: 4.8.x
Hardware: PC Mac OS
: Normal normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2013-07-23 20:17 UTC by Brendan Zagaeski (Xamarin Support)
Modified: 2017-07-13 19:49 UTC (History)
2 users (show)

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


Attachments
Logcat output (19.22 KB, application/octet-stream)
2013-07-23 20:17 UTC, Brendan Zagaeski (Xamarin Support)
Details

Description Brendan Zagaeski (Xamarin Support) 2013-07-23 20:17:44 UTC
Created attachment 4407 [details]
Logcat output

Prompted by a user's report that the available memory (according to `ActivityManager.MemoryInfo.AvailMem`) never recovered after repeatedly starting and quitting `SampleMapActivity`.


## Summary

The `SampleMapActivity` from `MapsAndLocationDemo_v2` [1] can cause the number of GREFs to pass 2000 without ever triggering a Mono garbage collection.

[1] https://github.com/xamarin/monodroid-samples/tree/master/MapsAndLocationDemo_v2


## Steps to reproduce

1. Run `SampleMapDemo_Froyo`. I tested on a `GoogleAPIs - API Level 17` emulator, but the original report mentioned problems on hardware devices too. Debug or Release doesn't matter. Targeting Froyo or Gingerbread doesn't matter.
2. Press the `Map with Markers Demo` button.
3. Press the Android Back button.
4. Repeat steps 2 and 3 approximately 95 times.


## Results

The GREFs pass 1700 after about 80 repetitions, 1800 after about 85, and 2000 after about 95. The app then terminates, and the SampleMapActivity restarts. During this time `adb logcat` does not mention any Mono garbage collections. In particular, the "full GC" message never appears:

    1800 outstanding GREFs. Performing a full GC!


Adding `GC.Collect ()` in `MainActivity.OnResume()` prevents the problem. So does allocating an array of Java objects in each `SampleMapActivity`:

    private GoogleMapOptions[] _foo

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

        _foo = new GoogleMapOptions[30];
        for (var i = 0; i < 30; i++) {
            _foo [i] = new GoogleMapOptions ();
        }

        ...
    }



Using just a `GC.Collect (0)` in `MainActivity.OnResume()` did not seem to help, nor did cleaning up the `GoogleMap` and `SupportMapFragment` during `SampleMapActivity.OnStop()`:

    protected override void OnStop ()
    {
        base.OnStop ();

        if (_map != null) {
            _map.Clear ();
            _map.Dispose ();
            _map = null;
        }
        if (_mapFragment != null) {
            _mapFragment.Dispose ();
            _mapFragment = null;
        }
    }


## `adb setprop` for attached `logcat` output
debug.mono.log = gc
debug.mono.env = MONO_LOG_LEVEL=debug|MONO_LOG_MASK=gc
Comment 1 Brendan Zagaeski (Xamarin Support) 2013-07-24 14:49:07 UTC
Running with debug.mono.log = gref

    D/dalvikvm(  919): GREF has increased to 701
    I/monodroid-gref(  919): +g+ grefc 517 gwrefc

and later...

    D/dalvikvm(  919): GREF has increased to 1901
    I/monodroid-gref(  919): +g+ grefc 1717 gwrefc 0


This time the GC kicked in before hitting 2000 grefs, so the app kept running!

Note You need to log in before you can comment on or make changes to this bug.