Bug 11921 - Removing an AndroidGameView causes CPU and GC to go crazy
Summary: Removing an AndroidGameView causes CPU and GC to go crazy
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 4.4.x
Hardware: PC Windows
: --- normal
Target Milestone: 4.6.7
Assignee: dean.ellis
Depends on:
Reported: 2013-04-23 17:05 UTC by randyficker
Modified: 2013-05-22 15:24 UTC (History)
3 users (show)

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

Repro app (2.69 KB, text/plain)
2013-04-23 17:05 UTC, randyficker
Full app test case (4.44 MB, application/zip)
2013-05-22 14:28 UTC, PJ

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:

Description randyficker 2013-04-23 17:05:06 UTC
Created attachment 3858 [details]
Repro app

I’m having a problem with my app that causes it to go crazy and consume tons of CPU, even after it’s closed.

I’ve isolated this down to a sample repro app, which I have attached.  The problem happens if I try to remove an instance of AndroidGameView from the tree.

The attached code is simply the OpenGL starter package with the addition of a button that allows you to add and remove the AndroidGameView from view.  Here’s what you’ll see when you run the attached code: (I ran it in Xamarin.Android 4.6.00049)

1.	When the app starts up, tap the button to add the AndroidGameView.
2.	When I look at logcat while it runs, I see a GC running about once every 3-5 seconds, and by running “adb shell top -m 10  -d 1” I can see the CPU of the app ranging from 4-15%.  This is all normal and expected.
3.	Now tap the button again to remove the AndroidGameView from view.
4.	Now, logcat shows the GC running multiple times per second, and the CPU of the app is 25-40%.  This is not normal, as the app is idle at this point.  It continues eating these resources forever, in some sort of infinite loop.  The app still works and responds, but the resources are being eaten even though the app is idle.
5.	Now, press the device’s home button.  Notice the resources continue to be eaten even when the app is not active.  Nothing short of a force-stop will free the CPU resources.

Obviously, this is a bug problem, and is already annoying my beta testers.  I can’t release my app having it use up so much of the CPU for no reason, especially considering it persists even after the app is closed.

Is there any workaround for this?
Comment 1 randyficker 2013-04-24 13:35:27 UTC
Thanks to Dean Ellis for providing a work-around -- if you call Pause() *before* removing the AndroidGameView from the view tree, it seems to not go crazy anymore.  However, the bug should still be fixed, since this still creates memory leaks (as outlined by bug 11922)
Comment 2 dean.ellis 2013-04-26 08:18:07 UTC
I found the issue and its been fixed in our codebase. Not sure when the next release is yet but it should make it into that.
Comment 4 PJ 2013-05-22 14:26:33 UTC
Hey Dean,

This commit got into the 4.6.7 release, but I'm trying to verify and I'm seeing similar behavior to the original description, with slightly different average CPU usages.

Starting the app and adding the initial AndroidGameView gives me about an average of 18% CPU usage.

Once I remove the AndroidGameView, the app is eating about 80% of my CPU.

When I press the home button, the CPU usage fluctuates between 35% and 80%, but never dips lower than that 35% mark.

The workaround still works for me, adding a _gl.Pause() call before the view removal makes the CPU sit idle when expected.

Gonna have to REOPEN this one.

Test environment:
ML 10.8.1
XS 4.0.7
Galaxy Nexus w/ 4.2.2
Comment 5 PJ 2013-05-22 14:28:27 UTC
Created attachment 4006 [details]
Full app test case

Here is the sample showing the issue.

Run `adb shell top -m 10  -d 1` to verify the CPU usage at different execution points.
Comment 6 dean.ellis 2013-05-22 15:18:07 UTC

The call to stop/Pause is needed when you remove it from the layout. The reason is you need to stop the render thread when you are not using the AndroidGameView. The fix was to make sure the render thread actually exited, which it didn't before and caused the view to never be disposed.
Comment 7 PJ 2013-05-22 15:24:49 UTC
Ah ok, so the original issue here was INVALID.

Sounds good.