The following simplest app lets you show a popup window and dismiss it. I've put a dummy byte array of 100000000 bytes into the content view of the popup window to better show the memory usage. If you repeatedly show the popup window and dismiss it, you will see the memory usage grow and grow. The question is: Why this memory leak?
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Util;
namespace TestPop
{
// The activity
[Activity (Label = "TestPop", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (new TestView(this));
}
}
// The main view
class TestView : RelativeLayout {
Button b;
public TestView(Context c) : base(c) {
b = new Button (Context);
b.Text = "Show Popup";
b.Click += HandleClick;
AddView (b, 200, 100);
}
void HandleClick (object sender, EventArgs e)
{
PopView popview = new PopView (Context);
PopupWindow pw = new PopupWindow (popview, ViewGroup.LayoutParams.MatchParent, 200);
pw.SetBackgroundDrawable(new ColorDrawable(Color.Argb(128, 255, 0, 0)));
pw.OutsideTouchable = true;
Log.Info ("Memory: ", GC.GetTotalMemory (forceFullCollection: true).ToString ());
pw.ShowAtLocation (this, GravityFlags.NoGravity, 0, 0);
}
}
// The content view of the popup
class PopView : RelativeLayout {
public PopView(Context c) : base(c) {
}
byte[] dummyMemory = new byte[100000000];
}
}
Output:
[Memory:] 104285152
[Memory:] 204285856
[Memory:] 304286952
[Memory:] 404286752
[Memory:] 504286816
[Memory:] 604286864
[Memory:] 704286912
[Memory:] 804286960
[Memory:] 904286992