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

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

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

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 for Bug 13425 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Brendan Zagaeski (Xamarin Team, assistant) 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 Team, assistant) 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!